mirror of
https://github.com/httprunner/httprunner.git
synced 2026-09-05 23:51:25 +08:00
Merge pull request #1701 from httprunner/swipe-random
tap/swipe with random offset
This commit is contained in:
@@ -1,5 +1,13 @@
|
|||||||
# Release History
|
# Release History
|
||||||
|
|
||||||
|
## v4.3.7 (2023-09-19)
|
||||||
|
|
||||||
|
**go version**
|
||||||
|
|
||||||
|
- feat: add `WithSwipeOffset` to set offset for swipe start/end point
|
||||||
|
- feat: set random offset for tap/swipe points with `WithOffsetRandomRange`
|
||||||
|
- change: set `WithOffset` deprecated, replace with `WithTapOffset`
|
||||||
|
|
||||||
## v4.3.6 (2023-09-07)
|
## v4.3.6 (2023-09-07)
|
||||||
|
|
||||||
**go version**
|
**go version**
|
||||||
|
|||||||
@@ -39,8 +39,8 @@ func TestIOSDouyinFollowLive(t *testing.T) {
|
|||||||
TapByOCR("关注", uixt.WithIndex(1)).Sleep(10),
|
TapByOCR("关注", uixt.WithIndex(1)).Sleep(10),
|
||||||
hrp.NewStep("向上滑动 2 次").
|
hrp.NewStep("向上滑动 2 次").
|
||||||
IOS().SwipeToTapTexts([]string{"理肤泉", "婉宝"}, uixt.WithCustomDirection(0.6, 0.2, 0.2, 0.2), uixt.WithIdentifier("click_live")).Sleep(10).
|
IOS().SwipeToTapTexts([]string{"理肤泉", "婉宝"}, uixt.WithCustomDirection(0.6, 0.2, 0.2, 0.2), uixt.WithIdentifier("click_live")).Sleep(10).
|
||||||
Swipe(0.9, 0.7, 0.9, 0.3, uixt.WithIdentifier("slide_in_live")).Sleep(10).ScreenShot(). // 上划 1 次,等待 10s,截图保存
|
Swipe(0.9, 0.7, 0.9, 0.3, uixt.WithIdentifier("slide_in_live"), uixt.WithOffsetRandomRange(-10, 10)).Sleep(10).ScreenShot(). // 上划 1 次,等待 10s,截图保存
|
||||||
Swipe(0.9, 0.7, 0.9, 0.3, uixt.WithIdentifier("slide_in_live")).Sleep(10).ScreenShot(), // 再上划 1 次,等待 10s,截图保存
|
Swipe(0.9, 0.7, 0.9, 0.3, uixt.WithIdentifier("slide_in_live"), uixt.WithOffsetRandomRange(-10, 10)).Sleep(10).ScreenShot(), // 再上划 1 次,等待 10s,截图保存
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -111,7 +111,7 @@ func TestAndroidExpertTest(t *testing.T) {
|
|||||||
hrp.NewStep("home 以及 swipe_to_tap_app 自定义配置").
|
hrp.NewStep("home 以及 swipe_to_tap_app 自定义配置").
|
||||||
Android().
|
Android().
|
||||||
Home().
|
Home().
|
||||||
SwipeToTapApp("$app_name", uixt.WithMaxRetryTimes(5), uixt.WithInterval(1), uixt.WithOffset(0, -50)).
|
SwipeToTapApp("$app_name", uixt.WithMaxRetryTimes(5), uixt.WithInterval(1), uixt.WithTapOffset(0, -50)).
|
||||||
Sleep(10),
|
Sleep(10),
|
||||||
hrp.NewStep("处理弹窗 close_popups 自定义配置 以及 ui_ocr exists 断言").
|
hrp.NewStep("处理弹窗 close_popups 自定义配置 以及 ui_ocr exists 断言").
|
||||||
Android().
|
Android().
|
||||||
@@ -265,7 +265,7 @@ func TestIOSExpertTest(t *testing.T) {
|
|||||||
hrp.NewStep("home 以及 swipe_to_tap_app 自定义配置").
|
hrp.NewStep("home 以及 swipe_to_tap_app 自定义配置").
|
||||||
IOS().
|
IOS().
|
||||||
Home().
|
Home().
|
||||||
SwipeToTapApp("$app_name", uixt.WithMaxRetryTimes(5), uixt.WithInterval(1), uixt.WithOffset(0, -50)).
|
SwipeToTapApp("$app_name", uixt.WithMaxRetryTimes(5), uixt.WithInterval(1), uixt.WithTapOffset(0, -50)).
|
||||||
Sleep(10),
|
Sleep(10),
|
||||||
hrp.NewStep("处理弹窗 close_popups 自定义配置 以及 ui_ocr exists 断言").
|
hrp.NewStep("处理弹窗 close_popups 自定义配置 以及 ui_ocr exists 断言").
|
||||||
IOS().
|
IOS().
|
||||||
|
|||||||
+82
-7
@@ -106,10 +106,11 @@ type ActionOptions struct {
|
|||||||
Scope Scope `json:"scope,omitempty" yaml:"scope,omitempty"`
|
Scope Scope `json:"scope,omitempty" yaml:"scope,omitempty"`
|
||||||
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
|
||||||
Offset []int `json:"offset,omitempty" yaml:"offset,omitempty"` // used to tap offset of point
|
Offset []int `json:"offset,omitempty" yaml:"offset,omitempty"` // used to tap offset of point
|
||||||
Index int `json:"index,omitempty" yaml:"index,omitempty"` // index of the target element
|
OffsetRandomRange []int `json:"offset_random_range,omitempty" yaml:"offset_random_range,omitempty"` // set random range [min, max] for tap/swipe points
|
||||||
MatchOne bool `json:"match_one,omitempty" yaml:"match_one,omitempty"` // match one of the targets if existed
|
Index int `json:"index,omitempty" yaml:"index,omitempty"` // index of the target element
|
||||||
|
MatchOne bool `json:"match_one,omitempty" yaml:"match_one,omitempty"` // match one of the targets if existed
|
||||||
|
|
||||||
// set custiom options such as textview, id, description
|
// set custiom options such as textview, id, description
|
||||||
Custom map[string]interface{} `json:"custom,omitempty" yaml:"custom,omitempty"`
|
Custom map[string]interface{} `json:"custom,omitempty" yaml:"custom,omitempty"`
|
||||||
@@ -181,8 +182,18 @@ func (o *ActionOptions) Options() []ActionOption {
|
|||||||
o.AbsScope[0], o.AbsScope[1], o.AbsScope[2], o.AbsScope[3]))
|
o.AbsScope[0], o.AbsScope[1], o.AbsScope[2], o.AbsScope[3]))
|
||||||
}
|
}
|
||||||
if len(o.Offset) == 2 {
|
if len(o.Offset) == 2 {
|
||||||
options = append(options, WithOffset(o.Offset[0], o.Offset[1]))
|
// for tap [x,y] offset
|
||||||
|
options = append(options, WithTapOffset(o.Offset[0], o.Offset[1]))
|
||||||
|
} else if len(o.Offset) == 4 {
|
||||||
|
// for swipe [fromX, fromY, toX, toY] offset
|
||||||
|
options = append(options, WithSwipeOffset(
|
||||||
|
o.Offset[0], o.Offset[1], o.Offset[2], o.Offset[3]))
|
||||||
}
|
}
|
||||||
|
if len(o.OffsetRandomRange) == 2 {
|
||||||
|
options = append(options, WithOffsetRandomRange(
|
||||||
|
o.OffsetRandomRange[0], o.OffsetRandomRange[1]))
|
||||||
|
}
|
||||||
|
|
||||||
if o.Regex {
|
if o.Regex {
|
||||||
options = append(options, WithRegex(true))
|
options = append(options, WithRegex(true))
|
||||||
}
|
}
|
||||||
@@ -238,6 +249,17 @@ func (o *ActionOptions) screenshotActions() []string {
|
|||||||
return actions
|
return actions
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (o *ActionOptions) getRandomOffset() int {
|
||||||
|
if len(o.OffsetRandomRange) != 2 {
|
||||||
|
// invalid offset random range, should be [min, max]
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
minOffset := o.OffsetRandomRange[0]
|
||||||
|
maxOffset := o.OffsetRandomRange[1]
|
||||||
|
return builtin.GetRandomNumber(minOffset, maxOffset)
|
||||||
|
}
|
||||||
|
|
||||||
func NewActionOptions(options ...ActionOption) *ActionOptions {
|
func NewActionOptions(options ...ActionOption) *ActionOptions {
|
||||||
actionOptions := &ActionOptions{}
|
actionOptions := &ActionOptions{}
|
||||||
for _, option := range options {
|
for _, option := range options {
|
||||||
@@ -260,11 +282,47 @@ func mergeDataWithOptions(data map[string]interface{}, options ...ActionOption)
|
|||||||
if len(actionOptions.Offset) == 2 {
|
if len(actionOptions.Offset) == 2 {
|
||||||
if x, ok := data["x"]; ok {
|
if x, ok := data["x"]; ok {
|
||||||
xf, _ := builtin.Interface2Float64(x)
|
xf, _ := builtin.Interface2Float64(x)
|
||||||
data["x"] = xf + float64(actionOptions.Offset[0])
|
data["x"] = xf + float64(actionOptions.Offset[0]+actionOptions.getRandomOffset())
|
||||||
}
|
}
|
||||||
if y, ok := data["y"]; ok {
|
if y, ok := data["y"]; ok {
|
||||||
yf, _ := builtin.Interface2Float64(y)
|
yf, _ := builtin.Interface2Float64(y)
|
||||||
data["y"] = yf + float64(actionOptions.Offset[1])
|
data["y"] = yf + float64(actionOptions.Offset[1]+actionOptions.getRandomOffset())
|
||||||
|
}
|
||||||
|
} else if len(actionOptions.Offset) == 4 {
|
||||||
|
// Android uia2: [startX, startY, endX, endY]
|
||||||
|
if startX, ok := data["startX"]; ok {
|
||||||
|
vf, _ := builtin.Interface2Float64(startX)
|
||||||
|
data["startX"] = vf + float64(actionOptions.Offset[0]+actionOptions.getRandomOffset())
|
||||||
|
}
|
||||||
|
if startY, ok := data["startY"]; ok {
|
||||||
|
vf, _ := builtin.Interface2Float64(startY)
|
||||||
|
data["startY"] = vf + float64(actionOptions.Offset[1]+actionOptions.getRandomOffset())
|
||||||
|
}
|
||||||
|
if endX, ok := data["endX"]; ok {
|
||||||
|
vf, _ := builtin.Interface2Float64(endX)
|
||||||
|
data["endX"] = vf + float64(actionOptions.Offset[2]+actionOptions.getRandomOffset())
|
||||||
|
}
|
||||||
|
if endY, ok := data["endY"]; ok {
|
||||||
|
vf, _ := builtin.Interface2Float64(endY)
|
||||||
|
data["endY"] = vf + float64(actionOptions.Offset[3]+actionOptions.getRandomOffset())
|
||||||
|
}
|
||||||
|
|
||||||
|
// iOS WDA: [fromX, fromY, toX, toY]
|
||||||
|
if fromX, ok := data["fromX"]; ok {
|
||||||
|
vf, _ := builtin.Interface2Float64(fromX)
|
||||||
|
data["fromX"] = vf + float64(actionOptions.Offset[0]+actionOptions.getRandomOffset())
|
||||||
|
}
|
||||||
|
if fromY, ok := data["fromY"]; ok {
|
||||||
|
vf, _ := builtin.Interface2Float64(fromY)
|
||||||
|
data["fromY"] = vf + float64(actionOptions.Offset[1]+actionOptions.getRandomOffset())
|
||||||
|
}
|
||||||
|
if toX, ok := data["toX"]; ok {
|
||||||
|
vf, _ := builtin.Interface2Float64(toX)
|
||||||
|
data["toX"] = vf + float64(actionOptions.Offset[2]+actionOptions.getRandomOffset())
|
||||||
|
}
|
||||||
|
if toY, ok := data["toY"]; ok {
|
||||||
|
vf, _ := builtin.Interface2Float64(toY)
|
||||||
|
data["toY"] = vf + float64(actionOptions.Offset[3]+actionOptions.getRandomOffset())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -377,12 +435,29 @@ func WithAbsScope(x1, y1, x2, y2 int) ActionOption {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Deprecated: use WithTapOffset instead
|
||||||
func WithOffset(offsetX, offsetY int) ActionOption {
|
func WithOffset(offsetX, offsetY int) ActionOption {
|
||||||
return func(o *ActionOptions) {
|
return func(o *ActionOptions) {
|
||||||
o.Offset = []int{offsetX, offsetY}
|
o.Offset = []int{offsetX, offsetY}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// tap [x, y] with offset [offsetX, offsetY]
|
||||||
|
var WithTapOffset = WithOffset
|
||||||
|
|
||||||
|
// swipe [fromX, fromY, toX, toY] with offset [offsetFromX, offsetFromY, offsetToX, offsetToY]
|
||||||
|
func WithSwipeOffset(offsetFromX, offsetFromY, offsetToX, offsetToY int) ActionOption {
|
||||||
|
return func(o *ActionOptions) {
|
||||||
|
o.Offset = []int{offsetFromX, offsetFromY, offsetToX, offsetToY}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func WithOffsetRandomRange(min, max int) ActionOption {
|
||||||
|
return func(o *ActionOptions) {
|
||||||
|
o.OffsetRandomRange = []int{min, max}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func WithRegex(regex bool) ActionOption {
|
func WithRegex(regex bool) ActionOption {
|
||||||
return func(o *ActionOptions) {
|
return func(o *ActionOptions) {
|
||||||
o.Regex = regex
|
o.Regex = regex
|
||||||
|
|||||||
@@ -225,8 +225,8 @@ func (ad *adbDriver) TapFloat(x, y float64, options ...ActionOption) (err error)
|
|||||||
actionOptions := NewActionOptions(options...)
|
actionOptions := NewActionOptions(options...)
|
||||||
|
|
||||||
if len(actionOptions.Offset) == 2 {
|
if len(actionOptions.Offset) == 2 {
|
||||||
x += float64(actionOptions.Offset[0])
|
x += float64(actionOptions.Offset[0] + actionOptions.getRandomOffset())
|
||||||
y += float64(actionOptions.Offset[1])
|
y += float64(actionOptions.Offset[1] + actionOptions.getRandomOffset())
|
||||||
}
|
}
|
||||||
|
|
||||||
// adb shell input tap x y
|
// adb shell input tap x y
|
||||||
@@ -272,6 +272,15 @@ func (ad *adbDriver) Swipe(fromX, fromY, toX, toY int, options ...ActionOption)
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (ad *adbDriver) SwipeFloat(fromX, fromY, toX, toY float64, options ...ActionOption) error {
|
func (ad *adbDriver) SwipeFloat(fromX, fromY, toX, toY float64, options ...ActionOption) error {
|
||||||
|
actionOptions := NewActionOptions(options...)
|
||||||
|
|
||||||
|
if len(actionOptions.Offset) == 4 {
|
||||||
|
fromX += float64(actionOptions.Offset[0] + actionOptions.getRandomOffset())
|
||||||
|
fromY += float64(actionOptions.Offset[1] + actionOptions.getRandomOffset())
|
||||||
|
toX += float64(actionOptions.Offset[2] + actionOptions.getRandomOffset())
|
||||||
|
toY += float64(actionOptions.Offset[3] + actionOptions.getRandomOffset())
|
||||||
|
}
|
||||||
|
|
||||||
// adb shell input swipe fromX fromY toX toY
|
// adb shell input swipe fromX fromY toX toY
|
||||||
_, err := ad.adbClient.RunShellCommand(
|
_, err := ad.adbClient.RunShellCommand(
|
||||||
"input", "swipe",
|
"input", "swipe",
|
||||||
|
|||||||
@@ -192,7 +192,7 @@ func (dExt *DriverExt) swipeToTapApp(appName string, options ...ActionOption) er
|
|||||||
}
|
}
|
||||||
// tap app icon above the text
|
// tap app icon above the text
|
||||||
if len(actionOptions.Offset) == 0 {
|
if len(actionOptions.Offset) == 0 {
|
||||||
options = append(options, WithOffset(0, -25))
|
options = append(options, WithTapOffset(0, -25))
|
||||||
}
|
}
|
||||||
// set default swipe interval to 1 second
|
// set default swipe interval to 1 second
|
||||||
if builtin.IsZeroFloat64(actionOptions.Interval) {
|
if builtin.IsZeroFloat64(actionOptions.Interval) {
|
||||||
|
|||||||
Reference in New Issue
Block a user