refactor: AssertAppForeground

This commit is contained in:
lilong.129
2023-04-28 16:15:53 +08:00
parent 1d41d276ab
commit 6067d5ddbd
9 changed files with 32 additions and 29 deletions

View File

@@ -362,30 +362,29 @@ func (ad *adbDriver) GetLastLaunchedApp() (packageName string) {
return ad.lastLaunchedPackageName
}
func (ad *adbDriver) IsAppInForeground(packageName string) (bool, error) {
func (ad *adbDriver) AssertAppForeground(packageName string) error {
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
output, err := ad.adbClient.RunShellCommand("dumpsys", "activity", "activities")
if err != nil {
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")
isInForeground := false
for _, line := range lines {
trimmedLine := strings.TrimSpace(line)
if strings.HasPrefix(trimmedLine, "mResumedActivity:") {
if strings.Contains(trimmedLine, packageName) {
isInForeground = true
return nil
}
break
}
}
return isInForeground, nil
return errors.New("app is not in foreground")
}

View File

@@ -361,18 +361,19 @@ func TestDriver_IsAppInForeground(t *testing.T) {
t.Fatal(err)
}
yes, err := driver.Driver.IsAppInForeground(driver.Driver.GetLastLaunchedApp())
if err != nil || !yes {
err = driver.Driver.AssertAppForeground(driver.Driver.GetLastLaunchedApp())
if err != nil {
t.Fatal(err)
}
time.Sleep(2 * time.Second)
_, err = driver.Driver.AppTerminate("com.android.settings")
if err != nil {
t.Fatal(err)
}
yes, err = driver.Driver.IsAppInForeground("com.android.settings")
if err != nil || yes {
err = driver.Driver.AssertAppForeground("com.android.settings")
if err == nil {
t.Fatal(err)
}
}

View File

@@ -217,16 +217,6 @@ 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
}
// (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
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:
result = (dExt.IsImageExist(expected) == exp)
case SelectorForegroundApp:
result = (dExt.IsAppInForeground(expected) == exp)
result = ((dExt.Driver.AssertAppForeground(expected) == nil) == exp)
}
if !result {

View File

@@ -621,8 +621,8 @@ type WebDriver interface {
AppTerminate(packageName string) (bool, error)
// GetLastLaunchedApp returns the package name of the last launched app
GetLastLaunchedApp() string
// IsAppInForeground returns true if the given package is in foreground
IsAppInForeground(packageName string) (bool, error)
// AssertAppForeground returns nil if the given package is in foreground
AssertAppForeground(packageName string) error
// StartCamera Starts a new camera for recording
StartCamera() error

View File

@@ -369,8 +369,8 @@ func (wd *wdaDriver) GetLastLaunchedApp() (packageName string) {
return wd.lastLaunchedPackageName
}
func (wd *wdaDriver) IsAppInForeground(packageName string) (bool, error) {
return false, errors.New("not implemented")
func (wd *wdaDriver) AssertAppForeground(packageName string) error {
return nil
}
func (wd *wdaDriver) Tap(x, y int, options ...DataOption) error {

View File

@@ -3,7 +3,10 @@ package uixt
import (
"time"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
"github.com/httprunner/httprunner/v4/hrp/internal/code"
)
type VideoCrawlerConfigs struct {
@@ -24,6 +27,14 @@ func (dExt *DriverExt) VideoCrawler(configs *VideoCrawlerConfigs) (err error) {
// loop until target count achieved
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
texts, err := dExt.GetScreenTextsByOCR()
if err != nil {