app自动化测试框架:pytest+appium+allure
整个框架主要分为三层:Base层、ActivityObject层、TestCase层,采用传统的互联网的垂直架构模式。
运行此项目前需要进行如下操作:
使用pycharm导入项目
打开pycharm的terminal,切换到 requirements.txt 所在的目录下,使用如下命令 ,就能在当前的 python 环境中导入所有需要的包:
pip install -r requirements.txt
环境说明:
有任何使用问题请联系我:848257135@qq.com
封装操作元素的公共方法,以及断言方法
封装的读取配置文件的公共方法,类似于util工具类
存放全局配置文件
用于生成日志文件
提取页面对象封装公共操作方法
存放测试报告,以及测试报告的生成模板allure
用于存放测试case
conftest.py
:封装了本地测试driver,分布式driver方法,定义了钩子函数,进行pytest功能拓展
pytest.ini
:配置了pytest的日志功能,以及测试用例扫描
requirements.txt
:框架运行所需要的jar包
run.py
:本地调试入口,ci集成测试入口
version:3.7
version 1.8
,win10系统中配置配置java环境,参考win10java环境配置
Invoke-Expression (New-Object System.Net.WebClient).DownloadString('https://get.scoop.sh')
Set-ExecutionPolicy RemoteSigned -scope CurrentUser
,运行成功后重新执行第一步Scoop was installed successfully!
scoop install allure
进行allure的安装'allure' (2.13.1) was installed successfully!
参考资料:https://download.csdn.net/download/qq_39214101/12721006
or进群下载:自动化测试-夜行者(816489363)
下面详细说明如何添加一条用例,以登录界面演示
在ActivityObject下Login_page模块新建一个页面元素yaml文件,Login_page.yaml
字段说明:
#封装需要操作的元素对象
desc: "登录页面元素操作对象"
parameters:
- elem_name: "phone_number"
desc: "请输入手机号"
data: {
method: "ANDROID_UIAUTOMATOR",
value: 'new UiSelector().text("请输入手机号")'
}
- elem_name: "code"
desc: "请输入验证码"
data: {
method: "ANDROID_UIAUTOMATOR",
value: 'new UiSelector().text("请输入验证码")'
}
- elem_name: "login_btn"
desc: "登录按钮"
data: {
method: "ANDROID_UIAUTOMATOR",
value: 'new UiSelector().text("登录")'
}
- elem_name: "message_id"
desc: "消息弹框信息"
data: {
method: "ID",
value: "android:id/message"
}
在ActivityObject模块下的elem_params.py文件中进行Login_activity.yaml注册,生成一个yaml文件对象,初始化传递两个参数,一个是模块名,一个是yaml配置文件名
# 注册yaml文件对象
class LoginActivityElem(ElemParams):
def __init__(self):
super(LoginActivityElem, self).__init__('Login_activity', 'Login_activity.yaml')
在ActivityObject下Login_activity模块创建一个login_activity.py封装login页面操作元素,导入Login_activity.yaml文件对象,初始化,然后获取yaml文件中封装的元素,底层通过传入locator定位器(元组),进行页面元素操作
# !/user/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2020/5/12 21:11
# @Author : chineseluo
# @Email : 848257135@qq.com
# @File : run.py
# @Software: PyCharm
from Base.base import Base
from selenium import webdriver
from ActivityObject.elemParams import LoginActivityElem
# 封装车联网app登录页面操作对象操作方法
class LoginActivity(Base):
def __init__(self, driver):
# 初始化页面元素对象,即yaml文件对象
self.elem_locator = LoginActivityElem()
# 初始化driver
super().__init__(driver)
def input_phone(self, value):
elem = self.elem_locator.get_locator("phone_number")
super().send_key(elem, value)
def input_code(self, value):
elem = self.elem_locator.get_locator("code")
super().send_key(elem, value)
def click_login_btn(self):
elem = self.elem_locator.get_locator("login_btn")
super().click_btn(elem)
def get_message_value(self):
elem = self.elem_locator.get_locator("message_id")
return super().get_text(elem)
if __name__ == "__main__":
home_activity = LoginActivity(webdriver.Chrome())
在TestCases下面新建一个包,例如Login模块,测试登录页面
在Login下面创建一个conftest.py和test_loginActivityCase.py
conftest.py中指定需要加载的测试页面对象,使用scope级别为function
# -*- coding: utf-8 -*-
# @Time : 2020/5/12 22:53
# @Author : chineseluo
# @Email : 848257135@qq.com
# @File : conftest.py
# @Software: PyCharm
import pytest
from ActivityObject.Login_activity.loginActivity import LoginActivity
@pytest.fixture(scope="function")
def login_activity_class_load(function_driver):
login_activity = LoginActivity(function_driver)
yield login_activity
test_loginActivityCase.py中每个测试case需要调用页面模块conftest.py中的页面对象,以及全局配置conftest.py中function_driver,断言使用Base模块中的assert_method的AssertMethod,里面封装了断言方法,包含了allure断言失败截图等操作,根据不同的断言场景取用,或者自己再进行封装
# !/user/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2020/5/12 21:11
# @Author : chineseluo
# @Email : 848257135@qq.com
# @File : run.py
# @Software: PyCharm
import pytest
import allure
import inspect
import logging
from Base.assertMethod import AssertMethod
@allure.feature("TestLoginPageCase")
class TestLoginPageCase:
@allure.story("Login")
@allure.severity("normal")
@allure.description("测试登录")
@allure.link("https://www.baidu.com", name="连接跳转百度")
@allure.testcase("https://www.sina.com", name="测试用例位置")
@allure.title("执行测试用例用于登录模块")
def test_d1(self, login_activity_class_load, function_driver):
logging.info("用例编号编码:{}".format(inspect.stack()[0][3]))
login_activity_class_load.input_phone("18383398524")
login_activity_class_load.input_code("123456")
login_activity_class_load.click_login_btn()
message_value = login_activity_class_load.get_message_value()
AssertMethod.assert_equal_screen_shot(function_driver, (message_value, "用户不存在"))
@allure.story("Login")
@allure.severity("normal")
@allure.description("测试登录")
@allure.link("https://www.baidu.com", name="连接跳转百度")
@allure.testcase("https://www.sina.com", name="测试用例位置")
@allure.title("执行测试用例用于登录模块")
def test_d2(self, login_activity_class_load, function_driver):
logging.info("用例编号编码:{}".format(inspect.stack()[0][3]))
login_activity_class_load.input_phone("18383398524")
login_activity_class_load.input_code("123456")
login_activity_class_load.click_login_btn()
message_value = login_activity_class_load.get_message_value()
AssertMethod.assert_equal_screen_shot(function_driver, (message_value, "用户存在"))
if __name__ == "__main__":
pytest.main(["test_loginActivityCase.py"])
执行用例
执行用例可以通过两种常用的方法进行
pycharm中配置test runner
为pytest
,配置路径为settings->Tools->Python Integrated Tools->Testing
;配置完成后就能够在打开测试用例文件后看到可执行的按钮了
在根目录下的run.py
文件中运行,在pytest.ini中修改默认配置项,配置文件中配置了用例的目录扫描:TestCases,配置了addopts,其中需要配置--mobile_system,根据测试的手机系统进行设置,可选项为ios or android。--allure-features可以配置也可以不进行配置,配置allure标记的feature可以标记执行某一个feature,如果需要执行多个,不同feature之间以,
号分割,如果需要执行全部,用;
号注释allure-features这行即可。其他日志项根据自己实际需要启用,默认配置的是启用控制台输出日志,日志不输出日志文件,如果需要输出到日志文件,将log_file取消注释即可。
[pytest]
testpaths = TestCases
addopts = -s
-q
-v
--mobile_system=android
--allure-features TestLoginPageCase
--alluredir=Report/android
--clean-alluredir
log_format = %(asctime)s %(levelname)s %(message)s
log_level = INFO
log_file_level = debug
log_file_date_format = %Y-%m-%d %H:%M:%S
log_file_format = %(asctime)s %(levelname)s %(message)s
;log_file = ./Logs/log.log
log_cli = true
log_cli_level = INFO
log_cli_format = %(asctime)s [%(levelname)8s] %(message)s (%(filename)s:%(lineno)s)
log_cli_date_format=%Y-%m-%d %H:%M:%S
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。