mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-13 08:59:44 +08:00
refactor: assert device UI
This commit is contained in:
@@ -453,3 +453,84 @@ func (ad *adbDriver) GetForegroundApp() (app AppInfo, err error) {
|
||||
|
||||
return AppInfo{}, errors.New("get foreground app failed")
|
||||
}
|
||||
|
||||
func (ad *adbDriver) AssertUI(packageName, activityType string) error {
|
||||
log.Debug().Str("pacakge_name", packageName).
|
||||
Str("activity_type", activityType).Msg("assert android activity")
|
||||
app, err := ad.GetForegroundApp()
|
||||
if err != nil {
|
||||
log.Warn().Err(err).Msg("get foreground app failed, skip app/activity assertion")
|
||||
return nil // Notice: ignore error when get foreground app failed
|
||||
}
|
||||
|
||||
if app.PackageName != packageName {
|
||||
return errors.Wrap(code.MobileUIAppNotInForegroundError,
|
||||
fmt.Sprintf("foreground app %s, expect %s", app.PackageName, packageName))
|
||||
}
|
||||
|
||||
activities, ok := androidActivities[app.PackageName]
|
||||
if !ok {
|
||||
msg := fmt.Sprintf("app package %s not configured", app.PackageName)
|
||||
log.Error().Interface("app", app.AppBaseInfo).Msg(msg)
|
||||
return errors.Wrap(code.MobileUIActivityNotMatchError, msg)
|
||||
}
|
||||
|
||||
expectActivities, ok := activities[activityType]
|
||||
if !ok {
|
||||
msg := fmt.Sprintf("app package %s %s not configured", app.PackageName, activityType)
|
||||
log.Error().Interface("app", app.AppBaseInfo).Msg(msg)
|
||||
return errors.Wrap(code.MobileUIActivityNotMatchError, msg)
|
||||
}
|
||||
|
||||
// assert success
|
||||
for _, expectActivity := range expectActivities {
|
||||
if strings.HasSuffix(app.Activity, expectActivity) {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// assert failed
|
||||
log.Error().
|
||||
Interface("app", app.AppBaseInfo).
|
||||
Str("expectActivityType", activityType).
|
||||
Strs("expectActivities", expectActivities).
|
||||
Msg("assert activity failed")
|
||||
|
||||
return errors.Wrap(code.MobileUIActivityNotMatchError, "assert activity failed")
|
||||
}
|
||||
|
||||
var androidActivities = map[string]map[string][]string{
|
||||
// DY
|
||||
"com.ss.android.ugc.aweme": {
|
||||
"feed": []string{".splash.SplashActivity"},
|
||||
"live": []string{".live.LivePlayActivity"},
|
||||
},
|
||||
// DY lite
|
||||
"com.ss.android.ugc.aweme.lite": {
|
||||
"feed": []string{".splash.SplashActivity"},
|
||||
"live": []string{".live.LivePlayActivity"},
|
||||
},
|
||||
// KS
|
||||
"com.smile.gifmaker": {
|
||||
"feed": []string{
|
||||
"com.yxcorp.gifshow.HomeActivity",
|
||||
"com.yxcorp.gifshow.detail.PhotoDetailActivity",
|
||||
},
|
||||
"live": []string{
|
||||
"com.kuaishou.live.core.basic.activity.LiveSlideActivity",
|
||||
"com.yxcorp.gifshow.detail.PhotoDetailActivity",
|
||||
},
|
||||
},
|
||||
// KS lite
|
||||
"com.kuaishou.nebula": {
|
||||
"feed": []string{
|
||||
"com.yxcorp.gifshow.HomeActivity",
|
||||
"com.yxcorp.gifshow.detail.PhotoDetailActivity",
|
||||
},
|
||||
"live": []string{
|
||||
"com.kuaishou.live.core.basic.activity.LiveSlideActivity",
|
||||
"com.yxcorp.gifshow.detail.PhotoDetailActivity",
|
||||
},
|
||||
},
|
||||
// TODO: SPH, XHS
|
||||
}
|
||||
|
||||
@@ -495,6 +495,8 @@ type WebDriver interface {
|
||||
AssertAppForeground(packageName string) error
|
||||
// GetForegroundApp returns current foreground app package name and activity name
|
||||
GetForegroundApp() (app AppInfo, err error)
|
||||
// AssertUI returns nil if the given package and activity are in foreground
|
||||
AssertUI(packageName, activityType string) error
|
||||
|
||||
// StartCamera Starts a new camera for recording
|
||||
StartCamera() error
|
||||
|
||||
@@ -394,6 +394,13 @@ func (wd *wdaDriver) GetForegroundApp() (app AppInfo, err error) {
|
||||
"GetForegroundApp not implemented for ios")
|
||||
}
|
||||
|
||||
func (wd *wdaDriver) AssertUI(bundleId, viewControllerType string) error {
|
||||
log.Debug().Str("bundleId", bundleId).
|
||||
Str("viewControllerType", viewControllerType).
|
||||
Msg("ios view controller assertion not implemented, skip")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (wd *wdaDriver) Tap(x, y int, options ...ActionOption) error {
|
||||
return wd.TapFloat(float64(x), float64(y), options...)
|
||||
}
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
package uixt
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
@@ -177,31 +175,6 @@ type VideoCrawlerConfigs struct {
|
||||
Live LiveConfig `json:"live"`
|
||||
}
|
||||
|
||||
var androidActivities = map[string]map[string]string{
|
||||
// DY
|
||||
"com.ss.android.ugc.aweme": {
|
||||
"feed": ".splash.SplashActivity",
|
||||
"live": ".live.LivePlayActivity",
|
||||
},
|
||||
// DY lite
|
||||
"com.ss.android.ugc.aweme.lite": {
|
||||
"feed": ".splash.SplashActivity",
|
||||
"live": ".live.LivePlayActivity",
|
||||
},
|
||||
// KS
|
||||
"com.smile.gifmaker": {
|
||||
"feed": "com.yxcorp.gifshow.HomeActivity",
|
||||
"live": "com.kuaishou.live.core.basic.activity.LiveSlideActivity",
|
||||
// "com.yxcorp.gifshow.detail.PhotoDetailActivity"
|
||||
},
|
||||
// KS lite
|
||||
"com.kuaishou.nebula": {
|
||||
"feed": "com.yxcorp.gifshow.HomeActivity",
|
||||
"live": "com.kuaishou.live.core.basic.activity.LiveSlideActivity",
|
||||
},
|
||||
// TODO: SPH, XHS
|
||||
}
|
||||
|
||||
type LiveCrawler struct {
|
||||
driver *DriverExt
|
||||
configs *VideoCrawlerConfigs // target video count
|
||||
@@ -258,7 +231,7 @@ func (l *LiveCrawler) Run(driver *DriverExt, enterPoint PointF) error {
|
||||
return errors.Wrap(code.InterruptError, "live crawler interrupted")
|
||||
default:
|
||||
// check if live room
|
||||
if err := l.driver.assertActivity(l.configs.AppPackageName, "live"); err != nil {
|
||||
if err := l.driver.Driver.AssertUI(l.configs.AppPackageName, "live"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -306,7 +279,7 @@ func (l *LiveCrawler) exitLiveRoom() error {
|
||||
time.Sleep(2 * time.Second)
|
||||
|
||||
// check if back to feed page
|
||||
if err := l.driver.assertActivity(l.configs.AppPackageName, "feed"); err == nil {
|
||||
if err := l.driver.Driver.AssertUI(l.configs.AppPackageName, "feed"); err == nil {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
@@ -322,7 +295,7 @@ func (l *LiveCrawler) exitLiveRoom() error {
|
||||
time.Sleep(2 * time.Second)
|
||||
|
||||
// check if back to feed page
|
||||
if err := l.driver.assertActivity(l.configs.AppPackageName, "feed"); err == nil {
|
||||
if err := l.driver.Driver.AssertUI(l.configs.AppPackageName, "feed"); err == nil {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
@@ -446,52 +419,9 @@ func (dExt *DriverExt) VideoCrawler(configs *VideoCrawlerConfigs) (err error) {
|
||||
time.Sleep(1 * time.Second)
|
||||
|
||||
// check if feed page
|
||||
if err := dExt.assertActivity(configs.AppPackageName, "feed"); err != nil {
|
||||
if err := dExt.Driver.AssertUI(configs.AppPackageName, "feed"); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (dExt *DriverExt) assertActivity(packageName, activityType string) error {
|
||||
log.Debug().Str("pacakge_name", packageName).
|
||||
Str("activity_type", activityType).Msg("assert activity")
|
||||
app, err := dExt.Driver.GetForegroundApp()
|
||||
if err != nil {
|
||||
log.Warn().Err(err).Msg("get foreground app failed, skip app/activity assertion")
|
||||
return nil // Notice: ignore error when get foreground app failed
|
||||
}
|
||||
|
||||
if app.PackageName != packageName {
|
||||
return errors.Wrap(code.MobileUIAppNotInForegroundError,
|
||||
fmt.Sprintf("foreground app %s, expect %s", app.PackageName, packageName))
|
||||
}
|
||||
|
||||
activities, ok := androidActivities[app.PackageName]
|
||||
if !ok {
|
||||
msg := fmt.Sprintf("app package %s not configured", app.PackageName)
|
||||
log.Error().Interface("app", app.AppBaseInfo).Msg(msg)
|
||||
return errors.Wrap(code.MobileUIActivityNotMatchError, msg)
|
||||
}
|
||||
|
||||
expectActivity, ok := activities[activityType]
|
||||
if !ok {
|
||||
msg := fmt.Sprintf("app package %s %s not configured", app.PackageName, activityType)
|
||||
log.Error().Interface("app", app.AppBaseInfo).Msg(msg)
|
||||
return errors.Wrap(code.MobileUIActivityNotMatchError, msg)
|
||||
}
|
||||
|
||||
// assert success
|
||||
if strings.HasSuffix(app.Activity, expectActivity) {
|
||||
return nil
|
||||
}
|
||||
|
||||
// assert failed
|
||||
log.Error().
|
||||
Interface("app", app.AppBaseInfo).
|
||||
Str("expectActivityType", activityType).
|
||||
Str("expectActivity", expectActivity).
|
||||
Msg("assert activity failed")
|
||||
|
||||
return errors.Wrap(code.MobileUIActivityNotMatchError, "assert activity failed")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user