Class: Puppeteer::Mouse

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

Defined Under Namespace

Modules: Button

Instance Method Summary collapse

Constructor Details

#initialize(client, keyboard) ⇒ Mouse

Returns a new instance of Mouse.

Parameters:



13
14
15
16
17
18
19
20
# File 'lib/puppeteer/mouse.rb', line 13

def initialize(client, keyboard)
  @client = client
  @keyboard = keyboard

  @x = 0
  @y = 0
  @button = Button::NONE
end

Instance Method Details

#async_clickFuture

Parameters:

  • x (number)
  • y (number)
  • options (!{delay?: number, button?: "left"|"right"|"middle", clickCount?: number}=)

Returns:

  • (Future)


82
83
84
# File 'lib/puppeteer/mouse.rb', line 82

async def async_click(x, y, delay: nil, button: nil, click_count: nil)
  click(x, y, delay: delay, button: button, click_count: click_count)
end

#async_downFuture

Parameters:

  • options (!{button?: "left"|"right"|"middle", clickCount?: number}=)

Returns:

  • (Future)


101
102
103
# File 'lib/puppeteer/mouse.rb', line 101

async def async_down(button: nil, click_count: nil)
  down(button: button, click_count: click_count)
end

#async_moveFuture

Parameters:

  • x (number)
  • y (number)
  • steps (number)

Returns:

  • (Future)


51
52
53
# File 'lib/puppeteer/mouse.rb', line 51

async def async_move(x, y, steps: nil)
  move(x, y, steps: steps)
end

#async_upFuture

Parameters:

  • options (!{button?: "left"|"right"|"middle", clickCount?: number}=)

Returns:

  • (Future)


120
121
122
# File 'lib/puppeteer/mouse.rb', line 120

async def async_up(button: nil, click_count: nil)
  up(button: button, click_count: click_count)
end

#click(x, y, delay: nil, button: nil, click_count: nil) ⇒ Object

Parameters:

  • x (number)
  • y (number)
  • options (!{delay?: number, button?: "left"|"right"|"middle", clickCount?: number}=)


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/puppeteer/mouse.rb', line 58

def click(x, y, delay: nil, button: nil, click_count: nil)
  # await_all(async_move, async_down, async_up) often breaks the order of CDP commands.
  # D, [2020-04-15T17:09:47.895895 #88683] DEBUG -- : RECV << {"id"=>23, "result"=>{"layoutViewport"=>{"pageX"=>0, "pageY"=>1, "clientWidth"=>375, "clientHeight"=>667}, "visualViewport"=>{"offsetX"=>0, "offsetY"=>0, "pageX"=>0, "pageY"=>1, "clientWidth"=>375, "clientHeight"=>667, "scale"=>1, "zoom"=>1}, "contentSize"=>{"x"=>0, "y"=>0, "width"=>375, "height"=>2007}}, "sessionId"=>"0B09EA5E18DEE403E525B3E7FCD7E225"}
  # D, [2020-04-15T17:09:47.898422 #88683] DEBUG -- : SEND >> {"sessionId":"0B09EA5E18DEE403E525B3E7FCD7E225","method":"Input.dispatchMouseEvent","params":{"type":"mouseReleased","button":"left","x":0,"y":0,"modifiers":0,"clickCount":1},"id":24}
  # D, [2020-04-15T17:09:47.899711 #88683] DEBUG -- : SEND >> {"sessionId":"0B09EA5E18DEE403E525B3E7FCD7E225","method":"Input.dispatchMouseEvent","params":{"type":"mousePressed","button":"left","x":0,"y":0,"modifiers":0,"clickCount":1},"id":25}
  # D, [2020-04-15T17:09:47.900237 #88683] DEBUG -- : SEND >> {"sessionId":"0B09EA5E18DEE403E525B3E7FCD7E225","method":"Input.dispatchMouseEvent","params":{"type":"mouseMoved","button":"left","x":187,"y":283,"modifiers":0},"id":26}
  # So we execute move in advance.
  move(x, y)
  if delay
    down(button: button, click_count: click_count)
    sleep(delay / 1000.0)
    up(button: button, click_count: click_count)
  else
    await_all(
      async_down(button: button, click_count: click_count),
      async_up(button: button, click_count: click_count),
    )
  end
end

#down(button: nil, click_count: nil) ⇒ Object

Parameters:

  • options (!{button?: "left"|"right"|"middle", clickCount?: number}=)


87
88
89
90
91
92
93
94
95
96
97
# File 'lib/puppeteer/mouse.rb', line 87

def down(button: nil, click_count: nil)
  @button = button || Button::LEFT
  @client.send_message('Input.dispatchMouseEvent',
    type: 'mousePressed',
    button: @button,
    x: @x,
    y: @y,
    modifiers: @keyboard.modifiers,
    clickCount: click_count || 1,
  )
end

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

Parameters:

  • x (number)
  • y (number)
  • steps (number) (defaults to: nil)


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/puppeteer/mouse.rb', line 25

def move(x, y, steps: nil)
  move_steps = (steps || 1).to_i

  from_x = @x
  from_y = @y
  @x = x
  @y = y

  return if move_steps <= 0

  move_steps.times do |i|
    n = i + 1
    @client.send_message('Input.dispatchMouseEvent',
      type: 'mouseMoved',
      button: @button,
      x: from_x + (@x - from_x) * n / move_steps,
      y: from_y + (@y - from_y) * n / move_steps,
      modifiers: @keyboard.modifiers,
    )
  end
end

#up(button: nil, click_count: nil) ⇒ Object

Parameters:

  • options (!{button?: "left"|"right"|"middle", clickCount?: number}=)


106
107
108
109
110
111
112
113
114
115
116
# File 'lib/puppeteer/mouse.rb', line 106

def up(button: nil, click_count: nil)
  @button = Button::NONE
  @client.send_message('Input.dispatchMouseEvent',
    type: 'mouseReleased',
    button: button || Button::LEFT,
    x: @x,
    y: @y,
    modifiers: @keyboard.modifiers,
    clickCount: click_count || 1,
  )
end