环境类型
- Web UI 自动化
- 主要模块:python+selenium+pytest+allure
环境准备
环境:windows 10 、 mac OS
python版本 : Python 3.6.1 :: Anaconda 4.4.0 (64-bit)
selenium版本:selenium-3.141.0
搭建步骤
1. 创建虚拟环境,在虚拟环境中安装selenium
# 1.创建虚拟环境
python -m venv ui_env
# 2.激活虚拟环境
# windows
ui_env\Scripts\activate
# mac/linux
source ui_env/bin/activate
# 3.在激活的虚拟环境中安装selenium
pip install selenium -i https://pypi.tuna.tsinghua.edu.cn/simple --trusted-host pypi.tuna.tsinghua.edu.cn
2. 下载安装webdirver
2.1 webdirver下载:
- Chrome:http://chromedriver.storage.googleapis.com/index.html
- IE:http://selenium-release.storage.googleapis.com/index.html
版本对应
-
Chrome:
- 查看浏览器版本: 帮助-关于Google-chrome 版本 87.0.4280.66
- 查看http://chromedriver.storage.googleapis.com/index.html 下的notes.txt
-
IE:
- 查看selenium版本:3.141.0
- 查看http://selenium-release.storage.googleapis.com/index.html 下的文件名和selenium版本号对应的
2.2 webdirver安装:
- 复制webdriver到Python安装目录下\uiautotest\ui_env\Scripts 或 该项目的任一目录中,需要在初始化浏览器中给出驱动的路径,如下所示:
if self.browser.lower() == 'chrome':
chrome_options = webdriver.ChromeOptions()
# 屏蔽自动化驱动的提示
chrome_options.add_experimental_option('excludeSwitches', ['enable-automation', 'load-extension'])
# 屏蔽保存密码提示 mac也起作用
prefs={}
prefs["credentials_enable_service"] = False
prefs["profile.password_manager_enabled"] = False
chrome_options.add_experimental_option("prefs", prefs)
# 驱动位置
webdriver_path = os.path.join(project_path(), 'webdrivers', 'chromedriver')
self.driver = webdriver.Chrome(webdriver_path, chrome_options=chrome_options)
在mac笔记本上安装
2.2.1 webdirver安装:
- 复制webdriver到/usr/local/bin #如何这个目录不行就放在python的bin目录下
- 遇到问题:无法打开“chromedriver”,因为无法验证开发, 解决办法:https://www.it1352.com/1892752.html
- 在webdirver的安装目录下执行:
xattr -d com.apple.quarantine chromedriver
2.2.2 测试安装是否成功
from selenium import webdriver
driver = webdriver.Chrome()
url = "http://www.baidu.com"
driver.get(url)
driver.find_element_by_id('kw').send_keys("python+selenium")
driver.find_element_by_id('su').click()
driver.quit()
3. pytest 和 allure 的安装
3.1 pip安装pytest和allure-pytest
pip install pytest==6.2.5 -i https://pypi.tuna.tsinghua.edu.cn/simple --trusted-host pypi.tuna.tsinghua.edu.cn
pip install allure-pytest==2.9.44 -i https://pypi.tuna.tsinghua.edu.cn/simple --trusted-host pypi.tuna.tsinghua.edu.cn
3.2 allure命令行工具安装配置
3.2.1 allure工具下载:
需要去github上下载最新版: https://github.com/allure-framework/allure2/releases
3.2.2 allure工具配置
# 1.把步骤1下载的allure的zip 复制到虚拟环境的lib目录下(也可以是其他任一目录)
cd /Users/xx.xx/uiautotest/venv/lib/
cp ~/Downloads/allure-2.15.0.zip ./
# 2.解压缩allure
unzip allure-2.15.0.zip
# 3.获取allure当前的绝对路径
cd ./allure-2.15.0.zip/bin
pwd # /Users/xxx.xxx/uiautotest/venv/lib/allure-2.15.0/bin
# 4. 把步骤3中获取的路径添加到如下文件中
vi ~/.bash_profile
# 按i进入编辑模式, 添加如下内容
# allure add PATH
export PATH=$PATH:/Users/xxx.xxx/uiautotest/venv/lib/allure-2.15.0/bin
# 退出保存 按esc进入命令模式 输入 :wq
# 5. 激活新添加的环境变量
source ~/.bash_profile
3.2.3 检查allure工具是否配置成功
# 在命令的任一目录下,输入如下指令
allure --version
3.2.4 window的安装方式
- 安装方式类似:下载文件、把allure/bin的路径添加的环境变量中
3.3 验证pytest+allure环境是否安装正确
3.3.1 在testcase 目录下添加test.py文件
import pytest
import allure
@pytest.fixture(scope='function')
def login():
print("登录")
yield
print("登录完成")
@allure.step("步骤1:点xxx")
def step_1():
print("点击xxx111")
@allure.step("步骤2:点xxx")
def step_2():
print("点xxx222")
@allure.feature("iam")
class TestEditPage():
@allure.story("这是一个xxx的用例")
def test_1(self, login):
'''用例描述:先登录,再去执行xxx'''
step_1()
step_2()
print("xxx")
@allure.story("打开a页面")
def test_2(self, login):
'''用例描述:先登录,再去执行yyy'''
print("yyy")
3.3.2 运行用例
- cd到test.py所在的目录文件,命令行执行
pytest --alluredir ./report/allure_raw
3.3.3 启动allure服务,查看测试报告
allure serve report/allure_raw
启动服务,会自动跳转到报告页面
目录结构
参考博客:
1、 https://blog.csdn.net/qq_25305833/article/details/111318468
2、https://vikyd.github.io/download-chromium-history-version/#/ #chrome历史版本下载