Class: Ferrum::Mouse
- Inherits:
-
Object
- Object
- Ferrum::Mouse
- Defined in:
- lib/ferrum/mouse.rb
Constant Summary collapse
- CLICK_WAIT =
ENV.fetch("FERRUM_CLICK_WAIT", 0.1).to_f
- BUTTON_MASKS =
{ "none" => 0, "left" => 1, "right" => 2, "middle" => 4, "back" => 8, "forward" => 16 }.freeze
Instance Method Summary collapse
-
#click(x:, y:, delay: 0, wait: CLICK_WAIT, **options) ⇒ self
Click given coordinates, fires mouse move, down and up events.
-
#down(**options) ⇒ self
Mouse down for given coordinates.
-
#initialize(page) ⇒ Mouse
constructor
A new instance of Mouse.
-
#move(x:, y:, steps: 1) ⇒ self
Mouse move to given x and y.
-
#scroll_by(x, y) ⇒ Object
Scroll page by the given amount x, y.
-
#scroll_to(top, left) ⇒ Object
Scroll page to a given x, y coordinates.
-
#up(**options) ⇒ self
Mouse up for given coordinates.
Constructor Details
#initialize(page) ⇒ Mouse
Returns a new instance of Mouse.
15 16 17 18 19 |
# File 'lib/ferrum/mouse.rb', line 15 def initialize(page) @page = page @x = @y = 0 = 0 end |
Instance Method Details
#click(x:, y:, delay: 0, wait: CLICK_WAIT, **options) ⇒ self
Click given coordinates, fires mouse move, down and up events.
82 83 84 85 86 87 88 89 90 |
# File 'lib/ferrum/mouse.rb', line 82 def click(x:, y:, delay: 0, wait: CLICK_WAIT, **) move(x: x, y: y) down(**) sleep(delay) # Potential wait because if some network event is triggered then we have # to wait until it's over and frame is loaded or failed to load. up(wait: wait, **) self end |
#down(**options) ⇒ self
Mouse down for given coordinates.
108 109 110 |
# File 'lib/ferrum/mouse.rb', line 108 def down(**) tap { mouse_event(type: "mousePressed", **) } end |
#move(x:, y:, steps: 1) ⇒ self
Mouse move to given x and y.
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/ferrum/mouse.rb', line 144 def move(x:, y:, steps: 1) from_x = @x from_y = @y @x = x @y = y steps.times do |i| new_x = from_x + ((@x - from_x) * ((i + 1) / steps.to_f)) new_y = from_y + ((@y - from_y) * ((i + 1) / steps.to_f)) @page.command("Input.dispatchMouseEvent", slowmoable: true, type: "mouseMoved", x: new_x, y: new_y, buttons: ) end self end |
#scroll_by(x, y) ⇒ Object
Scroll page by the given amount x, y.
34 35 36 |
# File 'lib/ferrum/mouse.rb', line 34 def scroll_by(x, y) tap { @page.execute("window.scrollBy(#{x}, #{y})") } end |
#scroll_to(top, left) ⇒ Object
Scroll page to a given x, y coordinates.
53 54 55 |
# File 'lib/ferrum/mouse.rb', line 53 def scroll_to(top, left) tap { @page.execute("window.scrollTo(#{top}, #{left})") } end |
#up(**options) ⇒ self
Mouse up for given coordinates.
128 129 130 |
# File 'lib/ferrum/mouse.rb', line 128 def up(**) tap { mouse_event(type: "mouseReleased", **) } end |