From 6bc5e11e2deb98709929077beb772867affc6fef Mon Sep 17 00:00:00 2001 From: "lilong.129" Date: Sun, 30 Apr 2023 00:40:44 +0800 Subject: [PATCH] fix: exit live room --- .../uitest/demo_android_video_crawler.json | 59 +++++++++++++++++++ .../uitest/demo_android_video_crawler_test.go | 47 +++++++++++++++ hrp/pkg/uixt/android_adb_driver.go | 6 +- hrp/pkg/uixt/android_test.go | 2 +- hrp/pkg/uixt/interface.go | 8 ++- hrp/pkg/uixt/video_crawler.go | 28 +++++---- 6 files changed, 133 insertions(+), 17 deletions(-) create mode 100644 examples/uitest/demo_android_video_crawler.json create mode 100644 examples/uitest/demo_android_video_crawler_test.go diff --git a/examples/uitest/demo_android_video_crawler.json b/examples/uitest/demo_android_video_crawler.json new file mode 100644 index 00000000..380a6377 --- /dev/null +++ b/examples/uitest/demo_android_video_crawler.json @@ -0,0 +1,59 @@ +{ + "config": { + "name": "抓取抖音视频信息", + "variables": { + "device": "${ENV(SerialNumber)}" + }, + "android": [ + { + "serial": "$device" + } + ] + }, + "teststeps": [ + { + "name": "滑动消费 feed 至少 10 个,live 至少 3 个;滑动过程中,70% 随机间隔 0-5s,30% 随机间隔 5-10s", + "android": { + "actions": [ + { + "method": "video_crawler", + "params": { + "app_package_name": "com.ss.android.ugc.aweme", + "sleep_random": [ + 0, + 5, + 0.7, + 5, + 10, + 0.3 +], + "target_count": { + "feed_count": 5, + "live_count": 3 +} + } + } + ] + } + }, + { + "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" + } + ] + } + ] +} diff --git a/examples/uitest/demo_android_video_crawler_test.go b/examples/uitest/demo_android_video_crawler_test.go new file mode 100644 index 00000000..c6f81bc0 --- /dev/null +++ b/examples/uitest/demo_android_video_crawler_test.go @@ -0,0 +1,47 @@ +//go:build localtest + +package uitest + +import ( + "testing" + + "github.com/httprunner/httprunner/v4/hrp" + "github.com/httprunner/httprunner/v4/hrp/pkg/uixt" +) + +func TestAndroidVideoCrawlerTest(t *testing.T) { + testCase := &hrp.TestCase{ + Config: hrp.NewConfig("抓取抖音视频信息"). + WithVariables(map[string]interface{}{ + "device": "${ENV(SerialNumber)}", + }). + SetAndroid(uixt.WithSerialNumber("$device")), + TestSteps: []hrp.IStep{ + hrp.NewStep("滑动消费 feed 至少 10 个,live 至少 3 个;滑动过程中,70% 随机间隔 0-5s,30% 随机间隔 5-10s"). + Android(). + VideoCrawler(map[string]interface{}{ + "app_package_name": "com.ss.android.ugc.aweme", + "target_count": map[string]interface{}{ + "feed_count": 5, + "live_count": 3, + }, + "sleep_random": []float64{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"), + }, + } + + if err := testCase.Dump2JSON("demo_android_video_crawler.json"); err != nil { + t.Fatal(err) + } + + runner := hrp.NewRunner(t).SetSaveTests(true) + err := runner.Run(testCase) + if err != nil { + t.Fatal(err) + } +} diff --git a/hrp/pkg/uixt/android_adb_driver.go b/hrp/pkg/uixt/android_adb_driver.go index 988b3c76..9bc6992c 100644 --- a/hrp/pkg/uixt/android_adb_driver.go +++ b/hrp/pkg/uixt/android_adb_driver.go @@ -371,7 +371,7 @@ func (ad *adbDriver) AssertAppForeground(packageName string) error { if err != nil { return err } - if app.BundleId != packageName { + if app.PackageName != packageName { return errors.New("app is not in foreground") } return nil @@ -397,8 +397,8 @@ func (ad *adbDriver) GetForegroundApp() (app AppInfo, err error) { s := strings.Split(str, "/") app := AppInfo{ AppBaseInfo: AppBaseInfo{ - BundleId: s[0], - Activity: s[1], + PackageName: s[0], + Activity: s[1], }, } return app, nil diff --git a/hrp/pkg/uixt/android_test.go b/hrp/pkg/uixt/android_test.go index a5061ee9..46ab5a0d 100644 --- a/hrp/pkg/uixt/android_test.go +++ b/hrp/pkg/uixt/android_test.go @@ -357,7 +357,7 @@ func TestDriver_IsAppInForeground(t *testing.T) { app, err := driverExt.Driver.GetForegroundApp() checkErr(t, err) - if app.BundleId != "com.android.settings" { + if app.PackageName != "com.android.settings" { t.FailNow() } if app.Activity != ".Settings" { diff --git a/hrp/pkg/uixt/interface.go b/hrp/pkg/uixt/interface.go index 95d71611..d996dfb0 100644 --- a/hrp/pkg/uixt/interface.go +++ b/hrp/pkg/uixt/interface.go @@ -251,9 +251,11 @@ type AppInfo struct { } type AppBaseInfo struct { - Pid int `json:"pid,omitempty"` - BundleId string `json:"bundleId"` // package name for android - Activity string // view controller for ios + Pid int `json:"pid,omitempty"` + BundleId string `json:"bundleId,omitempty"` // ios package name + ViewController string `json:"viewController,omitempty"` // ios view controller + PackageName string `json:"packageName,omitempty"` // android package name + Activity string `json:"activity,omitempty"` // android activity } type AppState int diff --git a/hrp/pkg/uixt/video_crawler.go b/hrp/pkg/uixt/video_crawler.go index 2b129c2a..8082f5a9 100644 --- a/hrp/pkg/uixt/video_crawler.go +++ b/hrp/pkg/uixt/video_crawler.go @@ -89,8 +89,17 @@ func (l *LiveCrawler) Run(driver *DriverExt, enterPoint PointF) error { return err } + // take screenshot and get screen texts by OCR + _, err := l.driver.GetScreenTextsByOCR() + if err != nil { + log.Error().Err(err).Msg("OCR GetTexts failed") + continue + } + + // TODO: check live type + // swipe to next live video - err := l.driver.SwipeUp() + err = l.driver.SwipeUp() // TODO: sleep custom random time time.Sleep(15 * time.Second) if err != nil { @@ -110,9 +119,8 @@ func (l *LiveCrawler) Run(driver *DriverExt, enterPoint PointF) error { } func (l *LiveCrawler) exitLiveRoom() error { - // FIXME: exit live room - for i := 0; i < 5; i++ { - l.driver.SwipeRelative(0, 0.5, 0.5, 0.5) + for i := 0; i < 3; i++ { + l.driver.SwipeRelative(0.1, 0.5, 0.9, 0.5) time.Sleep(2 * time.Second) // check if back to feed page @@ -159,7 +167,7 @@ func (dExt *DriverExt) VideoCrawler(configs *VideoCrawlerConfigs) (err error) { texts, err := dExt.GetScreenTextsByOCR() if err != nil { log.Error().Err(err).Msg("OCR GetTexts failed") - return err + continue } // automatic handling of pop-up windows @@ -202,8 +210,8 @@ func (dExt *DriverExt) VideoCrawler(configs *VideoCrawlerConfigs) (err error) { return nil } -func (dExt *DriverExt) assertActivity(pacakgeName, activityType string) error { - log.Debug().Str("pacakge_name", pacakgeName). +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 { @@ -211,11 +219,11 @@ func (dExt *DriverExt) assertActivity(pacakgeName, activityType string) error { return err } - if app.BundleId != pacakgeName { - return fmt.Errorf("app %s is not in foreground", pacakgeName) + if app.PackageName != packageName { + return fmt.Errorf("app %s is not in foreground", packageName) } - if activities, ok := androidActivities[app.BundleId]; ok { + if activities, ok := androidActivities[app.PackageName]; ok { if activity, ok := activities[activityType]; ok { if strings.HasSuffix(app.Activity, activity) { return nil