Class: Puppeteer::Bidi::Mouse

Inherits:
Object
  • Object
show all
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

Returns:

  • (String)
'left'
RIGHT =

: String

Returns:

  • (String)
'right'
MIDDLE =

: String

Returns:

  • (String)
'middle'
BACK =

: String

Returns:

  • (String)
'back'
FORWARD =

: String

Returns:

  • (String)
'forward'

Instance Method Summary collapse

Constructor Details

#initialize(browsing_context) ⇒ Mouse

Returns a new instance of Mouse.

Parameters:



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

Parameters:

  • button (String)

Returns:

  • (Integer)


158
159
160
161
162
163
164
165
166
167
# File 'lib/puppeteer/bidi/mouse.rb', line 158

def button_to_bidi(button)
  case button
  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

Parameters:

  • x (Numeric)
  • y (Numeric)
  • button: (String) (defaults to: LEFT)
  • count: (Integer) (defaults to: 1)
  • delay: (Numeric, nil) (defaults to: nil)


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

  bidi_button = button_to_bidi(button)

  # Perform clicks
  count.times do
    actions << {
      type: 'pointerDown',
      button: bidi_button
    }

    if delay
      actions << {
        type: 'pause',
        duration: delay.to_i
      }
    end

    actions << {
      type: 'pointerUp',
      button: bidi_button
    }
  end

  perform_actions(actions)
end

#down(button: LEFT) ⇒ void

This method returns an undefined value.

Press mouse button down

Parameters:

  • button: (String) (defaults to: LEFT)


55
56
57
58
59
60
61
# File 'lib/puppeteer/bidi/mouse.rb', line 55

def down(button: LEFT)
  actions = [{
    type: 'pointerDown',
    button: button_to_bidi(button)
  }]
  perform_actions(actions)
end

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

This method returns an undefined value.

Move mouse to coordinates

Parameters:

  • x (Numeric)
  • y (Numeric)
  • steps: (Integer) (defaults to: 1)


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

Parameters:

  • action_list (Array[Hash[Symbol, untyped]])


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

#resetvoid

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

Parameters:

  • button: (String) (defaults to: LEFT)


66
67
68
69
70
71
72
# File 'lib/puppeteer/bidi/mouse.rb', line 66

def up(button: LEFT)
  actions = [{
    type: 'pointerUp',
    button: button_to_bidi(button)
  }]
  perform_actions(actions)
end

#wheel(delta_x: 0, delta_y: 0) ⇒ void

This method returns an undefined value.

Scroll using mouse wheel

Parameters:

  • delta_x: (Numeric) (defaults to: 0)
  • delta_y: (Numeric) (defaults to: 0)


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