From 1b18976620a541bb802a0a3735a38e20695cec0b Mon Sep 17 00:00:00 2001 From: "lilong.129" Date: Wed, 19 Feb 2025 10:55:56 +0800 Subject: [PATCH] feat: ApplyOffset for tap xy --- internal/version/VERSION | 2 +- pkg/uixt/android_driver_adb.go | 14 +++++++------- pkg/uixt/android_driver_uia2.go | 9 +++++++-- pkg/uixt/driver_ext/ios_stub_driver.go | 4 +--- pkg/uixt/harmony_driver_hdc.go | 1 + pkg/uixt/ios_driver_wda.go | 5 +++++ pkg/uixt/option/action.go | 12 +++++++++++- 7 files changed, 33 insertions(+), 14 deletions(-) diff --git a/internal/version/VERSION b/internal/version/VERSION index e2cd73ae..f678a59d 100644 --- a/internal/version/VERSION +++ b/internal/version/VERSION @@ -1 +1 @@ -v5.0.0+2502182234 +v5.0.0+2502191055 diff --git a/pkg/uixt/android_driver_adb.go b/pkg/uixt/android_driver_adb.go index acd2cb48..7bb66216 100644 --- a/pkg/uixt/android_driver_adb.go +++ b/pkg/uixt/android_driver_adb.go @@ -298,6 +298,9 @@ func (ad *ADBDriver) TapXY(x, y float64, opts ...option.ActionOption) error { } func (ad *ADBDriver) TapAbsXY(x, y float64, opts ...option.ActionOption) error { + actionOptions := option.NewActionOptions(opts...) + x, y = actionOptions.ApplyOffset(x, y) + // adb shell input tap x y xStr := fmt.Sprintf("%.1f", x) yStr := fmt.Sprintf("%.1f", y) @@ -314,6 +317,9 @@ func (ad *ADBDriver) DoubleTapXY(x, y float64, opts ...option.ActionOption) erro if x, y, err = convertToAbsolutePoint(ad, x, y); err != nil { return err } + actionOptions := option.NewActionOptions(opts...) + x, y = actionOptions.ApplyOffset(x, y) + // adb shell input tap x y xStr := fmt.Sprintf("%.1f", x) yStr := fmt.Sprintf("%.1f", y) @@ -333,13 +339,7 @@ func (ad *ADBDriver) DoubleTapXY(x, y float64, opts ...option.ActionOption) erro func (ad *ADBDriver) TouchAndHold(x, y float64, opts ...option.ActionOption) (err 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() + x, y = actionOptions.ApplyOffset(x, y) duration := 1000.0 if actionOptions.Duration > 0 { duration = actionOptions.Duration * 1000 diff --git a/pkg/uixt/android_driver_uia2.go b/pkg/uixt/android_driver_uia2.go index 796dc6b8..6b17ded1 100644 --- a/pkg/uixt/android_driver_uia2.go +++ b/pkg/uixt/android_driver_uia2.go @@ -246,6 +246,9 @@ func (ud *UIA2Driver) DoubleTapXY(x, y float64, opts ...option.ActionOption) err if x, y, err = convertToAbsolutePoint(ud, x, y); err != nil { return err } + actionOptions := option.NewActionOptions(opts...) + x, y = actionOptions.ApplyOffset(x, y) + data := map[string]interface{}{ "actions": []interface{}{ map[string]interface{}{ @@ -279,6 +282,7 @@ func (ud *UIA2Driver) TapXY(x, y float64, opts ...option.ActionOption) error { 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...) + x, y = actionOptions.ApplyOffset(x, y) duration := 100.0 if actionOptions.PressDuration > 0 { @@ -306,8 +310,9 @@ func (ud *UIA2Driver) TapAbsXY(x, y float64, opts ...option.ActionOption) error } func (ud *UIA2Driver) TouchAndHold(x, y float64, opts ...option.ActionOption) (err error) { - actionOpts := option.NewActionOptions(opts...) - duration := actionOpts.Duration + actionOptions := option.NewActionOptions(opts...) + x, y = actionOptions.ApplyOffset(x, y) + duration := actionOptions.Duration if duration == 0 { duration = 1.0 } diff --git a/pkg/uixt/driver_ext/ios_stub_driver.go b/pkg/uixt/driver_ext/ios_stub_driver.go index c4d27e57..11f9f382 100644 --- a/pkg/uixt/driver_ext/ios_stub_driver.go +++ b/pkg/uixt/driver_ext/ios_stub_driver.go @@ -106,13 +106,11 @@ func (s *StubIOSDriver) Source(srcOpt ...option.SourceOption) (string, error) { func (s *StubIOSDriver) OpenUrl(urlStr string, options ...option.ActionOption) (err error) { targetUrl := fmt.Sprintf("/openURL?url=%s", url.QueryEscape(urlStr)) - fmt.Sprintln(targetUrl) - resp, err := s.Session.GET(targetUrl) + _, err = s.Session.GET(targetUrl) if err != nil { log.Error().Err(err).Msg("get source err") return nil } - fmt.Sprintln(resp) return nil } diff --git a/pkg/uixt/harmony_driver_hdc.go b/pkg/uixt/harmony_driver_hdc.go index e973accd..6be1fa98 100644 --- a/pkg/uixt/harmony_driver_hdc.go +++ b/pkg/uixt/harmony_driver_hdc.go @@ -151,6 +151,7 @@ func (hd *HDCDriver) TapXY(x, y float64, opts ...option.ActionOption) error { func (hd *HDCDriver) TapAbsXY(x, y float64, opts ...option.ActionOption) error { actionOptions := option.NewActionOptions(opts...) + x, y = actionOptions.ApplyOffset(x, y) if actionOptions.Identifier != "" { startTime := int(time.Now().UnixMilli()) diff --git a/pkg/uixt/ios_driver_wda.go b/pkg/uixt/ios_driver_wda.go index 1288d141..ffeb576d 100644 --- a/pkg/uixt/ios_driver_wda.go +++ b/pkg/uixt/ios_driver_wda.go @@ -566,6 +566,8 @@ func (wd *WDADriver) TapXY(x, y float64, opts ...option.ActionOption) error { func (wd *WDADriver) TapAbsXY(x, y float64, opts ...option.ActionOption) error { // [[FBRoute POST:@"/wda/tap/:uuid"] respondWithTarget:self action:@selector(handleTap:)] + actionOptions := option.NewActionOptions(opts...) + x, y = actionOptions.ApplyOffset(x, y) data := map[string]interface{}{ "x": wd.toScale(x), "y": wd.toScale(y), @@ -582,6 +584,8 @@ func (wd *WDADriver) DoubleTapXY(x, y float64, opts ...option.ActionOption) erro if x, y, err = convertToAbsolutePoint(wd, x, y); err != nil { return err } + actionOptions := option.NewActionOptions(opts...) + x, y = actionOptions.ApplyOffset(x, y) x = wd.toScale(x) y = wd.toScale(y) @@ -596,6 +600,7 @@ func (wd *WDADriver) DoubleTapXY(x, y float64, opts ...option.ActionOption) erro // FIXME: hold not work func (wd *WDADriver) TouchAndHold(x, y float64, opts ...option.ActionOption) (err error) { actionOptions := option.NewActionOptions(opts...) + x, y = actionOptions.ApplyOffset(x, y) if actionOptions.Duration == 0 { opts = append(opts, option.WithPressDuration(1)) } diff --git a/pkg/uixt/option/action.go b/pkg/uixt/option/action.go index 863f71e7..abd767f1 100644 --- a/pkg/uixt/option/action.go +++ b/pkg/uixt/option/action.go @@ -131,7 +131,17 @@ func (o *ActionOptions) GetScreenOptions() []ActionOption { return o.ScreenOptions.Options() } -func (o *ActionOptions) GetRandomOffset() float64 { +func (o *ActionOptions) ApplyOffset(absX, absY float64) (float64, float64) { + if len(o.Offset) == 2 { + absX += float64(o.Offset[0]) + absY += float64(o.Offset[1]) + } + absX += o.GenerateRandomOffset() + absY += o.GenerateRandomOffset() + return absX, absY +} + +func (o *ActionOptions) GenerateRandomOffset() float64 { if len(o.OffsetRandomRange) != 2 { // invalid offset random range, should be [min, max] return 0