diff --git a/internal/version/VERSION b/internal/version/VERSION index 6d74dfd4..79c1838a 100644 --- a/internal/version/VERSION +++ b/internal/version/VERSION @@ -1 +1 @@ -v5.0.0+2502111708 +v5.0.0+2502111735 diff --git a/pkg/uixt/android_driver_adb.go b/pkg/uixt/android_driver_adb.go index 3e8c0cf5..2fc76e1a 100644 --- a/pkg/uixt/android_driver_adb.go +++ b/pkg/uixt/android_driver_adb.go @@ -283,15 +283,14 @@ func (ad *ADBDriver) AppTerminate(packageName string) (successful bool, err erro } func (ad *ADBDriver) TapXY(x, y float64, opts ...option.ActionOption) error { - actionOptions := option.NewActionOptions(opts...) - - if len(actionOptions.Offset) == 2 { - x += float64(actionOptions.Offset[0]) - y += float64(actionOptions.Offset[1]) + absX, absY, err := convertToAbsolutePoint(ad, x, y) + if err != nil { + return err } - x += actionOptions.GetRandomOffset() - y += actionOptions.GetRandomOffset() + return ad.TapAbsXY(absX, absY, opts...) +} +func (ad *ADBDriver) TapAbsXY(x, y float64, opts ...option.ActionOption) error { // adb shell input tap x y xStr := fmt.Sprintf("%.1f", x) yStr := fmt.Sprintf("%.1f", y) diff --git a/pkg/uixt/android_driver_uia2.go b/pkg/uixt/android_driver_uia2.go index aba4ed97..a810fad7 100644 --- a/pkg/uixt/android_driver_uia2.go +++ b/pkg/uixt/android_driver_uia2.go @@ -263,17 +263,19 @@ func (ud *UIA2Driver) DoubleFloatTap(x, y float64) error { return err } -func (ud *UIA2Driver) TapXY(x, y float64, opts ...option.ActionOption) (err error) { +func (ud *UIA2Driver) TapXY(x, y float64, opts ...option.ActionOption) error { + // register(postHandler, new Tap("/wd/hub/session/:sessionId/appium/tap")) + absX, absY, err := convertToAbsolutePoint(ud, x, y) + if err != nil { + return err + } + return ud.TapAbsXY(absX, absY, opts...) +} + +func (ud *UIA2Driver) TapAbsXY(x, y float64, opts ...option.ActionOption) error { // register(postHandler, new Tap("/wd/hub/session/:sessionId/appium/tap")) actionOptions := option.NewActionOptions(opts...) - if len(actionOptions.Offset) == 2 { - x += float64(actionOptions.Offset[0]) - y += float64(actionOptions.Offset[1]) - } - x += actionOptions.GetRandomOffset() - y += actionOptions.GetRandomOffset() - duration := 100.0 if actionOptions.PressDuration > 0 { duration = actionOptions.PressDuration * 1000 @@ -297,7 +299,7 @@ func (ud *UIA2Driver) TapXY(x, y float64, opts ...option.ActionOption) (err erro // update data options in post data for extra uiautomator configurations actionOptions.UpdateData(data) - _, err = ud.httpPOST(data, "/session", ud.Session.ID, "actions/tap") + _, err := ud.httpPOST(data, "/session", ud.Session.ID, "actions/tap") return err } diff --git a/pkg/uixt/driver.go b/pkg/uixt/driver.go index 107cea70..bbf3a699 100644 --- a/pkg/uixt/driver.go +++ b/pkg/uixt/driver.go @@ -58,7 +58,8 @@ type IDriver interface { Unlock() error Back() error // tap - TapXY(x, y float64, opts ...option.ActionOption) error + TapXY(x, y float64, opts ...option.ActionOption) error // tap on [x, y] percent of window size + TapAbsXY(x, y float64, opts ...option.ActionOption) error DoubleTapXY(x, y float64, opts ...option.ActionOption) error TouchAndHold(x, y float64, opts ...option.ActionOption) error TapByText(text string, opts ...option.ActionOption) error // TODO: remove diff --git a/pkg/uixt/driver_tap.go b/pkg/uixt/driver_tap.go index 6b5c4c48..aaa7e8e3 100644 --- a/pkg/uixt/driver_tap.go +++ b/pkg/uixt/driver_tap.go @@ -9,30 +9,6 @@ import ( "github.com/httprunner/httprunner/v5/pkg/uixt/option" ) -func (dExt *XTDriver) TapAbsXY(x, y float64, opts ...option.ActionOption) error { - // tap on absolute coordinate [x, y] - err := dExt.TapXY(x, y, opts...) - if err != nil { - return errors.Wrap(code.MobileUITapError, err.Error()) - } - return nil -} - -func (dExt *XTDriver) TapXY(x, y float64, opts ...option.ActionOption) error { - // tap on [x, y] percent of window size - if x > 1 || y > 1 { - return fmt.Errorf("x, y percentage should be <= 1, got x=%v, y=%v", x, y) - } - - windowSize, err := dExt.WindowSize() - if err != nil { - return err - } - x = x * float64(windowSize.Width) - y = y * float64(windowSize.Height) - return dExt.TapAbsXY(x, y, opts...) -} - func (dExt *XTDriver) TapByOCR(ocrText string, opts ...option.ActionOption) error { actionOptions := option.NewActionOptions(opts...) if actionOptions.ScreenShotFileName == "" { diff --git a/pkg/uixt/driver_test.go b/pkg/uixt/driver_test.go index ce2361a1..10fd788d 100644 --- a/pkg/uixt/driver_test.go +++ b/pkg/uixt/driver_test.go @@ -26,4 +26,5 @@ func TestNewDriverExt(t *testing.T) { // get device device = driver.GetDevice().(*AndroidDevice) + t.Log(device) } diff --git a/pkg/uixt/driver_utils.go b/pkg/uixt/driver_utils.go index 8dc214c1..612988a4 100644 --- a/pkg/uixt/driver_utils.go +++ b/pkg/uixt/driver_utils.go @@ -8,11 +8,32 @@ import ( "github.com/httprunner/httprunner/v5/code" ) +func convertToAbsolutePoint(driver IDriver, x, y float64) (absX, absY float64, err error) { + if !assertRelative(x) || !assertRelative(y) { + err = errors.Wrap(code.InvalidCaseError, + fmt.Sprintf("x(%f), y(%f) must be less than 1", x, y)) + return + } + + windowSize, err := driver.WindowSize() + if err != nil { + err = errors.Wrap(code.DeviceGetInfoError, err.Error()) + return + } + + absX = float64(windowSize.Width) * x + absY = float64(windowSize.Height) * y + return +} + func convertToAbsoluteCoordinates(driver IDriver, fromX, fromY, toX, toY float64) ( absFromX, absFromY, absToX, absToY float64, err error) { - err = assertCoordinatesRelative(fromX, fromY, toX, toY) - if err != nil { + if !assertRelative(fromX) || !assertRelative(fromY) || + !assertRelative(toX) || !assertRelative(toY) { + err = errors.Wrap(code.InvalidCaseError, + fmt.Sprintf("fromX(%f), fromY(%f), toX(%f), toY(%f) must be less than 1", + fromX, fromY, toX, toY)) return } @@ -32,16 +53,6 @@ func convertToAbsoluteCoordinates(driver IDriver, fromX, fromY, toX, toY float64 return absFromX, absFromY, absToX, absToY, nil } -func assertCoordinatesRelative(fromX, fromY, toX, toY float64) error { - if !assertRelative(fromX) || !assertRelative(fromY) || - !assertRelative(toX) || !assertRelative(toY) { - return errors.Wrap(code.InvalidCaseError, - fmt.Sprintf("fromX(%f), fromY(%f), toX(%f), toY(%f) must be less than 1", - fromX, fromY, toX, toY)) - } - return nil -} - func assertRelative(p float64) bool { return p >= 0 && p <= 1 } diff --git a/pkg/uixt/harmony_driver_hdc.go b/pkg/uixt/harmony_driver_hdc.go index e488f860..66738c44 100644 --- a/pkg/uixt/harmony_driver_hdc.go +++ b/pkg/uixt/harmony_driver_hdc.go @@ -138,20 +138,22 @@ func (hd *HDCDriver) Orientation() (orientation types.Orientation, err error) { } func (hd *HDCDriver) TapXY(x, y float64, opts ...option.ActionOption) error { + absX, absY, err := convertToAbsolutePoint(hd, x, y) + if err != nil { + return err + } + return hd.TapAbsXY(absX, absY, opts...) +} + +func (hd *HDCDriver) TapAbsXY(x, y float64, opts ...option.ActionOption) error { actionOptions := option.NewActionOptions(opts...) - if len(actionOptions.Offset) == 2 { - x += float64(actionOptions.Offset[0]) - y += float64(actionOptions.Offset[1]) - } - - x += actionOptions.GetRandomOffset() - y += actionOptions.GetRandomOffset() if actionOptions.Identifier != "" { startTime := int(time.Now().UnixMilli()) hd.points = append(hd.points, ExportPoint{Start: startTime, End: startTime + 100, Ext: actionOptions.Identifier, RunTime: 100}) } - return hd.uiDriver.InjectGesture(ghdc.NewGesture().Start(ghdc.Point{X: int(x), Y: int(y)}).Pause(100)) + return hd.uiDriver.InjectGesture( + ghdc.NewGesture().Start(ghdc.Point{X: int(x), Y: int(y)}).Pause(100)) } func (hd *HDCDriver) DoubleTapXY(x, y float64, opts ...option.ActionOption) error { diff --git a/pkg/uixt/ios_driver_wda.go b/pkg/uixt/ios_driver_wda.go index 6a008a06..6cb9db74 100644 --- a/pkg/uixt/ios_driver_wda.go +++ b/pkg/uixt/ios_driver_wda.go @@ -514,28 +514,23 @@ func (wd *WDADriver) ForegroundInfo() (appInfo types.AppInfo, err error) { return appInfo, err } -func (wd *WDADriver) TapXY(x, y float64, opts ...option.ActionOption) (err error) { +func (wd *WDADriver) TapXY(x, y float64, opts ...option.ActionOption) error { // [[FBRoute POST:@"/wda/tap/:uuid"] respondWithTarget:self action:@selector(handleTap:)] - actionOptions := option.NewActionOptions(opts...) - - x = wd.toScale(x) - y = wd.toScale(y) - if len(actionOptions.Offset) == 2 { - x += float64(actionOptions.Offset[0]) - y += float64(actionOptions.Offset[1]) + absX, absY, err := convertToAbsolutePoint(wd, x, y) + if err != nil { + return err } - x += actionOptions.GetRandomOffset() - y += actionOptions.GetRandomOffset() + return wd.TapAbsXY(absX, absY, opts...) +} +func (wd *WDADriver) TapAbsXY(x, y float64, opts ...option.ActionOption) error { + // [[FBRoute POST:@"/wda/tap/:uuid"] respondWithTarget:self action:@selector(handleTap:)] data := map[string]interface{}{ - "x": x, - "y": y, + "x": wd.toScale(x), + "y": wd.toScale(y), } - // update data options in post data for extra WDA configurations - actionOptions.UpdateData(data) - - _, err = wd.httpPOST(data, "/session", wd.Session.ID, "/wda/tap/0") - return + _, err := wd.httpPOST(data, "/session", wd.Session.ID, "/wda/tap/0") + return err } func (wd *WDADriver) DoubleTapXY(x, y float64, opts ...option.ActionOption) (err error) {