mirror of
https://github.com/httprunner/httprunner.git
synced 2026-06-09 01:39:39 +08:00
feat: add validator AssertAppInForeground and AssertAppNotInForeground
This commit is contained in:
@@ -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
|
||||
output, err := ad.adbClient.RunShellCommand("dumpsys", "activity", "activities")
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("failed to dumpsys activities")
|
||||
return false, err
|
||||
}
|
||||
|
||||
|
||||
@@ -41,10 +41,15 @@ const (
|
||||
RecordStop MobileMethod = "record_stop"
|
||||
|
||||
// UI validation
|
||||
SelectorName string = "ui_name"
|
||||
SelectorLabel string = "ui_label"
|
||||
SelectorOCR string = "ui_ocr"
|
||||
SelectorImage string = "ui_image"
|
||||
// selectors
|
||||
SelectorName string = "ui_name"
|
||||
SelectorLabel string = "ui_label"
|
||||
SelectorOCR string = "ui_ocr"
|
||||
SelectorImage string = "ui_image"
|
||||
SelectorForegroundApp string = "ui_foreground_app"
|
||||
// assertions
|
||||
AssertionEqual string = "equal"
|
||||
AssertionNotEqual string = "not_equal"
|
||||
AssertionExists string = "exists"
|
||||
AssertionNotExists string = "not_exists"
|
||||
|
||||
@@ -349,6 +354,16 @@ func (dExt *DriverExt) IsImageExist(text string) bool {
|
||||
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")
|
||||
|
||||
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 {
|
||||
var exists bool
|
||||
if assert == AssertionExists {
|
||||
exists = true
|
||||
var exp bool
|
||||
if assert == AssertionExists || assert == AssertionEqual {
|
||||
exp = true
|
||||
} else {
|
||||
exists = false
|
||||
exp = false
|
||||
}
|
||||
var result bool
|
||||
switch check {
|
||||
case SelectorOCR:
|
||||
result = (dExt.IsOCRExist(expected) == exists)
|
||||
result = (dExt.IsOCRExist(expected) == exp)
|
||||
case SelectorImage:
|
||||
result = (dExt.IsImageExist(expected) == exists)
|
||||
result = (dExt.IsImageExist(expected) == exp)
|
||||
case SelectorForegroundApp:
|
||||
result = (dExt.IsAppInForeground(expected) == exp)
|
||||
}
|
||||
|
||||
if !result {
|
||||
|
||||
@@ -468,6 +468,36 @@ func (s *StepMobileUIValidation) AssertImageNotExists(expectedImagePath string,
|
||||
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 {
|
||||
return s.step.Name
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user