APP自动化测试框架搭建(六)--uiautomator2、web-editor基础操作

第一章 APP自动化环境搭建(Mac版)
第二章 APP自动化环境搭建(Windows版)
第三章 adb命令
第四章 元素定位、元素操作
第五章 APP自动化测试框架搭建 Python+Appium+pytest-html
第六章 uiautomator2、web-editor基础操作
第七章 Airtest基础操作
第八章 ATX Server2多设备集群环境搭建



一、UiAutoMator2

1、简介

UIAutomator2是⼀种Android UI⾃动化框架,该框架优点:
1.环境搭建便捷
2.UI控件识别可视化好
3.UI⾃动化编写采⽤python,学习成本低
4.UI⾃动化脚本运⾏稳定

缺点:
存在设备兼容性问题,如:
华为⼿机,⽆法通过IP离线执⾏UI⾃动化
三星S8⼿机微信webview⻚⾯⽆法识别元素

2、安装

1.将手机设备或虚拟机连接上电脑,adb devices确认设备已连接上
在这里插入图片描述

2.通过pip install uiautomator2 命令⾏安装
在这里插入图片描述

二、Web-editor

1、简介

web-editor 类似于appium中的inspector;其功能也是⽤于元素定位调试;

2、安装

通过 pip install weditor 命令⾏安装,如果出现以下报错好像是版本原因
在这里插入图片描述
可以试试指定版本安装pip install weditor==0.6.4,这里可以安装成功
在这里插入图片描述

3、启动

安装好之后,在终端直接输⼊ weditor 即可在默认浏览器中启动⼀个元素定位的调试界⾯,输入你的设备编码,点击Connect连接设备, Dump Hierarchy刷新同步设备界面。
在这里插入图片描述
如果页面提示python -m weditor,关闭终端重新打开输入提示命令运行即可。
在这里插入图片描述
在这里插入图片描述

三、UiAutoMator2 + Web-editor 自动化

1、实例化对象

点击右侧重置代码即可实例化
在这里插入图片描述

import uiautomator2 as u2

d = u2.connect()

2、启动app

d.app_start("想要启动app的包名")

3、元素定位

选中想要操作的元素,右侧即可看见XPath路径,底部的代码也可以直接复制使用。同时也可以跟appium一样使用className、resourceId、text等定位方式。详细方式可以查看:https://developer.android.com/reference/android/support/test/uiautomator/UiSelector
在这里插入图片描述

4、等待

uiautomator2中的等待⼤部分和appium相同,有活动等待和元素等待;ele返回的是⼀个元素对象可以被操作。

ele = d.xpath('//*[@resource-id="com.suning.mobile.ebuy:id/home_floor_tab_menu_layout"]/android.widget.LinearLayout[3]/android.widget.RelativeLayout[1]/android.widget.LinearLayout[1]/android.widget.RelativeLayout[1]').wait()

5、操作

滑动操作:swipe
从sx,sy坐标滑动⾄ex,ey坐标d.swipe(1000, 1000, 1000, 300)
拖拽操作:drag
从sx,sy坐标拖拽⾄ex,ey坐标d.drag(1000, 1000, 1000, 300)
滚动界⾯:scroll
向上滚动d(scrollable=True).scroll(steps=10)
向下滑动d(scrollable=True).scroll.vert.backward()
⽔平向右滚动d(scrollable=True).scroll.horiz.forward(steps=50)
⽔平向左滚动d(scrollable=True).scroll.horiz.backward(steps=50)
⽔平滑动到最左边:d(scrollable=True).scroll.horiz.toBeginning(steps=100,max_swipes=1000)
⽔平滑动到最右边d(scrollable=True).scroll.horiz.toEnd(steps=100,max_swipes=1000)
竖直滑动到结尾d(scrollable=True).scroll.toEnd()
竖直滑动到开头d(scrollable=True).scroll.toBeginning(steps=50)
滑动到指定位置(element)d(scrollable=True).scroll.to(resourceId=‘element_value’)

四、实例操作

1、举例某电商app,假设测试用例如下:

用例编号用例描述预期结果实际结果运行结果
test_001首页点击新品tab,校验新品tab第一个商品价格,与详情页价格一致新品tab第一个商品价格等于详情页价格新品tab第一个商品价格等于详情页价格成功

2、编写脚本

# -*- coding: utf-8 -*-
# @Time  : 2022/7/11 14:50
# Author : 拒绝内卷的小测试

import uiautomator2 as u2

# 实例化
d = u2.connect("eqb6w4zldmmvvwln")

# 打开app('包名', '界面名')
d.app_start('com.suning.mobile.ebuy')

# 滑动到新品tab标签位置
d(scrollable=True).scroll.to(resourceId='com.suning.mobile.ebuy:id/layout_big_title')

# 点击新品tab标签
d.xpath(
    '//*[@resource-id="com.suning.mobile.ebuy:id/home_floor_tab_menu_layout"]/android.widget.LinearLayout[3]/android.widget.RelativeLayout[1]/android.widget.LinearLayout[1]/android.widget.RelativeLayout[1]').click()

# 获取首页商品价格
home_price = d(resourceId="com.suning.mobile.ebuy:id/product_price_tv_1").get_text()
# print(f"home_price: {home_price}")

# 点击进入商品详情页
d(resourceId="com.suning.mobile.ebuy:id/product_price_tv_1").click()

# 获取详情页价格
page_price = d(resourceId="com.suning.mobile.ebuy:id/tv_flash_sale_price").get_text()
# print(f"page_price: {page_price}")

# 断言首页价格与详情页价格相等
assert home_price == page_price, f"首页价格为:{home_price},详情页价格为{page_price}"

五、参考文档

uiautomator2:https://github.com/openatx/uiautomator2
web-editor:https://github.com/alibaba/web-editor
uiautomator2 selector:https://github.com/openatx/uiautomator2#selector

随手点赞一次,运气增加一份。

  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Karate 是一个基于 Cucumber 的开源自动化测试框架,可用于测试 API、WebUI 等各种应用程序。以下是 Karate 的搭建步骤: 1. 安装 Java 开发环境(JDK) 确保已经安装了 Java 开发环境(JDK),可以在终端输入 java -version 命令查看是否安装成功。 2. 安装 Maven 下载并安装 Maven,可以在终端输入 mvn -version 命令查看是否安装成功。 3. 创建 Maven 项目 在终端中进入任意目录,执行如下命令: ``` mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false ``` 其中,groupId 是项目组织标识符,artifactId 是项目名称。 4. 添加 Karate 依赖 在项目根目录下的 pom.xml 文件中添加 Karate 的依赖: ``` <dependency> <groupId>com.intuit.karate</groupId> <artifactId>karate-junit5</artifactId> <version>1.1.0</version> <scope>test</scope> </dependency> ``` 5. 创建测试用例 在 src/test/java 目录下创建测试用例,可以使用 Karate 提供的特定语法编写测试用例。例如,以下是一个测试 HTTP GET 请求的示例: ``` Feature: HTTP GET test Scenario: test GET Given url 'http://httpbin.org/get' When method GET Then status 200 And match response.headers['content-type'] contains 'application/json' ``` 6. 运行测试用例 在终端中进入项目根目录,执行如下命令运行测试用例: ``` mvn test ``` 执行完毕后,将在 target/surefire-reports 目录下生成测试报告。 以上就是 Karate 自动化测试框架搭建步骤。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

拒绝内卷的小测试

感谢老板的投喂

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值