diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index f000201d2..6ce849b59 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -1,5 +1,16 @@ # Release History +## v4.3.1 (2022-12-15) + +**go version** + +- feat: add option WithScreenShot +- feat: run xctest before start ios automation +- feat: run step with specified loop times +- feat: add options for FindTexts +- refactor: move all UI APIs to uixt pkg +- docs: add examples for UI APIs + ## v4.3.0 (2022-10-27) Release hrp sub package `uixt` to support iOS/Android UI automation testing 🎉 diff --git a/examples/uitest/demo_android_douyin_test.go b/examples/uitest/demo_android_douyin_test.go index 01fa3f949..bbfa63916 100644 --- a/examples/uitest/demo_android_douyin_test.go +++ b/examples/uitest/demo_android_douyin_test.go @@ -6,32 +6,33 @@ import ( "testing" "github.com/httprunner/httprunner/v4/hrp" + "github.com/httprunner/httprunner/v4/hrp/pkg/uixt" ) func TestAndroidDouYinLive(t *testing.T) { testCase := &hrp.TestCase{ Config: hrp.NewConfig("通过 feed 头像进入抖音直播间"). - SetAndroid(hrp.WithAdbLogOn(true), hrp.WithSerialNumber("2d06bf70")), + SetAndroid(uixt.WithAdbLogOn(true), uixt.WithSerialNumber("2d06bf70")), TestSteps: []hrp.IStep{ hrp.NewStep("启动抖音"). Android(). Home(). AppTerminate("com.ss.android.ugc.aweme"). // 关闭已运行的抖音,确保启动抖音后在「抖音」首页 - SwipeToTapApp("抖音", hrp.WithMaxRetryTimes(5)). + SwipeToTapApp("抖音", uixt.WithMaxRetryTimes(5)). Sleep(10), hrp.NewStep("处理青少年弹窗"). Android(). Tap("推荐"). - TapByOCR("我知道了", hrp.WithIgnoreNotFoundError(true)). + TapByOCR("我知道了", uixt.WithIgnoreNotFoundError(true)). Validate(). AssertOCRExists("首页", "抖音启动失败,「首页」不存在"), hrp.NewStep("在推荐页上划,直到出现 feed 头像「直播」"). Android(). - SwipeToTapText("直播", hrp.WithMaxRetryTimes(10), hrp.WithIdentifier("进入直播间")), + SwipeToTapText("直播", uixt.WithMaxRetryTimes(10), uixt.WithIdentifier("进入直播间")), hrp.NewStep("向上滑动,等待 10s"). Android(). - SwipeUp(hrp.WithIdentifier("第一次上划")).Sleep(10).ScreenShot(). // 上划 1 次,等待 10s,截图保存 - SwipeUp(hrp.WithIdentifier("第二次上划")).Sleep(10).ScreenShot(), // 再上划 1 次,等待 10s,截图保存 + SwipeUp(uixt.WithIdentifier("第一次上划")).Sleep(10).ScreenShot(). // 上划 1 次,等待 10s,截图保存 + SwipeUp(uixt.WithIdentifier("第二次上划")).Sleep(10).ScreenShot(), // 再上划 1 次,等待 10s,截图保存 }, } diff --git a/examples/uitest/demo_douyin_follow_live.yaml b/examples/uitest/demo_douyin_follow_live.yaml deleted file mode 100644 index f6c34c3fc..000000000 --- a/examples/uitest/demo_douyin_follow_live.yaml +++ /dev/null @@ -1,83 +0,0 @@ -config: - name: 通过 关注天窗 进入指定主播抖音直播间 - variables: - app_name: 抖音 - ios: - - port: 8700 - mjpeg_port: 8800 - log_on: true -teststeps: - - name: 启动抖音 - ios: - actions: - - method: home - - method: app_terminate - params: com.ss.iphone.ugc.Aweme - - method: swipe_to_tap_app - params: $app_name - identifier: 启动抖音 - max_retry_times: 5 - - method: sleep - params: 5 - validate: - - check: ui_ocr - assert: exists - expect: 推荐 - msg: 抖音启动失败,「推荐」不存在 - - name: 处理青少年弹窗 - ios: - actions: - - method: tap_ocr - params: 我知道了 - ignore_NotFoundError: true - - name: 点击首页 - ios: - actions: - - method: tap_ocr - params: 首页 - index: -1 - - method: sleep - params: 10 - - name: 点击关注页 - ios: - actions: - - method: tap_ocr - params: 关注 - index: 1 - - method: sleep - params: 10 - - name: 向上滑动 2 次 - ios: - actions: - - method: swipe_to_tap_texts - params: - - 理肤泉 - - 婉宝 - identifier: click_live - direction: - - 0.6 - - 0.2 - - 0.2 - - 0.2 - - method: sleep - params: 10 - - method: swipe - params: - - 0.9 - - 0.7 - - 0.9 - - 0.3 - identifier: slide_in_live - - method: sleep - params: 10 - - method: screenshot - - method: swipe - params: - - 0.9 - - 0.7 - - 0.9 - - 0.3 - identifier: slide_in_live - - method: sleep - params: 10 - - method: screenshot diff --git a/examples/uitest/demo_douyin_follow_live_test.go b/examples/uitest/demo_douyin_follow_live_test.go index 1eb42f93f..59b223ef3 100644 --- a/examples/uitest/demo_douyin_follow_live_test.go +++ b/examples/uitest/demo_douyin_follow_live_test.go @@ -6,6 +6,7 @@ import ( "testing" "github.com/httprunner/httprunner/v4/hrp" + "github.com/httprunner/httprunner/v4/hrp/pkg/uixt" ) func TestIOSDouyinFollowLive(t *testing.T) { @@ -15,31 +16,31 @@ func TestIOSDouyinFollowLive(t *testing.T) { "app_name": "抖音", }). SetIOS( - hrp.WithLogOn(true), - hrp.WithWDAPort(8700), - hrp.WithWDAMjpegPort(8800), + uixt.WithWDALogOn(true), + uixt.WithWDAPort(8700), + uixt.WithWDAMjpegPort(8800), ), TestSteps: []hrp.IStep{ hrp.NewStep("启动抖音"). IOS(). Home(). AppTerminate("com.ss.iphone.ugc.Aweme"). // 关闭已运行的抖音 - SwipeToTapApp("$app_name", hrp.WithMaxRetryTimes(5), hrp.WithIdentifier("启动抖音")).Sleep(5). + SwipeToTapApp("$app_name", uixt.WithMaxRetryTimes(5), uixt.WithIdentifier("启动抖音")).Sleep(5). Validate(). AssertOCRExists("推荐", "抖音启动失败,「推荐」不存在"), hrp.NewStep("处理青少年弹窗"). IOS(). - TapByOCR("我知道了", hrp.WithIgnoreNotFoundError(true)), + TapByOCR("我知道了", uixt.WithIgnoreNotFoundError(true)), hrp.NewStep("点击首页"). IOS(). - TapByOCR("首页", hrp.WithIndex(-1)).Sleep(10), + TapByOCR("首页", uixt.WithIndex(-1)).Sleep(10), hrp.NewStep("点击关注页"). IOS(). - TapByOCR("关注", hrp.WithIndex(1)).Sleep(10), + TapByOCR("关注", uixt.WithIndex(1)).Sleep(10), hrp.NewStep("向上滑动 2 次"). - IOS().SwipeToTapTexts([]string{"理肤泉", "婉宝"}, hrp.WithCustomDirection(0.6, 0.2, 0.2, 0.2), hrp.WithIdentifier("click_live")).Sleep(10). - Swipe(0.9, 0.7, 0.9, 0.3, hrp.WithIdentifier("slide_in_live")).Sleep(10).ScreenShot(). // 上划 1 次,等待 10s,截图保存 - Swipe(0.9, 0.7, 0.9, 0.3, hrp.WithIdentifier("slide_in_live")).Sleep(10).ScreenShot(), // 再上划 1 次,等待 10s,截图保存 + IOS().SwipeToTapTexts([]string{"理肤泉", "婉宝"}, uixt.WithCustomDirection(0.6, 0.2, 0.2, 0.2), uixt.WithIdentifier("click_live")).Sleep(10). + Swipe(0.9, 0.7, 0.9, 0.3, uixt.WithIdentifier("slide_in_live")).Sleep(10).ScreenShot(). // 上划 1 次,等待 10s,截图保存 + Swipe(0.9, 0.7, 0.9, 0.3, uixt.WithIdentifier("slide_in_live")).Sleep(10).ScreenShot(), // 再上划 1 次,等待 10s,截图保存 }, } diff --git a/examples/uitest/demo_douyin_live.yaml b/examples/uitest/demo_douyin_live.yaml deleted file mode 100644 index e20f426c6..000000000 --- a/examples/uitest/demo_douyin_live.yaml +++ /dev/null @@ -1,57 +0,0 @@ -config: - name: 通过 feed 卡片进入抖音直播间 - variables: - app_name: 抖音 - ios: - - perf_options: - sys_cpu: true - sys_mem: true - port: 8700 - mjpeg_port: 8800 - log_on: true -teststeps: - - name: 启动抖音 - ios: - actions: - - method: home - - method: app_terminate - params: com.ss.iphone.ugc.Aweme - - method: swipe_to_tap_app - params: $app_name - identifier: 启动抖音 - max_retry_times: 5 - - method: sleep - params: 5 - validate: - - check: ui_ocr - assert: exists - expect: 推荐 - msg: 抖音启动失败,「推荐」不存在 - - name: 处理青少年弹窗 - ios: - actions: - - method: tap_ocr - params: 我知道了 - ignore_NotFoundError: true - - name: 向上滑动 2 次 - ios: - actions: - - method: swipe - params: up - identifier: 第一次上划 - - method: sleep - params: 2 - - method: screenshot - - method: swipe - params: up - identifier: 第二次上划 - - method: sleep - params: 2 - - method: screenshot - - name: 在推荐页上划,直到出现「点击进入直播间」 - ios: - actions: - - method: swipe_to_tap_text - params: 点击进入直播间 - identifier: 进入直播间 - max_retry_times: 10 diff --git a/examples/uitest/demo_douyin_test.go b/examples/uitest/demo_douyin_test.go index 4d3215bb6..def2e9d3b 100644 --- a/examples/uitest/demo_douyin_test.go +++ b/examples/uitest/demo_douyin_test.go @@ -6,6 +6,7 @@ import ( "testing" "github.com/httprunner/httprunner/v4/hrp" + "github.com/httprunner/httprunner/v4/hrp/pkg/uixt" ) func TestIOSDouyinLive(t *testing.T) { @@ -15,12 +16,12 @@ func TestIOSDouyinLive(t *testing.T) { "app_name": "抖音", }). SetIOS( - hrp.WithLogOn(true), - hrp.WithWDAPort(8700), - hrp.WithWDAMjpegPort(8800), - hrp.WithPerfOptions( - hrp.WithPerfSystemCPU(true), - hrp.WithPerfSystemMem(true), + uixt.WithWDALogOn(true), + uixt.WithWDAPort(8700), + uixt.WithWDAMjpegPort(8800), + uixt.WithIOSPerfOptions( + uixt.WithIOSPerfSystemCPU(true), + uixt.WithIOSPerfSystemMem(true), ), ), TestSteps: []hrp.IStep{ @@ -28,19 +29,19 @@ func TestIOSDouyinLive(t *testing.T) { IOS(). Home(). AppTerminate("com.ss.iphone.ugc.Aweme"). // 关闭已运行的抖音 - SwipeToTapApp("$app_name", hrp.WithMaxRetryTimes(5), hrp.WithIdentifier("启动抖音")).Sleep(5). + SwipeToTapApp("$app_name", uixt.WithMaxRetryTimes(5), uixt.WithIdentifier("启动抖音")).Sleep(5). Validate(). AssertOCRExists("推荐", "抖音启动失败,「推荐」不存在"), hrp.NewStep("处理青少年弹窗"). IOS(). - TapByOCR("我知道了", hrp.WithIgnoreNotFoundError(true)), + TapByOCR("我知道了", uixt.WithIgnoreNotFoundError(true)), hrp.NewStep("向上滑动 2 次"). IOS(). - SwipeUp(hrp.WithIdentifier("第一次上划")).Sleep(2).ScreenShot(). // 上划 1 次,等待 2s,截图保存 - SwipeUp(hrp.WithIdentifier("第二次上划")).Sleep(2).ScreenShot(), // 再上划 1 次,等待 2s,截图保存 + SwipeUp(uixt.WithIdentifier("第一次上划")).Sleep(2).ScreenShot(). // 上划 1 次,等待 2s,截图保存 + SwipeUp(uixt.WithIdentifier("第二次上划")).Sleep(2).ScreenShot(), // 再上划 1 次,等待 2s,截图保存 hrp.NewStep("在推荐页上划,直到出现「点击进入直播间」"). IOS(). - SwipeToTapText("点击进入直播间", hrp.WithMaxRetryTimes(10), hrp.WithIdentifier("进入直播间")), + SwipeToTapText("点击进入直播间", uixt.WithMaxRetryTimes(10), uixt.WithIdentifier("进入直播间")), }, } diff --git a/examples/uitest/demo_kuaishou_test.go b/examples/uitest/demo_kuaishou_test.go new file mode 100644 index 000000000..d0d47e275 --- /dev/null +++ b/examples/uitest/demo_kuaishou_test.go @@ -0,0 +1,68 @@ +//go:build localtest + +package uitest + +import ( + "testing" + + "github.com/httprunner/httprunner/v4/hrp" + "github.com/httprunner/httprunner/v4/hrp/pkg/uixt" +) + +func TestAndroidKuaiShouFeedCardLive(t *testing.T) { + testCase := &hrp.TestCase{ + Config: hrp.NewConfig("直播_快手_Feed卡片_android"). + WithVariables(map[string]interface{}{ + "device": "${ENV(SerialNumber)}", + }). + SetAndroid( + uixt.WithSerialNumber("$device"), + uixt.WithAdbLogOn(true)), + TestSteps: []hrp.IStep{ + hrp.NewStep("启动快手"). + Android(). + AppTerminate("com.smile.gifmaker"). + AppLaunch("com.smile.gifmaker"). + Home(). + SwipeToTapApp("快手", uixt.WithMaxRetryTimes(5)).Sleep(10), + hrp.NewStep("处理青少年弹窗"). + Android(). + TapByOCR("我知道了", uixt.WithIgnoreNotFoundError(true)). + Validate(). + AssertOCRExists("精选", "进入快手失败"), + hrp.NewStep("点击精选"). + Android(). + TapByOCR("精选", uixt.WithIndex(-1), uixt.WithOffset(0, -50)).Sleep(10), + hrp.NewStep("点击直播标签,进入直播间"). + Android(). + SwipeToTapText("点击进入直播间", + uixt.WithCustomDirection(0.9, 0.7, 0.9, 0.3), + uixt.WithScope(0.2, 0.5, 0.8, 0.8), + uixt.WithMaxRetryTimes(20), + uixt.WithWaitTime(60), + uixt.WithIdentifier("click_live"), + ), + hrp.NewStep("等待1分钟"). + Android(). + Sleep(60), + hrp.NewStep("上滑进入下一个直播间"). + Android(). + Swipe(0.9, 0.7, 0.9, 0.3, uixt.WithIdentifier("slide_in_live")).Sleep(60), + hrp.NewStep("返回主界面,并打开本地时间戳"). + Android(). + Home().SwipeToTapApp("local", uixt.WithMaxRetryTimes(5)).Sleep(10). + Validate(). + AssertOCRExists("16", "打开本地时间戳失败"), + }, + } + + if err := testCase.Dump2JSON("android_feed_card_live_test.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/examples/uitest/demo_weixin_live.yaml b/examples/uitest/demo_weixin_live.yaml deleted file mode 100644 index 3b064f09f..000000000 --- a/examples/uitest/demo_weixin_live.yaml +++ /dev/null @@ -1,58 +0,0 @@ -config: - name: 通过 feed 卡片进入微信直播间 - ios: - - port: 8700 - mjpeg_port: 8800 - log_on: true -teststeps: - - name: 启动微信 - ios: - actions: - - method: home - - method: app_terminate - params: com.tencent.xin - - method: swipe_to_tap_app - params: 微信 - max_retry_times: 5 - validate: - - check: ui_label - assert: exists - expect: 通讯录 - msg: 微信启动失败,「通讯录」不存在 - - name: 进入直播页 - ios: - actions: - - method: tap - params: 发现 - - method: tap_ocr - params: 视频号 - identifier: 进入视频号 - index: -1 - - name: 处理青少年弹窗 - ios: - actions: - - method: tap_ocr - params: 我知道了 - ignore_NotFoundError: true - - name: 在推荐页上划,直到出现「轻触进入直播间」 - ios: - actions: - - method: swipe_to_tap_text - params: 轻触进入直播间 - identifier: 进入直播间 - max_retry_times: 10 - - name: 向上滑动,等待 10s - ios: - actions: - - method: swipe - params: up - identifier: 第一次上划 - - method: sleep - params: 10 - - method: screenshot - - method: swipe - params: up - identifier: 第二次上划 - - method: sleep - params: 10 - - method: screenshot diff --git a/examples/uitest/demo_weixin_test.go b/examples/uitest/demo_weixin_test.go index 835411c5d..133ae18cb 100644 --- a/examples/uitest/demo_weixin_test.go +++ b/examples/uitest/demo_weixin_test.go @@ -6,34 +6,35 @@ import ( "testing" "github.com/httprunner/httprunner/v4/hrp" + "github.com/httprunner/httprunner/v4/hrp/pkg/uixt" ) func TestIOSWeixinLive(t *testing.T) { testCase := &hrp.TestCase{ Config: hrp.NewConfig("通过 feed 卡片进入微信直播间"). - SetIOS(hrp.WithLogOn(true), hrp.WithWDAPort(8700), hrp.WithWDAMjpegPort(8800)), + SetIOS(uixt.WithWDALogOn(true), uixt.WithWDAPort(8700), uixt.WithWDAMjpegPort(8800)), TestSteps: []hrp.IStep{ hrp.NewStep("启动微信"). IOS(). Home(). AppTerminate("com.tencent.xin"). // 关闭已运行的微信,确保启动微信后在「微信」首页 - SwipeToTapApp("微信", hrp.WithMaxRetryTimes(5)). + SwipeToTapApp("微信", uixt.WithMaxRetryTimes(5)). Validate(). AssertLabelExists("通讯录", "微信启动失败,「通讯录」不存在"), hrp.NewStep("进入直播页"). IOS(). - Tap("发现"). // 进入「发现页」 - TapByOCR("视频号", hrp.WithIdentifier("进入视频号"), hrp.WithIndex(-1)), // 通过 OCR 识别「视频号」 + Tap("发现"). // 进入「发现页」 + TapByOCR("视频号", uixt.WithIdentifier("进入视频号"), uixt.WithIndex(-1)), // 通过 OCR 识别「视频号」 hrp.NewStep("处理青少年弹窗"). IOS(). - TapByOCR("我知道了", hrp.WithIgnoreNotFoundError(true)), + TapByOCR("我知道了", uixt.WithIgnoreNotFoundError(true)), hrp.NewStep("在推荐页上划,直到出现「轻触进入直播间」"). IOS(). - SwipeToTapText("轻触进入直播间", hrp.WithMaxRetryTimes(10), hrp.WithIdentifier("进入直播间")), + SwipeToTapText("轻触进入直播间", uixt.WithMaxRetryTimes(10), uixt.WithIdentifier("进入直播间")), hrp.NewStep("向上滑动,等待 10s"). IOS(). - SwipeUp(hrp.WithIdentifier("第一次上划")).Sleep(10).ScreenShot(). // 上划 1 次,等待 10s,截图保存 - SwipeUp(hrp.WithIdentifier("第二次上划")).Sleep(10).ScreenShot(), // 再上划 1 次,等待 10s,截图保存 + SwipeUp(uixt.WithIdentifier("第一次上划")).Sleep(10).ScreenShot(). // 上划 1 次,等待 10s,截图保存 + SwipeUp(uixt.WithIdentifier("第二次上划")).Sleep(10).ScreenShot(), // 再上划 1 次,等待 10s,截图保存 }, } diff --git a/examples/uitest/ios_kuaishou_follow_live_test.go b/examples/uitest/ios_kuaishou_follow_live_test.go index e16b5be17..4fdf837de 100644 --- a/examples/uitest/ios_kuaishou_follow_live_test.go +++ b/examples/uitest/ios_kuaishou_follow_live_test.go @@ -6,6 +6,7 @@ import ( "testing" "github.com/httprunner/httprunner/v4/hrp" + "github.com/httprunner/httprunner/v4/hrp/pkg/uixt" ) func TestIOSKuaiShouLive(t *testing.T) { @@ -15,33 +16,37 @@ func TestIOSKuaiShouLive(t *testing.T) { "device": "${ENV(UDID)}", "ups": "${ENV(LIVEUPLIST)}", }). - SetIOS(hrp.WithUDID("$device"), hrp.WithLogOn(true), hrp.WithWDAPort(8100), hrp.WithWDAMjpegPort(9100)), + SetIOS( + uixt.WithUDID("$device"), + uixt.WithWDALogOn(true), + uixt.WithWDAPort(8100), + uixt.WithWDAMjpegPort(9100)), TestSteps: []hrp.IStep{ hrp.NewStep("启动快手"). IOS(). AppTerminate("com.jiangjia.gif"). AppLaunch("com.jiangjia.gif"). Home(). - SwipeToTapApp("快手", hrp.WithMaxRetryTimes(5)).Sleep(10). + SwipeToTapApp("快手", uixt.WithMaxRetryTimes(5)).Sleep(10). Validate(). AssertOCRExists("精选", "进入快手失败"), hrp.NewStep("点击首页"). IOS(). - TapByOCR("首页", hrp.WithIndex(-1)).Sleep(10), + TapByOCR("首页", uixt.WithIndex(-1)).Sleep(10), hrp.NewStep("点击发现页"). IOS(). - TapByOCR("发现", hrp.WithIndex(1)).Sleep(10), + TapByOCR("发现", uixt.WithIndex(1)).Sleep(10), hrp.NewStep("点击关注页"). IOS(). - TapByOCR("关注", hrp.WithIndex(1)).Sleep(10), + TapByOCR("关注", uixt.WithIndex(1)).Sleep(10), hrp.NewStep("点击直播标签,进入直播间"). IOS(). - SwipeToTapTexts("${split_by_comma($ups)}", hrp.WithCustomDirection(0.6, 0.2, 0.2, 0.2), hrp.WithIdentifier("click_live")).Sleep(60). + SwipeToTapTexts("${split_by_comma($ups)}", uixt.WithCustomDirection(0.6, 0.2, 0.2, 0.2), uixt.WithIdentifier("click_live")).Sleep(60). Validate(). AssertOCRExists("说点什么", "进入直播间失败"), hrp.NewStep("下滑进入下一个直播间"). IOS(). - Swipe(0.9, 0.7, 0.9, 0.3, hrp.WithIdentifier("slide_in_live")).Sleep(60), + Swipe(0.9, 0.7, 0.9, 0.3, uixt.WithIdentifier("slide_in_live")).Sleep(60), }, } diff --git a/examples/uitest/ios_kuaishou_follow_live_test.yaml b/examples/uitest/ios_kuaishou_follow_live_test.yaml deleted file mode 100644 index 484f08152..000000000 --- a/examples/uitest/ios_kuaishou_follow_live_test.yaml +++ /dev/null @@ -1,83 +0,0 @@ -config: - name: 直播_快手_关注天窗_ios - variables: - device: ${ENV(UDID)} - ups: ${ENV(LIVEUPLIST)} - ios: - - udid: $device - port: 8100 - mjpeg_port: 9100 - log_on: true -teststeps: - - name: 启动快手 - ios: - actions: - - method: app_terminate - params: com.jiangjia.gif - - method: app_launch - params: com.jiangjia.gif - - method: home - - method: swipe_to_tap_app - params: 快手 - max_retry_times: 5 - - method: sleep - params: 10 - validate: - - check: ui_ocr - assert: exists - expect: 精选 - msg: 进入快手失败 - - name: 点击首页 - ios: - actions: - - method: tap_ocr - params: 首页 - index: -1 - - method: sleep - params: 10 - - name: 点击发现页 - ios: - actions: - - method: tap_ocr - params: 发现 - index: 1 - - method: sleep - params: 10 - - name: 点击关注页 - ios: - actions: - - method: tap_ocr - params: 关注 - index: 1 - - method: sleep - params: 10 - - name: 点击直播标签,进入直播间 - ios: - actions: - - method: swipe_to_tap_texts - params: ${split_by_comma($ups)} - identifier: click_live - direction: - - 0.6 - - 0.2 - - 0.2 - - 0.2 - - method: sleep - params: 60 - validate: - - check: ui_ocr - assert: exists - expect: 说点什么 - msg: 进入直播间失败 - - name: 下滑进入下一个直播间 - ios: - actions: - - method: swipe - params: - - 0.9 - - 0.7 - - 0.9 - - 0.3 - identifier: slide_in_live - - method: sleep - params: 60 diff --git a/examples/uitest/wda_log_test.go b/examples/uitest/wda_log_test.go index 54e7a0273..3c03a458b 100644 --- a/examples/uitest/wda_log_test.go +++ b/examples/uitest/wda_log_test.go @@ -6,6 +6,7 @@ import ( "testing" "github.com/httprunner/httprunner/v4/hrp" + "github.com/httprunner/httprunner/v4/hrp/pkg/uixt" ) func TestWDALog(t *testing.T) { @@ -14,29 +15,38 @@ func TestWDALog(t *testing.T) { WithVariables(map[string]interface{}{ "app_name": "抖音", }). - SetIOS(hrp.WithLogOn(true), hrp.WithWDAPort(8700), hrp.WithWDAMjpegPort(8800)), + SetIOS( + uixt.WithWDALogOn(true), + uixt.WithWDAPort(8700), uixt.WithWDAMjpegPort(8800), + uixt.WithIOSPerfOptions( + uixt.WithIOSPerfSystemCPU(true), + uixt.WithIOSPerfSystemMem(true), + uixt.WithIOSPerfNetwork(true), + uixt.WithIOSPerfFPS(true), + ), + ), TestSteps: []hrp.IStep{ hrp.NewStep("启动抖音"). IOS(). Home(). AppTerminate("com.ss.iphone.ugc.Aweme"). // 关闭已运行的抖音 - SwipeToTapApp("$app_name", hrp.WithMaxRetryTimes(5), hrp.WithIdentifier("启动抖音")).Sleep(5). + SwipeToTapApp("$app_name", uixt.WithMaxRetryTimes(5), uixt.WithIdentifier("启动抖音")).Sleep(5). Validate(). AssertOCRExists("推荐", "抖音启动失败,「推荐」不存在"), hrp.NewStep("处理青少年弹窗"). IOS(). - TapByOCR("我知道了", hrp.WithIgnoreNotFoundError(true)), + TapByOCR("我知道了", uixt.WithIgnoreNotFoundError(true)), hrp.NewStep("进入购物页"). - IOS().TapByOCR("购物", hrp.WithIdentifier("点击购物")).Sleep(5), + IOS().TapByOCR("商城", uixt.WithIdentifier("点击商城")).Sleep(5), hrp.NewStep("进入推荐页"). - IOS().TapByOCR("推荐", hrp.WithIdentifier("点击推荐")).Sleep(5), + IOS().TapByOCR("推荐", uixt.WithIdentifier("点击推荐")).Sleep(5), hrp.NewStep("向上滑动 2 次"). IOS(). - SwipeUp(hrp.WithIdentifier("第 1 次上划")).Sleep(2). - SwipeUp(hrp.WithIdentifier("第 2 次上划")).Sleep(2). - SwipeUp(hrp.WithIdentifier("第 3 次上划")).Sleep(2). - TapXY(0.9, 0.1, hrp.WithIdentifier("点击进入搜索框")).Sleep(2). - Input("httprunner", hrp.WithIdentifier("输入搜索关键词")), + SwipeUp(uixt.WithIdentifier("第 1 次上划")).Sleep(2). + SwipeUp(uixt.WithIdentifier("第 2 次上划")).Sleep(2). + SwipeUp(uixt.WithIdentifier("第 3 次上划")).Sleep(2). + TapXY(0.9, 0.1, uixt.WithIdentifier("点击进入搜索框")).Sleep(2). + Input("httprunner", uixt.WithIdentifier("输入搜索关键词")), }, } diff --git a/examples/worldcup/README.md b/examples/worldcup/README.md new file mode 100644 index 000000000..62a2d5c49 --- /dev/null +++ b/examples/worldcup/README.md @@ -0,0 +1,36 @@ +# World Cup Live + +```text +$ wcl -h +Monitor FIFA World Cup Live + +Usage: + wcl [flags] + +Flags: + --android string run android app + --auto auto enter live + -d, --duration int set duration in seconds (default 30) + -h, --help help for wcl + -i, --interval int set interval in seconds (default 15) + --ios string run ios app + -l, --log-level string set log level (default "INFO") + -n, --match-name string specify match name + -p, --perf strings specify performance monitor, e.g. sys_cpu,sys_mem,sys_net,sys_disk,fps,network,gpu + -u, --uuid string specify device serial or udid + -v, --version version for wcl +``` + + +```bash +$ wcl -n "比利时vs摩洛哥" --android com.ss.android.ugc.aweme -d 300 -i 15 -u caf0cd51 +$ wcl -n "比利时vs摩洛哥" --ios com.ss.iphone.ugc.Aweme -d 300 -i 15 -p sys_cpu,sys_mem,sys_disk,sys_net,fps,network,gpu -u 00008030-000438191421802E +``` + +## bundle id + +| app | iOS | Android | +| -- | -- | -- | +| 抖音 | com.ss.iphone.ugc.Aweme | com.ss.android.ugc.aweme | +| 央视频 | com.cctv.yangshipin.app.iphone | com.cctv.yangshipin.app.androidp | +| 咪咕视频 | com.wondertek.hecmccmobile | com.cmcc.cmvideo | diff --git a/examples/worldcup/cli.go b/examples/worldcup/cli.go new file mode 100644 index 000000000..f28eed1d9 --- /dev/null +++ b/examples/worldcup/cli.go @@ -0,0 +1,99 @@ +package main + +import ( + "errors" + "os" + "strings" + + "github.com/rs/zerolog" + "github.com/rs/zerolog/log" + "github.com/spf13/cobra" + + "github.com/httprunner/httprunner/v4/hrp/pkg/uixt" +) + +var rootCmd = &cobra.Command{ + Use: "wcl", + Short: "Monitor FIFA World Cup Live", + Version: "2022.12.03.0018", + PreRun: func(cmd *cobra.Command, args []string) { + log.Logger = zerolog.New( + zerolog.ConsoleWriter{NoColor: false, Out: os.Stderr}, + ).With().Timestamp().Logger() + zerolog.SetGlobalLevel(zerolog.InfoLevel) + setLogLevel(logLevel) + }, + RunE: func(cmd *cobra.Command, args []string) error { + var device uixt.Device + var bundleID string + if iosApp != "" { + log.Info().Str("bundleID", iosApp).Msg("init ios device") + device = initIOSDevice(uuid) + bundleID = iosApp + } else if androidApp != "" { + log.Info().Str("bundleID", androidApp).Msg("init android device") + device = initAndroidDevice(uuid) + bundleID = androidApp + } else { + return errors.New("android or ios app bundldID is required") + } + + wc := NewWorldCupLive(device, matchName, bundleID, duration, interval) + + if auto { + wc.EnterLive(bundleID) + } + + wc.Start() + return nil + }, +} + +var ( + uuid string + iosApp string + androidApp string + auto bool + duration int + interval int + logLevel string + matchName string + perf []string +) + +func main() { + rootCmd.PersistentFlags().StringVarP(&logLevel, "log-level", "l", "INFO", "set log level") + rootCmd.PersistentFlags().StringVarP(&uuid, "uuid", "u", "", "specify device serial or udid") + rootCmd.PersistentFlags().StringVar(&iosApp, "ios", "", "run ios app") + rootCmd.PersistentFlags().StringVar(&androidApp, "android", "", "run android app") + rootCmd.PersistentFlags().BoolVar(&auto, "auto", false, "auto enter live") + rootCmd.PersistentFlags().IntVarP(&duration, "duration", "d", 30, "set duration in seconds") + rootCmd.PersistentFlags().IntVarP(&interval, "interval", "i", 15, "set interval in seconds") + rootCmd.PersistentFlags().StringVarP(&matchName, "match-name", "n", "", "specify match name") + rootCmd.PersistentFlags().StringSliceVarP(&perf, "perf", "p", nil, + "specify performance monitor, e.g. sys_cpu,sys_mem,sys_net,sys_disk,fps,network,gpu") + + err := rootCmd.Execute() + if err != nil { + os.Exit(1) + } +} + +func setLogLevel(level string) { + level = strings.ToUpper(level) + log.Info().Str("level", level).Msg("Set log level") + switch level { + case "DEBUG": + zerolog.SetGlobalLevel(zerolog.DebugLevel) + case "INFO": + zerolog.SetGlobalLevel(zerolog.InfoLevel) + case "WARN": + zerolog.SetGlobalLevel(zerolog.WarnLevel) + case "ERROR": + zerolog.SetGlobalLevel(zerolog.ErrorLevel) + case "FATAL": + zerolog.SetGlobalLevel(zerolog.FatalLevel) + case "PANIC": + zerolog.SetGlobalLevel(zerolog.PanicLevel) + } +} diff --git a/examples/worldcup/main.go b/examples/worldcup/main.go new file mode 100644 index 000000000..3217a293b --- /dev/null +++ b/examples/worldcup/main.go @@ -0,0 +1,286 @@ +package main + +import ( + "bytes" + "encoding/json" + "fmt" + "math" + "os" + "os/signal" + "path/filepath" + "strconv" + "strings" + "syscall" + "time" + + "github.com/rs/zerolog/log" + + "github.com/httprunner/httprunner/v4/hrp/pkg/uixt" +) + +func convertTimeToSeconds(timeStr string) (int, error) { + if !strings.Contains(timeStr, ":") { + return 0, fmt.Errorf("invalid time string: %s", timeStr) + } + + ss := strings.Split(timeStr, ":") + var seconds int + for idx, s := range ss { + i, err := strconv.Atoi(s) + if err != nil { + return 0, err + } + seconds += i * int(math.Pow(60, float64(len(ss)-idx-1))) + } + + return seconds, nil +} + +func initIOSDevice(uuid string) uixt.Device { + perfOptions := []uixt.IOSPerfOption{} + for _, p := range perf { + switch p { + case "sys_cpu": + perfOptions = append(perfOptions, uixt.WithIOSPerfSystemCPU(true)) + case "sys_mem": + perfOptions = append(perfOptions, uixt.WithIOSPerfSystemMem(true)) + case "sys_net": + perfOptions = append(perfOptions, uixt.WithIOSPerfSystemNetwork(true)) + case "sys_disk": + perfOptions = append(perfOptions, uixt.WithIOSPerfSystemDisk(true)) + case "network": + perfOptions = append(perfOptions, uixt.WithIOSPerfNetwork(true)) + case "fps": + perfOptions = append(perfOptions, uixt.WithIOSPerfFPS(true)) + case "gpu": + perfOptions = append(perfOptions, uixt.WithIOSPerfGPU(true)) + } + } + perfOptions = append(perfOptions, uixt.WithIOSPerfOutputInterval(interval*1000)) + + device, err := uixt.NewIOSDevice( + uixt.WithUDID(uuid), + uixt.WithWDAPort(8700), uixt.WithWDAMjpegPort(8800), + uixt.WithResetHomeOnStartup(false), // not reset home on startup + uixt.WithIOSPerfOptions(perfOptions...), + uixt.WithXCTest("com.gtf.wda.runner.xctrunner"), + ) + if err != nil { + log.Fatal().Err(err).Msg("failed to init ios device") + } + return device +} + +func initAndroidDevice(uuid string) uixt.Device { + device, err := uixt.NewAndroidDevice(uixt.WithSerialNumber(uuid)) + if err != nil { + log.Fatal().Err(err).Msg("failed to init android device") + } + return device +} + +type timeLog struct { + UTCTimeStr string `json:"utc_time_str"` + UTCTime int64 `json:"utc_time"` + LiveTime string `json:"live_time"` + LiveTimeSeconds int `json:"live_time_seconds"` +} + +type WorldCupLive struct { + driver *uixt.DriverExt + file *os.File + resultDir string + UUID string `json:"uuid"` + MatchName string `json:"matchName"` + BundleID string `json:"bundleID"` + StartTime string `json:"startTime"` + EndTime string `json:"endTime"` + Interval int `json:"interval"` // seconds + Duration int `json:"duration"` // seconds + Timelines []timeLog `json:"timelines"` + PerfData []string `json:"perfData"` +} + +func NewWorldCupLive(device uixt.Device, matchName, bundleID string, duration, interval int) *WorldCupLive { + driverExt, err := device.NewDriver(nil) + if err != nil { + log.Fatal().Err(err).Msg("failed to init driver") + } + + if matchName == "" { + matchName = "unknown-match" + } + + startTime := time.Now() + matchName = fmt.Sprintf("%s-%s", startTime.Format("2006-01-02"), matchName) + resultDir := filepath.Join("worldcuplive", matchName, startTime.Format("15:04:05")) + + if err = os.MkdirAll(filepath.Join(resultDir, "screenshot"), 0o755); err != nil { + log.Fatal().Err(err).Msg("failed to create result dir") + } + + filename := filepath.Join(resultDir, "log.txt") + f, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0o755) + if err != nil { + log.Fatal().Err(err).Msg("failed to open file") + } + // write title + f.WriteString(fmt.Sprintf("%s\t%s\t%s\n", matchName, device.UUID(), bundleID)) + f.WriteString("utc_time\tutc_timestamp\tlive_time\tlive_seconds\n") + + if interval == 0 { + interval = 15 + } + if duration == 0 { + duration = 30 + } + + return &WorldCupLive{ + driver: driverExt, + file: f, + resultDir: resultDir, + UUID: device.UUID(), + BundleID: bundleID, + Duration: duration, + Interval: interval, + StartTime: startTime.Format("2006-01-02 15:04:05"), + MatchName: matchName, + } +} + +func (wc *WorldCupLive) getCurrentLiveTime(utcTime time.Time) error { + utcTimeStr := utcTime.Format("15:04:05") + fileName := filepath.Join( + wc.resultDir, "screenshot", utcTimeStr) + ocrTexts, err := wc.driver.GetTextsByOCR( + uixt.WithScreenShot(fileName), + ) + if err != nil { + log.Error().Err(err).Msg("get ocr texts failed") + return err + } + + // filter ocr texts with time format + secondsMap := map[string]int{} + var secondsTexts []string + for _, ocrText := range ocrTexts { + seconds, err := convertTimeToSeconds(ocrText.Text) + if err == nil { + secondsTexts = append(secondsTexts, ocrText.Text) + secondsMap[ocrText.Text] = seconds + } + } + + var secondsText string + if len(secondsTexts) == 1 { + secondsText = secondsTexts[0] + } else if len(secondsTexts) >= 2 { + // select the second, the first maybe mobile system time + secondsText = secondsTexts[1] + } else { + log.Warn().Msg("no time text found") + return nil + } + + liveTimeSeconds := secondsMap[secondsText] + line := fmt.Sprintf("%s\t%d\t%s\t%d\n", + utcTimeStr, utcTime.Unix(), secondsText, liveTimeSeconds) + log.Info().Str("utcTime", utcTimeStr).Str("liveTime", secondsText).Msg("log live time") + if _, err := wc.file.WriteString(line); err != nil { + log.Error().Err(err).Str("line", line).Msg("write timeseries failed") + } + wc.Timelines = append(wc.Timelines, timeLog{ + UTCTimeStr: utcTimeStr, + UTCTime: utcTime.Unix(), + LiveTime: secondsText, + LiveTimeSeconds: liveTimeSeconds, + }) + return nil +} + +func (wc *WorldCupLive) EnterLive(bundleID string) error { + log.Info().Msg("enter world cup live") + + // kill app + _, err := wc.driver.Driver.AppTerminate(bundleID) + if err != nil { + log.Error().Err(err).Msg("terminate app failed") + } + + // launch app + err = wc.driver.Driver.AppLaunch(bundleID) + if err != nil { + log.Error().Err(err).Msg("launch app failed") + return err + } + time.Sleep(5 * time.Second) + + // 青少年弹窗处理 + if points, err := wc.driver.GetTextXYs([]string{"青少年模式", "我知道了"}); err == nil { + _ = wc.driver.TapAbsXY(points[1].X, points[1].Y) + } + + // 进入世界杯 tab + if err = wc.driver.TapByOCR("世界杯"); err != nil { + log.Error().Err(err).Msg("enter 直播中 failed") + return err + } + time.Sleep(3 * time.Second) + + // 进入世界杯直播 + if err = wc.driver.TapByOCR("直播中"); err != nil { + log.Error().Err(err).Msg("enter 直播中 failed") + return err + } + + return nil +} + +func (wc *WorldCupLive) Start() { + c := make(chan os.Signal, 1) + signal.Notify(c, syscall.SIGTERM, syscall.SIGINT) + timer := time.NewTimer(time.Duration(wc.Duration) * time.Second) + for { + select { + case <-timer.C: + wc.dumpResult() + return + case <-c: + wc.dumpResult() + return + default: + utcTime := time.Now() + if utcTime.Unix()%int64(wc.Interval) == 0 { + wc.getCurrentLiveTime(utcTime) + } else { + time.Sleep(500 * time.Millisecond) + } + } + } +} + +func (wc *WorldCupLive) dumpResult() error { + wc.EndTime = time.Now().Format("2006-01-02 15:04:05") + + // init json encoder + buffer := new(bytes.Buffer) + encoder := json.NewEncoder(buffer) + encoder.SetEscapeHTML(false) + encoder.SetIndent("", " ") + + wc.PerfData = wc.driver.GetPerfData() + err := encoder.Encode(wc) + if err != nil { + log.Error().Err(err).Msg("encode json failed") + return err + } + + path := filepath.Join(wc.resultDir, "summary.json") + err = os.WriteFile(path, buffer.Bytes(), 0o755) + if err != nil { + log.Error().Err(err).Msg("dump json path failed") + return err + } + log.Info().Str("path", path).Msg("dump summary success") + return nil +} diff --git a/examples/worldcup/main_test.go b/examples/worldcup/main_test.go new file mode 100644 index 000000000..314b94a5c --- /dev/null +++ b/examples/worldcup/main_test.go @@ -0,0 +1,100 @@ +//go:build localtest + +package main + +import ( + "testing" + + "github.com/stretchr/testify/assert" + + "github.com/httprunner/httprunner/v4/hrp" + "github.com/httprunner/httprunner/v4/hrp/pkg/uixt" +) + +func TestConvertTimeToSeconds(t *testing.T) { + testData := []struct { + timeStr string + seconds int + }{ + {"00:00", 0}, + {"00:01", 1}, + {"01:00", 60}, + {"01:01", 61}, + {"00:01:02", 62}, + {"01:02:03", 3723}, + } + + for _, td := range testData { + seconds, err := convertTimeToSeconds(td.timeStr) + assert.Nil(t, err) + assert.Equal(t, td.seconds, seconds) + } +} + +func TestMainIOS(t *testing.T) { + uuid := "00008030-00194DA421C1802E" + device := initIOSDevice(uuid) + bundleID := "com.ss.iphone.ugc.Aweme" + wc := NewWorldCupLive(device, "", bundleID, 30, 10) + wc.EnterLive(bundleID) + wc.Start() +} + +func TestMainAndroid(t *testing.T) { + device := initAndroidDevice(uuid) + bundleID := "com.ss.android.ugc.aweme" + wc := NewWorldCupLive(device, "", bundleID, 30, 10) + wc.EnterLive(bundleID) + wc.Start() +} + +func TestIOSDouyinWorldCupLive(t *testing.T) { + testCase := &hrp.TestCase{ + Config: hrp.NewConfig("直播_抖音_世界杯_ios"). + WithVariables(map[string]interface{}{ + "appBundleID": "com.ss.iphone.ugc.Aweme", + }). + SetIOS( + uixt.WithUDID(uuid), + uixt.WithWDALogOn(true), + uixt.WithWDAPort(8700), + uixt.WithWDAMjpegPort(8800), + uixt.WithXCTest("com.gtf.wda.runner.xctrunner"), + ), + TestSteps: []hrp.IStep{ + hrp.NewStep("启动抖音"). + IOS(). + Home(). + AppTerminate("$appBundleID"). // 关闭已运行的抖音 + AppLaunch("$appBundleID"). + TapByOCR("我知道了", uixt.WithIgnoreNotFoundError(true)). // 处理青少年弹窗 + Validate(). + AssertOCRExists("首页", "抖音启动失败,「首页」不存在"), + hrp.NewStep("点击首页"). + IOS(). + TapByOCR("首页", uixt.WithIndex(-1)).Sleep(2), + hrp.NewStep("点击世界杯页"). + IOS(). + SwipeToTapText("世界杯", + uixt.WithMaxRetryTimes(5), + uixt.WithCustomDirection(0.4, 0.07, 0.6, 0.07), // 滑动 tab,从左到右,解决「世界杯」被遮挡的问题 + uixt.WithScope(0, 0, 1, 0.15), // 限定 tab 区域 + uixt.WithWaitTime(1), + ), + hrp.NewStep("点击进入赛程晋级"). + Loop(5). // 重复执行 5 次 + IOS(). + TapByOCR("赛程晋级", uixt.WithIdentifier("click_live"), uixt.WithIndex(-1)). + Sleep(3).Back().Sleep(3), + hrp.NewStep("关闭抖音"). + IOS(). + AppTerminate("$appBundleID"), + }, + } + + runner := hrp.NewRunner(t).SetSaveTests(true) + err := runner.Run(testCase) + if err != nil { + t.Fatal(err) + } +} diff --git a/hrp/cmd/ios/devices.go b/hrp/cmd/ios/devices.go index bc9724a44..c1f230d3b 100644 --- a/hrp/cmd/ios/devices.go +++ b/hrp/cmd/ios/devices.go @@ -85,11 +85,13 @@ var listDevicesCmd = &cobra.Command{ } for _, d := range devices { - deviceByte, _ := json.Marshal(d.Properties()) + deviceProperties := d.Properties() device := &Device{ - d: d, + d: d, + UDID: deviceProperties.SerialNumber, + ConnectionType: deviceProperties.ConnectionType, + ConnectionSpeed: deviceProperties.ConnectionSpeed, } - json.Unmarshal(deviceByte, device) device.Status = device.GetStatus() if isDetail { diff --git a/hrp/cmd/ios/ios_test.go b/hrp/cmd/ios/ios_test.go new file mode 100644 index 000000000..43f20b7b4 --- /dev/null +++ b/hrp/cmd/ios/ios_test.go @@ -0,0 +1,14 @@ +//go:build localtest + +package ios + +import "testing" + +func TestGetDevice(t *testing.T) { + device, err := getDevice(udid) + if err != nil { + t.Fatal(err) + } + + t.Logf("device: %v", device) +} diff --git a/hrp/cmd/ios/mount.go b/hrp/cmd/ios/mount.go index d17d34039..cb077ae3c 100644 --- a/hrp/cmd/ios/mount.go +++ b/hrp/cmd/ios/mount.go @@ -62,12 +62,13 @@ var mountCmd = &cobra.Command{ dmgPath = filepath.Join(developerDiskImageDir, version, "DeveloperDiskImage.dmg") signaturePath = filepath.Join(developerDiskImageDir, version, "DeveloperDiskImage.dmg.signature") } else { - log.Error().Str("dir", developerDiskImageDir).Msg("developer disk image not found in directory") - return fmt.Errorf("developer disk image not found") + log.Error().Str("dir", developerDiskImageDir).Msgf( + "developer disk image %s not found in directory", version) + return fmt.Errorf("developer disk image %s not found", version) } if err = device.MountDeveloperDiskImage(dmgPath, signaturePath); err != nil { - return fmt.Errorf("mount developer disk image failed: %s", err) + return fmt.Errorf("mount developer disk image %s failed: %s", version, err) } log.Info().Msg("mount developer disk image successfully") diff --git a/hrp/internal/version/VERSION b/hrp/internal/version/VERSION index 1ddc0f604..9198ad674 100644 --- a/hrp/internal/version/VERSION +++ b/hrp/internal/version/VERSION @@ -1 +1 @@ -v4.3.0 \ No newline at end of file +v4.3.1 \ No newline at end of file diff --git a/hrp/pkg/gidevice/perfd.go b/hrp/pkg/gidevice/perfd.go index 334e879e4..f4f1a6ff5 100644 --- a/hrp/pkg/gidevice/perfd.go +++ b/hrp/pkg/gidevice/perfd.go @@ -5,6 +5,7 @@ import ( "encoding/binary" "encoding/json" "fmt" + "log" "net" "strconv" "time" @@ -32,8 +33,8 @@ type PerfOptions struct { func defaulPerfOption() *PerfOptions { return &PerfOptions{ - SysCPU: true, // default on - SysMem: true, // default on + SysCPU: false, + SysMem: false, SysDisk: false, SysNetwork: false, gpu: false, @@ -179,10 +180,13 @@ type perfdSysmontap struct { func (c *perfdSysmontap) Start() (data <-chan []byte, err error) { // set config + interval := time.Millisecond * time.Duration(c.options.OutputInterval) + log.Printf("set sysmontap sample interval: %dms\n", c.options.OutputInterval) + config := map[string]interface{}{ "bm": 0, "cpuUsage": true, - "sampleInterval": time.Second * 1, // 1s + "sampleInterval": interval, // time.Duration "ur": c.options.OutputInterval, // 输出频率 "procAttrs": c.options.ProcessAttributes, // process performance "sysAttrs": c.options.SystemAttributes, // system performance @@ -740,7 +744,7 @@ func (c *perfdGraphicsOpengl) Start() (data <-chan []byte, err error) { if _, err = c.i.call( instrumentsServiceGraphicsOpengl, "setSamplingRate:", - float64(c.options.OutputInterval)/100, + float64(c.options.OutputInterval)/100, // FIXME: unable to set sampling rate, always 1.0 ); err != nil { return nil, err } diff --git a/hrp/pkg/uixt/android_device.go b/hrp/pkg/uixt/android_device.go index e86206c65..6391dfac9 100644 --- a/hrp/pkg/uixt/android_device.go +++ b/hrp/pkg/uixt/android_device.go @@ -68,6 +68,8 @@ func GetAndroidDeviceOptions(dev *AndroidDevice) (deviceOptions []AndroidDeviceO return } +// uiautomator2 server must be started before +// adb shell am instrument -w io.appium.uiautomator2.server.test/androidx.test.runner.AndroidJUnitRunner func NewAndroidDevice(options ...AndroidDeviceOption) (device *AndroidDevice, err error) { deviceList, err := DeviceList() if err != nil { @@ -114,7 +116,7 @@ func DeviceList() (devices []gadb.Device, err error) { type AndroidDevice struct { d gadb.Device - logcat *DeviceLogcat + logcat *AdbLogcat SerialNumber string `json:"serial,omitempty" yaml:"serial,omitempty"` IP string `json:"ip,omitempty" yaml:"ip,omitempty"` Port int `json:"port,omitempty" yaml:"port,omitempty"` @@ -201,7 +203,7 @@ func getFreePort() (int, error) { return l.Addr().(*net.TCPAddr).Port, nil } -type DeviceLogcat struct { +type AdbLogcat struct { serial string logBuffer *bytes.Buffer errs []error @@ -210,8 +212,8 @@ type DeviceLogcat struct { cmd *exec.Cmd } -func NewAdbLogcat(serial string) *DeviceLogcat { - return &DeviceLogcat{ +func NewAdbLogcat(serial string) *AdbLogcat { + return &AdbLogcat{ serial: serial, logBuffer: new(bytes.Buffer), stopping: make(chan struct{}), @@ -220,7 +222,7 @@ func NewAdbLogcat(serial string) *DeviceLogcat { } // CatchLogcatContext starts logcat with timeout context -func (l *DeviceLogcat) CatchLogcatContext(timeoutCtx context.Context) (err error) { +func (l *AdbLogcat) CatchLogcatContext(timeoutCtx context.Context) (err error) { if err = l.CatchLogcat(); err != nil { return } @@ -234,7 +236,7 @@ func (l *DeviceLogcat) CatchLogcatContext(timeoutCtx context.Context) (err error return } -func (l *DeviceLogcat) Stop() error { +func (l *AdbLogcat) Stop() error { select { case <-l.stopping: default: @@ -245,7 +247,7 @@ func (l *DeviceLogcat) Stop() error { return l.Errors() } -func (l *DeviceLogcat) Errors() (err error) { +func (l *AdbLogcat) Errors() (err error) { for _, e := range l.errs { if err != nil { err = fmt.Errorf("%v |[DeviceLogcatErr] %v", err, e) @@ -256,7 +258,7 @@ func (l *DeviceLogcat) Errors() (err error) { return } -func (l *DeviceLogcat) CatchLogcat() (err error) { +func (l *AdbLogcat) CatchLogcat() (err error) { if l.cmd != nil { log.Warn().Msg("logcat already start") return nil @@ -284,7 +286,7 @@ func (l *DeviceLogcat) CatchLogcat() (err error) { return } -func (l *DeviceLogcat) BufferedLogcat() (err error) { +func (l *AdbLogcat) BufferedLogcat() (err error) { // -d: dump the current buffered logcat result and exits cmd := myexec.Command("adb", "-s", l.serial, "logcat", "-d") cmd.Stdout = l.logBuffer diff --git a/hrp/pkg/uixt/android_driver.go b/hrp/pkg/uixt/android_driver.go index 5ebf69dde..31d43c074 100644 --- a/hrp/pkg/uixt/android_driver.go +++ b/hrp/pkg/uixt/android_driver.go @@ -35,7 +35,7 @@ type uiaDriver struct { Driver adbDevice gadb.Device - logcat *DeviceLogcat + logcat *AdbLogcat localPort int } @@ -478,7 +478,7 @@ func (ud *uiaDriver) AppTerminate(bundleId string) (successful bool, err error) return false, err } - _, err = ud.adbDevice.RunShellCommand("am force-stop", bundleId) + _, err = ud.adbDevice.RunShellCommand("am", "force-stop", bundleId) return err == nil, err } @@ -508,9 +508,9 @@ func (ud *uiaDriver) TapFloat(x, y float64, options ...DataOption) (err error) { "y": y, } // new data options in post data for extra uiautomator configurations - d := NewData(data, options...) + newData := NewData(data, options...) - _, err = ud.httpPOST(d.Data, "/session", ud.sessionId, "appium/tap") + _, err = ud.httpPOST(newData, "/session", ud.sessionId, "appium/tap") return } @@ -566,9 +566,9 @@ func (ud *uiaDriver) DragFloat(fromX, fromY, toX, toY float64, options ...DataOp } // new data options in post data for extra uiautomator configurations - d := NewData(data, options...) + newData := NewData(data, options...) - return ud._drag(d.Data) + return ud._drag(newData) } func (ud *uiaDriver) _swipe(startX, startY, endX, endY interface{}, options ...DataOption) (err error) { @@ -581,9 +581,9 @@ func (ud *uiaDriver) _swipe(startX, startY, endX, endY interface{}, options ...D } // new data options in post data for extra uiautomator configurations - d := NewData(data, options...) + newData := NewData(data, options...) - _, err = ud.httpPOST(d.Data, "/session", ud.sessionId, "touch/perform") + _, err = ud.httpPOST(newData, "/session", ud.sessionId, "touch/perform") return } @@ -672,9 +672,9 @@ func (ud *uiaDriver) SendKeys(text string, options ...DataOption) (err error) { "text": text, } // new data options in post data for extra uiautomator configurations - d := NewData(data, options...) + newData := NewData(data, options...) - _, err = ud.httpPOST(d.Data, "/session", ud.sessionId, "keys") + _, err = ud.httpPOST(newData, "/session", ud.sessionId, "keys") return } @@ -683,14 +683,14 @@ func (ud *uiaDriver) Input(text string, options ...DataOption) (err error) { "view": text, } // new data options in post data for extra uiautomator configurations - d := NewData(data, options...) + newData := NewData(data, options...) var element WebElement - if valuetext, ok := d.Data["textview"]; ok { + if valuetext, ok := newData["textview"]; ok { element, err = ud.FindElement(BySelector{UiAutomator: NewUiSelectorHelper().TextContains(fmt.Sprintf("%v", valuetext)).String()}) - } else if valueid, ok := d.Data["id"]; ok { + } else if valueid, ok := newData["id"]; ok { element, err = ud.FindElement(BySelector{ResourceIdID: fmt.Sprintf("%v", valueid)}) - } else if valuedesc, ok := d.Data["description"]; ok { + } else if valuedesc, ok := newData["description"]; ok { element, err = ud.FindElement(BySelector{UiAutomator: NewUiSelectorHelper().Description(fmt.Sprintf("%v", valuedesc)).String()}) } else { element, err = ud.FindElement(BySelector{ClassName: ElementType{EditText: true}}) diff --git a/hrp/pkg/uixt/android_elment.go b/hrp/pkg/uixt/android_elment.go index 9d45cd397..760f52abe 100644 --- a/hrp/pkg/uixt/android_elment.go +++ b/hrp/pkg/uixt/android_elment.go @@ -30,9 +30,9 @@ func (ue uiaElement) SendKeys(text string, options ...DataOption) (err error) { } // new data options in post data for extra uiautomator configurations - d := NewData(data, options...) + newData := NewData(data, options...) - _, err = ue.parent.httpPOST(d.Data, "/session", ue.parent.sessionId, "/element", ue.id, "/value") + _, err = ue.parent.httpPOST(newData, "/session", ue.parent.sessionId, "/element", ue.id, "/value") return } diff --git a/hrp/pkg/uixt/ext.go b/hrp/pkg/uixt/ext.go index 531f95b8b..903c3558b 100644 --- a/hrp/pkg/uixt/ext.go +++ b/hrp/pkg/uixt/ext.go @@ -205,6 +205,7 @@ type DriverExt struct { frame *bytes.Buffer doneMjpegStream chan bool scale float64 + ocrService OCRService // used to get text from image StartTime time.Time // used to associate screenshots name ScreenShots []string // save screenshots path perfStop chan struct{} // stop performance monitor @@ -227,6 +228,10 @@ func extend(driver WebDriver) (dExt *DriverExt, err error) { return nil, err } + if dExt.ocrService, err = newVEDEMOCRService(); err != nil { + return nil, err + } + return dExt, nil } @@ -254,21 +259,14 @@ func (dExt *DriverExt) takeScreenShot() (raw *bytes.Buffer, err error) { return raw, nil } -// saveScreenShot saves image file to $CWD/screenshots/ folder -func (dExt *DriverExt) saveScreenShot(raw *bytes.Buffer, fileName string) (string, error) { +// saveScreenShot saves image file with file name +func saveScreenShot(raw *bytes.Buffer, fileName string) (string, error) { img, format, err := image.Decode(raw) if err != nil { return "", errors.Wrap(err, "decode screenshot image failed") } - dir, _ := os.Getwd() - screenshotsDir := filepath.Join(dir, "screenshots") - if err = os.MkdirAll(screenshotsDir, os.ModePerm); err != nil { - return "", errors.Wrap(err, "create screenshots directory failed") - } - screenshotPath := filepath.Join(screenshotsDir, - fmt.Sprintf("%s.%s", fileName, format)) - + screenshotPath := filepath.Join(fmt.Sprintf("%s.%s", fileName, format)) file, err := os.Create(screenshotPath) if err != nil { return "", errors.Wrap(err, "create screenshot image file failed") @@ -299,7 +297,13 @@ func (dExt *DriverExt) ScreenShot(fileName string) (string, error) { return "", errors.Wrap(err, "screenshot failed") } - path, err := dExt.saveScreenShot(raw, fileName) + dir, _ := os.Getwd() + screenshotsDir := filepath.Join(dir, "screenshots") + if err = os.MkdirAll(screenshotsDir, os.ModePerm); err != nil { + return "", errors.Wrap(err, "create screenshots directory failed") + } + fileName = filepath.Join(screenshotsDir, fileName) + path, err := saveScreenShot(raw, fileName) if err != nil { return "", errors.Wrap(err, "save screenshot failed") } diff --git a/hrp/pkg/uixt/input.go b/hrp/pkg/uixt/input.go new file mode 100644 index 000000000..f23df857b --- /dev/null +++ b/hrp/pkg/uixt/input.go @@ -0,0 +1,5 @@ +package uixt + +func (dExt *DriverExt) Input(text string) (err error) { + return dExt.Driver.Input(text) +} diff --git a/hrp/pkg/uixt/interface.go b/hrp/pkg/uixt/interface.go index 77ae84cbc..59c0b643a 100644 --- a/hrp/pkg/uixt/interface.go +++ b/hrp/pkg/uixt/interface.go @@ -784,6 +784,7 @@ type DataOptions struct { IgnoreNotFoundError bool // ignore error if target element not found MaxRetryTimes int // max retry times if target element not found Interval float64 // interval between retries in seconds + ScreenShotFilename string // turn on screenshot and specify file name } type DataOption func(data *DataOptions) @@ -860,12 +861,19 @@ func WithDataWaitTime(sec float64) DataOption { } } -func NewData(data map[string]interface{}, options ...DataOption) *DataOptions { - if data == nil { - data = make(map[string]interface{}) +func WithScreenShot(fileName ...string) DataOption { + return func(data *DataOptions) { + if len(fileName) > 0 { + data.ScreenShotFilename = fileName[0] + } else { + data.ScreenShotFilename = fmt.Sprintf("screenshot_%d", time.Now().Unix()) + } } +} + +func NewDataOptions(options ...DataOption) *DataOptions { dataOptions := &DataOptions{ - Data: data, + Data: make(map[string]interface{}), } for _, option := range options { option(dataOptions) @@ -874,7 +882,18 @@ func NewData(data map[string]interface{}, options ...DataOption) *DataOptions { if len(dataOptions.Scope) == 0 { dataOptions.Scope = []int{0, 0, math.MaxInt64, math.MaxInt64} // default scope } + return dataOptions +} + +func NewData(data map[string]interface{}, options ...DataOption) map[string]interface{} { + dataOptions := NewDataOptions(options...) + + // merge with data options + for k, v := range dataOptions.Data { + data[k] = v + } + // handle point offset if len(dataOptions.Offset) == 2 { if x, ok := data["x"]; ok { xf, _ := builtin.Interface2Float64(x) @@ -886,23 +905,24 @@ func NewData(data map[string]interface{}, options ...DataOption) *DataOptions { } } - if _, ok := dataOptions.Data["steps"]; !ok { - dataOptions.Data["steps"] = 12 // default steps + // add default options + if _, ok := data["steps"]; !ok { + data["steps"] = 12 // default steps } - if _, ok := dataOptions.Data["duration"]; !ok { - dataOptions.Data["duration"] = 0 // default duration + if _, ok := data["duration"]; !ok { + data["duration"] = 0 // default duration } - if _, ok := dataOptions.Data["frequency"]; !ok { - dataOptions.Data["frequency"] = 60 // default frequency + if _, ok := data["frequency"]; !ok { + data["frequency"] = 60 // default frequency } - if _, ok := dataOptions.Data["isReplace"]; !ok { - dataOptions.Data["isReplace"] = true // default true + if _, ok := data["isReplace"]; !ok { + data["isReplace"] = true // default true } - return dataOptions + return data } // current implemeted device: IOSDevice, AndroidDevice diff --git a/hrp/pkg/uixt/ios_device.go b/hrp/pkg/uixt/ios_device.go index ff318ea21..6236a3eee 100644 --- a/hrp/pkg/uixt/ios_device.go +++ b/hrp/pkg/uixt/ios_device.go @@ -2,10 +2,12 @@ package uixt import ( "bytes" + "context" "encoding/base64" builtinJSON "encoding/json" "fmt" "io" + builtinLog "log" "mime" "mime/multipart" "net" @@ -46,6 +48,23 @@ const ( dismissAlertButtonSelector = "**/XCUIElementTypeButton[`label IN {'不允许','暂不'}`]" ) +type IOSPerfOption = gidevice.PerfOption + +var ( + WithIOSPerfSystemCPU = gidevice.WithPerfSystemCPU + WithIOSPerfSystemMem = gidevice.WithPerfSystemMem + WithIOSPerfSystemDisk = gidevice.WithPerfSystemDisk + WithIOSPerfSystemNetwork = gidevice.WithPerfSystemNetwork + WithIOSPerfGPU = gidevice.WithPerfGPU + WithIOSPerfFPS = gidevice.WithPerfFPS + WithIOSPerfNetwork = gidevice.WithPerfNetwork + WithIOSPerfBundleID = gidevice.WithPerfBundleID + WithIOSPerfPID = gidevice.WithPerfPID + WithIOSPerfOutputInterval = gidevice.WithPerfOutputInterval + WithIOSPerfProcessAttributes = gidevice.WithPerfProcessAttributes + WithIOSPerfSystemAttributes = gidevice.WithPerfSystemAttributes +) + type IOSDeviceOption func(*IOSDevice) func WithUDID(udid string) IOSDeviceOption { @@ -66,7 +85,7 @@ func WithWDAMjpegPort(port int) IOSDeviceOption { } } -func WithLogOn(logOn bool) IOSDeviceOption { +func WithWDALogOn(logOn bool) IOSDeviceOption { return func(device *IOSDevice) { device.LogOn = logOn } @@ -96,7 +115,13 @@ func WithDismissAlertButtonSelector(selector string) IOSDeviceOption { } } -func WithPerfOptions(options ...gidevice.PerfOption) IOSDeviceOption { +func WithXCTest(bundleID string) IOSDeviceOption { + return func(device *IOSDevice) { + device.XCTestBundleID = bundleID + } +} + +func WithIOSPerfOptions(options ...gidevice.PerfOption) IOSDeviceOption { return func(device *IOSDevice) { device.PerfOptions = &gidevice.PerfOptions{} for _, option := range options { @@ -124,6 +149,10 @@ func IOSDevices(udid ...string) (devices []gidevice.Device, err error) { if u != "" && u != d.Properties().SerialNumber { continue } + // filter non-usb ios devices + if d.Properties().ConnectionType != "USB" { + continue + } deviceList = append(deviceList, d) } } @@ -142,10 +171,13 @@ func GetIOSDeviceOptions(dev *IOSDevice) (deviceOptions []IOSDeviceOption) { deviceOptions = append(deviceOptions, WithWDAMjpegPort(dev.MjpegPort)) } if dev.LogOn { - deviceOptions = append(deviceOptions, WithLogOn(true)) + deviceOptions = append(deviceOptions, WithWDALogOn(true)) } if dev.PerfOptions != nil { - deviceOptions = append(deviceOptions, WithPerfOptions(dev.perfOpitons()...)) + deviceOptions = append(deviceOptions, WithIOSPerfOptions(dev.perfOpitons()...)) + } + if dev.XCTestBundleID != "" { + deviceOptions = append(deviceOptions, WithXCTest(dev.XCTestBundleID)) } if dev.ResetHomeOnStartup { deviceOptions = append(deviceOptions, WithResetHomeOnStartup(true)) @@ -182,10 +214,21 @@ func NewIOSDevice(options ...IOSDeviceOption) (device *IOSDevice, err error) { return nil, err } - if len(deviceList) > 0 { - device.UDID = deviceList[0].Properties().SerialNumber + for _, dev := range deviceList { + udid := dev.Properties().SerialNumber + device.UDID = udid + device.d = dev + + // run xctest if XCTestBundleID is set + if device.XCTestBundleID != "" { + _, err = device.RunXCTest(device.XCTestBundleID) + if err != nil { + log.Error().Err(err).Str("udid", udid).Msg("failed to init XCTest") + continue + } + } + log.Info().Str("udid", device.UDID).Msg("select device") - device.d = deviceList[0] return device, nil } @@ -194,12 +237,13 @@ func NewIOSDevice(options ...IOSDeviceOption) (device *IOSDevice, err error) { } type IOSDevice struct { - d gidevice.Device - PerfOptions *gidevice.PerfOptions `json:"perf_options,omitempty" yaml:"perf_options,omitempty"` - UDID string `json:"udid,omitempty" yaml:"udid,omitempty"` - Port int `json:"port,omitempty" yaml:"port,omitempty"` // WDA remote port - MjpegPort int `json:"mjpeg_port,omitempty" yaml:"mjpeg_port,omitempty"` // WDA remote MJPEG port - LogOn bool `json:"log_on,omitempty" yaml:"log_on,omitempty"` + d gidevice.Device + PerfOptions *gidevice.PerfOptions `json:"perf_options,omitempty" yaml:"perf_options,omitempty"` + UDID string `json:"udid,omitempty" yaml:"udid,omitempty"` + Port int `json:"port,omitempty" yaml:"port,omitempty"` // WDA remote port + MjpegPort int `json:"mjpeg_port,omitempty" yaml:"mjpeg_port,omitempty"` // WDA remote MJPEG port + LogOn bool `json:"log_on,omitempty" yaml:"log_on,omitempty"` + XCTestBundleID string `json:"xctest_bundle_id,omitempty" yaml:"xctest_bundle_id,omitempty"` // switch to iOS springboard before init WDA session ResetHomeOnStartup bool `json:"reset_home_on_startup,omitempty" yaml:"reset_home_on_startup,omitempty"` @@ -475,6 +519,33 @@ func (dev *IOSDevice) NewUSBDriver(capabilities Capabilities) (driver WebDriver, return wd, nil } +func (dev *IOSDevice) RunXCTest(bundleID string) (cancel context.CancelFunc, err error) { + log.Info().Str("bundleID", bundleID).Msg("run xctest") + out, cancel, err := dev.d.XCTest(bundleID) + if err != nil { + return nil, errors.Wrap(err, "run xctest failed") + } + // wait for xctest to start + time.Sleep(5 * time.Second) + + f, err := os.OpenFile(fmt.Sprintf("xctest_%s.log", dev.UDID), + os.O_RDWR|os.O_CREATE|os.O_APPEND, 0o666) + if err != nil { + return nil, err + } + defer builtinLog.SetOutput(f) + + // print xctest running logs + go func() { + for s := range out { + builtinLog.Print(s) + } + f.Close() + }() + + return cancel, nil +} + func (dExt *DriverExt) ConnectMjpegStream(httpClient *http.Client) (err error) { if httpClient == nil { return errors.New(`'httpClient' can't be nil`) diff --git a/hrp/pkg/uixt/ios_driver.go b/hrp/pkg/uixt/ios_driver.go index a14c08c8a..03f3e2135 100644 --- a/hrp/pkg/uixt/ios_driver.go +++ b/hrp/pkg/uixt/ios_driver.go @@ -381,9 +381,9 @@ func (wd *wdaDriver) TapFloat(x, y float64, options ...DataOption) (err error) { "y": y, } // new data options in post data for extra WDA configurations - d := NewData(data, options...) + newData := NewData(data, options...) - _, err = wd.httpPOST(d.Data, "/session", wd.sessionId, "/wda/tap/0") + _, err = wd.httpPOST(newData, "/session", wd.sessionId, "/wda/tap/0") return } @@ -433,9 +433,9 @@ func (wd *wdaDriver) DragFloat(fromX, fromY, toX, toY float64, options ...DataOp } // new data options in post data for extra WDA configurations - d := NewData(data, options...) + newData := NewData(data, options...) - _, err = wd.httpPOST(d.Data, "/session", wd.sessionId, "/wda/dragfromtoforduration") + _, err = wd.httpPOST(newData, "/session", wd.sessionId, "/wda/dragfromtoforduration") return } @@ -506,9 +506,9 @@ func (wd *wdaDriver) SendKeys(text string, options ...DataOption) (err error) { data := map[string]interface{}{"value": strings.Split(text, "")} // new data options in post data for extra WDA configurations - d := NewData(data, options...) + newData := NewData(data, options...) - _, err = wd.httpPOST(d.Data, "/session", wd.sessionId, "/wda/keys") + _, err = wd.httpPOST(newData, "/session", wd.sessionId, "/wda/keys") return } @@ -541,9 +541,9 @@ func (wd *wdaDriver) PressBack(options ...DataOption) (err error) { } // new data options in post data for extra WDA configurations - d := NewData(data, options...) + newData := NewData(data, options...) - _, err = wd.httpPOST(d.Data, "/session", wd.sessionId, "/wda/dragfromtoforduration") + _, err = wd.httpPOST(newData, "/session", wd.sessionId, "/wda/dragfromtoforduration") return } diff --git a/hrp/pkg/uixt/ios_element.go b/hrp/pkg/uixt/ios_element.go index 9e2208b5e..02f54aacb 100644 --- a/hrp/pkg/uixt/ios_element.go +++ b/hrp/pkg/uixt/ios_element.go @@ -31,9 +31,9 @@ func (we wdaElement) SendKeys(text string, options ...DataOption) (err error) { "value": strings.Split(text, ""), } // new data options in post data for extra uiautomator configurations - d := NewData(data, options...) + newData := NewData(data, options...) - _, err = we.parent.httpPOST(d.Data, "/session", we.parent.sessionId, "/element", we.id, "/value") + _, err = we.parent.httpPOST(newData, "/session", we.parent.sessionId, "/element", we.id, "/value") return } diff --git a/hrp/pkg/uixt/ocr_vedem.go b/hrp/pkg/uixt/ocr_vedem.go index 1bcc96add..03d8a2717 100644 --- a/hrp/pkg/uixt/ocr_vedem.go +++ b/hrp/pkg/uixt/ocr_vedem.go @@ -56,7 +56,7 @@ func checkEnv() error { return nil } -func (s *veDEMOCRService) getOCRResult(imageBuf []byte) ([]OCRResult, error) { +func (s *veDEMOCRService) getOCRResult(imageBuf *bytes.Buffer) ([]OCRResult, error) { bodyBuf := &bytes.Buffer{} bodyWriter := multipart.NewWriter(bodyBuf) bodyWriter.WriteField("withDet", "true") @@ -67,7 +67,7 @@ func (s *veDEMOCRService) getOCRResult(imageBuf []byte) ([]OCRResult, error) { return nil, errors.Wrap(code.OCRRequestError, fmt.Sprintf("create form file error: %v", err)) } - size, err := formWriter.Write(imageBuf) + size, err := formWriter.Write(imageBuf.Bytes()) if err != nil { return nil, errors.Wrap(code.OCRRequestError, fmt.Sprintf("write form error: %v", err)) @@ -147,8 +147,22 @@ func getLogID(header http.Header) string { return logID[0] } -func (s *veDEMOCRService) FindText(text string, imageBuf []byte, options ...DataOption) (rect image.Rectangle, err error) { - data := NewData(map[string]interface{}{}, options...) +type OCRText struct { + Text string + Rect image.Rectangle +} + +type OCRTexts []OCRText + +func (t OCRTexts) Texts() (texts []string) { + for _, text := range t { + texts = append(texts, text.Text) + } + return texts +} + +func (s *veDEMOCRService) GetTexts(imageBuf *bytes.Buffer, options ...DataOption) ( + ocrTexts OCRTexts, err error) { ocrResults, err := s.getOCRResult(imageBuf) if err != nil { @@ -156,10 +170,18 @@ func (s *veDEMOCRService) FindText(text string, imageBuf []byte, options ...Data return } - var rects []image.Rectangle - var ocrTexts []string + dataOptions := NewDataOptions(options...) + + if dataOptions.ScreenShotFilename != "" { + path, err := saveScreenShot(imageBuf, dataOptions.ScreenShotFilename) + if err != nil { + return nil, errors.Wrap(err, "save screenshot failed") + } + log.Debug().Str("path", path).Msg("save screenshot") + } + for _, ocrResult := range ocrResults { - rect = image.Rectangle{ + rect := image.Rectangle{ // ocrResult.Points 顺序:左上 -> 右上 -> 右下 -> 左下 Min: image.Point{ X: int(ocrResult.Points[0].X), @@ -170,35 +192,62 @@ func (s *veDEMOCRService) FindText(text string, imageBuf []byte, options ...Data Y: int(ocrResult.Points[2].Y), }, } - if rect.Min.X >= data.Scope[0] && rect.Max.X <= data.Scope[2] && rect.Min.Y >= data.Scope[1] && rect.Max.Y <= data.Scope[3] { - ocrTexts = append(ocrTexts, ocrResult.Text) - // not contains text - if !strings.Contains(ocrResult.Text, text) { - continue - } + // check if text in scope + if rect.Min.X < dataOptions.Scope[0] || rect.Max.X > dataOptions.Scope[2] || + rect.Min.Y < dataOptions.Scope[1] || rect.Max.Y > dataOptions.Scope[3] { + // not in scope + continue + } - rects = append(rects, rect) + ocrTexts = append(ocrTexts, OCRText{ + Text: ocrResult.Text, + Rect: rect, + }) + } + return +} - // contains text while not match exactly - if ocrResult.Text != text { - continue - } +func (s *veDEMOCRService) FindText(text string, imageBuf *bytes.Buffer, options ...DataOption) ( + rect image.Rectangle, err error) { - // match exactly, and not specify index, return the first one - if data.Index == 0 { - return rect, nil - } + ocrTexts, err := s.GetTexts(imageBuf, options...) + if err != nil { + log.Error().Err(err).Msg("GetTexts failed") + return + } + + dataOptions := NewDataOptions(options...) + + var rects []image.Rectangle + for _, ocrText := range ocrTexts { + rect = ocrText.Rect + + // not contains text + if !strings.Contains(ocrText.Text, text) { + continue + } + + rects = append(rects, rect) + + // contains text while not match exactly + if ocrText.Text != text { + continue + } + + // match exactly, and not specify index, return the first one + if dataOptions.Index == 0 { + return rect, nil } } if len(rects) == 0 { return image.Rectangle{}, errors.Wrap(code.OCRTextNotFoundError, - fmt.Sprintf("text %s not found in %v", text, ocrTexts)) + fmt.Sprintf("text %s not found in %v", text, ocrTexts.Texts())) } // get index - idx := data.Index + idx := dataOptions.Index if idx > 0 { // NOTICE: index start from 1 idx = idx - 1 @@ -215,45 +264,29 @@ func (s *veDEMOCRService) FindText(text string, imageBuf []byte, options ...Data return rects[idx], nil } -func (s *veDEMOCRService) FindTexts(texts []string, imageBuf []byte, options ...DataOption) (rects []image.Rectangle, err error) { - ocrResults, err := s.getOCRResult(imageBuf) +func (s *veDEMOCRService) FindTexts(texts []string, imageBuf *bytes.Buffer, options ...DataOption) ( + rects []image.Rectangle, err error) { + + ocrTexts, err := s.GetTexts(imageBuf, options...) if err != nil { - log.Error().Err(err).Msg("getOCRResult failed") + log.Error().Err(err).Msg("GetTexts failed") return } - data := NewData(map[string]interface{}{}, options...) - ocrTexts := map[string]bool{} - var success bool - var rect image.Rectangle for _, text := range texts { var found bool - for _, ocrResult := range ocrResults { - rect = image.Rectangle{ - // ocrResult.Points 顺序:左上 -> 右上 -> 右下 -> 左下 - Min: image.Point{ - X: int(ocrResult.Points[0].X), - Y: int(ocrResult.Points[0].Y), - }, - Max: image.Point{ - X: int(ocrResult.Points[2].X), - Y: int(ocrResult.Points[2].Y), - }, - } - - if rect.Min.X >= data.Scope[0] && rect.Max.X <= data.Scope[2] && rect.Min.Y >= data.Scope[1] && rect.Max.Y <= data.Scope[3] { - ocrTexts[ocrResult.Text] = true + for _, ocrText := range ocrTexts { + rect := ocrText.Rect - // not contains text - if !strings.Contains(ocrResult.Text, text) { - continue - } - - found = true - rects = append(rects, rect) - break + // not contains text + if !strings.Contains(ocrText.Text, text) { + continue } + + found = true + rects = append(rects, rect) + break } if !found { rects = append(rects, image.Rectangle{}) @@ -263,28 +296,42 @@ func (s *veDEMOCRService) FindTexts(texts []string, imageBuf []byte, options ... if !success { return rects, errors.Wrap(code.OCRTextNotFoundError, - fmt.Sprintf("texts %s not found in %v", texts, ocrTexts)) + fmt.Sprintf("texts %s not found in %v", texts, ocrTexts.Texts())) } return rects, nil } type OCRService interface { - FindText(text string, imageBuf []byte, index ...int) (rect image.Rectangle, err error) + GetTexts(imageBuf *bytes.Buffer, options ...DataOption) (ocrTexts OCRTexts, err error) + FindText(text string, imageBuf *bytes.Buffer, options ...DataOption) (rect image.Rectangle, err error) + FindTexts(texts []string, imageBuf *bytes.Buffer, options ...DataOption) (rects []image.Rectangle, err error) } -func (dExt *DriverExt) FindTextByOCR(ocrText string, options ...DataOption) (x, y, width, height float64, err error) { +func (dExt *DriverExt) GetTextsByOCR(options ...DataOption) (texts OCRTexts, err error) { var bufSource *bytes.Buffer if bufSource, err = dExt.takeScreenShot(); err != nil { err = fmt.Errorf("takeScreenShot error: %v", err) return } - service, err := newVEDEMOCRService() + ocrTexts, err := dExt.ocrService.GetTexts(bufSource, options...) if err != nil { + log.Error().Err(err).Msg("GetTexts failed") return } - rect, err := service.FindText(ocrText, bufSource.Bytes(), options...) + + return ocrTexts, nil +} + +func (dExt *DriverExt) FindTextByOCR(ocrText string, options ...DataOption) (x, y, width, height float64, err error) { + var bufSource *bytes.Buffer + if bufSource, err = dExt.takeScreenShot(); err != nil { + err = fmt.Errorf("takeScreenShot error: %v", err) + return + } + + rect, err := dExt.ocrService.FindText(ocrText, bufSource, options...) if err != nil { log.Warn().Msgf("FindText failed: %s", err.Error()) return @@ -303,11 +350,7 @@ func (dExt *DriverExt) FindTextsByOCR(ocrTexts []string, options ...DataOption) return } - service, err := newVEDEMOCRService() - if err != nil { - return - } - rects, err := service.FindTexts(ocrTexts, bufSource.Bytes(), options...) + rects, err := dExt.ocrService.FindTexts(ocrTexts, bufSource, options...) if err != nil { log.Warn().Msgf("FindTexts failed: %s", err.Error()) return diff --git a/hrp/pkg/uixt/ocr_vedem_test.go b/hrp/pkg/uixt/ocr_vedem_test.go index 2bb650b7d..f3fe1d32b 100644 --- a/hrp/pkg/uixt/ocr_vedem_test.go +++ b/hrp/pkg/uixt/ocr_vedem_test.go @@ -3,12 +3,13 @@ package uixt import ( + "bytes" "fmt" "os" "testing" ) -func checkOCR(buff []byte) error { +func checkOCR(buff *bytes.Buffer) error { service, err := newVEDEMOCRService() if err != nil { return err @@ -33,19 +34,23 @@ func TestOCRWithScreenshot(t *testing.T) { t.Fatal(err) } - if err := checkOCR(raw.Bytes()); err != nil { + if err := checkOCR(raw); err != nil { t.Fatal(err) } } func TestOCRWithLocalFile(t *testing.T) { imagePath := "/Users/debugtalk/Downloads/s1.png" + file, err := os.ReadFile(imagePath) if err != nil { t.Fatal(err) } - if err := checkOCR(file); err != nil { + buf := new(bytes.Buffer) + buf.Read(file) + + if err := checkOCR(buf); err != nil { t.Fatal(err) } } diff --git a/hrp/pkg/uixt/swipe.go b/hrp/pkg/uixt/swipe.go index 4c4e865f2..b1f8f6d03 100644 --- a/hrp/pkg/uixt/swipe.go +++ b/hrp/pkg/uixt/swipe.go @@ -69,9 +69,9 @@ type Action func(driver *DriverExt) error // findCondition indicates the condition to find a UI element // foundAction indicates the action to do after a UI element is found func (dExt *DriverExt) SwipeUntil(direction interface{}, findCondition Action, foundAction Action, options ...DataOption) error { - d := NewData(nil, options...) - maxRetryTimes := d.MaxRetryTimes - interval := d.Interval + dataOptions := NewDataOptions(options...) + maxRetryTimes := dataOptions.MaxRetryTimes + interval := dataOptions.Interval for i := 0; i < maxRetryTimes; i++ { if err := findCondition(dExt); err == nil { @@ -103,9 +103,9 @@ func (dExt *DriverExt) SwipeUntil(direction interface{}, findCondition Action, f } func (dExt *DriverExt) LoopUntil(findAction, findCondition, foundAction Action, options ...DataOption) error { - d := NewData(nil, options...) - maxRetryTimes := d.MaxRetryTimes - interval := d.Interval + dataOptions := NewDataOptions(options...) + maxRetryTimes := dataOptions.MaxRetryTimes + interval := dataOptions.Interval for i := 0; i < maxRetryTimes; i++ { if err := findCondition(dExt); err == nil { diff --git a/hrp/pkg/uixt/tap.go b/hrp/pkg/uixt/tap.go index 1629f1379..3d4b584aa 100644 --- a/hrp/pkg/uixt/tap.go +++ b/hrp/pkg/uixt/tap.go @@ -65,11 +65,11 @@ func (dExt *DriverExt) GetImageXY(imagePath string, options ...DataOption) (poin } func (dExt *DriverExt) TapByOCR(ocrText string, options ...DataOption) error { - data := NewData(map[string]interface{}{}, options...) + dataOptions := NewDataOptions(options...) point, err := dExt.GetTextXY(ocrText, options...) if err != nil { - if data.IgnoreNotFoundError { + if dataOptions.IgnoreNotFoundError { return nil } return err @@ -79,11 +79,11 @@ func (dExt *DriverExt) TapByOCR(ocrText string, options ...DataOption) error { } func (dExt *DriverExt) TapByCV(imagePath string, options ...DataOption) error { - data := NewData(map[string]interface{}{}, options...) + dataOptions := NewDataOptions(options...) point, err := dExt.GetImageXY(imagePath, options...) if err != nil { - if data.IgnoreNotFoundError { + if dataOptions.IgnoreNotFoundError { return nil } return err @@ -103,11 +103,11 @@ func (dExt *DriverExt) TapOffset(param string, xOffset, yOffset float64, options return ele.Click() } - data := NewData(map[string]interface{}{}, options...) + dataOptions := NewDataOptions(options...) x, y, width, height, err := dExt.FindUIRectInUIKit(param, options...) if err != nil { - if data.IgnoreNotFoundError { + if dataOptions.IgnoreNotFoundError { return nil } return err diff --git a/hrp/runner.go b/hrp/runner.go index e5a770b3f..561d509ec 100644 --- a/hrp/runner.go +++ b/hrp/runner.go @@ -3,6 +3,7 @@ package hrp import ( "crypto/tls" _ "embed" + "fmt" "net" "net/http" "net/http/cookiejar" @@ -499,12 +500,34 @@ func (r *SessionRunner) Start(givenVars map[string]interface{}) error { log.Info().Str("step", stepName). Str("type", string(step.Type())).Msg("run step start") - // run step - stepResult, err := step.Run(r) - stepResult.Name = stepName + // run times of step + loopTimes := step.Struct().Loops + if loopTimes < 0 { + log.Warn().Int("loops", loopTimes).Msg("loop times should be positive, set to 1") + loopTimes = 1 + } else if loopTimes == 0 { + loopTimes = 1 + } else if loopTimes > 1 { + log.Info().Int("loops", loopTimes).Msg("run step with specified loop times") + } + + // run step with specified loop times + var stepResult *StepResult + for i := 1; i <= loopTimes; i++ { + var loopIndex string + if loopTimes > 1 { + log.Info().Int("index", i).Msg("start running step in loop") + loopIndex = fmt.Sprintf("_loop_%d", i) + } + + // run step + stepResult, err = step.Run(r) + stepResult.Name = stepName + loopIndex + + // update summary + r.summary.Records = append(r.summary.Records, stepResult) + } - // update summary - r.summary.Records = append(r.summary.Records, stepResult) r.summary.Stat.Total += 1 if stepResult.Success { r.summary.Stat.Successes += 1 diff --git a/hrp/step.go b/hrp/step.go index 0df0347a8..4776e3118 100644 --- a/hrp/step.go +++ b/hrp/step.go @@ -1,10 +1,5 @@ package hrp -import ( - "github.com/httprunner/httprunner/v4/hrp/pkg/gidevice" - "github.com/httprunner/httprunner/v4/hrp/pkg/uixt" -) - type StepType string const ( @@ -19,39 +14,6 @@ const ( stepTypeIOS StepType = "ios" ) -var ( - WithIdentifier = uixt.WithIdentifier - WithMaxRetryTimes = uixt.WithMaxRetryTimes - WithWaitTime = uixt.WithWaitTime // only applicable to SwipeToTap* action - WithIndex = uixt.WithIndex // index of the target element, should start from 1, only applicable to ocr actions - WithTimeout = uixt.WithTimeout - WithIgnoreNotFoundError = uixt.WithIgnoreNotFoundError - WithText = uixt.WithText - WithID = uixt.WithID - WithDescription = uixt.WithDescription - WithDuration = uixt.WithDuration // only applicable to ios swipe action - WithSteps = uixt.WithSteps // only applicable to android swipe action - WithDirection = uixt.WithDirection - WithCustomDirection = uixt.WithCustomDirection - WithScope = uixt.WithScope // only applicable to ocr actions - WithOffset = uixt.WithOffset -) - -var ( - WithPerfSystemCPU = gidevice.WithPerfSystemCPU - WithPerfSystemMem = gidevice.WithPerfSystemMem - WithPerfSystemDisk = gidevice.WithPerfSystemDisk - WithPerfSystemNetwork = gidevice.WithPerfSystemNetwork - WithPerfGPU = gidevice.WithPerfGPU - WithPerfFPS = gidevice.WithPerfFPS - WithPerfNetwork = gidevice.WithPerfNetwork - WithPerfBundleID = gidevice.WithPerfBundleID - WithPerfPID = gidevice.WithPerfPID - WithPerfOutputInterval = gidevice.WithPerfOutputInterval - WithPerfProcessAttributes = gidevice.WithPerfProcessAttributes - WithPerfSystemAttributes = gidevice.WithPerfSystemAttributes -) - type StepResult struct { Name string `json:"name" yaml:"name"` // step name StepType StepType `json:"step_type" yaml:"step_type"` // step type, testcase/request/transaction/rendezvous @@ -83,6 +45,7 @@ type TStep struct { Extract map[string]string `json:"extract,omitempty" yaml:"extract,omitempty"` Validators []interface{} `json:"validate,omitempty" yaml:"validate,omitempty"` Export []string `json:"export,omitempty" yaml:"export,omitempty"` + Loops int `json:"loops,omitempty" yaml:"loops,omitempty"` } // IStep represents interface for all types for teststeps, includes: diff --git a/hrp/step_mobile_ui.go b/hrp/step_mobile_ui.go index 4dcf2be84..e56f16f95 100644 --- a/hrp/step_mobile_ui.go +++ b/hrp/step_mobile_ui.go @@ -11,27 +11,6 @@ import ( "github.com/httprunner/httprunner/v4/hrp/pkg/uixt" ) -// ios setting options -var ( - WithUDID = uixt.WithUDID - WithWDAPort = uixt.WithWDAPort - WithWDAMjpegPort = uixt.WithWDAMjpegPort - WithLogOn = uixt.WithLogOn - WithResetHomeOnStartup = uixt.WithResetHomeOnStartup - WithSnapshotMaxDepth = uixt.WithSnapshotMaxDepth - WithAcceptAlertButtonSelector = uixt.WithAcceptAlertButtonSelector - WithDismissAlertButtonSelector = uixt.WithDismissAlertButtonSelector - WithPerfOptions = uixt.WithPerfOptions -) - -// android setting options -var ( - WithSerialNumber = uixt.WithSerialNumber - WithAdbIP = uixt.WithAdbIP - WithAdbPort = uixt.WithAdbPort - WithAdbLogOn = uixt.WithAdbLogOn -) - type MobileStep struct { Serial string `json:"serial,omitempty" yaml:"serial,omitempty"` uixt.MobileAction `yaml:",inline"` @@ -301,28 +280,6 @@ func (s *StepMobile) Input(text string, options ...uixt.ActionOption) *StepMobil return &StepMobile{step: s.step} } -// Times specify running times for run last action -func (s *StepMobile) Times(n int) *StepMobile { - if n <= 0 { - log.Warn().Int("n", n).Msg("times should be positive, set to 1") - n = 1 - } - - mobileStep := s.mobileStep() - actionsTotal := len(mobileStep.Actions) - if actionsTotal == 0 { - return s - } - - // actionsTotal >=1 && n >= 1 - lastAction := mobileStep.Actions[actionsTotal-1 : actionsTotal][0] - for i := 0; i < n-1; i++ { - // duplicate last action n-1 times - mobileStep.Actions = append(mobileStep.Actions, lastAction) - } - return &StepMobile{step: s.step} -} - // Sleep specify sleep seconds after last action func (s *StepMobile) Sleep(n float64) *StepMobile { s.mobileStep().Actions = append(s.mobileStep().Actions, uixt.MobileAction{ diff --git a/hrp/step_mobile_ui_test.go b/hrp/step_mobile_ui_test.go index c9c7dae30..814ad3ba2 100644 --- a/hrp/step_mobile_ui_test.go +++ b/hrp/step_mobile_ui_test.go @@ -4,12 +4,14 @@ package hrp import ( "testing" + + "github.com/httprunner/httprunner/v4/hrp/pkg/uixt" ) func TestIOSSettingsAction(t *testing.T) { testCase := &TestCase{ Config: NewConfig("ios ui action on Settings"). - SetIOS(WithWDAPort(8700), WithWDAMjpegPort(8800)), + SetIOS(uixt.WithWDAPort(8700), uixt.WithWDAMjpegPort(8800)), TestSteps: []IStep{ NewStep("launch Settings"). IOS().Home().Tap("设置"). @@ -32,7 +34,7 @@ func TestIOSSearchApp(t *testing.T) { Config: NewConfig("ios ui action on Search App 资源库"), TestSteps: []IStep{ NewStep("进入 App 资源库 搜索框"). - IOS().Home().SwipeLeft().Times(2).Tap("dewey-search-field"). + IOS().Home().SwipeLeft().SwipeLeft().Tap("dewey-search-field"). Validate(). AssertLabelExists("取消"), NewStep("搜索抖音"). @@ -48,7 +50,7 @@ func TestIOSSearchApp(t *testing.T) { func TestIOSAppLaunch(t *testing.T) { testCase := &TestCase{ Config: NewConfig("启动 & 关闭 App"). - SetIOS(WithWDAPort(8700), WithWDAMjpegPort(8800)), + SetIOS(uixt.WithWDAPort(8700), uixt.WithWDAMjpegPort(8800)), TestSteps: []IStep{ NewStep("终止今日头条"). IOS().AppTerminate("com.ss.iphone.article.News"), @@ -69,7 +71,7 @@ func TestIOSAppLaunch(t *testing.T) { func TestIOSWeixinLive(t *testing.T) { testCase := &TestCase{ Config: NewConfig("ios ui action on 微信直播"). - SetIOS(WithLogOn(true), WithWDAPort(8100), WithWDAMjpegPort(9100)), + SetIOS(uixt.WithWDALogOn(true), uixt.WithWDAPort(8100), uixt.WithWDAMjpegPort(9100)), TestSteps: []IStep{ NewStep("启动微信"). IOS(). @@ -84,10 +86,10 @@ func TestIOSWeixinLive(t *testing.T) { TapByOCR("直播"). // 通过 OCR 识别「直播」 Validate(). AssertLabelExists("直播"), - NewStep("向上滑动 5 次"). + NewStep("向上滑动 3 次,截图保存"). + Loop(3). // 整体循环 3 次 IOS(). - SwipeUp().Times(3).ScreenShot(). // 上划 3 次,截图保存 - SwipeUp().Times(2).ScreenShot(), // 再上划 2 次,截图保存 + SwipeUp().SwipeUp().ScreenShot(), // 上划 2 次,截图保存 }, } err := NewRunner(t).Run(testCase) @@ -154,7 +156,9 @@ func TestIOSDouyinAction(t *testing.T) { AssertLabelExists("首页", "首页 tab 不存在"). AssertLabelExists("消息", "消息 tab 不存在"), NewStep("swipe up and down"). - IOS().SwipeUp().Times(3).SwipeDown(), + Loop(3). + IOS(). + SwipeUp().SwipeDown(), }, } err := NewRunner(t).Run(testCase) diff --git a/hrp/step_request.go b/hrp/step_request.go index 4a25d02e1..d56536bde 100644 --- a/hrp/step_request.go +++ b/hrp/step_request.go @@ -566,6 +566,12 @@ func (s *StepRequest) HTTP2() *StepRequest { return s } +// Loop specify running times for the current step +func (s *StepRequest) Loop(times int) *StepRequest { + s.step.Loops = times + return s +} + // GET makes a HTTP GET request. func (s *StepRequest) GET(url string) *StepRequestWithOptionalArgs { if s.step.Request != nil { diff --git a/hrp/summary.go b/hrp/summary.go index 0883a3d8e..e6d1be7ba 100644 --- a/hrp/summary.go +++ b/hrp/summary.go @@ -45,7 +45,7 @@ type Summary struct { func (s *Summary) appendCaseSummary(caseSummary *TestCaseSummary) { s.Success = s.Success && caseSummary.Success s.Stat.TestCases.Total += 1 - s.Stat.TestSteps.Total += len(caseSummary.Records) + s.Stat.TestSteps.Total += caseSummary.Stat.Total if caseSummary.Success { s.Stat.TestCases.Success += 1 } else {