python-搭建android UI自动化测试框架

环境搭建

整体技术方案:python+pytest+uiautomator2+allure

pytest介绍

pytest 是 python 的第三方单元测试框架,比自带 unittest 更简洁和高效,支持非常丰富的插件,同时兼容 unittest 框架。其优点:

1. 简单灵活,如能自动发现测试用例、优先级、顺序、参数化等;比unittest更高效简洁,且兼容unittest框架

2. 插件支持更丰富,并可自定义插件

3. 支持单元测试、接口测试(request)和复杂的功能/UI测试(selenium/appium/uiautomator2)

uiautomator2介绍

uiautomator2是一个用于Android设备自动化测试的Python库。它基于Google的UI Automator框架,提供了一组API,可以用来模拟用户操作设备界面,如点击、滑动、输入文本等。简而言之,它是android官方推出的自动化测试框架。appium针对android 的app测试也是基于uiautomator进行的二次封装。

基础环境安装

1. python安装:不再赘述,本章使用的是python 3.9.6

2. 安装pytest模块,使用的是 pytest-7.4.0

pip install pytest

3. 安装uiautomator2

pip install uiautomator2

4. pytest其他插件安装

#pytest 执行测试时需要用到
pip install pytest-sugar # 打印进度
pip install pytest-rerunfailures # 失败重试
pip install pytest-ordering # 执行顺序

allure环境安装

Allure是一个开源的测试报告工具,用于生成清晰、简洁的测试报告。

brew install allure
pip3 install allure-pytest

安装好以后可以通过  allure --version 查看allure的版本,这里使用的allure版本为:2.24.0

框架设计

框架结构

Base目录:BaseClass类主要是封装uiautomator2的一些基础操作方法和自定义通用的方法,目的是为了解耦,方便日后更换自动化框架(如更换uiautomator2),不需要变更其他业务层代码逻辑。AppClass类是继承BaseClass类,是针对app进行一些通用方法的封装。

page目录:App中每个页面可以封装成一个类,每个页面里的一些功能封装为一个方法。可以在page目录再设二级app目录作为区分。

Case目录:测试用例集合。

用例设计

pytest用例读取规则

pytest 执行用例时,会根据规则自动递归遍历执行路径下所有的目录,自动收集测试用例。

1. pytest 默认用例识别规则

a. 用例文件:所有文件名为 test_ 开头 或者 _test 开头的文件会被识别为用例文件。
b. 用例类,测试文件中每个 Test 开头的类就是一个测试用例类。
c. 测试用例:测试类中每个 test 开头的方法就是一条测试用例,测试文件中每个 test 开头的函数也是一条测试用例。

2. pytest 配置文件规则:

可以通过配置文件修改默认规则,命名为pytest.inisetup.cfg,存放在项目的根目录下(是固定且不可更改)

[pytest] ini-options in the first pytest.ini|tox.ini|setup.cfg|pyproject.toml file found:

pytest.ini的配置语法,可以通过pytest --help查看,有具体的说明。

pytest.ini

markers:可以给每条case标注一个标签,在执行的时候加上-m参数即可指定运行某一类标签的case集合

[pytest]
# 配置测试搜索的文件名称
testpaths = ./case/  # 当前目录下的文件夹 -可自定义
# 当前目录下的scripts文件夹下,以test开头,以.py结尾的所有文件 -可自定义
python_files = test_*.py
#配置测试搜索的测试类名
python_classes = Test*
#当前目录下的scripts文件夹下,以test开头,以.py结尾的所有文件中,以Test开头的类 -可自定义
#配置测试搜索的测试函数名
python_functions = test_*
#当前目录下的scripts文件夹下,以test开头,以.py结尾的所有文件中,以Test开头的类内,以test_开头的方法 -可自定义
markers =
    demo : marks test as demo
    smoke : marks test as smoke
    uat : marks test as uat
    test : marks test as test

pytest框架常用注解

@pytest.mark.parametrize   #参数化,方法前,给指定参数赋值(数据矩阵)

@pytest.mark.run(order=1) #用例执行顺序

@pytest.mark.demo #给用例标识不同的flag,如优先级也可以,执行时用-m区分

@pytest.mark.flaky(reruns=2, reruns_delay=2) #重试,方法前,指定方法失败后间隔2s后重试最多2次,全局重试可以在配置文件中配置,或者执行时设置参数。

@pytest.fixture()

@pytest.hookimpl()

@allure.epic(‘项目名称’)

@allure.feature("功能/模块描述") #报告时使用,类前,定义功能

@allure.story("") #报告时使用,方法前

@allure.step()

android app元素获取

通过weditor模块可以方便的进行android ui元素的获取,启动python -m weiditor启动,启动后通过浏览器访问http://localhost:17310/#

uiautomator2 常用API

uiautomator2框架本质,是先给DUT设备(待测设备)推送安装一个agent(atx-agent)作为uiautomator2的服务端(运行在android DUT设备中),其作用是接收、解析python或其他客户端发来的请求(http请求),并转化为uiautomator2的代码,进而操控设备端的行为。

启动服务
python -m uiautomator2 init #启动服务,在android上安装apt-agent.apk,且启动
初始化设备
import uiautomator2 as u2
device = u2.connect("emulator-5554")
基础API

参考uiautomator2的api,元素查找总结起来有几种方法:

1. resourceId    2. text    3.description     4. className    4. xpath  5. 坐标   6. 混合方法

7. 子元素(child)和兄弟元素(sibling)  8. 相对定位(left、right、top、bottom)

详细参考文档:

uiautomator2详细使用方法_uiautomator2教程_古墙白的博客-CSDN博客

测试集合执行

项目根目录下新建run.py作为主程序入口,执行此py文件,其本质是执行pytest指令

pytest -s ./case/ --reruns=1 --alluredir=./result_tmp/   --clean-alluredir 

./case/ : 搜索执行case文件夹下的所有符合条件的case,当然还可以指定到具体的方法中。

#指定测试文件夹路径(会逐级寻找)
pytest -s 文件夹

#指定测试文件名称
pytest -s 文件.py

#指定测试类
pytest -s 测试文件::测试类

#指定测试方法
pytest -s 测试文件::测试类::测试方法

--reruns:执行失败的case,重试1次

--alluredir:case结果(json串)保存到result_tmp临时文件夹下(生成报告的时候需要)

-m=smoke: 执行@pytest.mark.somke标识的用例,smoke可以自定义为任何标签。

run.py 脚本

"""
测试主方法入口
"""
import os
import subprocess
import pytest
from uiautomator2 import logger


def init_env():
    cmd = "python -m uiautomator2 clear-cache"
    subprocess.call(cmd, shell=True)
    cmd = "python -m uiautomator2 init"
    subprocess.call(cmd, shell=True)
    logger.info("初始化运行环境!")


def init_report():
    cmd = "allure generate ./result_tmp/ -o reports --clean"
    subprocess.call(cmd, shell=True)
    project_path = os.path.abspath(os.path.dirname(__file__))
    report_path = project_path + "/reports/" + "index.html"
    logger.info("报告地址:{}".format(report_path))


if __name__ == "__main__":
    init_env()
    pytest.main(["-s", "--reruns=1", "./case", "--alluredir=./result_tmp/","--clean-alluredir"])
    init_report()

报告展示

pytest执行完成后,会每条case生成一个json文件保留到--alluredir目录下,此时通过allure generate命令可以生成allure报告,allure支持自定义(1. 在脚本中增加allure注解,如标题、描述等。 2. 修改allure的配置文件allure.yml,可以更改风格如css等),参考附件文献。

如下:

参考文献

pytest文档

1. pytest官方文档:Get Started — pytest documentation

2. pytest.ini的使用:

pytest之配置文件pytest.ini - 爱码网

uiautomator文档

https://github.com/openatx/uiautomator2

 uiautomator2详细使用方法_uiautomator2教程_古墙白的博客-CSDN博客

allure文档

https://www.cnblogs.com/R-bear/p/15026920.html

https://www.cnblogs.com/gezirui/p/17621884.html

其他参考文献:

使用 uiautomator2+pytest+allure 进行 Android 的 UI 自动化测试_自由家的博客-CSDN博客

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值