Skip to content

Commit bf90047

Browse files
authored
correctly set mouse events buttons property (#509)
1 parent 9464c0a commit bf90047

File tree

2 files changed

+64
-12
lines changed

2 files changed

+64
-12
lines changed

lib/ferrum/mouse.rb

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,19 @@
33
module Ferrum
44
class Mouse
55
CLICK_WAIT = ENV.fetch("FERRUM_CLICK_WAIT", 0.1).to_f
6-
VALID_BUTTONS = %w[none left middle right back forward].freeze
6+
BUTTON_MASKS = {
7+
"none" => 0,
8+
"left" => 1,
9+
"right" => 2,
10+
"middle" => 4,
11+
"back" => 8,
12+
"forward" => 16
13+
}.freeze
714

815
def initialize(page)
916
@page = page
1017
@x = @y = 0
18+
@buttons = 0
1119
end
1220

1321
#
@@ -130,7 +138,8 @@ def move(x:, y:, steps: 1)
130138
slowmoable: true,
131139
type: "mouseMoved",
132140
x: new_x.to_i,
133-
y: new_y.to_i)
141+
y: new_y.to_i,
142+
buttons: @buttons)
134143
end
135144

136145
self
@@ -140,16 +149,26 @@ def move(x:, y:, steps: 1)
140149

141150
def mouse_event(type:, button: :left, count: 1, modifiers: nil, wait: 0)
142151
button = validate_button(button)
143-
options = { x: @x, y: @y, type: type, button: button, clickCount: count }
152+
register_event_button(type, button)
153+
options = { x: @x, y: @y, type: type, button: button, buttons: @buttons, clickCount: count }
144154
options.merge!(modifiers: modifiers) if modifiers
145155
@page.command("Input.dispatchMouseEvent", wait: wait, slowmoable: true, **options)
146156
end
147157

148158
def validate_button(button)
149159
button = button.to_s
150-
raise "Invalid button: #{button}" unless VALID_BUTTONS.include?(button)
160+
raise "Invalid button: #{button}" unless BUTTON_MASKS.key?(button)
151161

152162
button
153163
end
164+
165+
def register_event_button(type, button)
166+
case type
167+
when "mousePressed"
168+
@buttons |= BUTTON_MASKS[button]
169+
when "mouseReleased"
170+
@buttons &= ~BUTTON_MASKS[button]
171+
end
172+
end
154173
end
155174
end

spec/mouse_spec.rb

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,16 @@
4444
end
4545

4646
describe "#move" do
47-
let(:tracking_code) do
48-
<<~JS
47+
it "splits into steps" do
48+
browser.go_to("/ferrum/simple")
49+
browser.mouse.move(x: 100, y: 100)
50+
browser.evaluate_async(<<~JS, browser.timeout)
4951
window.result = [];
5052
document.addEventListener("mousemove", e => {
5153
window.result.push([e.clientX, e.clientY]);
5254
});
5355
arguments[0]();
5456
JS
55-
end
56-
57-
it "splits into steps" do
58-
browser.go_to("/ferrum/simple")
59-
browser.mouse.move(x: 100, y: 100)
60-
browser.evaluate_async(tracking_code, browser.timeout)
6157

6258
browser.mouse.move(x: 200, y: 300, steps: 5)
6359

@@ -69,6 +65,43 @@
6965
[200, 300]
7066
])
7167
end
68+
69+
it "sets buttons property" do
70+
browser.go_to("/ferrum/simple")
71+
browser.mouse.move(x: 100, y: 100)
72+
browser.evaluate_async(<<~JS, browser.timeout)
73+
window.result = [];
74+
["move", "up", "down"].forEach(type =>
75+
document.addEventListener(`mouse${type}`, e => {
76+
window.result.push([type, e.clientX, e.clientY, e.buttons]);
77+
})
78+
);
79+
arguments[0]();
80+
JS
81+
82+
browser.mouse
83+
.move(x: 101, y: 102)
84+
.down(button: :left)
85+
.move(x: 103, y: 104)
86+
.down(button: :right)
87+
.move(x: 105, y: 106)
88+
.up(button: :left)
89+
.move(x: 107, y: 108)
90+
.up(button: :right)
91+
.move(x: 109, y: 110)
92+
93+
expect(browser.evaluate("window.result")).to eq([
94+
["move", 101, 102, 0], # none pressed
95+
["down", 101, 102, 1], # left down
96+
["move", 103, 104, 1], # left pressed
97+
["down", 103, 104, 3], # right down, left pressed
98+
["move", 105, 106, 3], # both pressed
99+
["up", 105, 106, 2], # left up, right pressed
100+
["move", 107, 108, 2], # right pressed
101+
["up", 107, 108, 0], # right up
102+
["move", 109, 110, 0] # none pressed
103+
])
104+
end
72105
end
73106

74107
context "mouse support", skip: true do

0 commit comments

Comments
 (0)