Class: Puppeteer::Bidi::Mouse
- Inherits:
-
Object
- Object
- Puppeteer::Bidi::Mouse
- Defined in:
- lib/puppeteer/bidi/mouse.rb,
sig/puppeteer/bidi/mouse.rbs
Overview
Mouse class for mouse input operations Based on Puppeteer's BidiMouse implementation
Constant Summary collapse
- LEFT =
Mouse button constants
'left'- RIGHT =
: String
'right'- MIDDLE =
: String
'middle'- BACK =
: String
'back'- FORWARD =
: String
'forward'
Instance Method Summary collapse
-
#button_to_bidi(button) ⇒ Integer
Convert mouse button name to BiDi button number.
-
#click(x, y, button: LEFT, count: 1, delay: nil) ⇒ void
Click at coordinates.
-
#down(button: LEFT) ⇒ void
Press mouse button down.
-
#initialize(browsing_context) ⇒ Mouse
constructor
A new instance of Mouse.
-
#move(x, y, steps: 1) ⇒ void
Move mouse to coordinates.
-
#perform_actions(action_list) ⇒ void
Perform input actions via BiDi.
-
#reset ⇒ void
Reset mouse state Resets position to origin and releases all pressed buttons.
-
#up(button: LEFT) ⇒ void
Release mouse button.
-
#wheel(delta_x: 0, delta_y: 0) ⇒ void
Scroll using mouse wheel.
Constructor Details
#initialize(browsing_context) ⇒ Mouse
Returns a new instance of Mouse.
18 19 20 21 22 |
# File 'lib/puppeteer/bidi/mouse.rb', line 18 def initialize(browsing_context) @browsing_context = browsing_context @x = 0 @y = 0 end |
Instance Method Details
#button_to_bidi(button) ⇒ Integer
Convert mouse button name to BiDi button number
158 159 160 161 162 163 164 165 166 167 |
# File 'lib/puppeteer/bidi/mouse.rb', line 158 def () case when LEFT then 0 when MIDDLE then 1 when RIGHT then 2 when BACK then 3 when FORWARD then 4 else 0 end end |
#click(x, y, button: LEFT, count: 1, delay: nil) ⇒ void
This method returns an undefined value.
Click at coordinates
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/puppeteer/bidi/mouse.rb', line 81 def click(x, y, button: LEFT, count: 1, delay: nil) actions = [] # Move to coordinates if @x != x || @y != y actions << { type: 'pointerMove', x: x.to_i, y: y.to_i, origin: 'viewport' # BiDi expects string, not hash } end @x = x @y = y = () # Perform clicks count.times do actions << { type: 'pointerDown', button: } if delay actions << { type: 'pause', duration: delay.to_i } end actions << { type: 'pointerUp', button: } end perform_actions(actions) end |
#down(button: LEFT) ⇒ void
This method returns an undefined value.
Press mouse button down
55 56 57 58 59 60 61 |
# File 'lib/puppeteer/bidi/mouse.rb', line 55 def down(button: LEFT) actions = [{ type: 'pointerDown', button: () }] perform_actions(actions) end |
#move(x, y, steps: 1) ⇒ void
This method returns an undefined value.
Move mouse to coordinates
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/puppeteer/bidi/mouse.rb', line 29 def move(x, y, steps: 1) from_x = @x from_y = @y @x = x @y = y actions = [] (1..steps).each do |step| # Linear interpolation intermediate_x = from_x + (x - from_x) * step / steps intermediate_y = from_y + (y - from_y) * step / steps actions << { type: 'pointerMove', x: intermediate_x.to_i, y: intermediate_y.to_i } end perform_actions(actions) end |
#perform_actions(action_list) ⇒ void
This method returns an undefined value.
Perform input actions via BiDi
172 173 174 175 176 177 178 179 180 |
# File 'lib/puppeteer/bidi/mouse.rb', line 172 def perform_actions(action_list) @browsing_context.perform_actions([ { type: 'pointer', id: 'default mouse', actions: action_list } ]).wait end |
#reset ⇒ void
This method returns an undefined value.
Reset mouse state Resets position to origin and releases all pressed buttons
147 148 149 150 151 |
# File 'lib/puppeteer/bidi/mouse.rb', line 147 def reset @x = 0 @y = 0 @browsing_context.release_actions.wait end |
#up(button: LEFT) ⇒ void
This method returns an undefined value.
Release mouse button
66 67 68 69 70 71 72 |
# File 'lib/puppeteer/bidi/mouse.rb', line 66 def up(button: LEFT) actions = [{ type: 'pointerUp', button: () }] perform_actions(actions) end |
#wheel(delta_x: 0, delta_y: 0) ⇒ void
This method returns an undefined value.
Scroll using mouse wheel
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/puppeteer/bidi/mouse.rb', line 126 def wheel(delta_x: 0, delta_y: 0) @browsing_context.perform_actions([ { type: 'wheel', id: '__puppeteer_wheel', actions: [ { type: 'scroll', x: @x.to_i, y: @y.to_i, deltaX: delta_x.to_i, deltaY: delta_y.to_i } ] } ]).wait end |