Skip to content

Commit db2a3d9

Browse files
committed
feat: add --duration to longpress
1 parent 6b09091 commit db2a3d9

File tree

8 files changed

+24
-11
lines changed

8 files changed

+24
-11
lines changed

cli/io.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ var ioTapCmd = &cobra.Command{
5353
},
5454
}
5555

56+
var longPressDuration int
57+
5658
var ioLongPressCmd = &cobra.Command{
5759
Use: "longpress [x,y]",
5860
Short: "Long press on a device screen at the given coordinates",
@@ -80,6 +82,7 @@ var ioLongPressCmd = &cobra.Command{
8082
DeviceID: deviceId,
8183
X: x,
8284
Y: y,
85+
Duration: longPressDuration,
8386
}
8487

8588
response := commands.LongPressCommand(req)
@@ -186,6 +189,7 @@ func init() {
186189
// io command flags
187190
ioTapCmd.Flags().StringVar(&deviceId, "device", "", "ID of the device to tap on")
188191
ioLongPressCmd.Flags().StringVar(&deviceId, "device", "", "ID of the device to long press on")
192+
ioLongPressCmd.Flags().IntVar(&longPressDuration, "duration", 500, "duration of the long press in milliseconds")
189193
ioButtonCmd.Flags().StringVar(&deviceId, "device", "", "ID of the device to press button on")
190194
ioTextCmd.Flags().StringVar(&deviceId, "device", "", "ID of the device to send keys to")
191195
ioSwipeCmd.Flags().StringVar(&deviceId, "device", "", "ID of the device to swipe on")

commands/input.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ type LongPressRequest struct {
2020
DeviceID string `json:"deviceId"`
2121
X int `json:"x"`
2222
Y int `json:"y"`
23+
Duration int `json:"duration"`
2324
}
2425

2526
// TextRequest represents the parameters for a text input command
@@ -91,13 +92,13 @@ func LongPressCommand(req LongPressRequest) *CommandResponse {
9192
return NewErrorResponse(fmt.Errorf("failed to start agent on device %s: %v", targetDevice.ID(), err))
9293
}
9394

94-
err = targetDevice.LongPress(req.X, req.Y)
95+
err = targetDevice.LongPress(req.X, req.Y, req.Duration)
9596
if err != nil {
9697
return NewErrorResponse(fmt.Errorf("failed to long press on device %s: %v", targetDevice.ID(), err))
9798
}
9899

99100
return NewSuccessResponse(map[string]interface{}{
100-
"message": fmt.Sprintf("Long pressed on device %s at (%d,%d)", targetDevice.ID(), req.X, req.Y),
101+
"message": fmt.Sprintf("Long pressed on device %s at (%d,%d) for %dms", targetDevice.ID(), req.X, req.Y, req.Duration),
101102
})
102103
}
103104

devices/android.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,8 +319,8 @@ func (d *AndroidDevice) Tap(x, y int) error {
319319
}
320320

321321
// LongPress simulates a long press at (x, y) on the Android device.
322-
func (d *AndroidDevice) LongPress(x, y int) error {
323-
_, err := d.runAdbCommand("shell", "input", "swipe", fmt.Sprintf("%d", x), fmt.Sprintf("%d", y), fmt.Sprintf("%d", x), fmt.Sprintf("%d", y), "500")
322+
func (d *AndroidDevice) LongPress(x, y, duration int) error {
323+
_, err := d.runAdbCommand("shell", "input", "swipe", fmt.Sprintf("%d", x), fmt.Sprintf("%d", y), fmt.Sprintf("%d", x), fmt.Sprintf("%d", y), fmt.Sprintf("%d", duration))
324324
if err != nil {
325325
return err
326326
}

devices/common.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ type ControllableDevice interface {
5151
Boot() error // boot simulator/emulator
5252
Shutdown() error // shutdown simulator/emulator
5353
Tap(x, y int) error
54-
LongPress(x, y int) error
54+
LongPress(x, y, duration int) error
5555
Swipe(x1, y1, x2, y2 int) error
5656
Gesture(actions []wda.TapAction) error
5757
StartAgent(config StartAgentConfig) error

devices/ios.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@ func (d IOSDevice) Tap(x, y int) error {
139139
return d.wdaClient.Tap(x, y)
140140
}
141141

142-
func (d IOSDevice) LongPress(x, y int) error {
143-
return d.wdaClient.LongPress(x, y)
142+
func (d IOSDevice) LongPress(x, y, duration int) error {
143+
return d.wdaClient.LongPress(x, y, duration)
144144
}
145145

146146
func (d IOSDevice) Swipe(x1, y1, x2, y2 int) error {

devices/simulator.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -635,8 +635,8 @@ func (s SimulatorDevice) Tap(x, y int) error {
635635
return s.wdaClient.Tap(x, y)
636636
}
637637

638-
func (s SimulatorDevice) LongPress(x, y int) error {
639-
return s.wdaClient.LongPress(x, y)
638+
func (s SimulatorDevice) LongPress(x, y, duration int) error {
639+
return s.wdaClient.LongPress(x, y, duration)
640640
}
641641

642642
func (s SimulatorDevice) Swipe(x1, y1, x2, y2 int) error {

devices/wda/longpress.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"fmt"
55
)
66

7-
func (c *WdaClient) LongPress(x, y int) error {
7+
func (c *WdaClient) LongPress(x, y, duration int) error {
88

99
sessionId, err := c.GetOrCreateSession()
1010
if err != nil {
@@ -22,7 +22,7 @@ func (c *WdaClient) LongPress(x, y int) error {
2222
Actions: []TapAction{
2323
{Type: "pointerMove", Duration: 0, X: x, Y: y},
2424
{Type: "pointerDown", Button: 0},
25-
{Type: "pause", Duration: 500},
25+
{Type: "pause", Duration: duration},
2626
{Type: "pointerUp", Button: 0},
2727
},
2828
},

server/server.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ type IoLongPressParams struct {
278278
DeviceID string `json:"deviceId"`
279279
X int `json:"x"`
280280
Y int `json:"y"`
281+
Duration int `json:"duration"`
281282
}
282283

283284
type IoSwipeParams struct {
@@ -322,10 +323,17 @@ func handleIoLongPress(params json.RawMessage) (interface{}, error) {
322323
return nil, fmt.Errorf("invalid parameters: %v. Expected fields: deviceId, x, y", err)
323324
}
324325

326+
// default duration to 500ms if not provided
327+
duration := ioLongPressParams.Duration
328+
if duration == 0 {
329+
duration = 500
330+
}
331+
325332
req := commands.LongPressRequest{
326333
DeviceID: ioLongPressParams.DeviceID,
327334
X: ioLongPressParams.X,
328335
Y: ioLongPressParams.Y,
336+
Duration: duration,
329337
}
330338

331339
response := commands.LongPressCommand(req)

0 commit comments

Comments
 (0)