Skip to content

Commit

Permalink
Refactor mouse to reorder methods
Browse files Browse the repository at this point in the history
  • Loading branch information
inancgumus committed May 9, 2024
1 parent d0b981d commit 97006d4
Showing 1 changed file with 59 additions and 85 deletions.
144 changes: 59 additions & 85 deletions common/mouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ func NewMouse(ctx context.Context, s session, f *Frame, ts *TimeoutSettings, k *
}
}

// Click will trigger a series of MouseMove, MouseDown and MouseUp events in the browser.
func (m *Mouse) Click(x float64, y float64, opts goja.Value) error {
mouseOpts := NewMouseClickOptions()
if err := mouseOpts.Parse(m.ctx, opts); err != nil {
return fmt.Errorf("parsing mouse click options: %w", err)
}
if err := m.click(x, y, mouseOpts); err != nil {
return fmt.Errorf("clicking on x:%f y:%f: %w", x, y, err)
}
return nil
}

func (m *Mouse) click(x float64, y float64, opts *MouseClickOptions) error {
mouseDownUpOpts := opts.ToMouseDownUpOptions()
if err := m.move(x, y, NewMouseMoveOptions()); err != nil {
Expand All @@ -59,6 +71,30 @@ func (m *Mouse) click(x float64, y float64, opts *MouseClickOptions) error {
return nil
}

// DblClick will trigger Click twice in quick succession.
func (m *Mouse) DblClick(x float64, y float64, opts goja.Value) error {
mouseOpts := NewMouseDblClickOptions()
if err := mouseOpts.Parse(m.ctx, opts); err != nil {
return fmt.Errorf("parsing double click options: %w", err)
}
if err := m.click(x, y, mouseOpts.ToMouseClickOptions()); err != nil {
return fmt.Errorf("double clicking on x:%f y:%f: %w", x, y, err)
}
return nil
}

// Down will trigger a MouseDown event in the browser.
func (m *Mouse) Down(opts goja.Value) error {
mouseOpts := NewMouseDownUpOptions()
if err := mouseOpts.Parse(m.ctx, opts); err != nil {
return fmt.Errorf("parsing mouse down options: %w", err)
}
if err := m.down(mouseOpts); err != nil {
return fmt.Errorf("pressing the mouse button on x:%f y:%f: %w", m.x, m.y, err)
}
return nil
}

func (m *Mouse) down(opts *MouseDownUpOptions) error {
m.button = input.MouseButton(opts.Button)
action := input.DispatchMouseEvent(input.MousePressed, m.x, m.y).
Expand All @@ -71,22 +107,15 @@ func (m *Mouse) down(opts *MouseDownUpOptions) error {
return nil
}

func (m *Mouse) move(x float64, y float64, opts *MouseMoveOptions) error {
fromX := m.x
fromY := m.y
m.x = x
m.y = y
for i := int64(1); i <= opts.Steps; i++ {
x := fromX + (m.x-fromX)*float64(i/opts.Steps)
y := fromY + (m.y-fromY)*float64(i/opts.Steps)
action := input.DispatchMouseEvent(input.MouseMoved, x, y).
WithButton(m.button).
WithModifiers(input.Modifier(m.keyboard.modifiers))
if err := action.Do(cdp.WithExecutor(m.ctx, m.session)); err != nil {
return fmt.Errorf("mouse move: %w", err)
}
// Up will trigger a MouseUp event in the browser.
func (m *Mouse) Up(opts goja.Value) error {
mouseOpts := NewMouseDownUpOptions()
if err := mouseOpts.Parse(m.ctx, opts); err != nil {
return fmt.Errorf("parsing mouse up options: %w", err)
}
if err := m.up(mouseOpts); err != nil {
return fmt.Errorf("releasing the mouse button on x:%f y:%f: %w", m.x, m.y, err)
}

return nil
}

Expand All @@ -103,42 +132,6 @@ func (m *Mouse) up(opts *MouseDownUpOptions) error {
return nil
}

// Click will trigger a series of MouseMove, MouseDown and MouseUp events in the browser.
func (m *Mouse) Click(x float64, y float64, opts goja.Value) error {
mouseOpts := NewMouseClickOptions()
if err := mouseOpts.Parse(m.ctx, opts); err != nil {
return fmt.Errorf("parsing mouse click options: %w", err)
}
if err := m.click(x, y, mouseOpts); err != nil {
return fmt.Errorf("clicking on x:%f y:%f: %w", x, y, err)
}
return nil
}

// DblClick will trigger Click twice in quick succession.
func (m *Mouse) DblClick(x float64, y float64, opts goja.Value) error {
mouseOpts := NewMouseDblClickOptions()
if err := mouseOpts.Parse(m.ctx, opts); err != nil {
return fmt.Errorf("parsing double click options: %w", err)
}
if err := m.click(x, y, mouseOpts.ToMouseClickOptions()); err != nil {
return fmt.Errorf("double clicking on x:%f y:%f: %w", x, y, err)
}
return nil
}

// Down will trigger a MouseDown event in the browser.
func (m *Mouse) Down(opts goja.Value) error {
mouseOpts := NewMouseDownUpOptions()
if err := mouseOpts.Parse(m.ctx, opts); err != nil {
return fmt.Errorf("parsing mouse down options: %w", err)
}
if err := m.down(mouseOpts); err != nil {
return fmt.Errorf("pressing the mouse button on x:%f y:%f: %w", m.x, m.y, err)
}
return nil
}

// Move will trigger a MouseMoved event in the browser.
func (m *Mouse) Move(x float64, y float64, opts goja.Value) error {
mouseOpts := NewMouseMoveOptions()
Expand All @@ -151,40 +144,21 @@ func (m *Mouse) Move(x float64, y float64, opts goja.Value) error {
return nil
}

// Up will trigger a MouseUp event in the browser.
func (m *Mouse) Up(opts goja.Value) error {
mouseOpts := NewMouseDownUpOptions()
if err := mouseOpts.Parse(m.ctx, opts); err != nil {
return fmt.Errorf("parsing mouse up options: %w", err)
}
if err := m.up(mouseOpts); err != nil {
return fmt.Errorf("releasing the mouse button on x:%f y:%f: %w", m.x, m.y, err)
}
return nil
}

// Wheel will trigger a MouseWheel event in the browser
/*func (m *Mouse) Wheel(opts goja.Value) {
var deltaX float64 = 0.0
var deltaY float64 = 0.0
if opts != nil && !goja.IsUndefined(opts) && !goja.IsNull(opts) {
opts := opts.ToObject(rt)
for _, k := range opts.Keys() {
switch k {
case "deltaX":
deltaX = opts.Get(k).ToFloat()
case "deltaY":
deltaY = opts.Get(k).ToFloat()
}
func (m *Mouse) move(x float64, y float64, opts *MouseMoveOptions) error {
fromX := m.x
fromY := m.y
m.x = x
m.y = y
for i := int64(1); i <= opts.Steps; i++ {
x := fromX + (m.x-fromX)*float64(i/opts.Steps)
y := fromY + (m.y-fromY)*float64(i/opts.Steps)
action := input.DispatchMouseEvent(input.MouseMoved, x, y).
WithButton(m.button).
WithModifiers(input.Modifier(m.keyboard.modifiers))
if err := action.Do(cdp.WithExecutor(m.ctx, m.session)); err != nil {
return fmt.Errorf("mouse move: %w", err)
}
}

action := input.DispatchMouseEvent(input.MouseWheel, m.x, m.y).
WithModifiers(input.Modifier(m.keyboard.modifiers)).
WithDeltaX(deltaX).
WithDeltaY(deltaY)
if err := action.Do(cdp.WithExecutor(m.ctx, m.session)); err != nil {
k6Throw(m.ctx, "mouse down: %w", err)
}
}*/
return nil
}

0 comments on commit 97006d4

Please sign in to comment.