From 238ab977053bce4caaf47d84094874dd4a12e1aa Mon Sep 17 00:00:00 2001 From: debugtalk Date: Fri, 23 Sep 2022 22:58:32 +0800 Subject: [PATCH] refactor: restructure --- hrp/internal/uixt/README.md | 12 +- hrp/internal/uixt/android_webdriver.go | 2 + hrp/internal/uixt/ext.go | 58 --- hrp/internal/uixt/ios_device.go | 173 ++++----- .../uixt/{ios_webdriver.go => ios_driver.go} | 346 +++++++++--------- .../{ios_webelement.go => ios_element.go} | 148 ++++---- hrp/internal/uixt/ios_test.go | 49 +-- 7 files changed, 366 insertions(+), 422 deletions(-) rename hrp/internal/uixt/{ios_webdriver.go => ios_driver.go} (64%) rename hrp/internal/uixt/{ios_webelement.go => ios_element.go} (63%) diff --git a/hrp/internal/uixt/README.md b/hrp/internal/uixt/README.md index 111281c0..bfb7d26f 100644 --- a/hrp/internal/uixt/README.md +++ b/hrp/internal/uixt/README.md @@ -2,8 +2,8 @@ From v4.3.0,HttpRunner will support mobile UI automation testing: -- iOS: based on [appium/WebDriverAgent], with client library [electricbubble/gwda] in golang -- Android: based on UiAutomation +- iOS: based on [appium/WebDriverAgent], with forked client library [electricbubble/gwda] in golang +- Android: based on [appium-uiautomator2-server], with forked client library [electricbubble/guia2] in golang Some UI recognition algorithms are also introduced for both iOS and Android: @@ -34,12 +34,18 @@ $ make build tags=ocr ## Thanks -This uixt module is initially forked from [electricbubble/gwda-ext-opencv] and made a lot of changes. +This uixt module is initially forked from the following repos and made a lot of changes. + +- [electricbubble/gwda-ext-opencv] +- [electricbubble/gwda] +- [electricbubble/guia] [electricbubble/gwda-ext-opencv]: https://github.com/electricbubble/gwda-ext-opencv [appium/WebDriverAgent]: https://github.com/appium/WebDriverAgent [electricbubble/gwda]: https://github.com/electricbubble/gwda +[electricbubble/guia]: https://github.com/electricbubble/guia2 [OpenCV 4]: https://opencv.org/ [hybridgroup/gocv]: https://github.com/hybridgroup/gocv [volcengine]: https://www.volcengine.com/product/text-recognition +[appium-uiautomator2-server]: https://github.com/appium/appium-uiautomator2-server diff --git a/hrp/internal/uixt/android_webdriver.go b/hrp/internal/uixt/android_webdriver.go index 64c90179..91dd4070 100644 --- a/hrp/internal/uixt/android_webdriver.go +++ b/hrp/internal/uixt/android_webdriver.go @@ -1 +1,3 @@ package uixt + +type uiaWebDriver struct{} diff --git a/hrp/internal/uixt/ext.go b/hrp/internal/uixt/ext.go index a705d5b8..5eb7d0d1 100644 --- a/hrp/internal/uixt/ext.go +++ b/hrp/internal/uixt/ext.go @@ -7,9 +7,6 @@ import ( "image" "image/jpeg" "image/png" - "mime" - "mime/multipart" - "net/http" "os" "path/filepath" "strings" @@ -148,61 +145,6 @@ func extend(driver WebDriver) (dExt *DriverExt, err error) { return dExt, nil } -func (dExt *DriverExt) ConnectMjpegStream(httpClient *http.Client) (err error) { - if httpClient == nil { - return errors.New(`'httpClient' can't be nil`) - } - - var req *http.Request - if req, err = http.NewRequest(http.MethodGet, "http://*", nil); err != nil { - return err - } - - var resp *http.Response - if resp, err = httpClient.Do(req); err != nil { - return err - } - // defer func() { _ = resp.Body.Close() }() - - var boundary string - if _, param, err := mime.ParseMediaType(resp.Header.Get("Content-Type")); err != nil { - return err - } else { - boundary = strings.Trim(param["boundary"], "-") - } - - mjpegReader := multipart.NewReader(resp.Body, boundary) - - go func() { - for { - select { - case <-dExt.doneMjpegStream: - _ = resp.Body.Close() - return - default: - var part *multipart.Part - if part, err = mjpegReader.NextPart(); err != nil { - dExt.frame = nil - continue - } - - raw := new(bytes.Buffer) - if _, err = raw.ReadFrom(part); err != nil { - dExt.frame = nil - continue - } - dExt.frame = raw - } - } - }() - - return -} - -func (dExt *DriverExt) CloseMjpegStream() { - dExt.doneMjpegStream <- true -} - func (dExt *DriverExt) takeScreenShot() (raw *bytes.Buffer, err error) { // 优先使用 MJPEG 流进行截图,性能最优 // 如果 MJPEG 流未开启,则使用 WebDriver 的截图接口 diff --git a/hrp/internal/uixt/ios_device.go b/hrp/internal/uixt/ios_device.go index 10b37d74..eeee5e5f 100644 --- a/hrp/internal/uixt/ios_device.go +++ b/hrp/internal/uixt/ios_device.go @@ -7,10 +7,13 @@ import ( builtinJSON "encoding/json" "fmt" "io/ioutil" + "mime" + "mime/multipart" "net" "net/http" "net/url" "regexp" + "strings" "sync" "time" @@ -54,7 +57,7 @@ func InitWDAClient(device *IOSDevice) (*DriverExt, error) { } // init wda device - targetDevice, err := NewDevice(deviceOptions...) + iosDevice, err := NewIOSDevice(deviceOptions...) if err != nil { return nil, err } @@ -63,7 +66,7 @@ func InitWDAClient(device *IOSDevice) (*DriverExt, error) { // aviod getting stuck when some super app is activate such as douyin or wexin log.Info().Msg("switch to iOS springboard") bundleID := "com.apple.springboard" - _, err = targetDevice.GIDevice().AppLaunch(bundleID) + _, err = iosDevice.AppLaunch(bundleID) if err != nil { return nil, errors.Wrap(err, "launch springboard failed") } @@ -71,7 +74,7 @@ func InitWDAClient(device *IOSDevice) (*DriverExt, error) { // init WDA driver capabilities := NewCapabilities() capabilities.WithDefaultAlertAction(AlertActionAccept) - driver, err := NewUSBDriver(capabilities, *targetDevice) + driver, err := iosDevice.NewUSBDriver(capabilities) if err != nil { return nil, errors.Wrap(err, "failed to init WDA driver") } @@ -88,7 +91,7 @@ func InitWDAClient(device *IOSDevice) (*DriverExt, error) { } log.Info().Interface("appiumWDASettings", settings).Msg("set appium WDA settings") - driverExt.host = fmt.Sprintf("http://127.0.0.1:%d", targetDevice.Port) + driverExt.host = fmt.Sprintf("http://127.0.0.1:%d", iosDevice.Port) if device.LogOn { err = driverExt.StartLogRecording("hrp_wda_log") if err != nil { @@ -103,23 +106,6 @@ type Device interface { UUID() string } -type IOSDevice struct { - UDID string `json:"udid,omitempty" yaml:"udid,omitempty"` - Port int `json:"port,omitempty" yaml:"port,omitempty"` - MjpegPort int `json:"mjpeg_port,omitempty" yaml:"mjpeg_port,omitempty"` - LogOn bool `json:"log_on,omitempty" yaml:"log_on,omitempty"` - - d giDevice.Device -} - -func (d IOSDevice) UUID() string { - return d.UDID -} - -func (d IOSDevice) GIDevice() giDevice.Device { - return d.d -} - type IOSDeviceOption func(*IOSDevice) func WithUDID(udid string) IOSDeviceOption { @@ -146,7 +132,7 @@ func WithLogOn(logOn bool) IOSDeviceOption { } } -func NewDevice(options ...IOSDeviceOption) (device *IOSDevice, err error) { +func NewIOSDevice(options ...IOSDeviceOption) (device *IOSDevice, err error) { var usbmux giDevice.Usbmux if usbmux, err = giDevice.NewUsbmux(); err != nil { return nil, fmt.Errorf("init usbmux failed: %v", err) @@ -166,49 +152,37 @@ func NewDevice(options ...IOSDeviceOption) (device *IOSDevice, err error) { } serialNumber := device.UDID - for _, d := range deviceList { + for _, dev := range deviceList { // find device by serial number if specified - if serialNumber != "" && d.Properties().SerialNumber != serialNumber { + if serialNumber != "" && dev.Properties().SerialNumber != serialNumber { continue } - device.UDID = d.Properties().SerialNumber - device.d = d + device.UDID = dev.Properties().SerialNumber + device.Device = dev return device, nil } return nil, fmt.Errorf("device %s not found", device.UDID) } -func DeviceList() (devices []IOSDevice, err error) { - var usbmux giDevice.Usbmux - if usbmux, err = giDevice.NewUsbmux(); err != nil { - return nil, fmt.Errorf("usbmuxd: %w", err) - } - - var deviceList []giDevice.Device - if deviceList, err = usbmux.Devices(); err != nil { - return nil, fmt.Errorf("device list: %w", err) - } - - devices = make([]IOSDevice, len(deviceList)) - - for i := range devices { - devices[i].UDID = deviceList[i].Properties().SerialNumber - devices[i].Port = defaultPort - devices[i].MjpegPort = defaultMjpegPort - devices[i].d = deviceList[i] - } - - return +type IOSDevice struct { + giDevice.Device + UDID string `json:"udid,omitempty" yaml:"udid,omitempty"` + Port int `json:"port,omitempty" yaml:"port,omitempty"` + MjpegPort int `json:"mjpeg_port,omitempty" yaml:"mjpeg_port,omitempty"` + LogOn bool `json:"log_on,omitempty" yaml:"log_on,omitempty"` } -// 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{defaultMjpegPort} - } - wd := new(remoteWD) +func (dev *IOSDevice) UUID() string { + return dev.UDID +} + +// NewHTTPDriver creates new remote HTTP client, this will also start a new session. +func (dev *IOSDevice) NewHTTPDriver(capabilities Capabilities) (driver WebDriver, err error) { + wd := new(wdaDriver) + + urlPrefix := fmt.Sprintf("http://127.0.0.1:%d", dev.Port) if wd.urlPrefix, err = url.Parse(urlPrefix); err != nil { return nil, err } @@ -218,7 +192,11 @@ func NewDriver(capabilities Capabilities, urlPrefix string, mjpegPort ...int) (d } wd.sessionId = sessionInfo.SessionId - if wd.mjpegConn, err = net.Dial("tcp", fmt.Sprintf("%s:%d", wd.urlPrefix.Hostname(), mjpegPort[0])); err != nil { + if wd.mjpegConn, err = net.Dial( + "tcp", + fmt.Sprintf("%s:%d", wd.urlPrefix.Hostname(), + dev.MjpegPort), + ); err != nil { return nil, err } wd.mjpegClient = convertToHTTPClient(wd.mjpegConn) @@ -227,30 +205,20 @@ func NewDriver(capabilities Capabilities, urlPrefix string, mjpegPort ...int) (d } // NewUSBDriver creates new client via USB connected device, this will also start a new session. -func NewUSBDriver(capabilities Capabilities, device ...IOSDevice) (driver WebDriver, err error) { - if len(device) == 0 { - if device, err = DeviceList(); err != nil { - return nil, err - } - if len(device) == 0 { - return nil, errors.New("no device") - } - } - dev := device[0] - - wd := &remoteWD{ +func (dev *IOSDevice) NewUSBDriver(capabilities Capabilities) (driver WebDriver, err error) { + wd := &wdaDriver{ usbCli: &struct { httpCli *http.Client defaultConn, mjpegConn giDevice.InnerConn sync.Mutex }{}, } - if wd.usbCli.defaultConn, err = dev.d.NewConnect(dev.Port, 0); err != nil { + if wd.usbCli.defaultConn, err = dev.NewConnect(dev.Port, 0); err != nil { return nil, fmt.Errorf("create connection: %w", err) } wd.usbCli.httpCli = convertToHTTPClient(wd.usbCli.defaultConn.RawConn()) - if wd.usbCli.mjpegConn, err = dev.d.NewConnect(dev.MjpegPort, 0); err != nil { + if wd.usbCli.mjpegConn, err = dev.NewConnect(dev.MjpegPort, 0); err != nil { return nil, fmt.Errorf("create connection MJPEG: %w", err) } wd.mjpegClient = convertToHTTPClient(wd.usbCli.mjpegConn.RawConn()) @@ -277,20 +245,6 @@ func NewUSBDriver(capabilities Capabilities, device ...IOSDevice) (driver WebDri return wd, err } -func newRequest(method string, url string, rawBody []byte) (request *http.Request, err error) { - header := map[string]string{ - "Content-Type": "application/json;charset=UTF-8", - "Accept": "application/json", - } - if request, err = http.NewRequest(method, url, bytes.NewBuffer(rawBody)); err != nil { - return nil, err - } - for k, v := range header { - request.Header.Set(k, v) - } - return -} - func convertToHTTPClient(_conn net.Conn) *http.Client { return &http.Client{ Transport: &http.Transport{ @@ -360,6 +314,61 @@ func (dExt *DriverExt) triggerWDALog(data map[string]interface{}) (*wdaResponse, return reply, nil } +func (dExt *DriverExt) ConnectMjpegStream(httpClient *http.Client) (err error) { + if httpClient == nil { + return errors.New(`'httpClient' can't be nil`) + } + + var req *http.Request + if req, err = http.NewRequest(http.MethodGet, "http://*", nil); err != nil { + return err + } + + var resp *http.Response + if resp, err = httpClient.Do(req); err != nil { + return err + } + // defer func() { _ = resp.Body.Close() }() + + var boundary string + if _, param, err := mime.ParseMediaType(resp.Header.Get("Content-Type")); err != nil { + return err + } else { + boundary = strings.Trim(param["boundary"], "-") + } + + mjpegReader := multipart.NewReader(resp.Body, boundary) + + go func() { + for { + select { + case <-dExt.doneMjpegStream: + _ = resp.Body.Close() + return + default: + var part *multipart.Part + if part, err = mjpegReader.NextPart(); err != nil { + dExt.frame = nil + continue + } + + raw := new(bytes.Buffer) + if _, err = raw.ReadFrom(part); err != nil { + dExt.frame = nil + continue + } + dExt.frame = raw + } + } + }() + + return +} + +func (dExt *DriverExt) CloseMjpegStream() { + dExt.doneMjpegStream <- true +} + type rawResponse []byte func (r rawResponse) checkErr() (err error) { diff --git a/hrp/internal/uixt/ios_webdriver.go b/hrp/internal/uixt/ios_driver.go similarity index 64% rename from hrp/internal/uixt/ios_webdriver.go rename to hrp/internal/uixt/ios_driver.go index fb1c5337..6464fd2d 100644 --- a/hrp/internal/uixt/ios_webdriver.go +++ b/hrp/internal/uixt/ios_driver.go @@ -22,9 +22,7 @@ import ( "github.com/httprunner/httprunner/v4/hrp/internal/json" ) -// var _ WebDriver = (*remoteWD)(nil) - -type remoteWD struct { +type wdaDriver struct { urlPrefix *url.URL sessionId string @@ -38,39 +36,11 @@ type remoteWD struct { mjpegConn net.Conn } -func (wd *remoteWD) _requestURL(tmpURL *url.URL, elem ...string) string { - var tmp *url.URL - if tmpURL == nil { - tmpURL = wd.urlPrefix - } - tmp, _ = url.Parse(tmpURL.String()) - tmp.Path = path.Join(append([]string{tmpURL.Path}, elem...)...) - return tmp.String() -} - -func (wd *remoteWD) executeGet(pathElem ...string) (rawResp rawResponse, err error) { - return wd.executeHTTP(http.MethodGet, wd._requestURL(nil, pathElem...), nil) -} - -func (wd *remoteWD) executePost(data interface{}, pathElem ...string) (rawResp rawResponse, err error) { - var bsJSON []byte = nil - if data != nil { - if bsJSON, err = json.Marshal(data); err != nil { - return nil, err - } - } - return wd.executeHTTP(http.MethodPost, wd._requestURL(nil, pathElem...), bsJSON) -} - -func (wd *remoteWD) executeDelete(pathElem ...string) (rawResp rawResponse, err error) { - return wd.executeHTTP(http.MethodDelete, wd._requestURL(nil, pathElem...), nil) -} - -func (wd *remoteWD) GetMjpegHTTPClient() *http.Client { +func (wd *wdaDriver) GetMjpegHTTPClient() *http.Client { return wd.mjpegClient } -func (wd *remoteWD) Close() error { +func (wd *wdaDriver) Close() error { if wd.usbCli == nil { wd.mjpegClient.CloseIdleConnections() return wd.mjpegConn.Close() @@ -88,7 +58,7 @@ func (wd *remoteWD) Close() error { return nil } -func (wd *remoteWD) NewSession(capabilities Capabilities) (sessionInfo SessionInfo, err error) { +func (wd *wdaDriver) NewSession(capabilities Capabilities) (sessionInfo SessionInfo, err error) { // [[FBRoute POST:@"/session"].withoutSession respondWithTarget:self action:@selector(handleCreateSession:)] data := make(map[string]interface{}) if len(capabilities) == 0 { @@ -98,7 +68,7 @@ func (wd *remoteWD) NewSession(capabilities Capabilities) (sessionInfo SessionIn } var rawResp rawResponse - if rawResp, err = wd.executePost(data, "/session"); err != nil { + if rawResp, err = wd.httpPOST(data, "/session"); err != nil { return SessionInfo{}, err } if sessionInfo, err = rawResp.valueConvertToSessionInfo(); err != nil { @@ -108,10 +78,10 @@ func (wd *remoteWD) NewSession(capabilities Capabilities) (sessionInfo SessionIn return } -func (wd *remoteWD) ActiveSession() (sessionInfo SessionInfo, err error) { +func (wd *wdaDriver) ActiveSession() (sessionInfo SessionInfo, err error) { // [[FBRoute GET:@""] respondWithTarget:self action:@selector(handleGetActiveSession:)] var rawResp rawResponse - if rawResp, err = wd.executeGet("/session", wd.sessionId); err != nil { + if rawResp, err = wd.httpGET("/session", wd.sessionId); err != nil { return SessionInfo{}, err } if sessionInfo, err = rawResp.valueConvertToSessionInfo(); err != nil { @@ -120,16 +90,16 @@ func (wd *remoteWD) ActiveSession() (sessionInfo SessionInfo, err error) { return } -func (wd *remoteWD) DeleteSession() (err error) { +func (wd *wdaDriver) DeleteSession() (err error) { // [[FBRoute DELETE:@""] respondWithTarget:self action:@selector(handleDeleteSession:)] - _, err = wd.executeDelete("/session", wd.sessionId) + _, err = wd.httpDELETE("/session", wd.sessionId) return } -func (wd *remoteWD) Status() (deviceStatus DeviceStatus, err error) { +func (wd *wdaDriver) Status() (deviceStatus DeviceStatus, err error) { // [[FBRoute GET:@"/status"].withoutSession respondWithTarget:self action:@selector(handleGetStatus:)] var rawResp rawResponse - if rawResp, err = wd.executeGet("/status"); err != nil { + if rawResp, err = wd.httpGET("/status"); err != nil { return DeviceStatus{}, err } reply := new(struct{ Value struct{ DeviceStatus } }) @@ -140,11 +110,11 @@ func (wd *remoteWD) Status() (deviceStatus DeviceStatus, err error) { return } -func (wd *remoteWD) DeviceInfo() (deviceInfo DeviceInfo, err error) { +func (wd *wdaDriver) DeviceInfo() (deviceInfo DeviceInfo, err error) { // [[FBRoute GET:@"/wda/device/info"] respondWithTarget:self action:@selector(handleGetDeviceInfo:)] // [[FBRoute GET:@"/wda/device/info"].withoutSession var rawResp rawResponse - if rawResp, err = wd.executeGet("/session", wd.sessionId, "/wda/device/info"); err != nil { + if rawResp, err = wd.httpGET("/session", wd.sessionId, "/wda/device/info"); err != nil { return DeviceInfo{}, err } reply := new(struct{ Value struct{ DeviceInfo } }) @@ -155,11 +125,11 @@ func (wd *remoteWD) DeviceInfo() (deviceInfo DeviceInfo, err error) { return } -func (wd *remoteWD) Location() (location Location, err error) { +func (wd *wdaDriver) Location() (location Location, err error) { // [[FBRoute GET:@"/wda/device/location"] respondWithTarget:self action:@selector(handleGetLocation:)] // [[FBRoute GET:@"/wda/device/location"].withoutSession var rawResp rawResponse - if rawResp, err = wd.executeGet("/session", wd.sessionId, "/wda/device/location"); err != nil { + if rawResp, err = wd.httpGET("/session", wd.sessionId, "/wda/device/location"); err != nil { return Location{}, err } reply := new(struct{ Value struct{ Location } }) @@ -170,10 +140,10 @@ func (wd *remoteWD) Location() (location Location, err error) { return } -func (wd *remoteWD) BatteryInfo() (batteryInfo BatteryInfo, err error) { +func (wd *wdaDriver) BatteryInfo() (batteryInfo BatteryInfo, err error) { // [[FBRoute GET:@"/wda/batteryInfo"] respondWithTarget:self action:@selector(handleGetBatteryInfo:)] var rawResp rawResponse - if rawResp, err = wd.executeGet("/session", wd.sessionId, "/wda/batteryInfo"); err != nil { + if rawResp, err = wd.httpGET("/session", wd.sessionId, "/wda/batteryInfo"); err != nil { return BatteryInfo{}, err } reply := new(struct{ Value struct{ BatteryInfo } }) @@ -184,10 +154,10 @@ func (wd *remoteWD) BatteryInfo() (batteryInfo BatteryInfo, err error) { return } -func (wd *remoteWD) WindowSize() (size Size, err error) { +func (wd *wdaDriver) WindowSize() (size Size, err error) { // [[FBRoute GET:@"/window/size"] respondWithTarget:self action:@selector(handleGetWindowSize:)] var rawResp rawResponse - if rawResp, err = wd.executeGet("/session", wd.sessionId, "/window/size"); err != nil { + if rawResp, err = wd.httpGET("/session", wd.sessionId, "/window/size"); err != nil { return Size{}, err } reply := new(struct{ Value struct{ Size } }) @@ -198,10 +168,10 @@ func (wd *remoteWD) WindowSize() (size Size, err error) { return } -func (wd *remoteWD) Screen() (screen Screen, err error) { +func (wd *wdaDriver) Screen() (screen Screen, err error) { // [[FBRoute GET:@"/wda/screen"] respondWithTarget:self action:@selector(handleGetScreen:)] var rawResp rawResponse - if rawResp, err = wd.executeGet("/session", wd.sessionId, "/wda/screen"); err != nil { + if rawResp, err = wd.httpGET("/session", wd.sessionId, "/wda/screen"); err != nil { return Screen{}, err } reply := new(struct{ Value struct{ Screen } }) @@ -212,7 +182,7 @@ func (wd *remoteWD) Screen() (screen Screen, err error) { return } -func (wd *remoteWD) Scale() (float64, error) { +func (wd *wdaDriver) Scale() (float64, error) { screen, err := wd.Screen() if err != nil { return 0, err @@ -220,11 +190,11 @@ func (wd *remoteWD) Scale() (float64, error) { return screen.Scale, nil } -func (wd *remoteWD) ActiveAppInfo() (info AppInfo, err error) { +func (wd *wdaDriver) ActiveAppInfo() (info AppInfo, err error) { // [[FBRoute GET:@"/wda/activeAppInfo"] respondWithTarget:self action:@selector(handleActiveAppInfo:)] // [[FBRoute GET:@"/wda/activeAppInfo"].withoutSession var rawResp rawResponse - if rawResp, err = wd.executeGet("/session", wd.sessionId, "/wda/activeAppInfo"); err != nil { + if rawResp, err = wd.httpGET("/session", wd.sessionId, "/wda/activeAppInfo"); err != nil { return AppInfo{}, err } reply := new(struct{ Value struct{ AppInfo } }) @@ -235,10 +205,10 @@ func (wd *remoteWD) ActiveAppInfo() (info AppInfo, err error) { return } -func (wd *remoteWD) ActiveAppsList() (appsList []AppBaseInfo, err error) { +func (wd *wdaDriver) ActiveAppsList() (appsList []AppBaseInfo, err error) { // [[FBRoute GET:@"/wda/apps/list"] respondWithTarget:self action:@selector(handleGetActiveAppsList:)] var rawResp rawResponse - if rawResp, err = wd.executeGet("/session", wd.sessionId, "/wda/apps/list"); err != nil { + if rawResp, err = wd.httpGET("/session", wd.sessionId, "/wda/apps/list"); err != nil { return nil, err } reply := new(struct{ Value []AppBaseInfo }) @@ -249,11 +219,11 @@ func (wd *remoteWD) ActiveAppsList() (appsList []AppBaseInfo, err error) { return } -func (wd *remoteWD) AppState(bundleId string) (runState AppState, err error) { +func (wd *wdaDriver) AppState(bundleId string) (runState AppState, err error) { // [[FBRoute POST:@"/wda/apps/state"] respondWithTarget:self action:@selector(handleSessionAppState:)] data := map[string]interface{}{"bundleId": bundleId} var rawResp rawResponse - if rawResp, err = wd.executePost(data, "/session", wd.sessionId, "/wda/apps/state"); err != nil { + if rawResp, err = wd.httpPOST(data, "/session", wd.sessionId, "/wda/apps/state"); err != nil { return 0, err } reply := new(struct{ Value AppState }) @@ -265,11 +235,11 @@ func (wd *remoteWD) AppState(bundleId string) (runState AppState, err error) { return } -func (wd *remoteWD) IsLocked() (locked bool, err error) { +func (wd *wdaDriver) IsLocked() (locked bool, err error) { // [[FBRoute GET:@"/wda/locked"] respondWithTarget:self action:@selector(handleIsLocked:)] // [[FBRoute GET:@"/wda/locked"].withoutSession var rawResp rawResponse - if rawResp, err = wd.executeGet("/session", wd.sessionId, "/wda/locked"); err != nil { + if rawResp, err = wd.httpGET("/session", wd.sessionId, "/wda/locked"); err != nil { return false, err } if locked, err = rawResp.valueConvertToBool(); err != nil { @@ -278,31 +248,31 @@ func (wd *remoteWD) IsLocked() (locked bool, err error) { return } -func (wd *remoteWD) Unlock() (err error) { +func (wd *wdaDriver) Unlock() (err error) { // [[FBRoute POST:@"/wda/unlock"] respondWithTarget:self action:@selector(handleUnlock:)] // [[FBRoute POST:@"/wda/unlock"].withoutSession - _, err = wd.executePost(nil, "/session", wd.sessionId, "/wda/unlock") + _, err = wd.httpPOST(nil, "/session", wd.sessionId, "/wda/unlock") return } -func (wd *remoteWD) Lock() (err error) { +func (wd *wdaDriver) Lock() (err error) { // [[FBRoute POST:@"/wda/lock"] respondWithTarget:self action:@selector(handleLock:)] // [[FBRoute POST:@"/wda/lock"].withoutSession - _, err = wd.executePost(nil, "/session", wd.sessionId, "/wda/lock") + _, err = wd.httpPOST(nil, "/session", wd.sessionId, "/wda/lock") return } -func (wd *remoteWD) Homescreen() (err error) { +func (wd *wdaDriver) Homescreen() (err error) { // [[FBRoute POST:@"/wda/homescreen"].withoutSession respondWithTarget:self action:@selector(handleHomescreenCommand:)] - _, err = wd.executePost(nil, "/wda/homescreen") + _, err = wd.httpPOST(nil, "/wda/homescreen") return } -func (wd *remoteWD) AlertText() (text string, err error) { +func (wd *wdaDriver) AlertText() (text string, err error) { // [[FBRoute GET:@"/alert/text"] respondWithTarget:self action:@selector(handleAlertGetTextCommand:)] // [[FBRoute GET:@"/alert/text"].withoutSession var rawResp rawResponse - if rawResp, err = wd.executeGet("/session", wd.sessionId, "/alert/text"); err != nil { + if rawResp, err = wd.httpGET("/session", wd.sessionId, "/alert/text"); err != nil { return "", err } if text, err = rawResp.valueConvertToString(); err != nil { @@ -311,10 +281,10 @@ func (wd *remoteWD) AlertText() (text string, err error) { return } -func (wd *remoteWD) AlertButtons() (btnLabels []string, err error) { +func (wd *wdaDriver) AlertButtons() (btnLabels []string, err error) { // [[FBRoute GET:@"/wda/alert/buttons"] respondWithTarget:self action:@selector(handleGetAlertButtonsCommand:)] var rawResp rawResponse - if rawResp, err = wd.executeGet("/session", wd.sessionId, "/wda/alert/buttons"); err != nil { + if rawResp, err = wd.httpGET("/session", wd.sessionId, "/wda/alert/buttons"); err != nil { return nil, err } reply := new(struct{ Value []string }) @@ -325,58 +295,58 @@ func (wd *remoteWD) AlertButtons() (btnLabels []string, err error) { return } -func (wd *remoteWD) AlertAccept(label ...string) (err error) { +func (wd *wdaDriver) AlertAccept(label ...string) (err error) { // [[FBRoute POST:@"/alert/accept"] respondWithTarget:self action:@selector(handleAlertAcceptCommand:)] // [[FBRoute POST:@"/alert/accept"].withoutSession data := make(map[string]interface{}) if len(label) != 0 && label[0] != "" { data["name"] = label[0] } - _, err = wd.executePost(data, "/alert/accept") + _, err = wd.httpPOST(data, "/alert/accept") return } -func (wd *remoteWD) AlertDismiss(label ...string) (err error) { +func (wd *wdaDriver) AlertDismiss(label ...string) (err error) { // [[FBRoute POST:@"/alert/dismiss"] respondWithTarget:self action:@selector(handleAlertDismissCommand:)] // [[FBRoute POST:@"/alert/dismiss"].withoutSession data := make(map[string]interface{}) if len(label) != 0 && label[0] != "" { data["name"] = label[0] } - _, err = wd.executePost(data, "/alert/dismiss") + _, err = wd.httpPOST(data, "/alert/dismiss") return } -func (wd *remoteWD) AlertSendKeys(text string) (err error) { +func (wd *wdaDriver) AlertSendKeys(text string) (err error) { // [[FBRoute POST:@"/alert/text"] respondWithTarget:self action:@selector(handleAlertSetTextCommand:)] data := map[string]interface{}{"value": strings.Split(text, "")} - _, err = wd.executePost(data, "/session", wd.sessionId, "/alert/text") + _, err = wd.httpPOST(data, "/session", wd.sessionId, "/alert/text") return } -func (wd *remoteWD) AppLaunch(bundleId string, launchOpt ...AppLaunchOption) (err error) { +func (wd *wdaDriver) AppLaunch(bundleId string, launchOpt ...AppLaunchOption) (err error) { // [[FBRoute POST:@"/wda/apps/launch"] respondWithTarget:self action:@selector(handleSessionAppLaunch:)] data := make(map[string]interface{}) if len(launchOpt) != 0 { data = launchOpt[0] } data["bundleId"] = bundleId - _, err = wd.executePost(data, "/session", wd.sessionId, "/wda/apps/launch") + _, err = wd.httpPOST(data, "/session", wd.sessionId, "/wda/apps/launch") return } -func (wd *remoteWD) AppLaunchUnattached(bundleId string) (err error) { +func (wd *wdaDriver) AppLaunchUnattached(bundleId string) (err error) { // [[FBRoute POST:@"/wda/apps/launchUnattached"].withoutSession respondWithTarget:self action:@selector(handleLaunchUnattachedApp:)] data := map[string]interface{}{"bundleId": bundleId} - _, err = wd.executePost(data, "/wda/apps/launchUnattached") + _, err = wd.httpPOST(data, "/wda/apps/launchUnattached") return } -func (wd *remoteWD) AppTerminate(bundleId string) (successful bool, err error) { +func (wd *wdaDriver) AppTerminate(bundleId string) (successful bool, err error) { // [[FBRoute POST:@"/wda/apps/terminate"] respondWithTarget:self action:@selector(handleSessionAppTerminate:)] data := map[string]interface{}{"bundleId": bundleId} var rawResp rawResponse - if rawResp, err = wd.executePost(data, "/session", wd.sessionId, "/wda/apps/terminate"); err != nil { + if rawResp, err = wd.httpPOST(data, "/session", wd.sessionId, "/wda/apps/terminate"); err != nil { return false, err } if successful, err = rawResp.valueConvertToBool(); err != nil { @@ -385,35 +355,35 @@ func (wd *remoteWD) AppTerminate(bundleId string) (successful bool, err error) { return } -func (wd *remoteWD) AppActivate(bundleId string) (err error) { +func (wd *wdaDriver) AppActivate(bundleId string) (err error) { // [[FBRoute POST:@"/wda/apps/activate"] respondWithTarget:self action:@selector(handleSessionAppActivate:)] data := map[string]interface{}{"bundleId": bundleId} - _, err = wd.executePost(data, "/session", wd.sessionId, "/wda/apps/activate") + _, err = wd.httpPOST(data, "/session", wd.sessionId, "/wda/apps/activate") return } -func (wd *remoteWD) AppDeactivate(second float64) (err error) { +func (wd *wdaDriver) AppDeactivate(second float64) (err error) { // [[FBRoute POST:@"/wda/deactivateApp"] respondWithTarget:self action:@selector(handleDeactivateAppCommand:)] if second < 3 { second = 3.0 } data := map[string]interface{}{"duration": second} - _, err = wd.executePost(data, "/session", wd.sessionId, "/wda/deactivateApp") + _, err = wd.httpPOST(data, "/session", wd.sessionId, "/wda/deactivateApp") return } -func (wd *remoteWD) AppAuthReset(resource ProtectedResource) (err error) { +func (wd *wdaDriver) AppAuthReset(resource ProtectedResource) (err error) { // [[FBRoute POST:@"/wda/resetAppAuth"] respondWithTarget:self action:@selector(handleResetAppAuth:)] data := map[string]interface{}{"resource": resource} - _, err = wd.executePost(data, "/session", wd.sessionId, "/wda/resetAppAuth") + _, err = wd.httpPOST(data, "/session", wd.sessionId, "/wda/resetAppAuth") return } -func (wd *remoteWD) Tap(x, y int, options ...DataOption) error { +func (wd *wdaDriver) Tap(x, y int, options ...DataOption) error { return wd.TapFloat(float64(x), float64(y), options...) } -func (wd *remoteWD) TapFloat(x, y float64, options ...DataOption) (err error) { +func (wd *wdaDriver) TapFloat(x, y float64, options ...DataOption) (err error) { // [[FBRoute POST:@"/wda/tap/:uuid"] respondWithTarget:self action:@selector(handleTap:)] data := map[string]interface{}{ "x": x, @@ -424,29 +394,29 @@ func (wd *remoteWD) TapFloat(x, y float64, options ...DataOption) (err error) { for _, option := range options { option(data) } - _, err = wd.executePost(data, "/session", wd.sessionId, "/wda/tap/0") + _, err = wd.httpPOST(data, "/session", wd.sessionId, "/wda/tap/0") return } -func (wd *remoteWD) DoubleTap(x, y int) error { +func (wd *wdaDriver) DoubleTap(x, y int) error { return wd.DoubleTapFloat(float64(x), float64(y)) } -func (wd *remoteWD) DoubleTapFloat(x, y float64) (err error) { +func (wd *wdaDriver) DoubleTapFloat(x, y float64) (err error) { // [[FBRoute POST:@"/wda/doubleTap"] respondWithTarget:self action:@selector(handleDoubleTapCoordinate:)] data := map[string]interface{}{ "x": x, "y": y, } - _, err = wd.executePost(data, "/session", wd.sessionId, "/wda/doubleTap") + _, err = wd.httpPOST(data, "/session", wd.sessionId, "/wda/doubleTap") return } -func (wd *remoteWD) TouchAndHold(x, y int, second ...float64) error { +func (wd *wdaDriver) TouchAndHold(x, y int, second ...float64) error { return wd.TouchAndHoldFloat(float64(x), float64(y), second...) } -func (wd *remoteWD) TouchAndHoldFloat(x, y float64, second ...float64) (err error) { +func (wd *wdaDriver) TouchAndHoldFloat(x, y float64, second ...float64) (err error) { // [[FBRoute POST:@"/wda/touchAndHold"] respondWithTarget:self action:@selector(handleTouchAndHoldCoordinate:)] data := map[string]interface{}{ "x": x, @@ -456,15 +426,15 @@ func (wd *remoteWD) TouchAndHoldFloat(x, y float64, second ...float64) (err erro second = []float64{1.0} } data["duration"] = second[0] - _, err = wd.executePost(data, "/session", wd.sessionId, "/wda/touchAndHold") + _, err = wd.httpPOST(data, "/session", wd.sessionId, "/wda/touchAndHold") return } -func (wd *remoteWD) Drag(fromX, fromY, toX, toY int, options ...DataOption) error { +func (wd *wdaDriver) 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, options ...DataOption) (err error) { +func (wd *wdaDriver) 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, @@ -482,25 +452,25 @@ func (wd *remoteWD) DragFloat(fromX, fromY, toX, toY float64, options ...DataOpt if _, ok := data["duration"]; !ok { data["duration"] = 1.0 // default duration } - _, err = wd.executePost(data, "/session", wd.sessionId, "/wda/dragfromtoforduration") + _, err = wd.httpPOST(data, "/session", wd.sessionId, "/wda/dragfromtoforduration") return } -func (wd *remoteWD) Swipe(fromX, fromY, toX, toY int, options ...DataOption) error { +func (wd *wdaDriver) 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 { +func (wd *wdaDriver) SwipeFloat(fromX, fromY, toX, toY float64, options ...DataOption) error { options = append(options, WithPressDuration(0)) return wd.DragFloat(fromX, fromY, toX, toY, options...) } -func (wd *remoteWD) ForceTouch(x, y int, pressure float64, second ...float64) error { +func (wd *wdaDriver) ForceTouch(x, y int, pressure float64, second ...float64) error { return wd.ForceTouchFloat(float64(x), float64(y), pressure, second...) } -func (wd *remoteWD) ForceTouchFloat(x, y, pressure float64, second ...float64) error { +func (wd *wdaDriver) ForceTouchFloat(x, y, pressure float64, second ...float64) error { if len(second) == 0 || second[0] <= 0 { second = []float64{1.0} } @@ -512,36 +482,36 @@ func (wd *remoteWD) ForceTouchFloat(x, y, pressure float64, second ...float64) e return wd.PerformAppiumTouchActions(actions) } -func (wd *remoteWD) PerformW3CActions(actions *W3CActions) (err error) { +func (wd *wdaDriver) PerformW3CActions(actions *W3CActions) (err error) { // [[FBRoute POST:@"/actions"] respondWithTarget:self action:@selector(handlePerformW3CTouchActions:)] data := map[string]interface{}{"actions": actions} - _, err = wd.executePost(data, "/session", wd.sessionId, "/actions") + _, err = wd.httpPOST(data, "/session", wd.sessionId, "/actions") return } -func (wd *remoteWD) PerformAppiumTouchActions(touchActs *TouchActions) (err error) { +func (wd *wdaDriver) PerformAppiumTouchActions(touchActs *TouchActions) (err error) { // [[FBRoute POST:@"/wda/touch/perform"] respondWithTarget:self action:@selector(handlePerformAppiumTouchActions:)] // [[FBRoute POST:@"/wda/touch/multi/perform"] data := map[string]interface{}{"actions": touchActs} - _, err = wd.executePost(data, "/session", wd.sessionId, "/wda/touch/multi/perform") + _, err = wd.httpPOST(data, "/session", wd.sessionId, "/wda/touch/multi/perform") return } -func (wd *remoteWD) SetPasteboard(contentType PasteboardType, content string) (err error) { +func (wd *wdaDriver) SetPasteboard(contentType PasteboardType, content string) (err error) { // [[FBRoute POST:@"/wda/setPasteboard"] respondWithTarget:self action:@selector(handleSetPasteboard:)] data := map[string]interface{}{ "contentType": contentType, "content": base64.StdEncoding.EncodeToString([]byte(content)), } - _, err = wd.executePost(data, "/session", wd.sessionId, "/wda/setPasteboard") + _, err = wd.httpPOST(data, "/session", wd.sessionId, "/wda/setPasteboard") return } -func (wd *remoteWD) GetPasteboard(contentType PasteboardType) (raw *bytes.Buffer, err error) { +func (wd *wdaDriver) GetPasteboard(contentType PasteboardType) (raw *bytes.Buffer, err error) { // [[FBRoute POST:@"/wda/getPasteboard"] respondWithTarget:self action:@selector(handleGetPasteboard:)] data := map[string]interface{}{"contentType": contentType} var rawResp rawResponse - if rawResp, err = wd.executePost(data, "/session", wd.sessionId, "/wda/getPasteboard"); err != nil { + if rawResp, err = wd.httpPOST(data, "/session", wd.sessionId, "/wda/getPasteboard"); err != nil { return nil, err } if raw, err = rawResp.valueDecodeAsBase64(); err != nil { @@ -550,7 +520,7 @@ func (wd *remoteWD) GetPasteboard(contentType PasteboardType) (raw *bytes.Buffer return } -func (wd *remoteWD) SendKeys(text string, options ...DataOption) (err error) { +func (wd *wdaDriver) SendKeys(text string, options ...DataOption) (err error) { // [[FBRoute POST:@"/wda/keys"] respondWithTarget:self action:@selector(handleKeys:)] data := map[string]interface{}{"value": strings.Split(text, "")} @@ -563,28 +533,28 @@ func (wd *remoteWD) SendKeys(text string, options ...DataOption) (err error) { if _, ok := data["frequency"]; !ok { data["frequency"] = 60 // default frequency } - _, err = wd.executePost(data, "/session", wd.sessionId, "/wda/keys") + _, err = wd.httpPOST(data, "/session", wd.sessionId, "/wda/keys") return } -func (wd *remoteWD) KeyboardDismiss(keyNames ...string) (err error) { +func (wd *wdaDriver) KeyboardDismiss(keyNames ...string) (err error) { // [[FBRoute POST:@"/wda/keyboard/dismiss"] respondWithTarget:self action:@selector(handleDismissKeyboardCommand:)] if len(keyNames) == 0 { keyNames = []string{"return"} } data := map[string]interface{}{"keyNames": keyNames} - _, err = wd.executePost(data, "/session", wd.sessionId, "/wda/keyboard/dismiss") + _, err = wd.httpPOST(data, "/session", wd.sessionId, "/wda/keyboard/dismiss") return } -func (wd *remoteWD) PressButton(devBtn DeviceButton) (err error) { +func (wd *wdaDriver) PressButton(devBtn DeviceButton) (err error) { // [[FBRoute POST:@"/wda/pressButton"] respondWithTarget:self action:@selector(handlePressButtonCommand:)] data := map[string]interface{}{"name": devBtn} - _, err = wd.executePost(data, "/session", wd.sessionId, "/wda/pressButton") + _, err = wd.httpPOST(data, "/session", wd.sessionId, "/wda/pressButton") return } -func (wd *remoteWD) IOHIDEvent(pageID EventPageID, usageID EventUsageID, duration ...float64) (err error) { +func (wd *wdaDriver) IOHIDEvent(pageID EventPageID, usageID EventUsageID, duration ...float64) (err error) { // [[FBRoute POST:@"/wda/performIoHidEvent"] respondWithTarget:self action:@selector(handlePeformIOHIDEvent:)] if len(duration) == 0 || duration[0] <= 0 { duration = []float64{0.005} @@ -594,11 +564,11 @@ func (wd *remoteWD) IOHIDEvent(pageID EventPageID, usageID EventUsageID, duratio "usage": usageID, "duration": duration[0], } - _, err = wd.executePost(data, "/session", wd.sessionId, "/wda/performIoHidEvent") + _, err = wd.httpPOST(data, "/session", wd.sessionId, "/wda/performIoHidEvent") return } -func (wd *remoteWD) ExpectNotification(notifyName string, notifyType NotificationType, second ...int) (err error) { +func (wd *wdaDriver) ExpectNotification(notifyName string, notifyType NotificationType, second ...int) (err error) { // [[FBRoute POST:@"/wda/expectNotification"] respondWithTarget:self action:@selector(handleExpectNotification:)] if len(second) == 0 { second = []int{60} @@ -608,28 +578,28 @@ func (wd *remoteWD) ExpectNotification(notifyName string, notifyType Notificatio "type": notifyType, "timeout": second[0], } - _, err = wd.executePost(data, "/session", wd.sessionId, "/wda/expectNotification") + _, err = wd.httpPOST(data, "/session", wd.sessionId, "/wda/expectNotification") return } -func (wd *remoteWD) SiriActivate(text string) (err error) { +func (wd *wdaDriver) SiriActivate(text string) (err error) { // [[FBRoute POST:@"/wda/siri/activate"] respondWithTarget:self action:@selector(handleActivateSiri:)] data := map[string]interface{}{"text": text} - _, err = wd.executePost(data, "/session", wd.sessionId, "/wda/siri/activate") + _, err = wd.httpPOST(data, "/session", wd.sessionId, "/wda/siri/activate") return } -func (wd *remoteWD) SiriOpenUrl(url string) (err error) { +func (wd *wdaDriver) SiriOpenUrl(url string) (err error) { // [[FBRoute POST:@"/url"] respondWithTarget:self action:@selector(handleOpenURL:)] data := map[string]interface{}{"url": url} - _, err = wd.executePost(data, "/session", wd.sessionId, "/url") + _, err = wd.httpPOST(data, "/session", wd.sessionId, "/url") return } -func (wd *remoteWD) Orientation() (orientation Orientation, err error) { +func (wd *wdaDriver) Orientation() (orientation Orientation, err error) { // [[FBRoute GET:@"/orientation"] respondWithTarget:self action:@selector(handleGetOrientation:)] var rawResp rawResponse - if rawResp, err = wd.executeGet("/session", wd.sessionId, "/orientation"); err != nil { + if rawResp, err = wd.httpGET("/session", wd.sessionId, "/orientation"); err != nil { return "", err } reply := new(struct{ Value Orientation }) @@ -640,17 +610,17 @@ func (wd *remoteWD) Orientation() (orientation Orientation, err error) { return } -func (wd *remoteWD) SetOrientation(orientation Orientation) (err error) { +func (wd *wdaDriver) SetOrientation(orientation Orientation) (err error) { // [[FBRoute POST:@"/orientation"] respondWithTarget:self action:@selector(handleSetOrientation:)] data := map[string]interface{}{"orientation": orientation} - _, err = wd.executePost(data, "/session", wd.sessionId, "/orientation") + _, err = wd.httpPOST(data, "/session", wd.sessionId, "/orientation") return } -func (wd *remoteWD) Rotation() (rotation Rotation, err error) { +func (wd *wdaDriver) Rotation() (rotation Rotation, err error) { // [[FBRoute GET:@"/rotation"] respondWithTarget:self action:@selector(handleGetRotation:)] var rawResp rawResponse - if rawResp, err = wd.executeGet("/session", wd.sessionId, "/rotation"); err != nil { + if rawResp, err = wd.httpGET("/session", wd.sessionId, "/rotation"); err != nil { return Rotation{}, err } reply := new(struct{ Value Rotation }) @@ -661,34 +631,34 @@ func (wd *remoteWD) Rotation() (rotation Rotation, err error) { return } -func (wd *remoteWD) SetRotation(rotation Rotation) (err error) { +func (wd *wdaDriver) SetRotation(rotation Rotation) (err error) { // [[FBRoute POST:@"/rotation"] respondWithTarget:self action:@selector(handleSetRotation:)] - _, err = wd.executePost(rotation, "/session", wd.sessionId, "/rotation") + _, err = wd.httpPOST(rotation, "/session", wd.sessionId, "/rotation") return } -func (wd *remoteWD) MatchTouchID(isMatch bool) (err error) { +func (wd *wdaDriver) MatchTouchID(isMatch bool) (err error) { // [FBRoute POST:@"/wda/touch_id"] data := map[string]interface{}{"match": isMatch} - _, err = wd.executePost(data, "/session", wd.sessionId, "/wda/touch_id") + _, err = wd.httpPOST(data, "/session", wd.sessionId, "/wda/touch_id") return } -func (wd *remoteWD) ActiveElement() (element WebElement, err error) { +func (wd *wdaDriver) ActiveElement() (element WebElement, err error) { // [[FBRoute GET:@"/element/active"] respondWithTarget:self action:@selector(handleGetActiveElement:)] var rawResp rawResponse - if rawResp, err = wd.executeGet("/session", wd.sessionId, "/element/active"); err != nil { + if rawResp, err = wd.httpGET("/session", wd.sessionId, "/element/active"); err != nil { return nil, err } var elementID string if elementID, err = rawResp.valueConvertToElementID(); err != nil { return nil, err } - element = &remoteWE{parent: wd, id: elementID} + element = &wdaElement{parent: wd, id: elementID} return } -func (wd *remoteWD) FindElement(by BySelector) (element WebElement, err error) { +func (wd *wdaDriver) FindElement(by BySelector) (element WebElement, err error) { // [[FBRoute POST:@"/element"] respondWithTarget:self action:@selector(handleFindElement:)] using, value := by.getUsingAndValue() data := map[string]interface{}{ @@ -696,7 +666,7 @@ func (wd *remoteWD) FindElement(by BySelector) (element WebElement, err error) { "value": value, } var rawResp rawResponse - if rawResp, err = wd.executePost(data, "/session", wd.sessionId, "/element"); err != nil { + if rawResp, err = wd.httpPOST(data, "/session", wd.sessionId, "/element"); err != nil { return nil, err } var elementID string @@ -706,11 +676,11 @@ func (wd *remoteWD) FindElement(by BySelector) (element WebElement, err error) { } return nil, err } - element = &remoteWE{parent: wd, id: elementID} + element = &wdaElement{parent: wd, id: elementID} return } -func (wd *remoteWD) FindElements(by BySelector) (elements []WebElement, err error) { +func (wd *wdaDriver) FindElements(by BySelector) (elements []WebElement, err error) { // [[FBRoute POST:@"/elements"] respondWithTarget:self action:@selector(handleFindElements:)] using, value := by.getUsingAndValue() data := map[string]interface{}{ @@ -718,7 +688,7 @@ func (wd *remoteWD) FindElements(by BySelector) (elements []WebElement, err erro "value": value, } var rawResp rawResponse - if rawResp, err = wd.executePost(data, "/session", wd.sessionId, "/elements"); err != nil { + if rawResp, err = wd.httpPOST(data, "/session", wd.sessionId, "/elements"); err != nil { return nil, err } var elementIDs []string @@ -730,16 +700,16 @@ func (wd *remoteWD) FindElements(by BySelector) (elements []WebElement, err erro } elements = make([]WebElement, len(elementIDs)) for i := range elementIDs { - elements[i] = &remoteWE{parent: wd, id: elementIDs[i]} + elements[i] = &wdaElement{parent: wd, id: elementIDs[i]} } return } -func (wd *remoteWD) Screenshot() (raw *bytes.Buffer, err error) { +func (wd *wdaDriver) Screenshot() (raw *bytes.Buffer, err error) { // [[FBRoute GET:@"/screenshot"] respondWithTarget:self action:@selector(handleGetScreenshot:)] // [[FBRoute GET:@"/screenshot"].withoutSession respondWithTarget:self action:@selector(handleGetScreenshot:)] var rawResp rawResponse - if rawResp, err = wd.executeGet("/session", wd.sessionId, "/screenshot"); err != nil { + if rawResp, err = wd.httpGET("/session", wd.sessionId, "/screenshot"); err != nil { return nil, err } @@ -749,10 +719,10 @@ func (wd *remoteWD) Screenshot() (raw *bytes.Buffer, err error) { return } -func (wd *remoteWD) Source(srcOpt ...SourceOption) (source string, err error) { +func (wd *wdaDriver) Source(srcOpt ...SourceOption) (source string, err error) { // [[FBRoute GET:@"/source"] respondWithTarget:self action:@selector(handleGetSourceCommand:)] // [[FBRoute GET:@"/source"].withoutSession - tmp, _ := url.Parse(wd._requestURL(nil, "/session", wd.sessionId)) + tmp, _ := url.Parse(wd.concatURL(nil, "/session", wd.sessionId)) toJsonRaw := false if len(srcOpt) != 0 { q := tmp.Query() @@ -767,7 +737,7 @@ func (wd *remoteWD) Source(srcOpt ...SourceOption) (source string, err error) { } var rawResp rawResponse - if rawResp, err = wd.executeHTTP(http.MethodGet, wd._requestURL(tmp, "/source"), nil); err != nil { + if rawResp, err = wd.httpRequest(http.MethodGet, wd.concatURL(tmp, "/source"), nil); err != nil { return "", nil } if toJsonRaw { @@ -783,11 +753,11 @@ func (wd *remoteWD) Source(srcOpt ...SourceOption) (source string, err error) { return } -func (wd *remoteWD) AccessibleSource() (source string, err error) { +func (wd *wdaDriver) AccessibleSource() (source string, err error) { // [[FBRoute GET:@"/wda/accessibleSource"] respondWithTarget:self action:@selector(handleGetAccessibleSourceCommand:)] // [[FBRoute GET:@"/wda/accessibleSource"].withoutSession var rawResp rawResponse - if rawResp, err = wd.executeGet("/session", wd.sessionId, "/wda/accessibleSource"); err != nil { + if rawResp, err = wd.httpGET("/session", wd.sessionId, "/wda/accessibleSource"); err != nil { return "", err } var jr builtinJSON.RawMessage @@ -798,16 +768,16 @@ func (wd *remoteWD) AccessibleSource() (source string, err error) { return } -func (wd *remoteWD) HealthCheck() (err error) { +func (wd *wdaDriver) HealthCheck() (err error) { // [[FBRoute GET:@"/wda/healthcheck"].withoutSession respondWithTarget:self action:@selector(handleGetHealthCheck:)] - _, err = wd.executeGet("/wda/healthcheck") + _, err = wd.httpGET("/wda/healthcheck") return } -func (wd *remoteWD) GetAppiumSettings() (settings map[string]interface{}, err error) { +func (wd *wdaDriver) GetAppiumSettings() (settings map[string]interface{}, err error) { // [[FBRoute GET:@"/appium/settings"] respondWithTarget:self action:@selector(handleGetSettings:)] var rawResp rawResponse - if rawResp, err = wd.executeGet("/session", wd.sessionId, "/appium/settings"); err != nil { + if rawResp, err = wd.httpGET("/session", wd.sessionId, "/appium/settings"); err != nil { return nil, err } reply := new(struct{ Value map[string]interface{} }) @@ -818,11 +788,11 @@ func (wd *remoteWD) GetAppiumSettings() (settings map[string]interface{}, err er return } -func (wd *remoteWD) SetAppiumSettings(settings map[string]interface{}) (ret map[string]interface{}, err error) { +func (wd *wdaDriver) SetAppiumSettings(settings map[string]interface{}) (ret map[string]interface{}, err error) { // [[FBRoute POST:@"/appium/settings"] respondWithTarget:self action:@selector(handleSetSettings:)] data := map[string]interface{}{"settings": settings} var rawResp rawResponse - if rawResp, err = wd.executePost(data, "/session", wd.sessionId, "/appium/settings"); err != nil { + if rawResp, err = wd.httpPOST(data, "/session", wd.sessionId, "/appium/settings"); err != nil { return nil, err } reply := new(struct{ Value map[string]interface{} }) @@ -833,9 +803,9 @@ func (wd *remoteWD) SetAppiumSettings(settings map[string]interface{}) (ret map[ return } -func (wd *remoteWD) IsHealthy() (healthy bool, err error) { +func (wd *wdaDriver) IsHealthy() (healthy bool, err error) { var rawResp rawResponse - if rawResp, err = wd.executeGet("/health"); err != nil { + if rawResp, err = wd.httpGET("/health"); err != nil { return false, err } if string(rawResp) != "I-AM-ALIVE" { @@ -844,12 +814,12 @@ func (wd *remoteWD) IsHealthy() (healthy bool, err error) { return true, nil } -func (wd *remoteWD) WdaShutdown() (err error) { - _, err = wd.executeGet("/wda/shutdown") +func (wd *wdaDriver) WdaShutdown() (err error) { + _, err = wd.httpGET("/wda/shutdown") return } -func (wd *remoteWD) WaitWithTimeoutAndInterval(condition Condition, timeout, interval time.Duration) error { +func (wd *wdaDriver) WaitWithTimeoutAndInterval(condition Condition, timeout, interval time.Duration) error { startTime := time.Now() for { done, err := condition(wd) @@ -867,23 +837,51 @@ func (wd *remoteWD) WaitWithTimeoutAndInterval(condition Condition, timeout, int } } -func (wd *remoteWD) WaitWithTimeout(condition Condition, timeout time.Duration) error { +func (wd *wdaDriver) WaitWithTimeout(condition Condition, timeout time.Duration) error { return wd.WaitWithTimeoutAndInterval(condition, timeout, DefaultWaitInterval) } -func (wd *remoteWD) Wait(condition Condition) error { +func (wd *wdaDriver) Wait(condition Condition) error { return wd.WaitWithTimeoutAndInterval(condition, DefaultWaitTimeout, DefaultWaitInterval) } -// HTTPClient The default client to use to communicate with the WebDriver server. -var HTTPClient = http.DefaultClient - -func (wd *remoteWD) executeHTTP(method string, rawURL string, rawBody []byte) (rawResp rawResponse, err error) { - log.Debug().Str("method", method).Str("url", rawURL).Str("body", string(rawBody)).Msg("request WDA") - var req *http.Request - if req, err = newRequest(method, rawURL, rawBody); err != nil { - return +func (wd *wdaDriver) concatURL(u *url.URL, elem ...string) string { + var tmp *url.URL + if u == nil { + u = wd.urlPrefix } + tmp, _ = url.Parse(u.String()) + tmp.Path = path.Join(append([]string{u.Path}, elem...)...) + return tmp.String() +} + +func (wd *wdaDriver) httpGET(pathElem ...string) (rawResp rawResponse, err error) { + return wd.httpRequest(http.MethodGet, wd.concatURL(nil, pathElem...), nil) +} + +func (wd *wdaDriver) httpPOST(data interface{}, pathElem ...string) (rawResp rawResponse, err error) { + var bsJSON []byte = nil + if data != nil { + if bsJSON, err = json.Marshal(data); err != nil { + return nil, err + } + } + return wd.httpRequest(http.MethodPost, wd.concatURL(nil, pathElem...), bsJSON) +} + +func (wd *wdaDriver) httpDELETE(pathElem ...string) (rawResp rawResponse, err error) { + return wd.httpRequest(http.MethodDelete, wd.concatURL(nil, pathElem...), nil) +} + +func (wd *wdaDriver) httpRequest(method string, rawURL string, rawBody []byte) (rawResp rawResponse, err error) { + log.Debug().Str("method", method).Str("url", rawURL).Str("body", string(rawBody)).Msg("request WDA") + + var req *http.Request + if req, err = http.NewRequest(method, rawURL, bytes.NewBuffer(rawBody)); err != nil { + return nil, err + } + req.Header.Set("Content-Type", "application/json;charset=UTF-8") + req.Header.Set("Accept", "application/json") var httpCli *http.Client if wd.usbCli != nil { @@ -891,7 +889,7 @@ func (wd *remoteWD) executeHTTP(method string, rawURL string, rawBody []byte) (r defer wd.usbCli.Unlock() httpCli = wd.usbCli.httpCli } else { - httpCli = HTTPClient + httpCli = http.DefaultClient } httpCli.Timeout = 0 diff --git a/hrp/internal/uixt/ios_webelement.go b/hrp/internal/uixt/ios_element.go similarity index 63% rename from hrp/internal/uixt/ios_webelement.go rename to hrp/internal/uixt/ios_element.go index 805a67c2..16479efe 100644 --- a/hrp/internal/uixt/ios_webelement.go +++ b/hrp/internal/uixt/ios_element.go @@ -14,72 +14,72 @@ import ( // 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 +type wdaElement struct { + parent *wdaDriver id string // element_id } -func (we remoteWE) Click() (err error) { +func (we wdaElement) Click() (err error) { // [[FBRoute POST:@"/element/:uuid/click"] respondWithTarget:self action:@selector(handleClick:)] - _, err = we.parent.executePost(nil, "/session", we.parent.sessionId, "/element", we.id, "/click") + _, err = we.parent.httpPOST(nil, "/session", we.parent.sessionId, "/element", we.id, "/click") return } -func (we remoteWE) SendKeys(text string, frequency ...int) (err error) { +func (we wdaElement) SendKeys(text string, frequency ...int) (err error) { // [[FBRoute POST:@"/element/:uuid/value"] respondWithTarget:self action:@selector(handleSetValue:)] data := map[string]interface{}{"value": strings.Split(text, "")} if len(frequency) == 0 || frequency[0] <= 0 { frequency = []int{60} } data["frequency"] = frequency[0] - _, err = we.parent.executePost(data, "/session", we.parent.sessionId, "/element", we.id, "/value") + _, err = we.parent.httpPOST(data, "/session", we.parent.sessionId, "/element", we.id, "/value") return } -func (we remoteWE) Clear() (err error) { +func (we wdaElement) Clear() (err error) { // [[FBRoute POST:@"/element/:uuid/clear"] respondWithTarget:self action:@selector(handleClear:)] - _, err = we.parent.executePost(nil, "/session", we.parent.sessionId, "/element", we.id, "/clear") + _, err = we.parent.httpPOST(nil, "/session", we.parent.sessionId, "/element", we.id, "/clear") return } -func (we remoteWE) Tap(x, y int) error { +func (we wdaElement) Tap(x, y int) error { return we.TapFloat(float64(x), float64(y)) } -func (we remoteWE) TapFloat(x, y float64) (err error) { +func (we wdaElement) TapFloat(x, y float64) (err error) { // [[FBRoute POST:@"/wda/tap/:uuid"] respondWithTarget:self action:@selector(handleTap:)] data := map[string]interface{}{ "x": x, "y": y, } - _, err = we.parent.executePost(data, "/session", we.parent.sessionId, "/wda/tap/", we.id) + _, err = we.parent.httpPOST(data, "/session", we.parent.sessionId, "/wda/tap/", we.id) return } -func (we remoteWE) DoubleTap() (err error) { +func (we wdaElement) DoubleTap() (err error) { // [[FBRoute POST:@"/wda/element/:uuid/doubleTap"] respondWithTarget:self action:@selector(handleDoubleTap:)] - _, err = we.parent.executePost(nil, "/session", we.parent.sessionId, "/wda/element", we.id, "/doubleTap") + _, err = we.parent.httpPOST(nil, "/session", we.parent.sessionId, "/wda/element", we.id, "/doubleTap") return } -func (we remoteWE) TouchAndHold(second ...float64) (err error) { +func (we wdaElement) TouchAndHold(second ...float64) (err error) { // [[FBRoute POST:@"/wda/element/:uuid/touchAndHold"] respondWithTarget:self action:@selector(handleTouchAndHold:)] data := make(map[string]interface{}) if len(second) == 0 || second[0] <= 0 { second = []float64{1.0} } data["duration"] = second[0] - _, err = we.parent.executePost(data, "/session", we.parent.sessionId, "/wda/element", we.id, "/touchAndHold") + _, err = we.parent.httpPOST(data, "/session", we.parent.sessionId, "/wda/element", we.id, "/touchAndHold") return } -func (we remoteWE) TwoFingerTap() (err error) { +func (we wdaElement) TwoFingerTap() (err error) { // [[FBRoute POST:@"/wda/element/:uuid/twoFingerTap"] respondWithTarget:self action:@selector(handleTwoFingerTap:)] - _, err = we.parent.executePost(nil, "/session", we.parent.sessionId, "/wda/element", we.id, "/twoFingerTap") + _, err = we.parent.httpPOST(nil, "/session", we.parent.sessionId, "/wda/element", we.id, "/twoFingerTap") return } -func (we remoteWE) TapWithNumberOfTaps(numberOfTaps, numberOfTouches int) (err error) { +func (we wdaElement) TapWithNumberOfTaps(numberOfTaps, numberOfTouches int) (err error) { // [[FBRoute POST:@"/wda/element/:uuid/tapWithNumberOfTaps"] respondWithTarget:self action:@selector(handleTapWithNumberOfTaps:)] if numberOfTouches <= 0 { return errors.New("'numberOfTouches' must be greater than zero") @@ -97,15 +97,15 @@ func (we remoteWE) TapWithNumberOfTaps(numberOfTaps, numberOfTouches int) (err e "numberOfTaps": numberOfTaps, "numberOfTouches": numberOfTouches, } - _, err = we.parent.executePost(data, "/session", we.parent.sessionId, "/wda/element", we.id, "/tapWithNumberOfTaps") + _, err = we.parent.httpPOST(data, "/session", we.parent.sessionId, "/wda/element", we.id, "/tapWithNumberOfTaps") return } -func (we remoteWE) ForceTouch(pressure float64, second ...float64) (err error) { +func (we wdaElement) ForceTouch(pressure float64, second ...float64) (err error) { return we.ForceTouchFloat(-1, -1, pressure, second...) } -func (we remoteWE) ForceTouchFloat(x, y, pressure float64, second ...float64) (err error) { +func (we wdaElement) ForceTouchFloat(x, y, pressure float64, second ...float64) (err error) { // [[FBRoute POST:@"/wda/element/:uuid/forceTouch"] respondWithTarget:self action:@selector(handleForceTouch:)] data := make(map[string]interface{}) if x != -1 && y != -1 { @@ -117,15 +117,15 @@ func (we remoteWE) ForceTouchFloat(x, y, pressure float64, second ...float64) (e } data["pressure"] = pressure data["duration"] = second[0] - _, err = we.parent.executePost(data, "/session", we.parent.sessionId, "/wda/element", we.id, "/forceTouch") + _, err = we.parent.httpPOST(data, "/session", we.parent.sessionId, "/wda/element", we.id, "/forceTouch") return } -func (we remoteWE) Drag(fromX, fromY, toX, toY int, pressForDuration ...float64) error { +func (we wdaElement) Drag(fromX, fromY, toX, toY int, pressForDuration ...float64) error { return we.DragFloat(float64(fromX), float64(fromY), float64(toX), float64(toY), pressForDuration...) } -func (we remoteWE) DragFloat(fromX, fromY, toX, toY float64, pressForDuration ...float64) (err error) { +func (we wdaElement) DragFloat(fromX, fromY, toX, toY float64, pressForDuration ...float64) (err error) { // [[FBRoute POST:@"/wda/element/:uuid/dragfromtoforduration"] respondWithTarget:self action:@selector(handleDrag:)] data := map[string]interface{}{ "fromX": fromX, @@ -137,29 +137,29 @@ func (we remoteWE) DragFloat(fromX, fromY, toX, toY float64, pressForDuration .. pressForDuration = []float64{1.0} } data["duration"] = pressForDuration[0] - _, err = we.parent.executePost(data, "/session", we.parent.sessionId, "/wda/element", we.id, "/dragfromtoforduration") + _, err = we.parent.httpPOST(data, "/session", we.parent.sessionId, "/wda/element", we.id, "/dragfromtoforduration") return } -func (we remoteWE) Swipe(fromX, fromY, toX, toY int) error { +func (we wdaElement) Swipe(fromX, fromY, toX, toY int) error { return we.SwipeFloat(float64(fromX), float64(fromY), float64(toX), float64(toY)) } -func (we remoteWE) SwipeFloat(fromX, fromY, toX, toY float64) error { +func (we wdaElement) SwipeFloat(fromX, fromY, toX, toY float64) error { return we.DragFloat(fromX, fromY, toX, toY, 0) } -func (we remoteWE) SwipeDirection(direction Direction, velocity ...float64) (err error) { +func (we wdaElement) SwipeDirection(direction Direction, velocity ...float64) (err error) { // [[FBRoute POST:@"/wda/element/:uuid/swipe"] respondWithTarget:self action:@selector(handleSwipe:)] data := map[string]interface{}{"direction": direction} if len(velocity) != 0 && velocity[0] > 0 { data["velocity"] = velocity[0] } - _, err = we.parent.executePost(data, "/session", we.parent.sessionId, "/wda/element", we.id, "/swipe") + _, err = we.parent.httpPOST(data, "/session", we.parent.sessionId, "/wda/element", we.id, "/swipe") return } -func (we remoteWE) Pinch(scale, velocity float64) (err error) { +func (we wdaElement) Pinch(scale, velocity float64) (err error) { // [[FBRoute POST:@"/wda/element/:uuid/pinch"] respondWithTarget:self action:@selector(handlePinch:)] if scale <= 0 { return errors.New("'scale' must be greater than zero") @@ -177,11 +177,11 @@ func (we remoteWE) Pinch(scale, velocity float64) (err error) { "scale": scale, "velocity": velocity, } - _, err = we.parent.executePost(data, "/session", we.parent.sessionId, "/wda/element", we.id, "/pinch") + _, err = we.parent.httpPOST(data, "/session", we.parent.sessionId, "/wda/element", we.id, "/pinch") return } -func (we remoteWE) PinchToZoomOutByW3CAction(scale ...float64) (err error) { +func (we wdaElement) PinchToZoomOutByW3CAction(scale ...float64) (err error) { if len(scale) == 0 { scale = []float64{1.0} } else if scale[0] > 23 { @@ -198,7 +198,7 @@ func (we remoteWE) PinchToZoomOutByW3CAction(scale ...float64) (err error) { return we.parent.PerformW3CActions(actions) } -func (we remoteWE) Rotate(rotation float64, velocity ...float64) (err error) { +func (we wdaElement) Rotate(rotation float64, velocity ...float64) (err error) { // [[FBRoute POST:@"/wda/element/:uuid/rotate"] respondWithTarget:self action:@selector(handleRotate:)] if rotation > math.Pi*2 || rotation < math.Pi*-2 { return errors.New("'rotation' must not be more than 2π or less than -2π") @@ -213,11 +213,11 @@ func (we remoteWE) Rotate(rotation float64, velocity ...float64) (err error) { "rotation": rotation, "velocity": velocity[0], } - _, err = we.parent.executePost(data, "/session", we.parent.sessionId, "/wda/element", we.id, "/rotate") + _, err = we.parent.httpPOST(data, "/session", we.parent.sessionId, "/wda/element", we.id, "/rotate") return } -func (we remoteWE) PickerWheelSelect(order PickerWheelOrder, offset ...int) (err error) { +func (we wdaElement) PickerWheelSelect(order PickerWheelOrder, offset ...int) (err error) { // [[FBRoute POST:@"/wda/pickerwheel/:uuid/select"] respondWithTarget:self action:@selector(handleWheelSelect:)] if len(offset) == 0 { offset = []int{2} @@ -228,32 +228,32 @@ func (we remoteWE) PickerWheelSelect(order PickerWheelOrder, offset ...int) (err "order": order, "offset": float64(offset[0]) * 0.1, } - _, err = we.parent.executePost(data, "/session", we.parent.sessionId, "/wda/pickerwheel", we.id, "/select") + _, err = we.parent.httpPOST(data, "/session", we.parent.sessionId, "/wda/pickerwheel", we.id, "/select") return } -func (we remoteWE) scroll(data interface{}) (err error) { +func (we wdaElement) scroll(data interface{}) (err error) { // [[FBRoute POST:@"/wda/element/:uuid/scroll"] respondWithTarget:self action:@selector(handleScroll:)] - _, err = we.parent.executePost(data, "/session", we.parent.sessionId, "/wda/element", we.id, "/scroll") + _, err = we.parent.httpPOST(data, "/session", we.parent.sessionId, "/wda/element", we.id, "/scroll") return } -func (we remoteWE) ScrollElementByName(name string) error { +func (we wdaElement) ScrollElementByName(name string) error { data := map[string]interface{}{"name": name} return we.scroll(data) } -func (we remoteWE) ScrollElementByPredicate(predicate string) error { +func (we wdaElement) ScrollElementByPredicate(predicate string) error { data := map[string]interface{}{"predicateString": predicate} return we.scroll(data) } -func (we remoteWE) ScrollToVisible() error { +func (we wdaElement) ScrollToVisible() error { data := map[string]interface{}{"toVisible": true} return we.scroll(data) } -func (we remoteWE) ScrollDirection(direction Direction, distance ...float64) error { +func (we wdaElement) ScrollDirection(direction Direction, distance ...float64) error { if len(distance) == 0 || distance[0] <= 0 { distance = []float64{0.5} } @@ -264,7 +264,7 @@ func (we remoteWE) ScrollDirection(direction Direction, distance ...float64) err return we.scroll(data) } -func (we remoteWE) FindElement(by BySelector) (element WebElement, err error) { +func (we wdaElement) FindElement(by BySelector) (element WebElement, err error) { // [[FBRoute POST:@"/element/:uuid/element"] respondWithTarget:self action:@selector(handleFindSubElement:)] using, value := by.getUsingAndValue() data := map[string]interface{}{ @@ -272,7 +272,7 @@ func (we remoteWE) FindElement(by BySelector) (element WebElement, err error) { "value": value, } var rawResp rawResponse - if rawResp, err = we.parent.executePost(data, "/session", we.parent.sessionId, "/element", we.id, "/element"); err != nil { + if rawResp, err = we.parent.httpPOST(data, "/session", we.parent.sessionId, "/element", we.id, "/element"); err != nil { return nil, err } var elementID string @@ -282,11 +282,11 @@ func (we remoteWE) FindElement(by BySelector) (element WebElement, err error) { } return nil, err } - element = &remoteWE{parent: we.parent, id: elementID} + element = &wdaElement{parent: we.parent, id: elementID} return } -func (we remoteWE) FindElements(by BySelector) (elements []WebElement, err error) { +func (we wdaElement) FindElements(by BySelector) (elements []WebElement, err error) { // [[FBRoute POST:@"/element/:uuid/elements"] respondWithTarget:self action:@selector(handleFindSubElements:)] using, value := by.getUsingAndValue() data := map[string]interface{}{ @@ -294,7 +294,7 @@ func (we remoteWE) FindElements(by BySelector) (elements []WebElement, err error "value": value, } var rawResp rawResponse - if rawResp, err = we.parent.executePost(data, "/session", we.parent.sessionId, "/element", we.id, "/elements"); err != nil { + if rawResp, err = we.parent.httpPOST(data, "/session", we.parent.sessionId, "/element", we.id, "/elements"); err != nil { return nil, err } var elementIDs []string @@ -306,15 +306,15 @@ func (we remoteWE) FindElements(by BySelector) (elements []WebElement, err error } elements = make([]WebElement, len(elementIDs)) for i := range elementIDs { - elements[i] = &remoteWE{parent: we.parent, id: elementIDs[i]} + elements[i] = &wdaElement{parent: we.parent, id: elementIDs[i]} } return } -func (we remoteWE) FindVisibleCells() (elements []WebElement, err error) { +func (we wdaElement) FindVisibleCells() (elements []WebElement, err error) { // [[FBRoute GET:@"/wda/element/:uuid/getVisibleCells"] respondWithTarget:self action:@selector(handleFindVisibleCells:)] var rawResp rawResponse - if rawResp, err = we.parent.executeGet("/session", we.parent.sessionId, "/wda/element", we.id, "/getVisibleCells"); err != nil { + if rawResp, err = we.parent.httpGET("/session", we.parent.sessionId, "/wda/element", we.id, "/getVisibleCells"); err != nil { return nil, err } var elementIDs []string @@ -326,15 +326,15 @@ func (we remoteWE) FindVisibleCells() (elements []WebElement, err error) { } elements = make([]WebElement, len(elementIDs)) for i := range elementIDs { - elements[i] = &remoteWE{parent: we.parent, id: elementIDs[i]} + elements[i] = &wdaElement{parent: we.parent, id: elementIDs[i]} } return } -func (we remoteWE) Rect() (rect Rect, err error) { +func (we wdaElement) Rect() (rect Rect, err error) { // [[FBRoute GET:@"/element/:uuid/rect"] respondWithTarget:self action:@selector(handleGetRect:)] var rawResp rawResponse - if rawResp, err = we.parent.executeGet("/session", we.parent.sessionId, "/element", we.id, "/rect"); err != nil { + if rawResp, err = we.parent.httpGET("/session", we.parent.sessionId, "/element", we.id, "/rect"); err != nil { return Rect{}, err } reply := new(struct{ Value struct{ Rect } }) @@ -345,7 +345,7 @@ func (we remoteWE) Rect() (rect Rect, err error) { return } -func (we remoteWE) Location() (Point, error) { +func (we wdaElement) Location() (Point, error) { rect, err := we.Rect() if err != nil { return Point{}, err @@ -353,7 +353,7 @@ func (we remoteWE) Location() (Point, error) { return rect.Point, nil } -func (we remoteWE) Size() (Size, error) { +func (we wdaElement) Size() (Size, error) { rect, err := we.Rect() if err != nil { return Size{}, err @@ -361,10 +361,10 @@ func (we remoteWE) Size() (Size, error) { return rect.Size, nil } -func (we remoteWE) Text() (text string, err error) { +func (we wdaElement) Text() (text string, err error) { // [[FBRoute GET:@"/element/:uuid/text"] respondWithTarget:self action:@selector(handleGetText:)] var rawResp rawResponse - if rawResp, err = we.parent.executeGet("/session", we.parent.sessionId, "/element", we.id, "/text"); err != nil { + if rawResp, err = we.parent.httpGET("/session", we.parent.sessionId, "/element", we.id, "/text"); err != nil { return "", err } if text, err = rawResp.valueConvertToString(); err != nil { @@ -373,10 +373,10 @@ func (we remoteWE) Text() (text string, err error) { return } -func (we remoteWE) Type() (elemType string, err error) { +func (we wdaElement) Type() (elemType string, err error) { // [[FBRoute GET:@"/element/:uuid/name"] respondWithTarget:self action:@selector(handleGetName:)] var rawResp rawResponse - if rawResp, err = we.parent.executeGet("/session", we.parent.sessionId, "/element", we.id, "/name"); err != nil { + if rawResp, err = we.parent.httpGET("/session", we.parent.sessionId, "/element", we.id, "/name"); err != nil { return "", err } if elemType, err = rawResp.valueConvertToString(); err != nil { @@ -385,10 +385,10 @@ func (we remoteWE) Type() (elemType string, err error) { return } -func (we remoteWE) IsEnabled() (enabled bool, err error) { +func (we wdaElement) IsEnabled() (enabled bool, err error) { // [[FBRoute GET:@"/element/:uuid/enabled"] respondWithTarget:self action:@selector(handleGetEnabled:)] var rawResp rawResponse - if rawResp, err = we.parent.executeGet("/session", we.parent.sessionId, "/element", we.id, "/enabled"); err != nil { + if rawResp, err = we.parent.httpGET("/session", we.parent.sessionId, "/element", we.id, "/enabled"); err != nil { return false, err } if enabled, err = rawResp.valueConvertToBool(); err != nil { @@ -397,10 +397,10 @@ func (we remoteWE) IsEnabled() (enabled bool, err error) { return } -func (we remoteWE) IsDisplayed() (displayed bool, err error) { +func (we wdaElement) IsDisplayed() (displayed bool, err error) { // [[FBRoute GET:@"/element/:uuid/displayed"] respondWithTarget:self action:@selector(handleGetDisplayed:)] var rawResp rawResponse - if rawResp, err = we.parent.executeGet("/session", we.parent.sessionId, "/element", we.id, "/displayed"); err != nil { + if rawResp, err = we.parent.httpGET("/session", we.parent.sessionId, "/element", we.id, "/displayed"); err != nil { return false, err } if displayed, err = rawResp.valueConvertToBool(); err != nil { @@ -409,10 +409,10 @@ func (we remoteWE) IsDisplayed() (displayed bool, err error) { return } -func (we remoteWE) IsSelected() (selected bool, err error) { +func (we wdaElement) IsSelected() (selected bool, err error) { // [[FBRoute GET:@"/element/:uuid/selected"] respondWithTarget:self action:@selector(handleGetSelected:)] var rawResp rawResponse - if rawResp, err = we.parent.executeGet("/session", we.parent.sessionId, "/element", we.id, "/selected"); err != nil { + if rawResp, err = we.parent.httpGET("/session", we.parent.sessionId, "/element", we.id, "/selected"); err != nil { return false, err } if selected, err = rawResp.valueConvertToBool(); err != nil { @@ -421,10 +421,10 @@ func (we remoteWE) IsSelected() (selected bool, err error) { return } -func (we remoteWE) IsAccessible() (accessible bool, err error) { +func (we wdaElement) IsAccessible() (accessible bool, err error) { // [[FBRoute GET:@"/wda/element/:uuid/accessible"] respondWithTarget:self action:@selector(handleGetAccessible:)] var rawResp rawResponse - if rawResp, err = we.parent.executeGet("/session", we.parent.sessionId, "/wda/element", we.id, "/accessible"); err != nil { + if rawResp, err = we.parent.httpGET("/session", we.parent.sessionId, "/wda/element", we.id, "/accessible"); err != nil { return false, err } if accessible, err = rawResp.valueConvertToBool(); err != nil { @@ -433,10 +433,10 @@ func (we remoteWE) IsAccessible() (accessible bool, err error) { return } -func (we remoteWE) IsAccessibilityContainer() (isAccessibilityContainer bool, err error) { +func (we wdaElement) IsAccessibilityContainer() (isAccessibilityContainer bool, err error) { // [[FBRoute GET:@"/wda/element/:uuid/accessibilityContainer"] respondWithTarget:self action:@selector(handleGetIsAccessibilityContainer:)] var rawResp rawResponse - if rawResp, err = we.parent.executeGet("/session", we.parent.sessionId, "/wda/element", we.id, "/accessibilityContainer"); err != nil { + if rawResp, err = we.parent.httpGET("/session", we.parent.sessionId, "/wda/element", we.id, "/accessibilityContainer"); err != nil { return false, err } if isAccessibilityContainer, err = rawResp.valueConvertToBool(); err != nil { @@ -445,10 +445,10 @@ func (we remoteWE) IsAccessibilityContainer() (isAccessibilityContainer bool, er return } -func (we remoteWE) GetAttribute(attr ElementAttribute) (value string, err error) { +func (we wdaElement) GetAttribute(attr ElementAttribute) (value string, err error) { // [[FBRoute GET:@"/element/:uuid/attribute/:name"] respondWithTarget:self action:@selector(handleGetAttribute:)] var rawResp rawResponse - if rawResp, err = we.parent.executeGet("/session", we.parent.sessionId, "/element", we.id, "/attribute", attr.getAttributeName()); err != nil { + if rawResp, err = we.parent.httpGET("/session", we.parent.sessionId, "/element", we.id, "/attribute", attr.getAttributeName()); err != nil { return "", err } if value, err = rawResp.valueConvertToString(); err != nil { @@ -457,17 +457,17 @@ func (we remoteWE) GetAttribute(attr ElementAttribute) (value string, err error) return } -func (we remoteWE) UID() (uid string) { +func (we wdaElement) UID() (uid string) { return we.id } -func (we remoteWE) Screenshot() (raw *bytes.Buffer, err error) { +func (we wdaElement) Screenshot() (raw *bytes.Buffer, err error) { // W3C element screenshot // [[FBRoute GET:@"/element/:uuid/screenshot"] respondWithTarget:self action:@selector(handleElementScreenshot:)] // JSONWP element screenshot // [[FBRoute GET:@"/screenshot/:uuid"] respondWithTarget:self action:@selector(handleElementScreenshot:)] var rawResp rawResponse - if rawResp, err = we.parent.executeGet("/session", we.parent.sessionId, "/element", we.id, "/screenshot"); err != nil { + if rawResp, err = we.parent.httpGET("/session", we.parent.sessionId, "/element", we.id, "/screenshot"); err != nil { return nil, err } if raw, err = rawResp.valueDecodeAsBase64(); err != nil { diff --git a/hrp/internal/uixt/ios_test.go b/hrp/internal/uixt/ios_test.go index a93b0bec..9f6f8236 100644 --- a/hrp/internal/uixt/ios_test.go +++ b/hrp/internal/uixt/ios_test.go @@ -9,66 +9,53 @@ import ( ) var ( - urlPrefix = "http://localhost:8100" - bundleId = "com.apple.Preferences" - driver WebDriver + bundleId = "com.apple.Preferences" + driver WebDriver ) func setup(t *testing.T) { - var err error - driver, err = NewUSBDriver(nil) + device, err := NewIOSDevice() + if err != nil { + t.Fatal(err) + } + + driver, err = device.NewUSBDriver(nil) if err != nil { t.Fatal(err) } } func TestViaUSB(t *testing.T) { - devices, err := DeviceList() - if err != nil { - t.Fatal(err) - } - - drivers := make([]WebDriver, 0, len(devices)) - - for _, dev := range devices { - d, err := NewUSBDriver(nil, dev) - if err != nil { - t.Errorf("%s: %s", dev.UUID(), err) - continue - } - drivers = append(drivers, d) - } - - for _, d := range drivers { - t.Log(d.Status()) - } + setup(t) + t.Log(driver.Status()) } -func TestNewDevice(t *testing.T) { - device, _ := NewDevice() +func TestNewIOSDevice(t *testing.T) { + device, _ := NewIOSDevice() if device != nil { t.Log(device) } - device, _ = NewDevice(WithUDID("xxxx")) + device, _ = NewIOSDevice(WithUDID("xxxx")) if device != nil { t.Log(device) } - device, _ = NewDevice(WithPort(8700), WithMjpegPort(8800)) + device, _ = NewIOSDevice(WithPort(8700), WithMjpegPort(8800)) if device != nil { t.Log(device) } - device, _ = NewDevice(WithUDID("xxxx"), WithPort(8700), WithMjpegPort(8800)) + device, _ = NewIOSDevice(WithUDID("xxxx"), WithPort(8700), WithMjpegPort(8800)) if device != nil { t.Log(device) } } -func TestNewDriver(t *testing.T) { +func TestNewWDAHTTPDriver(t *testing.T) { + device, _ := NewIOSDevice() var err error - driver, err = NewDriver(nil, urlPrefix) + _, err = device.NewHTTPDriver(nil) if err != nil { t.Fatal(err) }