From fae698f669928c9f9f4552a6302f22b80ce94254 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Tue, 23 Aug 2022 18:27:18 +0800 Subject: [PATCH 1/9] feat: init device with optional serial, port, mjpeg port --- device.go | 65 ++++++++++++++++++++++++++++++++++++++++++++++++-- driver.go | 2 +- driver_test.go | 22 +++++++++++++++++ go.mod | 1 + go.sum | 2 ++ 5 files changed, 89 insertions(+), 3 deletions(-) diff --git a/device.go b/device.go index 2b77ba4..b6c23b7 100644 --- a/device.go +++ b/device.go @@ -4,6 +4,12 @@ import ( "fmt" giDevice "github.com/electricbubble/gidevice" + "github.com/pkg/errors" +) + +const ( + defaultPort = 8100 + defaultMjpegPort = 9100 ) type Device struct { @@ -15,6 +21,61 @@ type Device struct { d giDevice.Device } +type DeviceOptions func(d *Device) + +func WithSerialNumber(serialNumber string) DeviceOptions { + return func(d *Device) { + d.serialNumber = serialNumber + } +} + +func WithPort(port int) DeviceOptions { + return func(d *Device) { + d.Port = port + } +} + +func WithMjpegPort(port int) DeviceOptions { + return func(d *Device) { + d.MjpegPort = port + } +} + +func NewDevice(options ...DeviceOptions) (device *Device, err error) { + var usbmux giDevice.Usbmux + if usbmux, err = giDevice.NewUsbmux(); err != nil { + return nil, errors.Wrap(err, "init usbmux failed") + } + + var deviceList []giDevice.Device + if deviceList, err = usbmux.Devices(); err != nil { + return nil, errors.Wrap(err, "get attached devices failed") + } + + device = &Device{ + Port: defaultPort, + MjpegPort: defaultMjpegPort, + } + for _, option := range options { + option(device) + } + + serialNumber := device.serialNumber + for _, d := range deviceList { + // find device by serial number if specified + if serialNumber != "" && d.Properties().SerialNumber != serialNumber { + continue + } + + device.deviceID = d.Properties().DeviceID + device.serialNumber = d.Properties().SerialNumber + device.d = d + return device, nil + } + + return nil, fmt.Errorf("device %s not found", device.serialNumber) +} + func DeviceList() (devices []Device, err error) { var usbmux giDevice.Usbmux if usbmux, err = giDevice.NewUsbmux(); err != nil { @@ -31,8 +92,8 @@ func DeviceList() (devices []Device, err error) { for i := range devices { devices[i].deviceID = deviceList[i].Properties().DeviceID devices[i].serialNumber = deviceList[i].Properties().SerialNumber - devices[i].Port = 8100 - devices[i].MjpegPort = 9100 + devices[i].Port = defaultPort + devices[i].MjpegPort = defaultMjpegPort devices[i].d = deviceList[i] } diff --git a/driver.go b/driver.go index c7f6457..e1246af 100644 --- a/driver.go +++ b/driver.go @@ -20,7 +20,7 @@ import ( // NewDriver creates new remote client, this will also start a new session. func NewDriver(capabilities Capabilities, urlPrefix string, mjpegPort ...int) (driver WebDriver, err error) { if len(mjpegPort) == 0 { - mjpegPort = []int{9100} + mjpegPort = []int{defaultMjpegPort} } wd := new(remoteWD) if wd.urlPrefix, err = url.Parse(urlPrefix); err != nil { diff --git a/driver_test.go b/driver_test.go index 6a234ba..6f380dc 100644 --- a/driver_test.go +++ b/driver_test.go @@ -41,6 +41,28 @@ func TestViaUSB(t *testing.T) { } } +func TestNewDevice(t *testing.T) { + device, _ := NewDevice() + if device != nil { + t.Log(device) + } + + device, _ = NewDevice(WithSerialNumber("xxxx")) + if device != nil { + t.Log(device) + } + + device, _ = NewDevice(WithPort(8700), WithMjpegPort(8800)) + if device != nil { + t.Log(device) + } + + device, _ = NewDevice(WithSerialNumber("xxxx"), WithPort(8700), WithMjpegPort(8800)) + if device != nil { + t.Log(device) + } +} + func TestNewDriver(t *testing.T) { var err error driver, err = NewDriver(nil, urlPrefix) diff --git a/go.mod b/go.mod index c6fb292..3c42563 100644 --- a/go.mod +++ b/go.mod @@ -4,5 +4,6 @@ go 1.16 require ( github.com/electricbubble/gidevice v0.6.2 + github.com/pkg/errors v0.9.1 howett.net/plist v1.0.0 // indirect ) diff --git a/go.sum b/go.sum index 7b3da7e..71e36f5 100644 --- a/go.sum +++ b/go.sum @@ -8,6 +8,8 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/lunixbochs/struc v0.0.0-20200707160740-784aaebc1d40 h1:EnfXoSqDfSNJv0VBNqY/88RNnhSGYkrHaO0mmFGbVsc= github.com/lunixbochs/struc v0.0.0-20200707160740-784aaebc1d40/go.mod h1:vy1vK6wD6j7xX6O6hXe621WabdtNkou2h7uRtTfRMyg= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww= github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= From 0e8c5b46fb5ccfef21a348a8edd2849083037909 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Wed, 24 Aug 2022 10:12:45 +0800 Subject: [PATCH 2/9] fix: solve some feedbacks --- device.go | 15 +++++++-------- go.mod | 1 - go.sum | 2 -- 3 files changed, 7 insertions(+), 11 deletions(-) diff --git a/device.go b/device.go index b6c23b7..f6f4f5c 100644 --- a/device.go +++ b/device.go @@ -4,7 +4,6 @@ import ( "fmt" giDevice "github.com/electricbubble/gidevice" - "github.com/pkg/errors" ) const ( @@ -21,35 +20,35 @@ type Device struct { d giDevice.Device } -type DeviceOptions func(d *Device) +type DeviceOption func(d *Device) -func WithSerialNumber(serialNumber string) DeviceOptions { +func WithSerialNumber(serialNumber string) DeviceOption { return func(d *Device) { d.serialNumber = serialNumber } } -func WithPort(port int) DeviceOptions { +func WithPort(port int) DeviceOption { return func(d *Device) { d.Port = port } } -func WithMjpegPort(port int) DeviceOptions { +func WithMjpegPort(port int) DeviceOption { return func(d *Device) { d.MjpegPort = port } } -func NewDevice(options ...DeviceOptions) (device *Device, err error) { +func NewDevice(options ...DeviceOption) (device *Device, err error) { var usbmux giDevice.Usbmux if usbmux, err = giDevice.NewUsbmux(); err != nil { - return nil, errors.Wrap(err, "init usbmux failed") + return nil, fmt.Errorf("init usbmux failed: %v", err) } var deviceList []giDevice.Device if deviceList, err = usbmux.Devices(); err != nil { - return nil, errors.Wrap(err, "get attached devices failed") + return nil, fmt.Errorf("get attached devices failed: %v", err) } device = &Device{ diff --git a/go.mod b/go.mod index 3c42563..c6fb292 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,5 @@ go 1.16 require ( github.com/electricbubble/gidevice v0.6.2 - github.com/pkg/errors v0.9.1 howett.net/plist v1.0.0 // indirect ) diff --git a/go.sum b/go.sum index 71e36f5..7b3da7e 100644 --- a/go.sum +++ b/go.sum @@ -8,8 +8,6 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/lunixbochs/struc v0.0.0-20200707160740-784aaebc1d40 h1:EnfXoSqDfSNJv0VBNqY/88RNnhSGYkrHaO0mmFGbVsc= github.com/lunixbochs/struc v0.0.0-20200707160740-784aaebc1d40/go.mod h1:vy1vK6wD6j7xX6O6hXe621WabdtNkou2h7uRtTfRMyg= -github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= -github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww= github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= From 8e1e872b629cf61a1300cb035c005ce8cf49ce1e Mon Sep 17 00:00:00 2001 From: debugtalk Date: Thu, 25 Aug 2022 00:29:58 +0800 Subject: [PATCH 3/9] docs: add comments --- element.go | 5 ++++- element_test.go | 9 +++++++++ gwda.go | 7 ++++--- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/element.go b/element.go index a17359c..a9573f2 100644 --- a/element.go +++ b/element.go @@ -9,9 +9,12 @@ import ( "strings" ) +// All elements returned by search endpoints have assigned element_id. +// Given element_id you can query properties like: +// enabled, rect, size, location, text, displayed, accessible, name type remoteWE struct { parent *remoteWD - id string + id string // element_id } func (we remoteWE) Click() (err error) { diff --git a/element_test.go b/element_test.go index 932ff9e..931c758 100644 --- a/element_test.go +++ b/element_test.go @@ -155,6 +155,9 @@ func Test_remoteWE_PickerWheelSelect(t *testing.T) { element := setupElement(t, BySelector{ClassName: ElementType{PickerWheel: true}}) err := element.PickerWheelSelect(PickerWheelOrderNext, 3) + if err != nil { + t.Fatal(err) + } err = element.PickerWheelSelect(PickerWheelOrderPrevious) if err != nil { t.Fatal(err) @@ -236,7 +239,13 @@ func Test_remoteWE_Rect(t *testing.T) { element := setupElement(t, BySelector{ClassName: ElementType{Switch: true}}) rect, err := element.Rect() + if err != nil { + t.Fatal(err) + } location, err := element.Location() + if err != nil { + t.Fatal(err) + } size, err := element.Size() if err != nil { t.Fatal(err) diff --git a/gwda.go b/gwda.go index db40589..5b74874 100644 --- a/gwda.go +++ b/gwda.go @@ -606,6 +606,7 @@ func elementIDFromValue(val map[string]string) string { return "" } +// performance ranking: class name > accessibility id > link text > predicate > class chain > xpath type BySelector struct { ClassName ElementType `json:"class name"` @@ -624,7 +625,7 @@ type BySelector struct { ClassChain string `json:"class chain"` - XPath string `json:"xpath"` + XPath string `json:"xpath"` // not recommended, it's slow because it is not supported by XCTest natively } func (wl BySelector) getUsingAndValue() (using, value string) { @@ -879,8 +880,8 @@ const ( ) type Point struct { - X int `json:"x"` - Y int `json:"y"` + X int `json:"x"` // upper left X coordinate of selected element + Y int `json:"y"` // upper left Y coordinate of selected element } type Rect struct { From 247146aaadbaefd43bfdc7741a94049f2637ae4c Mon Sep 17 00:00:00 2001 From: debugtalk Date: Sun, 28 Aug 2022 14:50:49 +0800 Subject: [PATCH 4/9] change: avoid printing screenshot data --- gwda.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/gwda.go b/gwda.go index 5b74874..db5edaa 100644 --- a/gwda.go +++ b/gwda.go @@ -68,7 +68,12 @@ func executeHTTP(method string, rawURL string, rawBody []byte, httpCli *http.Cli }() rawResp, err = ioutil.ReadAll(resp.Body) - debugLog(fmt.Sprintf("<-- %s %s %d %s\n%s\n", method, rawURL, resp.StatusCode, time.Since(start), rawResp)) + respBody := fmt.Sprintf("<-- %s %s %d %s", method, rawURL, resp.StatusCode, time.Since(start)) + if !strings.HasSuffix(rawURL, "screenshot") { + // avoid printing screenshot data + respBody += fmt.Sprintf("\n%s", rawResp) + } + debugLog(respBody) if err != nil { return nil, err } From 027e598b156b640d4a9b906fb74f92568a5543e3 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Mon, 19 Sep 2022 20:46:20 +0800 Subject: [PATCH 5/9] feat: append tap options for extra WDA configurations --- driver.go | 13 ++++++++++--- gwda.go | 6 ++++-- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/driver.go b/driver.go index e1246af..abb76b6 100644 --- a/driver.go +++ b/driver.go @@ -496,16 +496,23 @@ func (wd *remoteWD) AppAuthReset(resource ProtectedResource) (err error) { return } -func (wd *remoteWD) Tap(x, y int) error { - return wd.TapFloat(float64(x), float64(y)) +func (wd *remoteWD) Tap(x, y int, option ...DataOption) error { + return wd.TapFloat(float64(x), float64(y), option...) } -func (wd *remoteWD) TapFloat(x, y float64) (err error) { +func (wd *remoteWD) TapFloat(x, y float64, option ...DataOption) (err error) { // [[FBRoute POST:@"/wda/tap/:uuid"] respondWithTarget:self action:@selector(handleTap:)] data := map[string]interface{}{ "x": x, "y": y, } + // append options in post data for extra WDA configurations + // e.g. add identifier in tap event logs + if len(option) > 0 { + for k, v := range option[0] { + data[k] = v + } + } _, err = wd.executePost(data, "/session", wd.sessionId, "/wda/tap/0") return } diff --git a/gwda.go b/gwda.go index db5edaa..6d47adf 100644 --- a/gwda.go +++ b/gwda.go @@ -894,6 +894,8 @@ type Rect struct { Size } +type DataOption map[string]interface{} + // WebDriver defines methods supported by WebDriver drivers. type WebDriver interface { // NewSession starts a new session and returns the SessionInfo. @@ -976,8 +978,8 @@ type WebDriver interface { AppAuthReset(ProtectedResource) error // Tap Sends a tap event at the coordinate. - Tap(x, y int) error - TapFloat(x, y float64) error + Tap(x, y int, option ...DataOption) error + TapFloat(x, y float64, option ...DataOption) error // DoubleTap Sends a double tap event at the coordinate. DoubleTap(x, y int) error From ac88b3e98c1a956cda12b59b37b1decea32ea4da Mon Sep 17 00:00:00 2001 From: debugtalk Date: Tue, 20 Sep 2022 16:22:01 +0800 Subject: [PATCH 6/9] feat: append swipe options for extra WDA configurations --- driver.go | 38 +++++++++++++++++++++----------------- gwda.go | 28 ++++++++++++++++++++-------- 2 files changed, 41 insertions(+), 25 deletions(-) diff --git a/driver.go b/driver.go index abb76b6..f3c5f51 100644 --- a/driver.go +++ b/driver.go @@ -496,11 +496,11 @@ func (wd *remoteWD) AppAuthReset(resource ProtectedResource) (err error) { return } -func (wd *remoteWD) Tap(x, y int, option ...DataOption) error { - return wd.TapFloat(float64(x), float64(y), option...) +func (wd *remoteWD) Tap(x, y int, options ...DataOption) error { + return wd.TapFloat(float64(x), float64(y), options...) } -func (wd *remoteWD) TapFloat(x, y float64, option ...DataOption) (err error) { +func (wd *remoteWD) TapFloat(x, y float64, options ...DataOption) (err error) { // [[FBRoute POST:@"/wda/tap/:uuid"] respondWithTarget:self action:@selector(handleTap:)] data := map[string]interface{}{ "x": x, @@ -508,10 +508,8 @@ func (wd *remoteWD) TapFloat(x, y float64, option ...DataOption) (err error) { } // append options in post data for extra WDA configurations // e.g. add identifier in tap event logs - if len(option) > 0 { - for k, v := range option[0] { - data[k] = v - } + for _, option := range options { + option(data) } _, err = wd.executePost(data, "/session", wd.sessionId, "/wda/tap/0") return @@ -549,11 +547,11 @@ func (wd *remoteWD) TouchAndHoldFloat(x, y float64, second ...float64) (err erro return } -func (wd *remoteWD) Drag(fromX, fromY, toX, toY int, pressForDuration ...float64) error { - return wd.DragFloat(float64(fromX), float64(fromY), float64(toX), float64(toY), pressForDuration...) +func (wd *remoteWD) Drag(fromX, fromY, toX, toY int, options ...DataOption) error { + return wd.DragFloat(float64(fromX), float64(fromY), float64(toX), float64(toY), options...) } -func (wd *remoteWD) DragFloat(fromX, fromY, toX, toY float64, pressForDuration ...float64) (err error) { +func (wd *remoteWD) DragFloat(fromX, fromY, toX, toY float64, options ...DataOption) (err error) { // [[FBRoute POST:@"/wda/dragfromtoforduration"] respondWithTarget:self action:@selector(handleDragCoordinate:)] data := map[string]interface{}{ "fromX": fromX, @@ -561,20 +559,26 @@ func (wd *remoteWD) DragFloat(fromX, fromY, toX, toY float64, pressForDuration . "toX": toX, "toY": toY, } - if len(pressForDuration) == 0 || pressForDuration[0] < 0 { - pressForDuration = []float64{1.0} + + // append options in post data for extra WDA configurations + // e.g. use WithPressDuration to set pressForDuration + for _, option := range options { + option(data) + } + + if _, ok := data["duration"]; !ok { + data["duration"] = 1.0 // default duration } - data["duration"] = pressForDuration[0] _, err = wd.executePost(data, "/session", wd.sessionId, "/wda/dragfromtoforduration") return } -func (wd *remoteWD) Swipe(fromX, fromY, toX, toY int) error { - return wd.SwipeFloat(float64(fromX), float64(fromY), float64(toX), float64(toY)) +func (wd *remoteWD) Swipe(fromX, fromY, toX, toY int, options ...DataOption) error { + return wd.SwipeFloat(float64(fromX), float64(fromY), float64(toX), float64(toY), options...) } -func (wd *remoteWD) SwipeFloat(fromX, fromY, toX, toY float64) error { - return wd.DragFloat(fromX, fromY, toX, toY, 0) +func (wd *remoteWD) SwipeFloat(fromX, fromY, toX, toY float64, options ...DataOption) error { + return wd.DragFloat(fromX, fromY, toX, toY, options...) } func (wd *remoteWD) ForceTouch(x, y int, pressure float64, second ...float64) error { diff --git a/gwda.go b/gwda.go index 6d47adf..8044896 100644 --- a/gwda.go +++ b/gwda.go @@ -894,7 +894,19 @@ type Rect struct { Size } -type DataOption map[string]interface{} +type DataOption func(data map[string]interface{}) + +func WithCustomOption(key string, value interface{}) DataOption { + return func(data map[string]interface{}) { + data[key] = value + } +} + +func WithPressDuration(duraion float64) DataOption { + return func(data map[string]interface{}) { + data["duration"] = duraion + } +} // WebDriver defines methods supported by WebDriver drivers. type WebDriver interface { @@ -978,8 +990,8 @@ type WebDriver interface { AppAuthReset(ProtectedResource) error // Tap Sends a tap event at the coordinate. - Tap(x, y int, option ...DataOption) error - TapFloat(x, y float64, option ...DataOption) error + Tap(x, y int, options ...DataOption) error + TapFloat(x, y float64, options ...DataOption) error // DoubleTap Sends a double tap event at the coordinate. DoubleTap(x, y int) error @@ -991,13 +1003,13 @@ type WebDriver interface { TouchAndHoldFloat(x, y float64, second ...float64) error // Drag Initiates a press-and-hold gesture at the coordinate, then drags to another coordinate. - // pressForDuration: The default value is 1 second. - Drag(fromX, fromY, toX, toY int, pressForDuration ...float64) error - DragFloat(fromX, fromY, toX, toY float64, pressForDuration ...float64) error + // WithPressDuration option can be used to set pressForDuration (default to 1 second). + Drag(fromX, fromY, toX, toY int, options ...DataOption) error + DragFloat(fromX, fromY, toX, toY float64, options ...DataOption) error // Swipe works like Drag, but `pressForDuration` value is 0 - Swipe(fromX, fromY, toX, toY int) error - SwipeFloat(fromX, fromY, toX, toY float64) error + Swipe(fromX, fromY, toX, toY int, options ...DataOption) error + SwipeFloat(fromX, fromY, toX, toY float64, options ...DataOption) error ForceTouch(x, y int, pressure float64, second ...float64) error ForceTouchFloat(x, y, pressure float64, second ...float64) error From 30695b270987901b947d5d569d8b6de37cc3c0e3 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Tue, 20 Sep 2022 17:19:20 +0800 Subject: [PATCH 7/9] feat: append send keys options for extra WDA configurations --- driver.go | 14 ++++++++++---- gwda.go | 10 ++++++++-- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/driver.go b/driver.go index f3c5f51..5281ed6 100644 --- a/driver.go +++ b/driver.go @@ -635,13 +635,19 @@ func (wd *remoteWD) GetPasteboard(contentType PasteboardType) (raw *bytes.Buffer return } -func (wd *remoteWD) SendKeys(text string, frequency ...int) (err error) { +func (wd *remoteWD) SendKeys(text string, options ...DataOption) (err error) { // [[FBRoute POST:@"/wda/keys"] respondWithTarget:self action:@selector(handleKeys:)] data := map[string]interface{}{"value": strings.Split(text, "")} - if len(frequency) == 0 || frequency[0] <= 0 { - frequency = []int{60} + + // append options in post data for extra WDA configurations + // e.g. use WithFrequency to set frequency of typing + for _, option := range options { + option(data) + } + + if _, ok := data["frequency"]; !ok { + data["frequency"] = 60 // default frequency } - data["frequency"] = frequency[0] _, err = wd.executePost(data, "/session", wd.sessionId, "/wda/keys") return } diff --git a/gwda.go b/gwda.go index 8044896..c8631a5 100644 --- a/gwda.go +++ b/gwda.go @@ -908,6 +908,12 @@ func WithPressDuration(duraion float64) DataOption { } } +func WithFrequency(frequency int) DataOption { + return func(data map[string]interface{}) { + data["frequency"] = frequency + } +} + // WebDriver defines methods supported by WebDriver drivers. type WebDriver interface { // NewSession starts a new session and returns the SessionInfo. @@ -1026,8 +1032,8 @@ type WebDriver interface { // SendKeys Types a string into active element. There must be element with keyboard focus, // otherwise an error is raised. - // frequency: Frequency of typing (letters per sec). The default value is 60 - SendKeys(text string, frequency ...int) error + // WithFrequency option can be used to set frequency of typing (letters per sec). The default value is 60 + SendKeys(text string, options ...DataOption) error // KeyboardDismiss Tries to dismiss the on-screen keyboard KeyboardDismiss(keyNames ...string) error From e75132bc28bc10b445cc5db95d72acea1e1f3d8d Mon Sep 17 00:00:00 2001 From: debugtalk Date: Tue, 20 Sep 2022 17:33:18 +0800 Subject: [PATCH 8/9] change: add tests --- driver_test.go | 4 ++-- examples/keyboard/main.go | 9 ++++++++- examples/touch/main.go | 33 ++++++++++++++++++++++++++++++++- 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/driver_test.go b/driver_test.go index 6f380dc..817d96e 100644 --- a/driver_test.go +++ b/driver_test.go @@ -454,7 +454,7 @@ func Test_remoteWD_TouchAndHold(t *testing.T) { func Test_remoteWD_Drag(t *testing.T) { setup(t) - // err := driver.Drag(200, 300, 200, 500, -1) + // err := driver.Drag(200, 300, 200, 500, WithPressDuration(0.5)) err := driver.Swipe(200, 300, 200, 500) if err != nil { t.Fatal(err) @@ -511,7 +511,7 @@ func Test_remoteWD_SendKeys(t *testing.T) { setup(t) err := driver.SendKeys("App Store") - // err := driver.SendKeys("App Store", 3) + // err := driver.SendKeys("App Store", WithFrequency(3)) if err != nil { t.Fatal(err) } diff --git a/examples/keyboard/main.go b/examples/keyboard/main.go index 8016d95..485521d 100644 --- a/examples/keyboard/main.go +++ b/examples/keyboard/main.go @@ -1,8 +1,9 @@ package main import ( - "github.com/electricbubble/gwda" "log" + + "github.com/electricbubble/gwda" ) func main() { @@ -16,6 +17,12 @@ func main() { log.Fatalln(err) } + // send keys with specified frequency + err = driver.SendKeys("world", gwda.WithFrequency(30)) + if err != nil { + log.Fatalln(err) + } + // element, err := driver.ActiveElement() // if err != nil { // log.Fatalln(err) diff --git a/examples/touch/main.go b/examples/touch/main.go index 00f0e50..2164ae4 100644 --- a/examples/touch/main.go +++ b/examples/touch/main.go @@ -1,8 +1,9 @@ package main import ( - "github.com/electricbubble/gwda" "log" + + "github.com/electricbubble/gwda" ) func main() { @@ -18,6 +19,16 @@ func main() { log.Fatalln(err) } + // tap action with specified identifier + option := gwda.WithCustomOption("log", map[string]interface{}{ + "enable": true, + "data": "identifier-tap-A", + }) + err = driver.Tap(x, y, option) + if err != nil { + log.Fatalln(err) + } + err = driver.DoubleTap(x, y) if err != nil { log.Fatalln(err) @@ -35,11 +46,31 @@ func main() { log.Fatalln(err) } + // drag action with specified identifier + option = gwda.WithCustomOption("log", map[string]interface{}{ + "enable": true, + "data": "identifier-drag-B", + }) + err = driver.Drag(fromX, fromY, toX, toY, option) + if err != nil { + log.Fatalln(err) + } + err = driver.Swipe(fromX, fromY, toX, toY) if err != nil { log.Fatalln(err) } + // swipe action with specified identifier + option = gwda.WithCustomOption("log", map[string]interface{}{ + "enable": true, + "data": "identifier-swipe-C", + }) + err = driver.Swipe(fromX, fromY, toX, toY, option) + if err != nil { + log.Fatalln(err) + } + // 需要 3D Touch 硬件支持 // err = driver.ForceTouch(x, y, 0.8) // if err != nil { From 8c05b6218f459e4668ffc59e29f7acaa0f922736 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Tue, 20 Sep 2022 18:37:57 +0800 Subject: [PATCH 9/9] fix: swipe with 0 press duration --- driver.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/driver.go b/driver.go index 5281ed6..86f56da 100644 --- a/driver.go +++ b/driver.go @@ -574,10 +574,12 @@ func (wd *remoteWD) DragFloat(fromX, fromY, toX, toY float64, options ...DataOpt } func (wd *remoteWD) Swipe(fromX, fromY, toX, toY int, options ...DataOption) error { + options = append(options, WithPressDuration(0)) return wd.SwipeFloat(float64(fromX), float64(fromY), float64(toX), float64(toY), options...) } func (wd *remoteWD) SwipeFloat(fromX, fromY, toX, toY float64, options ...DataOption) error { + options = append(options, WithPressDuration(0)) return wd.DragFloat(fromX, fromY, toX, toY, options...) }