Class: Ferrum::Mouse

Inherits:
Object
  • Object
show all
Defined in:
lib/ferrum/mouse.rb

Constant Summary collapse

CLICK_WAIT =
ENV.fetch("FERRUM_CLICK_WAIT", 0.1).to_f
VALID_BUTTONS =
%w[none left middle right back forward].freeze

Instance Method Summary collapse

Constructor Details

#initialize(page) ⇒ Mouse

Returns a new instance of Mouse.



8
9
10
11
# File 'lib/ferrum/mouse.rb', line 8

def initialize(page)
  @page = page
  @x = @y = 0
end

Instance Method Details

#click(x:, y:, delay: 0, wait: CLICK_WAIT, **options) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/ferrum/mouse.rb', line 17

def click(x:, y:, delay: 0, wait: CLICK_WAIT, **options)
  move(x: x, y: y)
  down(**options)
  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, **options)
  self
end

#down(**options) ⇒ Object



27
28
29
# File 'lib/ferrum/mouse.rb', line 27

def down(**options)
  tap { mouse_event(type: "mousePressed", **options) }
end

#move(x:, y:, steps: 1) ⇒ Object

FIXME: steps



36
37
38
39
40
# File 'lib/ferrum/mouse.rb', line 36

def move(x:, y:, steps: 1)
  @x, @y = x, y
  @page.command("Input.dispatchMouseEvent", type: "mouseMoved", x: @x, y: @y)
  self
end

#scroll_to(top, left) ⇒ Object



13
14
15
# File 'lib/ferrum/mouse.rb', line 13

def scroll_to(top, left)
  tap { @page.execute("window.scrollTo(#{top}, #{left})") }
end

#up(**options) ⇒ Object



31
32
33
# File 'lib/ferrum/mouse.rb', line 31

def up(**options)
  tap { mouse_event(type: "mouseReleased", **options) }
end