From e26f79e32660d114f9cf4a7f2624fbcf2503cdeb Mon Sep 17 00:00:00 2001 From: debugtalk Date: Mon, 26 Dec 2022 20:22:59 +0800 Subject: [PATCH] fix: specify uia2 ip and port --- hrp/pkg/gadb/device.go | 10 ++++- hrp/pkg/gadb/transport.go | 1 - hrp/pkg/uixt/android_device.go | 33 ++++++++------- hrp/pkg/uixt/android_driver.go | 75 ++++++++-------------------------- hrp/pkg/uixt/android_test.go | 37 ----------------- hrp/pkg/uixt/interface.go | 1 - hrp/pkg/uixt/ios_driver.go | 12 ------ hrp/pkg/uixt/ios_test.go | 12 ------ 8 files changed, 43 insertions(+), 138 deletions(-) diff --git a/hrp/pkg/gadb/device.go b/hrp/pkg/gadb/device.go index 9d07bd2e..59c4d1e6 100644 --- a/hrp/pkg/gadb/device.go +++ b/hrp/pkg/gadb/device.go @@ -7,6 +7,8 @@ import ( "os" "strings" "time" + + "github.com/rs/zerolog/log" ) type DeviceFileInfo struct { @@ -20,7 +22,7 @@ func (info DeviceFileInfo) IsDir() bool { return (info.Mode & (1 << 14)) == (1 << 14) } -const DefaultFileMode = os.FileMode(0664) +const DefaultFileMode = os.FileMode(0o664) type DeviceState string @@ -147,6 +149,9 @@ func (d Device) RunShellCommandWithBytes(cmd string, args ...string) ([]byte, er if strings.TrimSpace(cmd) == "" { return nil, errors.New("adb shell: command cannot be empty") } + log.Debug().Str("cmd", + fmt.Sprintf("adb -s %s shell %s", d.serial, cmd)). + Msg("run adb command") raw, err := d.executeCommand(fmt.Sprintf("shell:%s", cmd)) return raw, err } @@ -156,6 +161,9 @@ func (d Device) EnableAdbOverTCP(port ...int) (err error) { port = []int{AdbDaemonPort} } + log.Info().Str("cmd", + fmt.Sprintf("adb -s %s tcpip %d", d.serial, port[0])). + Msg("enable adb over tcp") _, err = d.executeCommand(fmt.Sprintf("tcpip:%d", port[0]), true) return } diff --git a/hrp/pkg/gadb/transport.go b/hrp/pkg/gadb/transport.go index abf3adf6..ae900429 100644 --- a/hrp/pkg/gadb/transport.go +++ b/hrp/pkg/gadb/transport.go @@ -34,7 +34,6 @@ func newTransport(address string, readTimeout ...time.Duration) (tp transport, e func (t transport) Send(command string) (err error) { msg := fmt.Sprintf("%04x%s", len(command), command) - log.Debug().Str("cmd", command).Msg("run adb command") return _send(t.sock, []byte(msg)) } diff --git a/hrp/pkg/uixt/android_device.go b/hrp/pkg/uixt/android_device.go index 6cf2f9e9..f5380edf 100644 --- a/hrp/pkg/uixt/android_device.go +++ b/hrp/pkg/uixt/android_device.go @@ -20,6 +20,7 @@ import ( var ( AdbServerHost = "localhost" AdbServerPort = gadb.AdbServerPort // 5037 + UIA2ServerHost = "localhost" UIA2ServerPort = 6790 DeviceTempPath = "/data/local/tmp" ) @@ -34,15 +35,15 @@ func WithSerialNumber(serial string) AndroidDeviceOption { } } -func WithAdbIP(ip string) AndroidDeviceOption { +func WithUIA2IP(ip string) AndroidDeviceOption { return func(device *AndroidDevice) { - device.IP = ip + device.UIA2IP = ip } } -func WithAdbPort(port int) AndroidDeviceOption { +func WithUIA2Port(port int) AndroidDeviceOption { return func(device *AndroidDevice) { - device.Port = port + device.UIA2Port = port } } @@ -56,11 +57,11 @@ func GetAndroidDeviceOptions(dev *AndroidDevice) (deviceOptions []AndroidDeviceO if dev.SerialNumber != "" { deviceOptions = append(deviceOptions, WithSerialNumber(dev.SerialNumber)) } - if dev.IP != "" { - deviceOptions = append(deviceOptions, WithAdbIP(dev.IP)) + if dev.UIA2IP != "" { + deviceOptions = append(deviceOptions, WithUIA2IP(dev.UIA2IP)) } - if dev.Port != 0 { - deviceOptions = append(deviceOptions, WithAdbPort(dev.Port)) + if dev.UIA2Port != 0 { + deviceOptions = append(deviceOptions, WithUIA2Port(dev.UIA2Port)) } if dev.LogOn { deviceOptions = append(deviceOptions, WithAdbLogOn(true)) @@ -81,8 +82,8 @@ func NewAndroidDevice(options ...AndroidDeviceOption) (device *AndroidDevice, er } device = &AndroidDevice{ - Port: UIA2ServerPort, - IP: AdbServerHost, + UIA2IP: UIA2ServerHost, + UIA2Port: UIA2ServerPort, } for _, option := range options { option(device) @@ -118,9 +119,8 @@ type AndroidDevice struct { d gadb.Device logcat *AdbLogcat SerialNumber string `json:"serial,omitempty" yaml:"serial,omitempty"` - IP string `json:"ip,omitempty" yaml:"ip,omitempty"` - Port int `json:"port,omitempty" yaml:"port,omitempty"` - MjpegPort int `json:"mjpeg_port,omitempty" yaml:"mjpeg_port,omitempty"` + UIA2IP string `json:"uia2_ip,omitempty" yaml:"uia2_ip,omitempty"` // uiautomator2 server ip + UIA2Port int `json:"uia2_port,omitempty" yaml:"uia2_port,omitempty"` // uiautomator2 server port LogOn bool `json:"log_on,omitempty" yaml:"log_on,omitempty"` } @@ -174,9 +174,8 @@ func (dev *AndroidDevice) NewUSBDriver(capabilities Capabilities) (driver *uiaDr _ = dev.d.ForwardKill(localPort) return nil, errors.Wrap(code.AndroidDeviceUSBDriverError, err.Error()) } - driver.adbDevice = dev.d + driver.adbClient = dev.d driver.logcat = dev.logcat - driver.localPort = localPort return driver, nil } @@ -184,11 +183,11 @@ func (dev *AndroidDevice) NewUSBDriver(capabilities Capabilities) (driver *uiaDr // NewHTTPDriver creates new remote HTTP client, this will also start a new session. // TODO: replace uiaDriver with WebDriver func (dev *AndroidDevice) NewHTTPDriver(capabilities Capabilities) (driver *uiaDriver, err error) { - rawURL := fmt.Sprintf("http://%s:%d/wd/hub", dev.IP, dev.Port) + rawURL := fmt.Sprintf("http://%s:%d/wd/hub", dev.UIA2IP, dev.UIA2Port) if driver, err = NewUIADriver(capabilities, rawURL); err != nil { return nil, err } - driver.adbDevice = dev.d + driver.adbClient = dev.d return driver, nil } diff --git a/hrp/pkg/uixt/android_driver.go b/hrp/pkg/uixt/android_driver.go index 144d00a3..25c5db35 100644 --- a/hrp/pkg/uixt/android_driver.go +++ b/hrp/pkg/uixt/android_driver.go @@ -34,9 +34,8 @@ var errDriverNotImplemented = errors.New("driver method not implemented") type uiaDriver struct { Driver - adbDevice gadb.Device + adbClient gadb.Device logcat *AdbLogcat - localPort int } func NewUIADriver(capabilities Capabilities, urlPrefix string) (driver *uiaDriver, err error) { @@ -126,44 +125,6 @@ func (ud *uiaDriver) NewSession(capabilities Capabilities) (sessionInfo SessionI return SessionInfo{SessionId: sessionID}, nil } -func (ud *uiaDriver) ActiveSession() (sessionInfo SessionInfo, err error) { - // [[FBRoute GET:@""] respondWithTarget:self action:@selector(handleGetActiveSession:)] - return SessionInfo{SessionId: ud.sessionId}, nil -} - -func (ud *uiaDriver) SessionIDs() (sessionIDs []string, err error) { - // register(getHandler, new GetSessions("/wd/hub/sessions")) - var rawResp rawResponse - if rawResp, err = ud.httpGET("/sessions"); err != nil { - return nil, err - } - reply := new(struct{ Value []struct{ SessionId string } }) - if err = json.Unmarshal(rawResp, reply); err != nil { - return nil, err - } - - sessionIDs = make([]string, len(reply.Value)) - for i := range reply.Value { - sessionIDs[i] = reply.Value[i].SessionId - } - return -} - -func (ud *uiaDriver) SessionDetails() (scrollData map[string]interface{}, err error) { - // register(getHandler, new GetSessionDetails("/wd/hub/session/:sessionId")) - var rawResp rawResponse - if rawResp, err = ud.httpGET("/session", ud.sessionId); err != nil { - return nil, err - } - reply := new(struct{ Value map[string]interface{} }) - if err = json.Unmarshal(rawResp, reply); err != nil { - return nil, err - } - - scrollData = reply.Value - return -} - func (ud *uiaDriver) DeleteSession() (err error) { // TODO return errDriverNotImplemented @@ -225,7 +186,7 @@ func (ud *uiaDriver) BatteryInfo() (batteryInfo BatteryInfo, err error) { func (ud *uiaDriver) WindowSize() (size Size, err error) { // adb shell wm size - resp, err := ud.adbDevice.RunShellCommand("wm", "size") + resp, err := ud.adbClient.RunShellCommand("wm", "size") if err == nil { // Physical size: 1080x2340 s := strings.Trim(strings.Split(resp, ": ")[1], "\n") @@ -261,7 +222,7 @@ func (ud *uiaDriver) Scale() (scale float64, err error) { // PressBack simulates a short press on the BACK button. func (ud *uiaDriver) PressBack(options ...DataOption) (err error) { // adb shell input keyevent 4 - _, err = ud.adbDevice.RunShellCommand("input", "keyevent", KEYCODE_BACK) + _, err = ud.adbClient.RunShellCommand("input", "keyevent", KEYCODE_BACK) if err == nil { return nil } @@ -271,33 +232,33 @@ func (ud *uiaDriver) PressBack(options ...DataOption) (err error) { } func (ud *uiaDriver) StartCamera() (err error) { - if _, err = ud.adbDevice.RunShellCommand("rm", "-r", "/sdcard/DCIM/Camera"); err != nil { + if _, err = ud.adbClient.RunShellCommand("rm", "-r", "/sdcard/DCIM/Camera"); err != nil { return err } time.Sleep(5 * time.Second) var version string - if version, err = ud.adbDevice.RunShellCommand("getprop", "ro.build.version.release"); err != nil { + if version, err = ud.adbClient.RunShellCommand("getprop", "ro.build.version.release"); err != nil { return err } if version == "11" || version == "12" { - if _, err = ud.adbDevice.RunShellCommand("am", "start", "-a", "android.media.action.STILL_IMAGE_CAMERA"); err != nil { + if _, err = ud.adbClient.RunShellCommand("am", "start", "-a", "android.media.action.STILL_IMAGE_CAMERA"); err != nil { return err } time.Sleep(5 * time.Second) - if _, err = ud.adbDevice.RunShellCommand("input", "swipe", "750", "1000", "250", "1000"); err != nil { + if _, err = ud.adbClient.RunShellCommand("input", "swipe", "750", "1000", "250", "1000"); err != nil { return err } time.Sleep(5 * time.Second) - if _, err = ud.adbDevice.RunShellCommand("input", "keyevent", KEYCODE_CAMERA); err != nil { + if _, err = ud.adbClient.RunShellCommand("input", "keyevent", KEYCODE_CAMERA); err != nil { return err } return } else { - if _, err = ud.adbDevice.RunShellCommand("am", "start", "-a", "android.media.action.VIDEO_CAPTURE"); err != nil { + if _, err = ud.adbClient.RunShellCommand("am", "start", "-a", "android.media.action.VIDEO_CAPTURE"); err != nil { return err } time.Sleep(5 * time.Second) - if _, err = ud.adbDevice.RunShellCommand("input", "keyevent", KEYCODE_CAMERA); err != nil { + if _, err = ud.adbClient.RunShellCommand("input", "keyevent", KEYCODE_CAMERA); err != nil { return err } return @@ -315,12 +276,12 @@ func (ud *uiaDriver) StopCamera() (err error) { } // kill samsung shell command - if _, err = ud.adbDevice.RunShellCommand("am", "force-stop", "com.sec.android.app.camera"); err != nil { + if _, err = ud.adbClient.RunShellCommand("am", "force-stop", "com.sec.android.app.camera"); err != nil { return err } // kill other camera (huawei mi) - if _, err = ud.adbDevice.RunShellCommand("am", "force-stop", "com.android.camera2"); err != nil { + if _, err = ud.adbClient.RunShellCommand("am", "force-stop", "com.android.camera2"); err != nil { return err } return @@ -332,7 +293,7 @@ func (ud *uiaDriver) Homescreen() (err error) { func (ud *uiaDriver) PressKeyCode(keyCode KeyCode, metaState KeyMeta, flags ...KeyFlag) (err error) { // adb shell input keyevent - _, err = ud.adbDevice.RunShellCommand( + _, err = ud.adbClient.RunShellCommand( "input", "keyevent", fmt.Sprintf("%d", keyCode)) if err == nil { return nil @@ -355,7 +316,7 @@ func (ud *uiaDriver) PressKeyCode(keyCode KeyCode, metaState KeyMeta, flags ...K func (ud *uiaDriver) AppLaunch(bundleId string) (err error) { // 不指定 Activity 名称启动(启动主 Activity) // adb shell monkey -p -c android.intent.category.LAUNCHER 1 - sOutput, err := ud.adbDevice.RunShellCommand( + sOutput, err := ud.adbClient.RunShellCommand( "monkey", "-p", bundleId, "-c", "android.intent.category.LAUNCHER", "1", ) if err != nil { @@ -370,7 +331,7 @@ func (ud *uiaDriver) AppLaunch(bundleId string) (err error) { func (ud *uiaDriver) AppTerminate(bundleId string) (successful bool, err error) { // 强制停止应用,停止 相关的进程 // adb shell am force-stop - _, err = ud.adbDevice.RunShellCommand("am", "force-stop", bundleId) + _, err = ud.adbClient.RunShellCommand("am", "force-stop", bundleId) return err == nil, err } @@ -380,7 +341,7 @@ func (ud *uiaDriver) Tap(x, y int, options ...DataOption) error { func (ud *uiaDriver) TapFloat(x, y float64, options ...DataOption) (err error) { // adb shell input tap x y - _, err = ud.adbDevice.RunShellCommand( + _, err = ud.adbClient.RunShellCommand( "input", "tap", fmt.Sprintf("%.1f", x), fmt.Sprintf("%.1f", y)) if err == nil { return nil @@ -461,7 +422,7 @@ func (ud *uiaDriver) Swipe(fromX, fromY, toX, toY int, options ...DataOption) er func (ud *uiaDriver) SwipeFloat(fromX, fromY, toX, toY float64, options ...DataOption) error { // adb shell input swipe fromX fromY toX toY - _, err := ud.adbDevice.RunShellCommand( + _, err := ud.adbClient.RunShellCommand( "input", "swipe", fmt.Sprintf("%.1f", fromX), fmt.Sprintf("%.1f", fromY), fmt.Sprintf("%.1f", toX), fmt.Sprintf("%.1f", toY), @@ -581,7 +542,7 @@ func (ud *uiaDriver) SetRotation(rotation Rotation) (err error) { func (ud *uiaDriver) Screenshot() (raw *bytes.Buffer, err error) { // adb shell screencap -p - resp, err := ud.adbDevice.RunShellCommandWithBytes( + resp, err := ud.adbClient.RunShellCommandWithBytes( "screencap", "-p", ) if err == nil { diff --git a/hrp/pkg/uixt/android_test.go b/hrp/pkg/uixt/android_test.go index aa119e0f..1382ca42 100644 --- a/hrp/pkg/uixt/android_test.go +++ b/hrp/pkg/uixt/android_test.go @@ -66,43 +66,6 @@ func TestDriver_Status(t *testing.T) { } } -func TestDriver_SessionIDs(t *testing.T) { - driver, err := NewUIADriver(nil, uiaServerURL) - if err != nil { - t.Fatal(err) - } - - sessions, err := driver.SessionIDs() - if err != nil { - t.Fatal(err) - } - if len(sessions) == 0 { - t.Fatal("should have at least one") - } - t.Log(len(sessions), sessions) -} - -func TestDriver_SessionDetails(t *testing.T) { - // firstMatchEntry := make(map[string]interface{}) - // firstMatchEntry["package"] = "com.android.settings" - // firstMatchEntry["activity"] = "com.android.settings/.Settings" - // caps = Capabilities{ - // "firstMatch": []interface{}{firstMatchEntry}, - // "alwaysMatch": struct{}{}, - // } - driver, err := NewUIADriver(nil, uiaServerURL) - if err != nil { - t.Fatal(err) - } - - scrollData, err := driver.SessionDetails() - if err != nil { - t.Fatal(err) - } - - t.Log(scrollData) -} - func TestDriver_Screenshot(t *testing.T) { driver, err := NewUIADriver(nil, uiaServerURL) if err != nil { diff --git a/hrp/pkg/uixt/interface.go b/hrp/pkg/uixt/interface.go index d324872f..3b2005fb 100644 --- a/hrp/pkg/uixt/interface.go +++ b/hrp/pkg/uixt/interface.go @@ -595,7 +595,6 @@ type WebDriver interface { // NewSession starts a new session and returns the SessionInfo. NewSession(capabilities Capabilities) (SessionInfo, error) - ActiveSession() (SessionInfo, error) // DeleteSession Kills application associated with that session and removes session // 1) alertsMonitor disable // 2) testedApplicationBundleId terminate diff --git a/hrp/pkg/uixt/ios_driver.go b/hrp/pkg/uixt/ios_driver.go index fc3aede9..16aa884c 100644 --- a/hrp/pkg/uixt/ios_driver.go +++ b/hrp/pkg/uixt/ios_driver.go @@ -69,18 +69,6 @@ func (wd *wdaDriver) NewSession(capabilities Capabilities) (sessionInfo SessionI return } -func (wd *wdaDriver) ActiveSession() (sessionInfo SessionInfo, err error) { - // [[FBRoute GET:@""] respondWithTarget:self action:@selector(handleGetActiveSession:)] - var rawResp rawResponse - if rawResp, err = wd.httpGET("/session", wd.sessionId); err != nil { - return SessionInfo{}, err - } - if sessionInfo, err = rawResp.valueConvertToSessionInfo(); err != nil { - return SessionInfo{}, err - } - return -} - func (wd *wdaDriver) DeleteSession() (err error) { // [[FBRoute DELETE:@""] respondWithTarget:self action:@selector(handleDeleteSession:)] _, err = wd.httpDELETE("/session", wd.sessionId) diff --git a/hrp/pkg/uixt/ios_test.go b/hrp/pkg/uixt/ios_test.go index 23fff97b..c894f5aa 100644 --- a/hrp/pkg/uixt/ios_test.go +++ b/hrp/pkg/uixt/ios_test.go @@ -68,18 +68,6 @@ func TestNewUSBDriver(t *testing.T) { // t.Log(driver.IsWdaHealthy()) } -func Test_remoteWD_ActiveSession(t *testing.T) { - setup(t) - - sessionInfo, err := driver.ActiveSession() - if err != nil { - t.Fatal(err) - } - if len(sessionInfo.SessionId) == 0 { - t.Fatal(sessionInfo) - } -} - func Test_remoteWD_DeleteSession(t *testing.T) { setup(t)