test: add android example as demo

This commit is contained in:
lilong.129
2025-07-04 14:43:30 +08:00
parent e734424382
commit 55c4f8b9b5
2 changed files with 230 additions and 0 deletions

View File

@@ -0,0 +1,170 @@
{
"config": {
"name": "验证 UIA2 打点数据准确性",
"variables": {
"app_name": "抖音"
},
"android": [
{
"log_on": true,
"adb_server_host": "localhost",
"adb_server_port": 5037,
"uia2_ip": "localhost",
"uia2_port": 6790,
"uia2_server_package_name": "io.appium.uiautomator2.server",
"uia2_server_test_package_name": "io.appium.uiautomator2.server.test"
}
]
},
"teststeps": [
{
"name": "启动抖音",
"android": {
"os_type": "android",
"actions": [
{
"method": "home"
},
{
"method": "app_terminate",
"params": "com.ss.android.ugc.aweme"
},
{
"method": "swipe_to_tap_app",
"params": "$app_name",
"options": {
"identifier": "启动抖音",
"max_retry_times": 5,
"pre_mark_operation": true
}
},
{
"method": "sleep",
"params": 5
}
]
},
"validate": [
{
"check": "ui_ocr",
"assert": "exists",
"expect": "推荐",
"msg": "抖音启动失败,「推荐」不存在"
}
]
},
{
"name": "处理青少年弹窗",
"android": {
"os_type": "android",
"actions": [
{
"method": "tap_ocr",
"params": "我知道了",
"options": {
"ignore_NotFoundError": true
}
}
]
}
},
{
"name": "进入推荐页",
"android": {
"os_type": "android",
"actions": [
{
"method": "tap_ocr",
"params": "推荐",
"options": {
"identifier": "点击推荐",
"offset": [
0,
-1
],
"pre_mark_operation": true
}
},
{
"method": "sleep",
"params": 5
}
]
}
},
{
"name": "向上滑动 2 次",
"android": {
"os_type": "android",
"actions": [
{
"method": "swipe_direction",
"params": "up",
"options": {
"identifier": "第 1 次上划",
"pre_mark_operation": true
}
},
{
"method": "sleep",
"params": 2
},
{
"method": "swipe_direction",
"params": "up",
"options": {
"identifier": "第 2 次上划",
"pre_mark_operation": true
}
},
{
"method": "sleep",
"params": 2
},
{
"method": "swipe_direction",
"params": "up",
"options": {
"identifier": "第 3 次上划",
"pre_mark_operation": true
}
},
{
"method": "sleep",
"params": 2
},
{
"method": "tap_xy",
"params": [
0.9,
0.1
],
"options": {
"identifier": "点击进入搜索框",
"pre_mark_operation": true
}
},
{
"method": "sleep",
"params": 2
},
{
"method": "input",
"params": "httprunner 发版记录",
"options": {
"identifier": "输入搜索关键词",
"pre_mark_operation": true
}
},
{
"method": "tap_ocr",
"params": "搜索",
"options": {
"identifier": "点击搜索"
}
}
]
}
}
]
}

View File

@@ -0,0 +1,60 @@
//go:build localtest
package uitest
import (
"testing"
hrp "github.com/httprunner/httprunner/v5"
"github.com/httprunner/httprunner/v5/uixt/option"
)
func TestUIA2Log(t *testing.T) {
testCase := &hrp.TestCase{
Config: hrp.NewConfig("验证 UIA2 打点数据准确性").
WithVariables(map[string]interface{}{
"app_name": "抖音",
}).
SetAndroid(
option.WithAdbLogOn(true),
),
TestSteps: []hrp.IStep{
hrp.NewStep("启动抖音").
Android().
Home().
AppTerminate("com.ss.android.ugc.aweme"). // 关闭已运行的抖音
SwipeToTapApp("$app_name",
option.WithMaxRetryTimes(5),
option.WithIdentifier("启动抖音"),
option.WithPreMarkOperation(true)).Sleep(5).
Validate().
AssertOCRExists("推荐", "抖音启动失败,「推荐」不存在"),
hrp.NewStep("处理青少年弹窗").
Android().
TapByOCR("我知道了",
option.WithIgnoreNotFoundError(true)),
hrp.NewStep("进入推荐页").
Android().TapByOCR("推荐",
option.WithIdentifier("点击推荐"),
option.WithPreMarkOperation(true),
option.WithTapOffset(0, -1)).Sleep(5),
hrp.NewStep("向上滑动 2 次").
Android().
SwipeUp(option.WithIdentifier("第 1 次上划"), option.WithPreMarkOperation(true)).Sleep(2).
SwipeUp(option.WithIdentifier("第 2 次上划"), option.WithPreMarkOperation(true)).Sleep(2).
SwipeUp(option.WithIdentifier("第 3 次上划"), option.WithPreMarkOperation(true)).Sleep(2).
TapXY(0.9, 0.1, option.WithIdentifier("点击进入搜索框"), option.WithPreMarkOperation(true)).Sleep(2).
Input("httprunner 发版记录", option.WithIdentifier("输入搜索关键词"), option.WithPreMarkOperation(true)).
TapByOCR("搜索", option.WithIdentifier("点击搜索")),
},
}
if err := testCase.Dump2JSON("demo_android_uia2_log.json"); err != nil {
t.Fatal(err)
}
err := hrp.Run(t, testCase)
if err != nil {
t.Fatal(err)
}
}