mirror of
https://github.com/httprunner/httprunner.git
synced 2026-09-05 23:51:25 +08:00
feat: add validator AssertAppInForeground and AssertAppNotInForeground
This commit is contained in:
+2
-1
@@ -1,6 +1,6 @@
|
|||||||
# Release History
|
# Release History
|
||||||
|
|
||||||
## v4.3.3 (2023-04-14)
|
## v4.3.3 (2023-04-16)
|
||||||
|
|
||||||
**go version**
|
**go version**
|
||||||
|
|
||||||
@@ -9,6 +9,7 @@
|
|||||||
- feat: add adb `screencap` sub command
|
- feat: add adb `screencap` sub command
|
||||||
- feat: add `IsAppInForeground` to check if the given package is in foreground
|
- feat: add `IsAppInForeground` to check if the given package is in foreground
|
||||||
- feat: check if app is in foreground when step failed
|
- feat: check if app is in foreground when step failed
|
||||||
|
- feat: add validator AssertAppInForeground and AssertAppNotInForeground
|
||||||
- fix: adb driver for TapFloat
|
- fix: adb driver for TapFloat
|
||||||
- fix: stop logcat only when enabled
|
- fix: stop logcat only when enabled
|
||||||
- fix: do not fail case when kill logcat error
|
- fix: do not fail case when kill logcat error
|
||||||
|
|||||||
@@ -28,7 +28,15 @@
|
|||||||
"params": 10
|
"params": 10
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
},
|
||||||
|
"validate": [
|
||||||
|
{
|
||||||
|
"check": "ui_foreground_app",
|
||||||
|
"assert": "equal",
|
||||||
|
"expect": "com.ss.android.ugc.aweme",
|
||||||
|
"msg": "app [com.ss.android.ugc.aweme] should be in foreground"
|
||||||
|
}
|
||||||
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "处理青少年弹窗",
|
"name": "处理青少年弹窗",
|
||||||
@@ -102,6 +110,25 @@
|
|||||||
]
|
]
|
||||||
},
|
},
|
||||||
"loops": 10
|
"loops": 10
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "exit",
|
||||||
|
"android": {
|
||||||
|
"actions": [
|
||||||
|
{
|
||||||
|
"method": "app_terminate",
|
||||||
|
"params": "com.ss.android.ugc.aweme"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"validate": [
|
||||||
|
{
|
||||||
|
"check": "ui_foreground_app",
|
||||||
|
"assert": "not_equal",
|
||||||
|
"expect": "com.ss.android.ugc.aweme",
|
||||||
|
"msg": "app [com.ss.android.ugc.aweme] should not be in foreground"
|
||||||
|
}
|
||||||
|
]
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,7 +21,9 @@ func TestAndroidDouyinFeedTest(t *testing.T) {
|
|||||||
Android().
|
Android().
|
||||||
AppTerminate("com.ss.android.ugc.aweme").
|
AppTerminate("com.ss.android.ugc.aweme").
|
||||||
AppLaunch("com.ss.android.ugc.aweme").
|
AppLaunch("com.ss.android.ugc.aweme").
|
||||||
Sleep(10),
|
Sleep(10).
|
||||||
|
Validate().
|
||||||
|
AssertAppInForeground("com.ss.android.ugc.aweme"),
|
||||||
hrp.NewStep("处理青少年弹窗").
|
hrp.NewStep("处理青少年弹窗").
|
||||||
Android().
|
Android().
|
||||||
TapByOCR("我知道了", uixt.WithIgnoreNotFoundError(true)),
|
TapByOCR("我知道了", uixt.WithIgnoreNotFoundError(true)),
|
||||||
@@ -40,6 +42,11 @@ func TestAndroidDouyinFeedTest(t *testing.T) {
|
|||||||
Android().
|
Android().
|
||||||
SwipeUp().
|
SwipeUp().
|
||||||
SleepRandom(0, 5, 0.7, 5, 10, 0.3),
|
SleepRandom(0, 5, 0.7, 5, 10, 0.3),
|
||||||
|
hrp.NewStep("exit").
|
||||||
|
Android().
|
||||||
|
AppTerminate("com.ss.android.ugc.aweme").
|
||||||
|
Validate().
|
||||||
|
AssertAppNotInForeground("com.ss.android.ugc.aweme"),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
v4.3.3.2304161232
|
v4.3.3.2304161855
|
||||||
@@ -368,6 +368,7 @@ func (ad *adbDriver) IsAppInForeground(packageName string) (bool, error) {
|
|||||||
// adb shell dumpsys activity activities | grep mResumedActivity
|
// adb shell dumpsys activity activities | grep mResumedActivity
|
||||||
output, err := ad.adbClient.RunShellCommand("dumpsys", "activity", "activities")
|
output, err := ad.adbClient.RunShellCommand("dumpsys", "activity", "activities")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Error().Err(err).Msg("failed to dumpsys activities")
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+27
-10
@@ -41,10 +41,15 @@ const (
|
|||||||
RecordStop MobileMethod = "record_stop"
|
RecordStop MobileMethod = "record_stop"
|
||||||
|
|
||||||
// UI validation
|
// UI validation
|
||||||
SelectorName string = "ui_name"
|
// selectors
|
||||||
SelectorLabel string = "ui_label"
|
SelectorName string = "ui_name"
|
||||||
SelectorOCR string = "ui_ocr"
|
SelectorLabel string = "ui_label"
|
||||||
SelectorImage string = "ui_image"
|
SelectorOCR string = "ui_ocr"
|
||||||
|
SelectorImage string = "ui_image"
|
||||||
|
SelectorForegroundApp string = "ui_foreground_app"
|
||||||
|
// assertions
|
||||||
|
AssertionEqual string = "equal"
|
||||||
|
AssertionNotEqual string = "not_equal"
|
||||||
AssertionExists string = "exists"
|
AssertionExists string = "exists"
|
||||||
AssertionNotExists string = "not_exists"
|
AssertionNotExists string = "not_exists"
|
||||||
|
|
||||||
@@ -349,6 +354,16 @@ func (dExt *DriverExt) IsImageExist(text string) bool {
|
|||||||
return err == nil
|
return err == nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (dExt *DriverExt) IsAppInForeground(packageName string) bool {
|
||||||
|
// check if app is in foreground
|
||||||
|
yes, err := dExt.Driver.IsAppInForeground(packageName)
|
||||||
|
if !yes || err != nil {
|
||||||
|
log.Info().Str("packageName", packageName).Msg("app is not in foreground")
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
var errActionNotImplemented = errors.New("UI action not implemented")
|
var errActionNotImplemented = errors.New("UI action not implemented")
|
||||||
|
|
||||||
func convertToFloat64(val interface{}) (float64, error) {
|
func convertToFloat64(val interface{}) (float64, error) {
|
||||||
@@ -700,18 +715,20 @@ func (dExt *DriverExt) getAbsScope(x1, y1, x2, y2 float64) (int, int, int, int)
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (dExt *DriverExt) DoValidation(check, assert, expected string, message ...string) bool {
|
func (dExt *DriverExt) DoValidation(check, assert, expected string, message ...string) bool {
|
||||||
var exists bool
|
var exp bool
|
||||||
if assert == AssertionExists {
|
if assert == AssertionExists || assert == AssertionEqual {
|
||||||
exists = true
|
exp = true
|
||||||
} else {
|
} else {
|
||||||
exists = false
|
exp = false
|
||||||
}
|
}
|
||||||
var result bool
|
var result bool
|
||||||
switch check {
|
switch check {
|
||||||
case SelectorOCR:
|
case SelectorOCR:
|
||||||
result = (dExt.IsOCRExist(expected) == exists)
|
result = (dExt.IsOCRExist(expected) == exp)
|
||||||
case SelectorImage:
|
case SelectorImage:
|
||||||
result = (dExt.IsImageExist(expected) == exists)
|
result = (dExt.IsImageExist(expected) == exp)
|
||||||
|
case SelectorForegroundApp:
|
||||||
|
result = (dExt.IsAppInForeground(expected) == exp)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !result {
|
if !result {
|
||||||
|
|||||||
@@ -468,6 +468,36 @@ func (s *StepMobileUIValidation) AssertImageNotExists(expectedImagePath string,
|
|||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *StepMobileUIValidation) AssertAppInForeground(packageName string, msg ...string) *StepMobileUIValidation {
|
||||||
|
v := Validator{
|
||||||
|
Check: uixt.SelectorForegroundApp,
|
||||||
|
Assert: uixt.AssertionEqual,
|
||||||
|
Expect: packageName,
|
||||||
|
}
|
||||||
|
if len(msg) > 0 {
|
||||||
|
v.Message = msg[0]
|
||||||
|
} else {
|
||||||
|
v.Message = fmt.Sprintf("app [%s] should be in foreground", packageName)
|
||||||
|
}
|
||||||
|
s.step.Validators = append(s.step.Validators, v)
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *StepMobileUIValidation) AssertAppNotInForeground(packageName string, msg ...string) *StepMobileUIValidation {
|
||||||
|
v := Validator{
|
||||||
|
Check: uixt.SelectorForegroundApp,
|
||||||
|
Assert: uixt.AssertionNotEqual,
|
||||||
|
Expect: packageName,
|
||||||
|
}
|
||||||
|
if len(msg) > 0 {
|
||||||
|
v.Message = msg[0]
|
||||||
|
} else {
|
||||||
|
v.Message = fmt.Sprintf("app [%s] should not be in foreground", packageName)
|
||||||
|
}
|
||||||
|
s.step.Validators = append(s.step.Validators, v)
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
func (s *StepMobileUIValidation) Name() string {
|
func (s *StepMobileUIValidation) Name() string {
|
||||||
return s.step.Name
|
return s.step.Name
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user