From 97006d4aa1f9f36a6c7d85bb65237b5d9a7194d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0nan=C3=A7=20G=C3=BCm=C3=BC=C5=9F?= Date: Thu, 9 May 2024 15:50:25 +0300 Subject: [PATCH] Refactor mouse to reorder methods --- common/mouse.go | 144 ++++++++++++++++++++---------------------------- 1 file changed, 59 insertions(+), 85 deletions(-) diff --git a/common/mouse.go b/common/mouse.go index 55bf9f335..865b6e6e5 100644 --- a/common/mouse.go +++ b/common/mouse.go @@ -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 { @@ -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). @@ -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 } @@ -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() @@ -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 +}