11package commands
22
33import (
4+ "encoding/json"
45 "fmt"
6+
7+ "github.com/mobile-next/mobilecli/devices/wda"
58)
69
710// TapRequest represents the parameters for a tap command
@@ -23,6 +26,12 @@ type ButtonRequest struct {
2326 Button string `json:"button"`
2427}
2528
29+ // GestureRequest represents the parameters for a gesture command
30+ type GestureRequest struct {
31+ DeviceID string `json:"deviceId"`
32+ Actions []interface {} `json:"actions"`
33+ }
34+
2635// TapCommand performs a tap operation on the specified device
2736func TapCommand (req TapRequest ) * CommandResponse {
2837 if req .X < 0 || req .Y < 0 {
@@ -100,3 +109,44 @@ func ButtonCommand(req ButtonRequest) *CommandResponse {
100109 "message" : fmt .Sprintf ("Pressed button '%s' on device %s" , req .Button , targetDevice .ID ()),
101110 })
102111}
112+
113+ // GestureCommand performs a gesture operation on the specified device
114+ func GestureCommand (req GestureRequest ) * CommandResponse {
115+ if len (req .Actions ) == 0 {
116+ return NewErrorResponse (fmt .Errorf ("actions array is required and cannot be empty" ))
117+ }
118+
119+ targetDevice , err := FindDeviceOrAutoSelect (req .DeviceID )
120+ if err != nil {
121+ return NewErrorResponse (fmt .Errorf ("error finding device: %v" , err ))
122+ }
123+
124+ err = targetDevice .StartAgent ()
125+ if err != nil {
126+ return NewErrorResponse (fmt .Errorf ("failed to start agent on device %s: %v" , targetDevice .ID (), err ))
127+ }
128+
129+ // Convert []interface{} to []wda.TapAction
130+ tapActions := make ([]wda.TapAction , len (req .Actions ))
131+ for i , action := range req .Actions {
132+ actionBytes , err := json .Marshal (action )
133+ if err != nil {
134+ return NewErrorResponse (fmt .Errorf ("failed to marshal action at index %d: %v" , i , err ))
135+ }
136+
137+ var tapAction wda.TapAction
138+ if err := json .Unmarshal (actionBytes , & tapAction ); err != nil {
139+ return NewErrorResponse (fmt .Errorf ("failed to unmarshal action at index %d: %v" , i , err ))
140+ }
141+ tapActions [i ] = tapAction
142+ }
143+
144+ err = targetDevice .Gesture (tapActions )
145+ if err != nil {
146+ return NewErrorResponse (fmt .Errorf ("failed to perform gesture on device %s: %v" , targetDevice .ID (), err ))
147+ }
148+
149+ return NewSuccessResponse (map [string ]interface {}{
150+ "message" : fmt .Sprintf ("Performed gesture on device %s with %d actions" , targetDevice .ID (), len (req .Actions )),
151+ })
152+ }
0 commit comments