feat: ApplyOffset for tap xy

This commit is contained in:
lilong.129
2025-02-19 10:55:56 +08:00
parent 8bccf709a7
commit 1b18976620
7 changed files with 33 additions and 14 deletions

View File

@@ -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

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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())

View File

@@ -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))
}

View File

@@ -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