diff --git a/hrp/pkg/uixt/android_adb_driver.go b/hrp/pkg/uixt/android_adb_driver.go index 2d552a36..7910e839 100644 --- a/hrp/pkg/uixt/android_adb_driver.go +++ b/hrp/pkg/uixt/android_adb_driver.go @@ -185,6 +185,11 @@ func (ad *adbDriver) StopCamera() (err error) { return } +func (ad *adbDriver) Orientation() (orientation Orientation, err error) { + err = errDriverNotImplemented + return +} + func (ad *adbDriver) Homescreen() (err error) { return ad.PressKeyCode(KCHome, KMEmpty) } diff --git a/hrp/pkg/uixt/android_test.go b/hrp/pkg/uixt/android_test.go index ffed7d4e..cd15736b 100644 --- a/hrp/pkg/uixt/android_test.go +++ b/hrp/pkg/uixt/android_test.go @@ -11,13 +11,14 @@ import ( ) var ( - uiaServerURL = "http://localhost:6790/wd/hub" + uiaServerURL = "http://forward-to-6790:6790/wd/hub" driverExt *DriverExt ) func setupAndroid(t *testing.T) { device, err := NewAndroidDevice() checkErr(t, err) + device.UIA2 = true driverExt, err = device.NewDriver() checkErr(t, err) } @@ -215,6 +216,14 @@ func TestDriver_Swipe(t *testing.T) { } } +func TestDriver_Swipe_Relative(t *testing.T) { + setupAndroid(t) + err := driverExt.SwipeRelative(0.5, 0.7, 0.5, 0.5) + if err != nil { + t.Fatal(err) + } +} + func TestDriver_Drag(t *testing.T) { driver, err := NewUIADriver(nil, uiaServerURL) if err != nil { diff --git a/hrp/pkg/uixt/android_uia2_driver.go b/hrp/pkg/uixt/android_uia2_driver.go index 5f38556d..819ec4ef 100644 --- a/hrp/pkg/uixt/android_uia2_driver.go +++ b/hrp/pkg/uixt/android_uia2_driver.go @@ -253,6 +253,20 @@ func (ud *uiaDriver) PressKeyCode(keyCode KeyCode, metaState KeyMeta, flags ...K return } +func (ud *uiaDriver) Orientation() (orientation Orientation, err error) { + // [[FBRoute GET:@"/orientation"] respondWithTarget:self action:@selector(handleGetOrientation:)] + var rawResp rawResponse + if rawResp, err = ud.httpGET("/session", ud.sessionId, "/orientation"); err != nil { + return "", err + } + reply := new(struct{ Value Orientation }) + if err = json.Unmarshal(rawResp, reply); err != nil { + return "", err + } + orientation = reply.Value + return +} + func (ud *uiaDriver) Tap(x, y int, options ...ActionOption) error { return ud.TapFloat(float64(x), float64(y), options...) } diff --git a/hrp/pkg/uixt/interface.go b/hrp/pkg/uixt/interface.go index e292d93c..a447d3a3 100644 --- a/hrp/pkg/uixt/interface.go +++ b/hrp/pkg/uixt/interface.go @@ -537,6 +537,8 @@ type WebDriver interface { // StopCamera Stops the camera for recording StopCamera() error + Orientation() (orientation Orientation, err error) + // Tap Sends a tap event at the coordinate. Tap(x, y int, options ...ActionOption) error TapFloat(x, y float64, options ...ActionOption) error diff --git a/hrp/pkg/uixt/swipe.go b/hrp/pkg/uixt/swipe.go index 4c97d212..23d3490e 100644 --- a/hrp/pkg/uixt/swipe.go +++ b/hrp/pkg/uixt/swipe.go @@ -19,17 +19,30 @@ func assertRelative(p float64) bool { func (dExt *DriverExt) SwipeRelative(fromX, fromY, toX, toY float64, options ...ActionOption) error { width := dExt.windowSize.Width height := dExt.windowSize.Height + orientation, err := dExt.Driver.Orientation() + if err != nil { + log.Warn().Err(err).Msgf("swipe from (%v, %v) to (%v, %v) get orientation failed, use default orientation", + fromX, fromY, toX, toY) + orientation = OrientationPortrait + } if !assertRelative(fromX) || !assertRelative(fromY) || !assertRelative(toX) || !assertRelative(toY) { return fmt.Errorf("fromX(%f), fromY(%f), toX(%f), toY(%f) must be less than 1", fromX, fromY, toX, toY) } - - fromX = float64(width) * fromX - fromY = float64(height) * fromY - toX = float64(width) * toX - toY = float64(height) * toY + // 左转和右转都是"LANDSCAPE" + if orientation == OrientationPortrait { + fromX = float64(width) * fromX + fromY = float64(height) * fromY + toX = float64(width) * toX + toY = float64(height) * toY + } else { + fromX = float64(height) * fromX + fromY = float64(width) * fromY + toX = float64(height) * toX + toY = float64(width) * toY + } return dExt.Driver.SwipeFloat(fromX, fromY, toX, toY, options...) } diff --git a/hrp/pkg/uixt/tap.go b/hrp/pkg/uixt/tap.go index a992c94e..f52b5785 100644 --- a/hrp/pkg/uixt/tap.go +++ b/hrp/pkg/uixt/tap.go @@ -2,6 +2,7 @@ package uixt import ( "fmt" + "github.com/rs/zerolog/log" ) func (dExt *DriverExt) TapAbsXY(x, y float64, options ...ActionOption) error { @@ -15,9 +16,19 @@ func (dExt *DriverExt) TapXY(x, y float64, options ...ActionOption) error { return fmt.Errorf("x, y percentage should be <= 1, got x=%v, y=%v", x, y) } - x = x * float64(dExt.windowSize.Width) - y = y * float64(dExt.windowSize.Height) - + orientation, err := dExt.Driver.Orientation() + if err != nil { + log.Warn().Err(err).Msgf("tap (%v, %v) get orientation failed, use default orientation", + x, y) + orientation = OrientationPortrait + } + if orientation == OrientationPortrait { + x = x * float64(dExt.windowSize.Width) + y = y * float64(dExt.windowSize.Height) + } else { + x = x * float64(dExt.windowSize.Height) + y = y * float64(dExt.windowSize.Width) + } return dExt.TapAbsXY(x, y, options...) } @@ -86,7 +97,19 @@ func (dExt *DriverExt) DoubleTapXY(x, y float64) error { if x > 1 || y > 1 { return fmt.Errorf("x, y percentage should be < 1, got x=%v, y=%v", x, y) } - + orientation, err := dExt.Driver.Orientation() + if err != nil { + log.Warn().Err(err).Msgf("tap (%v, %v) get orientation failed, use default orientation", + x, y) + orientation = OrientationPortrait + } + if orientation == OrientationPortrait { + x = x * float64(dExt.windowSize.Width) + y = y * float64(dExt.windowSize.Height) + } else { + x = x * float64(dExt.windowSize.Height) + y = y * float64(dExt.windowSize.Width) + } x = x * float64(dExt.windowSize.Width) y = y * float64(dExt.windowSize.Height) return dExt.Driver.DoubleTapFloat(x, y)