mirror of
https://github.com/httprunner/httprunner.git
synced 2026-06-07 00:39:34 +08:00
refactor: AssertAppForeground
This commit is contained in:
@@ -7,7 +7,7 @@
|
|||||||
- feat: add `sleep_random` to sleep random seconds, with weight for multiple time ranges
|
- feat: add `sleep_random` to sleep random seconds, with weight for multiple time ranges
|
||||||
- feat: input text with adb
|
- feat: input text with adb
|
||||||
- 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 `AssertAppInForeground` 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
|
- feat: add validator AssertAppInForeground and AssertAppNotInForeground
|
||||||
- feat: save screenshots of all steps including ocr and cv recognition process data
|
- feat: save screenshots of all steps including ocr and cv recognition process data
|
||||||
|
|||||||
@@ -61,6 +61,7 @@ var (
|
|||||||
var (
|
var (
|
||||||
AndroidDeviceConnectionError = errors.New("android device connection error") // 60
|
AndroidDeviceConnectionError = errors.New("android device connection error") // 60
|
||||||
AndroidDeviceUSBDriverError = errors.New("android device USB driver error") // 61
|
AndroidDeviceUSBDriverError = errors.New("android device USB driver error") // 61
|
||||||
|
AndroidShellExecError = errors.New("android shell exec error") // 62
|
||||||
AndroidScreenShotError = errors.New("android screenshot error") // 65
|
AndroidScreenShotError = errors.New("android screenshot error") // 65
|
||||||
AndroidCaptureLogError = errors.New("android capture log error") // 66
|
AndroidCaptureLogError = errors.New("android capture log error") // 66
|
||||||
)
|
)
|
||||||
@@ -121,6 +122,7 @@ var errorsMap = map[error]int{
|
|||||||
// android related
|
// android related
|
||||||
AndroidDeviceConnectionError: 60,
|
AndroidDeviceConnectionError: 60,
|
||||||
AndroidDeviceUSBDriverError: 61,
|
AndroidDeviceUSBDriverError: 61,
|
||||||
|
AndroidShellExecError: 62,
|
||||||
AndroidScreenShotError: 65,
|
AndroidScreenShotError: 65,
|
||||||
AndroidCaptureLogError: 66,
|
AndroidCaptureLogError: 66,
|
||||||
|
|
||||||
|
|||||||
@@ -362,30 +362,29 @@ func (ad *adbDriver) GetLastLaunchedApp() (packageName string) {
|
|||||||
return ad.lastLaunchedPackageName
|
return ad.lastLaunchedPackageName
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ad *adbDriver) IsAppInForeground(packageName string) (bool, error) {
|
func (ad *adbDriver) AssertAppForeground(packageName string) error {
|
||||||
if packageName == "" {
|
if packageName == "" {
|
||||||
return false, errors.New("package name is not given")
|
return errors.New("package name is not given")
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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")
|
log.Error().Err(err).Msg("failed to dumpsys activities")
|
||||||
return false, err
|
return errors.Wrap(code.AndroidShellExecError, err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
lines := strings.Split(string(output), "\n")
|
lines := strings.Split(string(output), "\n")
|
||||||
isInForeground := false
|
|
||||||
|
|
||||||
for _, line := range lines {
|
for _, line := range lines {
|
||||||
trimmedLine := strings.TrimSpace(line)
|
trimmedLine := strings.TrimSpace(line)
|
||||||
if strings.HasPrefix(trimmedLine, "mResumedActivity:") {
|
if strings.HasPrefix(trimmedLine, "mResumedActivity:") {
|
||||||
if strings.Contains(trimmedLine, packageName) {
|
if strings.Contains(trimmedLine, packageName) {
|
||||||
isInForeground = true
|
return nil
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return isInForeground, nil
|
return errors.New("app is not in foreground")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -361,18 +361,19 @@ func TestDriver_IsAppInForeground(t *testing.T) {
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
yes, err := driver.Driver.IsAppInForeground(driver.Driver.GetLastLaunchedApp())
|
err = driver.Driver.AssertAppForeground(driver.Driver.GetLastLaunchedApp())
|
||||||
if err != nil || !yes {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
time.Sleep(2 * time.Second)
|
||||||
_, err = driver.Driver.AppTerminate("com.android.settings")
|
_, err = driver.Driver.AppTerminate("com.android.settings")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
yes, err = driver.Driver.IsAppInForeground("com.android.settings")
|
err = driver.Driver.AssertAppForeground("com.android.settings")
|
||||||
if err != nil || yes {
|
if err == nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -217,16 +217,6 @@ 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
|
|
||||||
}
|
|
||||||
|
|
||||||
// (x1, y1) is the top left corner, (x2, y2) is the bottom right corner
|
// (x1, y1) is the top left corner, (x2, y2) is the bottom right corner
|
||||||
// the value of (x, y) is between 0 and 1, which means the percentage of the screen
|
// the value of (x, y) is between 0 and 1, which means the percentage of the screen
|
||||||
func (dExt *DriverExt) getAbsScope(x1, y1, x2, y2 float64) (int, int, int, int) {
|
func (dExt *DriverExt) getAbsScope(x1, y1, x2, y2 float64) (int, int, int, int) {
|
||||||
@@ -250,7 +240,7 @@ func (dExt *DriverExt) DoValidation(check, assert, expected string, message ...s
|
|||||||
case SelectorImage:
|
case SelectorImage:
|
||||||
result = (dExt.IsImageExist(expected) == exp)
|
result = (dExt.IsImageExist(expected) == exp)
|
||||||
case SelectorForegroundApp:
|
case SelectorForegroundApp:
|
||||||
result = (dExt.IsAppInForeground(expected) == exp)
|
result = ((dExt.Driver.AssertAppForeground(expected) == nil) == exp)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !result {
|
if !result {
|
||||||
|
|||||||
@@ -621,8 +621,8 @@ type WebDriver interface {
|
|||||||
AppTerminate(packageName string) (bool, error)
|
AppTerminate(packageName string) (bool, error)
|
||||||
// GetLastLaunchedApp returns the package name of the last launched app
|
// GetLastLaunchedApp returns the package name of the last launched app
|
||||||
GetLastLaunchedApp() string
|
GetLastLaunchedApp() string
|
||||||
// IsAppInForeground returns true if the given package is in foreground
|
// AssertAppForeground returns nil if the given package is in foreground
|
||||||
IsAppInForeground(packageName string) (bool, error)
|
AssertAppForeground(packageName string) error
|
||||||
|
|
||||||
// StartCamera Starts a new camera for recording
|
// StartCamera Starts a new camera for recording
|
||||||
StartCamera() error
|
StartCamera() error
|
||||||
|
|||||||
@@ -369,8 +369,8 @@ func (wd *wdaDriver) GetLastLaunchedApp() (packageName string) {
|
|||||||
return wd.lastLaunchedPackageName
|
return wd.lastLaunchedPackageName
|
||||||
}
|
}
|
||||||
|
|
||||||
func (wd *wdaDriver) IsAppInForeground(packageName string) (bool, error) {
|
func (wd *wdaDriver) AssertAppForeground(packageName string) error {
|
||||||
return false, errors.New("not implemented")
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (wd *wdaDriver) Tap(x, y int, options ...DataOption) error {
|
func (wd *wdaDriver) Tap(x, y int, options ...DataOption) error {
|
||||||
|
|||||||
@@ -3,7 +3,10 @@ package uixt
|
|||||||
import (
|
import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
|
|
||||||
|
"github.com/httprunner/httprunner/v4/hrp/internal/code"
|
||||||
)
|
)
|
||||||
|
|
||||||
type VideoCrawlerConfigs struct {
|
type VideoCrawlerConfigs struct {
|
||||||
@@ -24,6 +27,14 @@ func (dExt *DriverExt) VideoCrawler(configs *VideoCrawlerConfigs) (err error) {
|
|||||||
|
|
||||||
// loop until target count achieved
|
// loop until target count achieved
|
||||||
for {
|
for {
|
||||||
|
// check if app in foreground
|
||||||
|
err := dExt.Driver.AssertAppForeground(configs.AppPackageName)
|
||||||
|
if err != nil {
|
||||||
|
log.Error().Err(err).Str("packageName", configs.AppPackageName).Msg("app is not in foreground")
|
||||||
|
err = errors.Wrap(code.MobileUIAppNotInForegroundError, err.Error())
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
// take screenshot and get screen texts by OCR
|
// take screenshot and get screen texts by OCR
|
||||||
texts, err := dExt.GetScreenTextsByOCR()
|
texts, err := dExt.GetScreenTextsByOCR()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -601,9 +601,9 @@ func runStepMobileUI(s *SessionRunner, step *TStep) (stepResult *StepResult, err
|
|||||||
|
|
||||||
// check if app is in foreground
|
// check if app is in foreground
|
||||||
packageName := uiDriver.Driver.GetLastLaunchedApp()
|
packageName := uiDriver.Driver.GetLastLaunchedApp()
|
||||||
yes, err2 := uiDriver.Driver.IsAppInForeground(packageName)
|
err := uiDriver.Driver.AssertAppForeground(packageName)
|
||||||
if packageName != "" && (!yes || err2 != nil) {
|
if packageName != "" && err != nil {
|
||||||
log.Error().Err(err2).Str("packageName", packageName).Msg("app is not in foreground")
|
log.Error().Err(err).Str("packageName", packageName).Msg("app is not in foreground")
|
||||||
err = errors.Wrap(code.MobileUIAppNotInForegroundError, err.Error())
|
err = errors.Wrap(code.MobileUIAppNotInForegroundError, err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user