同事研究的基于 UI 层的 pytest 脚本

环境配置

1、软件包安装
python3.7
pip install pytest-ordering
pip install pytest
pip install selenium (https://www.cnblogs.com/eastonliu/p/9088301.html[元素定位教程])
https://npm.taobao.org/mirrors/chromedriverpython 环境目录下)(谷歌浏览器驱动,解压到到
pip install allure-pytest
pip install psycopg2
pip install pytest-assume
pip install pytest-rerunfailures
pip install Appium-Python-Client
安装 SDK 和 jdk

2、环境配置
allure 配置 bath 环境变量 (https://www.cnblogs.com/haohuixiang/p/12627551.html)

3、命令
脚本执行命令:pytest
报告生成命令:allure generate report/ -o report/html --clean

工程包结构

脚本解析

1、从 order=0 的脚本开始执行,将各页面浏览器的关闭操作禁止,原因是脚本中各页面都有写了个关闭浏览器的操作。(按住 ctrl,再点击函数即可跳转到相应的函数页面)

2、其余各页面的脚本逻辑基本是一致的
1)获取浏览器驱动: DriverUtils.get_test_driver()
2)获取当前页面的类方法: LoginProxy()
3)参数定义,读取 json 中的入参: @pytest.mark.parametrize
4)定义一个方法(传参):def test_login()
(1)调用已定义好的类中的方法,比如登录(传参)self.login_handle.input_id(id_1)
(2)调用输入用户 id 的方法:def input_id(self, id_1)
(3)定义查找用户 id 的方法:def find_id(self):
(4)元素定位方法:self.ID = (By.CSS_SELECTOR, "[placeholder = '账号']")
5)文本断言:assert is_not_element_by_text()
6)截屏操作:allure.attach(self.driver.get_screenshot_as_png(), "查询", allure.attachment_type.PNG)
7)数据库断言:inquire_sql_cs("select device_status from am_iot_device where bind_code= '{}';".format(web_base_page.sold_number))


↙↙↙阅读原文可查看相关链接,并与作者交流