Class: Puppeteer::Mouse
- Inherits:
-
Object
- Object
- Puppeteer::Mouse
- Defined in:
- lib/puppeteer/mouse.rb
Defined Under Namespace
Modules: Button
Instance Method Summary collapse
- #click(x, y, delay: nil, button: nil, click_count: nil) ⇒ Object
- #down(button: nil, click_count: nil) ⇒ Object
- #drag(start, target) ⇒ Object
- #drag_and_drop(start, target, delay: nil) ⇒ Object
- #drag_enter(target, data) ⇒ Object
- #drag_over(target, data) ⇒ Object
- #drop(target, data) ⇒ Object
-
#initialize(client, keyboard) ⇒ Mouse
constructor
A new instance of Mouse.
- #move(x, y, steps: nil) ⇒ Object
- #up(button: nil, click_count: nil) ⇒ Object
-
#wheel(delta_x: 0, delta_y: 0) ⇒ Object
Dispatches a ‘mousewheel` event.
Constructor Details
Instance Method Details
#click(x, y, delay: nil, button: nil, click_count: nil) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/puppeteer/mouse.rb', line 52 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 them sequential move(x, y) down(button: , click_count: click_count) if delay sleep(delay / 1000.0) end up(button: , click_count: click_count) end |
#down(button: nil, click_count: nil) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/puppeteer/mouse.rb', line 70 def down(button: nil, click_count: nil) @button = || Button::LEFT @client.('Input.dispatchMouseEvent', type: 'mousePressed', button: @button, x: @x, y: @y, modifiers: @keyboard.modifiers, clickCount: click_count || 1, ) end |
#drag(start, target) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/puppeteer/mouse.rb', line 115 def drag(start, target) promise = resolvable_future do |f| @client.once('Input.dragIntercepted') do |event| f.fulfill(event['data']) end end move(start.x, start.y) down move(target.x, target.y) promise.value! end |
#drag_and_drop(start, target, delay: nil) ⇒ Object
157 158 159 160 161 162 163 164 165 166 |
# File 'lib/puppeteer/mouse.rb', line 157 def drag_and_drop(start, target, delay: nil) data = drag(start, target) drag_enter(target, data) drag_over(target, data) if delay sleep(delay / 1000.0) end drop(target, data) up end |
#drag_enter(target, data) ⇒ Object
127 128 129 130 131 132 133 134 135 |
# File 'lib/puppeteer/mouse.rb', line 127 def drag_enter(target, data) @client.('Input.dispatchDragEvent', type: 'dragEnter', x: target.x, y: target.y, modifiers: @keyboard.modifiers, data: data, ) end |
#drag_over(target, data) ⇒ Object
137 138 139 140 141 142 143 144 145 |
# File 'lib/puppeteer/mouse.rb', line 137 def drag_over(target, data) @client.('Input.dispatchDragEvent', type: 'dragOver', x: target.x, y: target.y, modifiers: @keyboard.modifiers, data: data, ) end |
#drop(target, data) ⇒ Object
147 148 149 150 151 152 153 154 155 |
# File 'lib/puppeteer/mouse.rb', line 147 def drop(target, data) @client.('Input.dispatchDragEvent', type: 'drop', x: target.x, y: target.y, modifiers: @keyboard.modifiers, data: data, ) end |
#move(x, y, steps: nil) ⇒ Object
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.('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
85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/puppeteer/mouse.rb', line 85 def up(button: nil, click_count: nil) @button = Button::NONE @client.('Input.dispatchMouseEvent', type: 'mouseReleased', button: || Button::LEFT, x: @x, y: @y, modifiers: @keyboard.modifiers, clickCount: click_count || 1, ) end |
#wheel(delta_x: 0, delta_y: 0) ⇒ Object
Dispatches a ‘mousewheel` event.
103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/puppeteer/mouse.rb', line 103 def wheel(delta_x: 0, delta_y: 0) @client.('Input.dispatchMouseEvent', type: 'mouseWheel', x: @x, y: @y, deltaX: delta_x, deltaY: delta_y, modifiers: @keyboard.modifiers, pointerType: 'mouse', ) end |