diff --git a/cli/io.go b/cli/io.go index 16bec98..9e96d58 100644 --- a/cli/io.go +++ b/cli/io.go @@ -53,6 +53,8 @@ var ioTapCmd = &cobra.Command{ }, } +var longPressDuration int + var ioLongPressCmd = &cobra.Command{ Use: "longpress [x,y]", Short: "Long press on a device screen at the given coordinates", @@ -80,6 +82,7 @@ var ioLongPressCmd = &cobra.Command{ DeviceID: deviceId, X: x, Y: y, + Duration: longPressDuration, } response := commands.LongPressCommand(req) @@ -186,6 +189,7 @@ func init() { // io command flags ioTapCmd.Flags().StringVar(&deviceId, "device", "", "ID of the device to tap on") ioLongPressCmd.Flags().StringVar(&deviceId, "device", "", "ID of the device to long press on") + ioLongPressCmd.Flags().IntVar(&longPressDuration, "duration", 500, "duration of the long press in milliseconds") ioButtonCmd.Flags().StringVar(&deviceId, "device", "", "ID of the device to press button on") ioTextCmd.Flags().StringVar(&deviceId, "device", "", "ID of the device to send keys to") ioSwipeCmd.Flags().StringVar(&deviceId, "device", "", "ID of the device to swipe on") diff --git a/commands/input.go b/commands/input.go index 3be9e83..5c9dbf8 100644 --- a/commands/input.go +++ b/commands/input.go @@ -20,6 +20,7 @@ type LongPressRequest struct { DeviceID string `json:"deviceId"` X int `json:"x"` Y int `json:"y"` + Duration int `json:"duration"` } // TextRequest represents the parameters for a text input command @@ -91,13 +92,13 @@ func LongPressCommand(req LongPressRequest) *CommandResponse { return NewErrorResponse(fmt.Errorf("failed to start agent on device %s: %v", targetDevice.ID(), err)) } - err = targetDevice.LongPress(req.X, req.Y) + err = targetDevice.LongPress(req.X, req.Y, req.Duration) if err != nil { return NewErrorResponse(fmt.Errorf("failed to long press on device %s: %v", targetDevice.ID(), err)) } return NewSuccessResponse(map[string]interface{}{ - "message": fmt.Sprintf("Long pressed on device %s at (%d,%d)", targetDevice.ID(), req.X, req.Y), + "message": fmt.Sprintf("Long pressed on device %s at (%d,%d) for %dms", targetDevice.ID(), req.X, req.Y, req.Duration), }) } diff --git a/devices/android.go b/devices/android.go index 5c5d301..4b432be 100644 --- a/devices/android.go +++ b/devices/android.go @@ -319,8 +319,8 @@ func (d *AndroidDevice) Tap(x, y int) error { } // LongPress simulates a long press at (x, y) on the Android device. -func (d *AndroidDevice) LongPress(x, y int) error { - _, err := d.runAdbCommand("shell", "input", "swipe", fmt.Sprintf("%d", x), fmt.Sprintf("%d", y), fmt.Sprintf("%d", x), fmt.Sprintf("%d", y), "500") +func (d *AndroidDevice) LongPress(x, y, duration int) error { + _, 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)) if err != nil { return err } diff --git a/devices/common.go b/devices/common.go index 22f0899..20d413e 100644 --- a/devices/common.go +++ b/devices/common.go @@ -51,7 +51,7 @@ type ControllableDevice interface { Boot() error // boot simulator/emulator Shutdown() error // shutdown simulator/emulator Tap(x, y int) error - LongPress(x, y int) error + LongPress(x, y, duration int) error Swipe(x1, y1, x2, y2 int) error Gesture(actions []wda.TapAction) error StartAgent(config StartAgentConfig) error diff --git a/devices/ios.go b/devices/ios.go index 54de15a..626e8ad 100644 --- a/devices/ios.go +++ b/devices/ios.go @@ -139,8 +139,8 @@ func (d IOSDevice) Tap(x, y int) error { return d.wdaClient.Tap(x, y) } -func (d IOSDevice) LongPress(x, y int) error { - return d.wdaClient.LongPress(x, y) +func (d IOSDevice) LongPress(x, y, duration int) error { + return d.wdaClient.LongPress(x, y, duration) } func (d IOSDevice) Swipe(x1, y1, x2, y2 int) error { diff --git a/devices/simulator.go b/devices/simulator.go index c974d07..591edcc 100644 --- a/devices/simulator.go +++ b/devices/simulator.go @@ -635,8 +635,8 @@ func (s SimulatorDevice) Tap(x, y int) error { return s.wdaClient.Tap(x, y) } -func (s SimulatorDevice) LongPress(x, y int) error { - return s.wdaClient.LongPress(x, y) +func (s SimulatorDevice) LongPress(x, y, duration int) error { + return s.wdaClient.LongPress(x, y, duration) } func (s SimulatorDevice) Swipe(x1, y1, x2, y2 int) error { diff --git a/devices/wda/longpress.go b/devices/wda/longpress.go index be187e2..d7c91f7 100644 --- a/devices/wda/longpress.go +++ b/devices/wda/longpress.go @@ -4,7 +4,7 @@ import ( "fmt" ) -func (c *WdaClient) LongPress(x, y int) error { +func (c *WdaClient) LongPress(x, y, duration int) error { sessionId, err := c.GetOrCreateSession() if err != nil { @@ -22,7 +22,7 @@ func (c *WdaClient) LongPress(x, y int) error { Actions: []TapAction{ {Type: "pointerMove", Duration: 0, X: x, Y: y}, {Type: "pointerDown", Button: 0}, - {Type: "pause", Duration: 500}, + {Type: "pause", Duration: duration}, {Type: "pointerUp", Button: 0}, }, }, diff --git a/server/server.go b/server/server.go index 65a066e..872a124 100644 --- a/server/server.go +++ b/server/server.go @@ -278,6 +278,7 @@ type IoLongPressParams struct { DeviceID string `json:"deviceId"` X int `json:"x"` Y int `json:"y"` + Duration int `json:"duration"` } type IoSwipeParams struct { @@ -322,10 +323,17 @@ func handleIoLongPress(params json.RawMessage) (interface{}, error) { return nil, fmt.Errorf("invalid parameters: %v. Expected fields: deviceId, x, y", err) } + // default duration to 500ms if not provided + duration := ioLongPressParams.Duration + if duration == 0 { + duration = 500 + } + req := commands.LongPressRequest{ DeviceID: ioLongPressParams.DeviceID, X: ioLongPressParams.X, Y: ioLongPressParams.Y, + Duration: duration, } response := commands.LongPressCommand(req)