Skip to content

Commit

Permalink
add support for double click
Browse files Browse the repository at this point in the history
resolve go-rod#727
  • Loading branch information
ysmood committed Sep 23, 2022
1 parent da8f102 commit 3965e2f
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 13 deletions.
4 changes: 2 additions & 2 deletions element.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func (el *Element) MoveMouseOut() error {
// Click will press then release the button just like a human.
// Before the action, it will try to scroll to the element, hover the mouse over it,
// wait until the it's interactable and enabled.
func (el *Element) Click(button proto.InputMouseButton) error {
func (el *Element) Click(button proto.InputMouseButton, clickCount int) error {
err := el.Hover()
if err != nil {
return err
Expand All @@ -113,7 +113,7 @@ func (el *Element) Click(button proto.InputMouseButton) error {

defer el.tryTrace(TraceTypeInput, string(button)+" click")()

return el.page.Mouse.Click(button)
return el.page.Mouse.Click(button, clickCount)
}

// Tap will scroll to the button and tap it just like a human.
Expand Down
4 changes: 2 additions & 2 deletions element_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -689,11 +689,11 @@ func TestUseReleasedElement(t *testing.T) {
p := g.page.MustNavigate(g.srcFile("fixtures/click.html"))
btn := p.MustElement("button")
btn.MustRelease()
g.Err(btn.Click("left"))
g.Err(btn.Click("left", 1))

btn = p.MustElement("button")
g.E(proto.RuntimeReleaseObject{ObjectID: btn.Object.ObjectID}.Call(p))
g.Is(btn.Click("left"), cdp.ErrObjNotFound)
g.Is(btn.Click("left", 1), cdp.ErrObjNotFound)
}

func TestElementRemove(t *testing.T) {
Expand Down
5 changes: 5 additions & 0 deletions fixtures/double-click.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<html>
<body>
<button ondblclick='this.textContent = "ok"'>click me</button>
</body>
</html>
14 changes: 7 additions & 7 deletions input.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ func (m *Mouse) Scroll(offsetX, offsetY float64, steps int) error {
}

// Down holds the button down
func (m *Mouse) Down(button proto.InputMouseButton, clicks int) error {
func (m *Mouse) Down(button proto.InputMouseButton, clickCount int) error {
m.Lock()
defer m.Unlock()

Expand All @@ -306,7 +306,7 @@ func (m *Mouse) Down(button proto.InputMouseButton, clicks int) error {
Type: proto.InputDispatchMouseEventTypeMousePressed,
Button: button,
Buttons: gson.Int(buttons),
ClickCount: clicks,
ClickCount: clickCount,
Modifiers: m.page.Keyboard.getModifiers(),
X: m.x,
Y: m.y,
Expand All @@ -319,7 +319,7 @@ func (m *Mouse) Down(button proto.InputMouseButton, clicks int) error {
}

// Up releases the button
func (m *Mouse) Up(button proto.InputMouseButton, clicks int) error {
func (m *Mouse) Up(button proto.InputMouseButton, clickCount int) error {
m.Lock()
defer m.Unlock()

Expand All @@ -337,7 +337,7 @@ func (m *Mouse) Up(button proto.InputMouseButton, clicks int) error {
Type: proto.InputDispatchMouseEventTypeMouseReleased,
Button: button,
Buttons: gson.Int(buttons),
ClickCount: clicks,
ClickCount: clickCount,
Modifiers: m.page.Keyboard.getModifiers(),
X: m.x,
Y: m.y,
Expand All @@ -350,15 +350,15 @@ func (m *Mouse) Up(button proto.InputMouseButton, clicks int) error {
}

// Click the button. It's the combination of Mouse.Down and Mouse.Up
func (m *Mouse) Click(button proto.InputMouseButton) error {
func (m *Mouse) Click(button proto.InputMouseButton, clickCount int) error {
m.page.browser.trySlowmotion()

err := m.Down(button, 1)
err := m.Down(button, clickCount)
if err != nil {
return err
}

return m.Up(button, 1)
return m.Up(button, clickCount)
}

// Touch presents a touch device, such as a hand with fingers, each finger is a proto.InputTouchPoint.
Expand Down
12 changes: 12 additions & 0 deletions input_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,18 @@ func TestMouseClick(t *testing.T) {
g.True(page.MustHas("[a=ok]"))
}

func TestMouseDoubleClick(t *testing.T) {
g := setup(t)

g.browser.SlowMotion(1)
defer func() { g.browser.SlowMotion(0) }()

page := g.page.MustNavigate(g.srcFile("fixtures/double-click.html"))
el := page.MustElement("button")
el.MustDoubleClick()
g.Eq(el.MustText(), "ok")
}

func TestMouseDrag(t *testing.T) {
g := setup(t)

Expand Down
10 changes: 8 additions & 2 deletions must.go
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,7 @@ func (m *Mouse) MustUp(button proto.InputMouseButton) *Mouse {

// MustClick is similar to Mouse.Click
func (m *Mouse) MustClick(button proto.InputMouseButton) *Mouse {
m.page.e(m.Click(button))
m.page.e(m.Click(button, 1))
return m
}

Expand Down Expand Up @@ -726,7 +726,13 @@ func (el *Element) MustHover() *Element {

// MustClick is similar to Element.Click
func (el *Element) MustClick() *Element {
el.e(el.Click(proto.InputMouseButtonLeft))
el.e(el.Click(proto.InputMouseButtonLeft, 1))
return el
}

// MustDoubleClick is similar to Element.Click
func (el *Element) MustDoubleClick() *Element {
el.e(el.Click(proto.InputMouseButtonLeft, 2))
return el
}

Expand Down

0 comments on commit 3965e2f

Please sign in to comment.