Skip to content

Commit 3965e2f

Browse files
committed
add support for double click
resolve #727
1 parent da8f102 commit 3965e2f

File tree

6 files changed

+36
-13
lines changed

6 files changed

+36
-13
lines changed

element.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ func (el *Element) MoveMouseOut() error {
100100
// Click will press then release the button just like a human.
101101
// Before the action, it will try to scroll to the element, hover the mouse over it,
102102
// wait until the it's interactable and enabled.
103-
func (el *Element) Click(button proto.InputMouseButton) error {
103+
func (el *Element) Click(button proto.InputMouseButton, clickCount int) error {
104104
err := el.Hover()
105105
if err != nil {
106106
return err
@@ -113,7 +113,7 @@ func (el *Element) Click(button proto.InputMouseButton) error {
113113

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

116-
return el.page.Mouse.Click(button)
116+
return el.page.Mouse.Click(button, clickCount)
117117
}
118118

119119
// Tap will scroll to the button and tap it just like a human.

element_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -689,11 +689,11 @@ func TestUseReleasedElement(t *testing.T) {
689689
p := g.page.MustNavigate(g.srcFile("fixtures/click.html"))
690690
btn := p.MustElement("button")
691691
btn.MustRelease()
692-
g.Err(btn.Click("left"))
692+
g.Err(btn.Click("left", 1))
693693

694694
btn = p.MustElement("button")
695695
g.E(proto.RuntimeReleaseObject{ObjectID: btn.Object.ObjectID}.Call(p))
696-
g.Is(btn.Click("left"), cdp.ErrObjNotFound)
696+
g.Is(btn.Click("left", 1), cdp.ErrObjNotFound)
697697
}
698698

699699
func TestElementRemove(t *testing.T) {

fixtures/double-click.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<html>
2+
<body>
3+
<button ondblclick='this.textContent = "ok"'>click me</button>
4+
</body>
5+
</html>

input.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ func (m *Mouse) Scroll(offsetX, offsetY float64, steps int) error {
294294
}
295295

296296
// Down holds the button down
297-
func (m *Mouse) Down(button proto.InputMouseButton, clicks int) error {
297+
func (m *Mouse) Down(button proto.InputMouseButton, clickCount int) error {
298298
m.Lock()
299299
defer m.Unlock()
300300

@@ -306,7 +306,7 @@ func (m *Mouse) Down(button proto.InputMouseButton, clicks int) error {
306306
Type: proto.InputDispatchMouseEventTypeMousePressed,
307307
Button: button,
308308
Buttons: gson.Int(buttons),
309-
ClickCount: clicks,
309+
ClickCount: clickCount,
310310
Modifiers: m.page.Keyboard.getModifiers(),
311311
X: m.x,
312312
Y: m.y,
@@ -319,7 +319,7 @@ func (m *Mouse) Down(button proto.InputMouseButton, clicks int) error {
319319
}
320320

321321
// Up releases the button
322-
func (m *Mouse) Up(button proto.InputMouseButton, clicks int) error {
322+
func (m *Mouse) Up(button proto.InputMouseButton, clickCount int) error {
323323
m.Lock()
324324
defer m.Unlock()
325325

@@ -337,7 +337,7 @@ func (m *Mouse) Up(button proto.InputMouseButton, clicks int) error {
337337
Type: proto.InputDispatchMouseEventTypeMouseReleased,
338338
Button: button,
339339
Buttons: gson.Int(buttons),
340-
ClickCount: clicks,
340+
ClickCount: clickCount,
341341
Modifiers: m.page.Keyboard.getModifiers(),
342342
X: m.x,
343343
Y: m.y,
@@ -350,15 +350,15 @@ func (m *Mouse) Up(button proto.InputMouseButton, clicks int) error {
350350
}
351351

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

356-
err := m.Down(button, 1)
356+
err := m.Down(button, clickCount)
357357
if err != nil {
358358
return err
359359
}
360360

361-
return m.Up(button, 1)
361+
return m.Up(button, clickCount)
362362
}
363363

364364
// Touch presents a touch device, such as a hand with fingers, each finger is a proto.InputTouchPoint.

input_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,18 @@ func TestMouseClick(t *testing.T) {
169169
g.True(page.MustHas("[a=ok]"))
170170
}
171171

172+
func TestMouseDoubleClick(t *testing.T) {
173+
g := setup(t)
174+
175+
g.browser.SlowMotion(1)
176+
defer func() { g.browser.SlowMotion(0) }()
177+
178+
page := g.page.MustNavigate(g.srcFile("fixtures/double-click.html"))
179+
el := page.MustElement("button")
180+
el.MustDoubleClick()
181+
g.Eq(el.MustText(), "ok")
182+
}
183+
172184
func TestMouseDrag(t *testing.T) {
173185
g := setup(t)
174186

must.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,7 @@ func (m *Mouse) MustUp(button proto.InputMouseButton) *Mouse {
626626

627627
// MustClick is similar to Mouse.Click
628628
func (m *Mouse) MustClick(button proto.InputMouseButton) *Mouse {
629-
m.page.e(m.Click(button))
629+
m.page.e(m.Click(button, 1))
630630
return m
631631
}
632632

@@ -726,7 +726,13 @@ func (el *Element) MustHover() *Element {
726726

727727
// MustClick is similar to Element.Click
728728
func (el *Element) MustClick() *Element {
729-
el.e(el.Click(proto.InputMouseButtonLeft))
729+
el.e(el.Click(proto.InputMouseButtonLeft, 1))
730+
return el
731+
}
732+
733+
// MustDoubleClick is similar to Element.Click
734+
func (el *Element) MustDoubleClick() *Element {
735+
el.e(el.Click(proto.InputMouseButtonLeft, 2))
730736
return el
731737
}
732738

0 commit comments

Comments
 (0)