refactor: rename ApplyTapOffset

This commit is contained in:
lilong.129 2025-03-17 17:44:04 +08:00
parent b34a2218fe
commit 9fb53590ca
9 changed files with 29 additions and 25 deletions

View File

@ -1 +1 @@
v5.0.0-beta-2503171536 v5.0.0-beta-2503171744

View File

@ -300,7 +300,7 @@ func (ad *ADBDriver) TapXY(x, y float64, opts ...option.ActionOption) error {
func (ad *ADBDriver) TapAbsXY(x, y float64, opts ...option.ActionOption) error { func (ad *ADBDriver) TapAbsXY(x, y float64, opts ...option.ActionOption) error {
actionOptions := option.NewActionOptions(opts...) actionOptions := option.NewActionOptions(opts...)
x, y = actionOptions.ApplyOffset(x, y) x, y = actionOptions.ApplyTapOffset(x, y)
// adb shell input tap x y // adb shell input tap x y
xStr := fmt.Sprintf("%.1f", x) xStr := fmt.Sprintf("%.1f", x)
@ -320,7 +320,7 @@ func (ad *ADBDriver) DoubleTap(x, y float64, opts ...option.ActionOption) error
return err return err
} }
actionOptions := option.NewActionOptions(opts...) actionOptions := option.NewActionOptions(opts...)
x, y = actionOptions.ApplyOffset(x, y) x, y = actionOptions.ApplyTapOffset(x, y)
// adb shell input tap x y // adb shell input tap x y
xStr := fmt.Sprintf("%.1f", x) xStr := fmt.Sprintf("%.1f", x)
@ -341,7 +341,7 @@ func (ad *ADBDriver) DoubleTap(x, y float64, opts ...option.ActionOption) error
func (ad *ADBDriver) TouchAndHold(x, y float64, opts ...option.ActionOption) (err error) { func (ad *ADBDriver) TouchAndHold(x, y float64, opts ...option.ActionOption) (err error) {
actionOptions := option.NewActionOptions(opts...) actionOptions := option.NewActionOptions(opts...)
x, y = actionOptions.ApplyOffset(x, y) x, y = actionOptions.ApplyTapOffset(x, y)
duration := 1000.0 duration := 1000.0
if actionOptions.Duration > 0 { if actionOptions.Duration > 0 {
duration = actionOptions.Duration * 1000 duration = actionOptions.Duration * 1000

View File

@ -259,7 +259,7 @@ func (ud *UIA2Driver) DoubleTap(x, y float64, opts ...option.ActionOption) error
return err return err
} }
actionOptions := option.NewActionOptions(opts...) actionOptions := option.NewActionOptions(opts...)
x, y = actionOptions.ApplyOffset(x, y) x, y = actionOptions.ApplyTapOffset(x, y)
data := map[string]interface{}{ data := map[string]interface{}{
"actions": []interface{}{ "actions": []interface{}{
@ -295,7 +295,7 @@ func (ud *UIA2Driver) TapXY(x, y float64, opts ...option.ActionOption) error {
func (ud *UIA2Driver) TapAbsXY(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")) // register(postHandler, new Tap("/wd/hub/session/:sessionId/appium/tap"))
actionOptions := option.NewActionOptions(opts...) actionOptions := option.NewActionOptions(opts...)
x, y = actionOptions.ApplyOffset(x, y) x, y = actionOptions.ApplyTapOffset(x, y)
duration := 100.0 duration := 100.0
if actionOptions.PressDuration > 0 { if actionOptions.PressDuration > 0 {
@ -325,7 +325,7 @@ func (ud *UIA2Driver) TapAbsXY(x, y float64, opts ...option.ActionOption) error
func (ud *UIA2Driver) TouchAndHold(x, y float64, opts ...option.ActionOption) (err error) { func (ud *UIA2Driver) TouchAndHold(x, y float64, opts ...option.ActionOption) (err error) {
actionOptions := option.NewActionOptions(opts...) actionOptions := option.NewActionOptions(opts...)
x, y = actionOptions.ApplyOffset(x, y) x, y = actionOptions.ApplyTapOffset(x, y)
duration := actionOptions.Duration duration := actionOptions.Duration
if duration == 0 { if duration == 0 {
duration = 1.0 duration = 1.0

View File

@ -22,7 +22,7 @@ func (dExt *XTDriver) TapByOCR(text string, opts ...option.ActionOption) error {
} }
var point ai.PointF var point ai.PointF
if actionOptions.TapRandom { if actionOptions.TapRandomRect {
point = textRect.RandomPoint() point = textRect.RandomPoint()
} else { } else {
point = textRect.Center() point = textRect.Center()

View File

@ -173,15 +173,19 @@ func TestDriverExt_ClosePopupsHandler(t *testing.T) {
func TestDriverExt_Action_Risk(t *testing.T) { func TestDriverExt_Action_Risk(t *testing.T) {
driver := setupADBDriverExt(t) driver := setupADBDriverExt(t)
// tap point with offset // tap point with constant offset
err := driver.TapXY(0.5, 0.5, option.WithTapOffset(-10, 10)) err := driver.TapXY(0.5, 0.5, option.WithTapOffset(-10, 10))
assert.Nil(t, err) assert.Nil(t, err)
// tap point with random offset
err = driver.TapXY(0.5, 0.5, option.WithOffsetRandomRange(-10, 10))
assert.Nil(t, err)
// swipe direction with offset // swipe direction with offset
err = driver.Swipe(0.5, 0.5, 0.5, 0.9, err = driver.Swipe(0.5, 0.5, 0.5, 0.9,
option.WithSwipeOffset(-50, 50, -50, 50)) option.WithSwipeOffset(-50, 50, -50, 50))
assert.Nil(t, err) assert.Nil(t, err)
// tap random point in ocr text rect // tap random point in ocr text rect
err = driver.TapByOCR("首页", option.WithTapRandom(true)) err = driver.TapByOCR("首页", option.WithTapRandomRect(true))
} }

View File

@ -151,7 +151,7 @@ func (hd *HDCDriver) TapXY(x, y float64, opts ...option.ActionOption) error {
func (hd *HDCDriver) TapAbsXY(x, y float64, opts ...option.ActionOption) error { func (hd *HDCDriver) TapAbsXY(x, y float64, opts ...option.ActionOption) error {
actionOptions := option.NewActionOptions(opts...) actionOptions := option.NewActionOptions(opts...)
x, y = actionOptions.ApplyOffset(x, y) x, y = actionOptions.ApplyTapOffset(x, y)
if actionOptions.Identifier != "" { if actionOptions.Identifier != "" {
startTime := int(time.Now().UnixMilli()) startTime := int(time.Now().UnixMilli())

View File

@ -587,7 +587,7 @@ func (wd *WDADriver) TapXY(x, y float64, opts ...option.ActionOption) error {
func (wd *WDADriver) TapAbsXY(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:)] // [[FBRoute POST:@"/wda/tap/:uuid"] respondWithTarget:self action:@selector(handleTap:)]
actionOptions := option.NewActionOptions(opts...) actionOptions := option.NewActionOptions(opts...)
x, y = actionOptions.ApplyOffset(x, y) x, y = actionOptions.ApplyTapOffset(x, y)
data := map[string]interface{}{ data := map[string]interface{}{
"x": wd.toScale(x), "x": wd.toScale(x),
"y": wd.toScale(y), "y": wd.toScale(y),
@ -608,7 +608,7 @@ func (wd *WDADriver) DoubleTap(x, y float64, opts ...option.ActionOption) error
} }
actionOptions := option.NewActionOptions(opts...) actionOptions := option.NewActionOptions(opts...)
x, y = actionOptions.ApplyOffset(x, y) x, y = actionOptions.ApplyTapOffset(x, y)
x = wd.toScale(x) x = wd.toScale(x)
y = wd.toScale(y) y = wd.toScale(y)
data := map[string]interface{}{ data := map[string]interface{}{
@ -623,7 +623,7 @@ func (wd *WDADriver) DoubleTap(x, y float64, opts ...option.ActionOption) error
// FIXME: hold not work // FIXME: hold not work
func (wd *WDADriver) TouchAndHold(x, y float64, opts ...option.ActionOption) (err error) { func (wd *WDADriver) TouchAndHold(x, y float64, opts ...option.ActionOption) (err error) {
actionOptions := option.NewActionOptions(opts...) actionOptions := option.NewActionOptions(opts...)
x, y = actionOptions.ApplyOffset(x, y) x, y = actionOptions.ApplyTapOffset(x, y)
if actionOptions.Duration == 0 { if actionOptions.Duration == 0 {
opts = append(opts, option.WithPressDuration(1)) opts = append(opts, option.WithPressDuration(1))
} }

View File

@ -99,9 +99,9 @@ func (o *ActionOptions) Options() []ActionOption {
// for tap [x,y] offset // for tap [x,y] offset
options = append(options, WithTapOffset(o.TapOffset[0], o.TapOffset[1])) options = append(options, WithTapOffset(o.TapOffset[0], o.TapOffset[1]))
} }
if o.TapRandom { if o.TapRandomRect {
// tap random point in OCR/CV rectangle // tap random point in OCR/CV rectangle
options = append(options, WithTapRandom(true)) options = append(options, WithTapRandomRect(true))
} }
if len(o.SwipeOffset) == 4 { if len(o.SwipeOffset) == 4 {
// for swipe [fromX, fromY, toX, toY] offset // for swipe [fromX, fromY, toX, toY] offset
@ -136,17 +136,17 @@ func (o *ActionOptions) Options() []ActionOption {
return options return options
} }
func (o *ActionOptions) ApplyOffset(absX, absY float64) (float64, float64) { func (o *ActionOptions) ApplyTapOffset(absX, absY float64) (float64, float64) {
if len(o.TapOffset) == 2 { if len(o.TapOffset) == 2 {
absX += float64(o.TapOffset[0]) absX += float64(o.TapOffset[0])
absY += float64(o.TapOffset[1]) absY += float64(o.TapOffset[1])
} }
absX += o.GenerateRandomOffset() absX += o.generateRandomOffset()
absY += o.GenerateRandomOffset() absY += o.generateRandomOffset()
return absX, absY return absX, absY
} }
func (o *ActionOptions) GenerateRandomOffset() float64 { func (o *ActionOptions) generateRandomOffset() float64 {
if len(o.OffsetRandomRange) != 2 { if len(o.OffsetRandomRange) != 2 {
// invalid offset random range, should be [min, max] // invalid offset random range, should be [min, max]
return 0 return 0

View File

@ -216,8 +216,8 @@ type ScreenFilterOptions struct {
AbsScope AbsScope `json:"abs_scope,omitempty" yaml:"abs_scope,omitempty"` AbsScope AbsScope `json:"abs_scope,omitempty" yaml:"abs_scope,omitempty"`
Regex bool `json:"regex,omitempty" yaml:"regex,omitempty"` // use regex to match text Regex bool `json:"regex,omitempty" yaml:"regex,omitempty"` // use regex to match text
TapOffset []int `json:"tap_offset,omitempty" yaml:"tap_offset,omitempty"` // tap with point offset TapOffset []int `json:"tap_offset,omitempty" yaml:"tap_offset,omitempty"` // tap with absolute point offset
TapRandom bool `json:"tap_random,omitempty" yaml:"tap_random,omitempty"` // tap random point in text/image rectangle TapRandomRect bool `json:"tap_random_rect,omitempty" yaml:"tap_random_rect,omitempty"` // tap random point in text/image rectangle
SwipeOffset []int `json:"swipe_offset,omitempty" yaml:"swipe_offset,omitempty"` // swipe with direction offset SwipeOffset []int `json:"swipe_offset,omitempty" yaml:"swipe_offset,omitempty"` // swipe with direction offset
OffsetRandomRange []int `json:"offset_random_range,omitempty" yaml:"offset_random_range,omitempty"` // set random range [min, max] for tap/swipe points OffsetRandomRange []int `json:"offset_random_range,omitempty" yaml:"offset_random_range,omitempty"` // set random range [min, max] for tap/swipe points
Index int `json:"index,omitempty" yaml:"index,omitempty"` // index of the target element Index int `json:"index,omitempty" yaml:"index,omitempty"` // index of the target element
@ -248,11 +248,11 @@ func WithTapOffset(offsetX, offsetY int) ActionOption {
} }
} }
// WithTapRandom is used with TapByOCR and TapByCV // WithTapRandomRect is used with TapByOCR and TapByCV
// when set true, tap random point in text/image rectangle // when set true, tap random point in text/image rectangle
func WithTapRandom(tapRandom bool) ActionOption { func WithTapRandomRect(tapRandom bool) ActionOption {
return func(o *ActionOptions) { return func(o *ActionOptions) {
o.TapRandom = tapRandom o.TapRandomRect = tapRandom
} }
} }