atx测试框架实现手机应用UI自动化测试

  最近工作中遇到游戏APP需要实现UI自动化测试,这个app中真的是典型的混合App,有Android原生控件,有webview控件,以及游戏操作页面。研究了Appium,发现appium实现跨应用操作很困难,研究了好几天也没找到实现的方法。后来在公司大佬的带领下,接触到了atx这个自动化的框架。今天来说一下,使用atx,uiautomator2,pytest,selenium 来实现混合App的UI自动化及生成测试报告。
一、环境准备
本人使用的是系统是Mac,所以接线来的都是在Mac电脑上进行的。
1、安装adb,并将adb配置到环境变量中。具体请查看:https://blog.csdn.net/qq_26287435/article/details/81513649
2、安装pytest
1 # 安装
2 pip install -U pytest
3 # 查看安装版本
4 pip show pytest
5 # 或者
6 pytest --version
pytest的使用方法请自行查看:https://www.jianshu.com/p/75c27fe23b4e
3、安装uiautomator2
pip install --pre -U uiautomator2 #默认安装最新版本
pip install uiautomator2=0.1.11#指定版本安装
我使用的是0.1.11版本的,安装的时候指定版本:

4. 设备安装atx-agent
首先设备连接到PC,并能够adb devices发现该设备。

# 从github下载atx-agent文件,并推送到手机。在手机上安装包名为`com.github.uiautomator`的apk
$ python -m uiautomator2 init
success
看到success ,代表atx-agent初始化成功。手机上会出现一个小汽车图标的应用。
5.安装selenium
pip install selenium 
6.安装控件定位工具
weditor beta 针对Android和iOS原生应用快速定位元素,自动生成代码。
安装方式:
pip install --pre weditor
7.安装截图工具:
在进行游戏时,游戏界面的元素是无法使用原生的控件进行定位的,所以需要用到atx基于图片识别的方式来定位游戏控件。
截图工具使用方式:
python -m atx gui
二、UI自动化实现
接口描述了操作手机APP的各种方法。
1、废话不多说,直接上代码:
  1 # -*- coding: utf-8 -*-
  2 
  3 import atx
  4 import os
  5 from PIL import Image
  6 import pytest
  7 import allure
  8 from allure_commons.types import AttachmentType
  9 from logzero import logger
 10 from uiautomator2 import UiObjectNotFoundError
 11 # from base.chromedrvier import ChromeDriver
 12 from atx.ext.chromedriver import ChromeDriver
 13 
 14 
 15 ISMAC = 1
 16 TIMEOUT = 10
 17 @allure.step("{0}")
 18 def connect_phone(devices):
 19     global package_name,main_activity
 20     # devices = "D6JNOV5PCANFAURW"
 21     logger.info("连接:" + devices)
 22     package_name = 'com.netease.cloudmusic'#网易云音乐APP的包名
 23     main_activity = ".activity.LoadingActivity" #网易云音乐的activity
 24     d = atx.connect(devices)#手机的devices name
 25     d.start_app(package_name, main_activity)#启动网易云音乐app
 26     return d
 27 
 28 @allure.step("{1}")
 29 def click_text(atx_conn, action_m, text, timeout=TIMEOUT):
 30     """
 31     点击安卓原生的控件
 32     :param atx_conn:atx实例
 33     :param action_m:执行的行为描述
 34     :param text:需要查找的按钮的文本
 35     :param timeout:等待
 36     :return:
 37     """
 38 
 39     logger.info(action_m)
 40     atx_conn(text=text).click(timeout=timeout)
 41     report_jietu(atx_conn,action_m)#截图
 42 
 43 @allure.step("{1}")
 44 def click_id(atx_conn, action_m, id, timeout=TIMEOUT):
 45     """
 46 
 47     :param atx_conn: 连接实例
 48     :param action_m: 操作描述
 49     :param id: resureid
 50     :param timeout: 超时时间
 51     :return:
 52     """
 53     logger.info(action_m)
 54     atx_conn(resourceId = id).click(timeout=timeout)
 55     report_jietu(atx_conn,action_m)#截图
 56 
 57 def report_jietu(atx_conn,action_m):
 58     """
 59    截图
 60    :param atx_conn:atx实例
 61    :param action_m:action_m,被用作图片名称
 62    :return:
 63    """
 64     image = screenshot(atx_conn,action_m)#截图
 65     with open(image,"rb") as f :
 66         file = f.read()
 67     allure.attach(action_m,file,allure.attach_type.PNG)#截图附件
 68 
 69 def screenshot(atx_conn,url):
 70     """
 71     截图
 72     :param atx_conn:atx实例
 73     :param url:url,被用作图片名称
 74     :return:
 75     """
 76 
 77     path = os.path.abspath(os.path.dirname(os.getcwd()))
 78     if ISMAC:
 79         path = path + "/report/image/%s.png" % (url)
 80     else:
 81         path = path + "\report\image\%s.png" % (url)
 82     imgname = path
 83     atx_conn.screenshot(imgname)
 84     img(imgname)
 85     return imgname
 86 
 87 def img(image):
 88     """
 89     对图片进行压缩,覆盖原图进行保存
 90     :param image: 图片路径
 91     :return:
 92     """
 93 
 94     im = Image.open(image)
 95     # 获得图像尺寸
 96     w, h = im.size
 97     # print('原图尺寸: %sx%s' % (w, h))
 98     # 缩放到25%:
 99     im.thumbnail((w // 4, h // 4))
100     # 把缩放后的图像用jpeg格式保存:
101     im.save(image)
102 
103 
104 
105 @allure.feature('网易云音乐')
106 class Test_misuc(object):
107     @allure.story('进入每日推荐,点击播放第一首歌曲')
108     def test_meirituijian(self):#进入每日推荐,点击播放第一首歌曲
109         d = connect_phone("D6JNOV5PCANFAURW")#连接手机,启动云音乐
110         click_text(d,"点击每日推荐","每日推荐")#点击每日推荐
111         click_text(d,"点击播放全部","播放全部")#点击播放全部
112         click_id(d,"点击暂停播放按钮","com.netease.cloudmusic:id/tr")#点击暂停播放按钮
113         d.stop_app(package_name) #关闭云音乐
114 
115     @allure.story('进入我喜欢的音乐,将第一首歌分享给好友')
116     def test_share(self):#分享歌曲给好友
117         d = connect_phone("D6JNOV5PCANFAURW")#连接手机,启动云音乐
118         click_text(d,"点击我的","我的")
119         click_text(d,"点击我喜欢的音乐","我喜欢的音乐")
120         click_id(d,"点击更多","com.netease.cloudmusic:id/a")
121         click_text(d,"点击分享","分享")
122         click_text(d,"点击分享微信好友","微信好友")
123         # driver = ChromeDriver(d).driver() #启动selenium 如果是webview页面的话,需要启动selenium,然后根据selenium的定位方式查找元素
124         click_text(d,"分享给唯安格","唯安格")
125         click_text(d,"点击分享","分享")
126         click_text(d,"点击返回网易云音乐","返回网易云音乐")
127         d.stop_app(package_name)
128 
129 
130 
131 
132 if __name__ == '__main__':
133     a = Test_misuc()
134     a.test_meirituijian()
135     a.test_share()
View Code

运行上面代码并生成测试报告:

运行用例: py.test test_aa.py -s --alluredir ./reports
生成报告:allure generate --clean reports

其他工具启动:

启动weditor :python3 -m weditor
uiautomator2 初始化:python3 -m uiautomator2 init
启动atx gui: python3 -m atx gui
指定用例运行:py.test test_ddz.py::TestClass::test_share --alluredir ./reports

2、查看测试报告:执行完:allure generate --clean reports

 命令之后,会在当前文件夹生成:allure-report文件夹,该文件下会有一个index.html的文件,只用浏览器打开index.html文件,可以查看生成的测试报告。

如下图:

 
allure生成测试报告的方法请看官方文档:https://docs.qameta.io/allure/#_pytest

参考:

原文地址:https://www.cnblogs.com/xiehong/p/10711347.html