app自动遍历测试

https://blog.csdn.net/weixin_45912307/article/details/109393436

01自动遍历测试概述

1. 业务背景

  • 业务线众多业务流程复杂
  • 依赖传统券商一些资源

2. 解决思路

  • 自动化方式
    • 产品迭代快速
    • PO模式,学习成本高
  • 手工方式
    • 界面字段正确性:比如股票相关数据变化,共数十个字段。
    • 接口正确性:后端接口传输数据的变化和内容
    • 专项测试回归难度大:内存泄漏、健壮性测试、弱网等测试过程太多。
    • 回归工作量大:不回归又会漏测

3. 自动遍历技术

  • 用例维护成本降到最低
  • 尽可能的自动化覆盖回归业务

4. 常用遍历工具

  • google android原生monkey、app crawler
  • 百度smartmonkey
  • 腾讯newmonkey
  • vigossijj smart_monkeymacaca的NoSmoke
  • 头条的zhangzhao maxim
  • seveniruby appcrawler

02 Android monkey工具

1. monkey命令

  • adb shell monkey [options] 事件计数
  • adb shell monkey -help 查看帮助命令
  • adb shell monkey n 对所有包随机操作
  • adb shell monkey -p com.android.settings n 对指定包运行n次
  • adb shell monkey -p com.android.settings -s m n
  • adb shell monkey -p com.android.settings -vv n打印详细日志
  • 时间延迟(单位:ms)
    • adb shell monkey -p com.android.settings --throttle t n延迟时间t
  • 事件百分比
    • adb shell monkey -p com.android.settings --xx事件 m n在运行n次中xx事件共执行m次

2. 常用事件
--pct-touch 触摸事件,如点击
--pct-motion 动作时间,如滑动(直线)
--pct-trackball 轨迹事件,如移动+点击,曲线滑动
--pct-majornav 主要导航事件,如回退按钮、菜单按钮

3. 案例
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

03 Maxim

对Android Monkey的改造,基于遍历规则的高性能Android Monkey,适用于真机/模拟器的APP UI压力测试

3.1. 代码克隆

在这里插入图片描述

3.2. 推送jar包到模拟器/真机

adb push framework.jar /sdcard
adb push monkey.jar /sdcard

在这里插入图片描述

3.3执行命令

adb shell CLASSPATH=/sdcard/monkey.jar:/sdcard/framework.jar exec app_process /system/bin tv.panda.test.monkey.Monkey -p com.panda.videoliveplatform --uiautomatormix --running-minutes 60 -v -v

  • tv.panda.test.monkey.Monkey: monkey入口类,不要修改
  • com.panda.videoliveplatform: 被测app包名,需要修改,如本次案例使用的是同花顺app(com.hexin.plat.android)
  • --uiautomatormix: 遍历策略

3.4. 遍历策略

1. 模式 Mix (基于事件概率的压力测试)
--uiautomatormix

  • 直接使用底层accessibiltyserver获取界面接口 解析各控件,随机选取一个控件执行touch操作。
  • 同时与原monkey 其他操作按比例混合使用
  • 默认accessibilityserver action占比50%,其余各action分剩余的50%
  • accessibilityserver action占比可配置 --pct-uiautomatormix n

2. 模式 DFS

  • --uiautomatordfs 深度遍历算法

3. 模式Troy

  • --uiautomatortroy 控件选择策略按max.xpath.selector配置的高低优先级来进行深度遍历

4. 保留原始monkey

5. 总运行时长 --running-minutes n 运行n分钟

6. --act-whitelist-file /sdcard/awl.strings 定义白名单 --act-blacklist-file

7. 其他参数与原始monkey一致

3.5 执行效果

  • adb push framework.jar /sdcard
  • adb push monkey.jar /sdcard
  • adb shell dumpsys window w | findstr mCurrent
  • adb shell CLASSPATH=/sdcard/monkey.jar:/sdcard/framework.jar exec app_process /system/bin tv.panda.test.monkey.Monkey -p com.hexin.plat.android --uiautomatormix --running-minutes 1 -v -v
    在这里插入图片描述
    在这里插入图片描述

04 appcrawler

apprawler地址:https://github.com/seveniruby/AppCrawler

4.1 apprawler基础

1. apprawler 底层引擎
- appium
- adb
- macaca
- selenium
2. appiu底层引擎

  • wdb
  • uiautomator2Appcra

4.2 apprawler环境安装

  1. jdk1.8
  2. 真机/模拟器
  3. appcrawler
  4. 脚本启动命令
java -jar 包名

在这里插入图片描述

4.3 快速入门

  • 启动appium
  • 启动模拟器或真机
  • 启动命令
appcrawler --capability "appPackage=包名,appActivity=activity名"

4.4常用事件

1. 参数模式

java -jar appcrawler.jar 路径| --demo
appcrawler --capability "appPackage=包名,appActivity=activity名

2. 配置文件[推荐方式]

java -jar <appcrawler.jar 路径 l \
-c example.yml \ 
--capability 'appPackage=包名,appActivity=activity名'  \ 
-o 存放路径

4.5 执行参数与配置文件

  • capability设置与appium完全一致
  • testcase:用于启动app后的基础测试用例
  • selectedList:遍历范围设定
  • triggerActions:特定条件触发执行动作的设置
  • 执行参数比配置文件优先级别高

4.6 capability设置示例

  • appPackage:“”
  • appActivity:“”
  • appium:“http://127.0.0.1:4723/wd/hub”
  • noReset:true
  • automationName:uiautomator2
  • donStopAppOnReset:true

4.7 自动化支持的testcase

  • testcase的完整形态
    • given:所有的先决条件
    • when:先决条件成立后的行为
    • then:断言集合
  • testcase的简写形态
    • xpath:对应when里的xpath
    • action:对应when的action

4.8 支持的action动作

- back:后退

  • backApp 回退到当前的app 默认等价于back行为可定制
  • monkey:随机事件
    - xxx():执行代码
  • Thread.sleep( ts) :线程等待,单位毫秒
  • driver.swipe(0.9,0.5,0.1 , 0.5) :滑屏
  • click :点击
    - longTap :长触摸

4.9 自动遍历支持

  • selectedList:需要被遍历的元素范围
    -firstList:优先被点击
  • lastList:最后被点击
  • tagLimitMax:同祖先(同类型)的元素最多点击多少次
  • backButton:当所有元素都被点击后默认后退控件定位
  • blackList:黑名单
  • maxDepth:遍历的最大深度为10

4.10 自动遍历过程

  • 信息的获取:
    • 把当前app的界面dump为xml结构
  • 获取待遍历元素
    • 遍历范围 selectedList
    • 过滤黑名单小控件不可见控件blackList
    • 重排控件顺序firstList lastList
    • 跳过已点击+跳过限制点击的控件tagLimit
    • 根据匹配的规则执行action
  • 循环上面的步骤

4.11 实战部分(以同花顺app为例)

不要用nodpad++修改,可以用pycharm

  1. 启动appium
  2. 获取包名和activity名
adb logcat |findstr -i displayed
  1. 生成–demo配置文件(原来已生成,此步跳过)
java -jar appcrawler-2.7.4-hogwarts.jar  --demo  -e GBK # 不编码为utf-8在运行的时候会报编码错误
  1. 修改demo.yml的capability、resultDir等信息
  2. 执行自动化遍历
java -jar appcrawler-2.7.4-hogwarts.jar -c demo.yaml  

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
6. 遍历指定模块下的元素

selectedList:
  - xpath: "//androidx.recyclerview.widget.RecyclerView[contains(@resource-id,'rv_item_list')]//*[@clickable='true']"
  - xpath: "//android.widget.LinearLayout[contains(@resource-id,'entry_grid_layout')]//*[@clickable='true']"
  - xpath: "//android.widget.HorizontalScrollView[contains(@resource-id,'tablayout')]//*[@clickable='true']"
firstListDescription: "首先遍历"
firstList:
  - xpath: "//*[contains(@resource-id,'rv_item_list')]//*[@clickable='true']"
lastListDescription: "最后遍历"
lastList:
  - xpath: "//android.widget.LinearLayout[contains(@resource-id,'tab_widget')]//*[@clickable='true']"

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

4.12 demo.yaml文件

---
capabilityDescription: "appium的capability通用配置,其中automationName代表自动化的驱动引擎,除了支持appium的所有引擎外,额外增加了adb和selenium的支持"
capability:
  appActivity: "com.hexin.plat.android.AndroidLogoActivity"
  appium: "http://127.0.0.1:4723/wd/hub"
  noReset: "true"
  dontStopAppOnReset: "false"
  appPackage: "com.hexin.plat.android"
  fullReset: "false"
waitAppLoadedTimeoutDescription: "等待加载超时时间"
waitAppLoadedTimeout: 10000
waitAppLoadedDescription: "需等待加载的元素"
waitAppLoaded:
- given: []
  when: null
  then: []
  xpath: "//*[contains(@text, 'App')]"
  action: ""
  actions: []
  times: -1
implicitlyWaitTestCaseDescription: "隐式等待测试用例"
implicitlyWaitTestCase: 3000
implicitlyWaitCrawlDescription: "隐式等待爬虫"
implicitlyWaitCrawl: 0
testcaseDescription: "测试用例设置,用于遍历开始之前的一些前置操作,比如自动登录"
testcase:
  name: "AppCrawler TestCase"
  steps:
  - given: []
    when: null
    then: []
    xpath: "/*/*"
    action: "Thread.sleep(1000)"
    actions: []
    times: -1
maxTimeDescription: "最大运行时间"
maxTime: 10800
maxDepthDescription: "默认的最大深度10, 结合baseUrl可很好的控制遍历的范围"
maxDepth: 10
selectedListDescription: "默认遍历列表,只有出现在这个列表里的控件范围才会被遍历"
selectedList:
  - xpath: "//androidx.recyclerview.widget.RecyclerView[contains(@resource-id,'rv_item_list')]//*[@clickable='true']"
  - xpath: "//android.widget.LinearLayout[contains(@resource-id,'entry_grid_layout')]//*[@clickable='true']"
  - xpath: "//android.widget.HorizontalScrollView[contains(@resource-id,'tablayout')]//*[@clickable='true']"
firstListDescription: "首先遍历"
firstList:
  - xpath: "//*[contains(@resource-id,'rv_item_list')]//*[@clickable='true']"
lastListDescription: "最后遍历"
lastList:
  - xpath: "//android.widget.LinearLayout[contains(@resource-id,'tab_widget')]//*[@clickable='true']"
triggerActionsDescription: "在遍历过程中需要随时处理的一些操作,比如弹框、登录等"
triggerActions:
- given: []
  when: null
  then: []
  xpath: "permission_allow_button"
  action: ""
  actions: []
  times: 3
- given: []
  when: null
  then: []
  xpath: "允许"
  action: ""
  actions: []
  times: 3
blackListDescription: "黑名单列表 matches风格, 默认排除内容包含2个数字的控件"
blackList:
- given: []
  when: null
  then: []
  xpath: ".*[0-9]{2}.*"
  action: ""
  actions: []
  times: -1
- given: []
  when: null
  then: []
  xpath: "Get Music"
  action: ""
  actions: []
  times: -1
backButtonDescription: "后退按钮列表,默认在所有控件遍历完成后,才会最后点击后退按钮。目前具备了自动判断返回按钮的能力,默认不需要配置"
backButton:
- given: []
  when: null
  then: []
  xpath: "Navigate up"
  action: ""
  actions: []
  times: -1
xpathAttributesDescription: "在生成一个控件的唯一定位符中应该包含的关键属性"
xpathAttributes:
- "name()"
- "name"
- "label"
- "value"
- "resource-id"
- "content-desc"
- "text"
- "id"
- "name"
- "innerText"
- "tag"
- "class"
sortByAttributeDescription: "陆续根据属性进行遍历排序微调,depth表示从dom中最深层的控件开始遍历,list表示dom中列表优先,selected表示菜单最后遍历,这是默认规则,一般不需要改变"
sortByAttribute:
- "depth"
- "list"
- "selected"
findByDescription: "默认生成控件唯一定位符的表达式风格,可选项 default|android|id|xpath,默认会自动判断是否使用android定位或者ios定位"
findBy: "xpath"
baseUrlDescription: "设置一个起始点,从这个起始点开始计算深度,比如默认从登录后的界面开始计算"
baseUrl: []
appWhiteListDescription: "app白名单,允许在这些app里进行遍历"
appWhiteList: []
urlBlackListDescription: "url黑名单,用于排除某些页面的遍历"
urlBlackList: []
urlWhiteListDescription: "url白名单,仅在这些界面内遍历"
urlWhiteList: []
beforeRestartDescription: "启动一个app默认等待的时间"
beforeRestart: []
beforeElementDescription: "在遍历每个控件之前默认执行的动作"
beforeElement: []
afterElementDescription: "在遍历每个控件之后默认执行的动作"
afterElement: []
afterElementWaitDescription: "在遍历每个控件之后默认等待的时间,用于等待新页面加载"
afterElementWait: 1000
afterAllDescription: "在遍历完当前页面内的所有控件后,是否需要刷新或者滑动"
afterAll: []
afterAllMaxDescription: "fterAll的最大重试次数,比如连续滑动2次都没新元素即取消"
afterAllMax: 2
tagLimitMaxDescription: "相似控件最多点击几次"
tagLimitMax: 20
tagAttributesDescription: "标签属性列表"
tagAttributes:
- "name()"
- "name"
- "label"
- "resource-id"
- "content-desc"
- "id"
- "name"
- "tag"
- "class"
tagLimitDescription: "设置部分相似控件的最大遍历次数"
tagLimit:
- given: []
  when: null
  then: []
  xpath: "确定"
  action: ""
  actions: []
  times: 1000
- given: []
  when: null
  then: []
  xpath: "取消"
  action: ""
  actions: []
  times: 1000
- given: []
  when: null
  then: []
  xpath: "share_comment_guide_btn_name"
  action: ""
  actions: []
  times: 1000
- given: []
  when: null
  then: []
  xpath: "//*[contains(@class, 'List')]//*"
  action: ""
  actions: []
  times: 2
assertGlobalDescription: "全局断言"
assertGlobal: []
suiteNameDescription: "报告中的测试套件名字可以由列表内的控件内容替换,增强报告中关键界面的辨识度"
suiteName:
- "//*[@selected='true']//android.widget.TextView/@text"
screenshotDescription: "是否截图"
screenshot: true
reportTitleDescription: "报告的title"
reportTitle: "同花顺首页自动化遍历"
resultDirDescription: "结果目录,如果为空会自动创建对应时间戳_报名的结果目录"
resultDir: ""
showCancelDescription: "是否展示跳过的控件记录"
showCancel: true
pluginListDescription: "插件列表,暂时禁用,太高级了,很多人不会用"
Description: "在selectedList firstList lastList等很多配置中,需要填充的是测试步骤Step类型。Step类型由given(满足条件)when(条件满足的行为)then(断言)三部分组成。Step可以简化为xpath(定位表达式,支持xpath 正则 包含关系)与action(点击输入等行为)"
pluginList: []
自动遍历app测试是一种通过使用自动测试工具来模拟用户操作和验证app功能的方法。在这种测试中,通过编写测试脚本或配置文件,可以实现自动启动应用程序、执行一系列测试用例和断言,并生成测试报告。 在自动遍历app测试中,有几个关键的步骤和概念需要了解: 1. 配置文件:配置文件是指通过指定一些参数和设置来定义测试的方式和范围的文件。它可以包含应用程序的包名、启动的activity、appium的地址等信息[2]。 2. 测试用例:测试用例是指在自动测试过程中要执行的一系列步骤和验证的操作。在配置文件中,可以定义测试用例的范围、条件触发和执行动作的设置。 3. 动作:动作是指在测试用例中要执行的操作,例如点击、滑动、输入等。在自动遍历app测试中,可以使用XPath定位元素并执行相应的动作[4.8]。 4. 自动测试工具:在自动遍历app测试中,可以使用一些自动测试工具来实现测试。一些常用的工具包括appcrawler、appium等。 要进行自动遍历app测试,你可以按照以下步骤进行: 1. 安装所需的环境和工具:首先,需要确保你已经安装了Java开发环境(JDK)和相应的自动测试工具,例如appcrawler。你还需要安装appium和adb等底层引擎[3.4.2]。 2. 配置测试环境:在配置文件中,设置好应用程序的包名、启动的activity、appium的地址等信息。可以根据需要设置其他参数,例如设备的capabilities。 3. 编写测试用例:根据应用程序的功能和需求,编写相应的测试用例。在测试用例中,定义测试的范围、条件触发和执行动作的设置。可以使用XPath定位元素并执行相应的动作[4.8]。 4. 执行测试:使用命令行或脚本执行自动遍历app测试。根据配置文件和测试用例,自动测试工具将启动应用程序,并按照定义的测试用例执行相应的动作和验证。执行过程中,工具会生成测试报告,记录测试结果和错误信息。 总结起来,自动遍历app测试是一种通过编写配置文件和测试用例,使用自动测试工具来模拟用户操作和验证app功能的方法。通过定义测试范围、条件触发和执行动作的设置,可以实现自动执行测试并生成测试报告。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值