当前位置:网站首页>Pytest电商项目实战(上)
Pytest电商项目实战(上)
2022-07-31 03:20:00 【司小幽】
1.Web自动化登陆及登陆验证处理
from selenium import webdriver
# 浏览器对象的初始化
driver = webdriver.Chrome()
# 打开淘宝购物车的登录页面
driver.get("https://cart.taobao.com/cart.htm")
# 定位到账户名文本框
driver.find_element("id","fm-login-id").send_keys("云纱璃英")
driver.find_element("id","fm-login-password").send_keys("xxx")
driver.find_element("xpath","//div[@class='fm-btn']").click()
import os
from selenium import webdriver
from time import sleep
# 淘宝登录验证码解决方案
""" 使用编写的脚本以debug模式启动谷歌浏览器,脚本内容: chrome.exe --remote-debugging-port=9222 必须通过os.popen执行,通过os.system执行命令无效 """
os.popen("d:/chrome.bat")
sleep(5)
# 加载浏览器驱动
options = webdriver.ChromeOptions()
options.debugger_address = '127.0.0.1:9222'
driver = webdriver.Chrome(options = options)
sleep(2)
# 浏览器对象的初始化
# driver = webdriver.Chrome()
# 打开淘宝购物车的登陆页面
# driver.get("https://cart.taobao.com/cart.htm")
#
# # 定位到账户名文本框
# driver.find_element_by_id('fm-login-id').send_keys('云纱璃英')
# # 定位到密码
# driver.find_element_by_id('fm-login-password').send_keys('xxx')
#
# # 点击登陆按钮
# driver.find_element_by_xpath("//div[@class='fm-btn']").click()
# 点击商品链接
# driver.find_element_by_xpath("//ul/li[2]/div/div[2]/div/a").click()
driver.find_element_by_xpath("//ul/li[2]/div/div[2]/div[1]/a")
# 浏览器页签切换
# 获取所有的浏览器页签(句柄)
handls = driver.window_handles
print(handls)
# 切换到新开的第二个页签,下标填1就可以了,下标都是从0开始的
driver.switch_to.window(handls[1])
# 在页签,选择颜色
driver.find_element_by_xpath('//ul[@data-property="颜色分类"]/li').click()
2.Web自动化项目框架搭建
1.基于Pytest进行用例管理
# 购物车登录
def test_cart_login(browser):
# 购物车登录
# 打开淘宝购物车的登陆页面
browser.get("https://cart.taobao.com/cart.htm")
# 定位到账户名文本框
browser.find_element('id','fm-login-id').send_keys('云纱璃英')
# 定位到密码
browser.find_element('id','fm-login-password').send_keys('xxx')
# 点击登陆按钮
browser.find_element('xpath',"//div[@class='fm-btn']").click()
""" 装饰器@pytest.hookimpl(hookwrapper=True)等价于@pytest.mark.hookwrapper的作用: a.可以获取测试用例的信息,比如用例函数的描述 b.可以获取测试用例的结果,yield,返回一个result对象 """
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport():
# 可以获取测试用例的执行结果,yield,返回一个result对象
out = yield
""" 从返回一个result对象(out)获取调用结果的测试报告,返回一个report对象 report对象的属性 包括when(setup,call,teardown三个值)、nodeid(测试用例的名字)、 outcome(用例的执行结果:passed,failed) """
report = out.get_result()
# 仅仅获取用例call阶段的执行结果,不包含setup和teardown
if report.when == 'call':
# 获取用例call执行结果为结果为失败的情况
xfail = hasattr(report,"wasfail")
if(report.skipped and xfail) or (report.failed and not xfail):
# 添加allure报告截图
with allure.step("添加失败截图。。"):
# 使用allure自带的添加附件的方法:三个参数分别为:源文件、文件名 、 文件类型
allure.attach(driver.get_screenshot_as_png(),"失败截图",allure.attachment_type.PNG)
import os
import pytest
def run():
pytest.main(['-v','--alluredir','./result','--clean-alluredir','--allure-no-capture'])
os.system('allure generate ./result -o ./report_allure/ --clean')
if __name__ == '__main__':
run()
2.关键字驱动
# 所谓的关键字驱动,本质就是封装函数
# 自动化测试中,封装的目的:拆分测试数据和重复的行为代码,增加可维护性和复用性
# 定义关键字驱动类/工具类/基类
from selenium import webdriver
class WebKeys:
# 构造方法,用于接收driver对象
def __init__(self,driver):
self.driver = driver
# 编代码用,写完删掉
# self.driver = webdriver.Chrome()
# 打开浏览器
def open(self,url):
self.driver.get(url)
# 元素定位
def locator(self,name,value):
""" :param name: 元素定位的方式 :param value: 元素定位的路径 :return: """
el = self.driver.find_element(name,value)
# 将定位的元素框出来
self.locate_station(el)
return el
# 显示定位的地方,方便确认定位的位置
def locate_station(self,element):
self.driver.execute_script(
"arguments[0].setAttribute('style',arguments[1]);",
element,
"border: 2px solid green" #边框,green绿色
)
# 购物车登录
import pytest
# 跳过用例
from class36.p03.key_world.keyword import WebKeys
@pytest.mark.skip
def test_cart_login(browser):
# 购物车登录
# 打开淘宝购物车的登陆页面
browser.get("https://cart.taobao.com/cart.htm")
# 定位到账户名文本框
browser.find_element('id','fm-login-id').send_keys('云纱璃英')
# 定位到密码
browser.find_element('id','fm-login-password').send_keys('xxx')
# 点击登陆按钮
browser.find_element('xpath',"//div[@class='fm-btn']").click()
# 使用关键字驱动模式实现购物车登录
def test_case_login2(browser):
# 初始化工具类,传递浏览器对象
wk = WebKeys(browser)
wk.open("https://cart.taobao.com/cart.htm")
# 定位到账户名文本框
wk.locator('id','fm-login-id').send_keys('云纱璃英')
# 定位到密码框
wk.locator('id','fm-login-password').send_keys('xxx')
# 点击方式
wk.locator('xpath',"//div[@class='fm-btn']").click()
import os
from time import sleep
import allure
import pytest
# 每个用例如果引用了这个fixture,都会在用例执行前执行一下这个fixture
from selenium import webdriver
@pytest.fixture(scope='function')
def browser():
""" 全局定义浏览器驱动,方便下面的hook函数引用driver :return: """
global driver
os.popen("d:/chrome.bat")
sleep(3)
# 加载浏览器驱动
options = webdriver.ChromeOptions()
options.debugger_address = '127.0.0.1:9222'
driver = webdriver.Chrome(options=options)
sleep(2)
# 隐式等待10秒
driver.implicitly_wait(10)
""" yield之前的代码是用例前置 yield之后的代码是用例后置 """
yield driver
# 浏览器退出
""" 这种debug模式,下面的方法无法退出浏览器 driver.quit() driver.close() """
# 通过命令杀死进程
os.system('taskkill /im chromedriver.exe /F')
os.system('taskkill /im chrome.exe /F')
""" 装饰器@pytest.hookimpl(hookwrapper=True)等价于@pytest.mark.hookwrapper的作用: a.可以获取测试用例的信息,比如用例函数的描述 b.可以获取测试用例的结果,yield,返回一个result对象 """
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport():
# 可以获取测试用例的执行结果,yield,返回一个result对象
out = yield
""" 从返回一个result对象(out)获取调用结果的测试报告,返回一个report对象 report对象的属性 包括when(setup,call,teardown三个值)、nodeid(测试用例的名字)、 outcome(用例的执行结果:passed,failed) """
report = out.get_result()
# 仅仅获取用例call阶段的执行结果,不包含setup和teardown
if report.when == 'call':
# 获取用例call执行结果为结果为失败的情况
xfail = hasattr(report,"wasfail")
if(report.skipped and xfail) or (report.failed and not xfail):
# 添加allure报告截图
with allure.step("添加失败截图。。"):
# 使用allure自带的添加附件的方法:三个参数分别为:源文件、文件名 、 文件类型
allure.attach(driver.get_screenshot_as_png(),"失败截图",allure.attachment_type.PNG)
import os
import pytest
def run():
pytest.main(['-v','--alluredir','./result','--clean-alluredir','--allure-no-capture'])
os.system('allure generate ./result -o ./report_allure/ --clean')
if __name__ == '__main__':
run()
3.PO模式
# PO模式,关键字驱动模式的升级版
# 以每个网页为单位,进行关键字的封装,也就是通常所说的页面对象模型
# 定义关键字驱动类/工具类/基类
from selenium import webdriver
class WebKeys:
# 构造方法,用于接收driver对象
def __init__(self,driver):
self.driver = driver
# 编代码用,写完删掉
# self.driver = webdriver.Chrome()
# 打开浏览器
def open(self,url):
self.driver.get(url)
# 元素定位
def locator(self,name,value):
""" :param name: 元素定位的方式 :param value: 元素定位的路径 :return: """
el = self.driver.find_element(name,value)
# 将定位的元素框出来
self.locate_station(el)
return el
# 显示定位的地方,方便确认定位的位置
def locate_station(self,element):
self.driver.execute_script(
"arguments[0].setAttribute('style',arguments[1]);",
element,
"border: 2px solid green" #边框,green绿色
)
# 以页面为单位,封装常用的行为操作代码和元素定位代码
from class36.p04.key_world.keyword import WebKeys
class CatLoginPage(WebKeys):
#登录操作
def login(self,username,password):
self.open("https://cart.taobao.com/cart.htm")
# 定位到账户名文本框
self.locator('id', 'fm-login-id').send_keys(username)
# 定位到密码框
self.locator('id', 'fm-login-password').send_keys(password)
# 点击方式
self.locator('xpath', "//div[@class='fm-btn']").click()
# 购物车登录
import pytest
# 跳过用例
from class36.p04.key_world.keyword import WebKeys
from class36.p04.page.cartLogin_page import CatLoginPage
@pytest.mark.skip
def test_cart_login(browser):
# 购物车登录
# 打开淘宝购物车的登陆页面
browser.get("https://cart.taobao.com/cart.htm")
# 定位到账户名文本框
browser.find_element('id','fm-login-id').send_keys('云纱璃英')
# 定位到密码
browser.find_element('id','fm-login-password').send_keys('xxx')
# 点击登陆按钮
browser.find_element('xpath',"//div[@class='fm-btn']").click()
# 使用关键字驱动模式实现购物车登录
@pytest.mark.skip
def test_case_login2(browser):
# 初始化工具类,传递浏览器对象
wk = WebKeys(browser)
wk.open("https://cart.taobao.com/cart.htm")
# 定位到账户名文本框
wk.locator('id','fm-login-id').send_keys('云纱璃英')
# 定位到密码框
wk.locator('id','fm-login-password').send_keys('xxx')
# 点击方式
wk.locator('xpath',"//div[@class='fm-btn']").click()
# 使用PO模式实现购物车登录
def test_case_login3(browser):
# 初始化页面对象
cartLogin = CatLoginPage(browser)
cartLogin.login('云纱璃英','xxx')
import os
from time import sleep
import allure
import pytest
# 每个用例如果引用了这个fixture,都会在用例执行前执行一下这个fixture
from selenium import webdriver
@pytest.fixture(scope='function')
def browser():
""" 全局定义浏览器驱动,方便下面的hook函数引用driver :return: """
global driver
os.popen("d:/chrome.bat")
sleep(3)
# 加载浏览器驱动
options = webdriver.ChromeOptions()
options.debugger_address = '127.0.0.1:9222'
driver = webdriver.Chrome(options=options)
sleep(2)
# 隐式等待10秒
driver.implicitly_wait(10)
""" yield之前的代码是用例前置 yield之后的代码是用例后置 """
yield driver
# 浏览器退出
""" 这种debug模式,下面的方法无法退出浏览器 driver.quit() driver.close() """
# 通过命令杀死进程
os.system('taskkill /im chromedriver.exe /F')
os.system('taskkill /im chrome.exe /F')
""" 装饰器@pytest.hookimpl(hookwrapper=True)等价于@pytest.mark.hookwrapper的作用: a.可以获取测试用例的信息,比如用例函数的描述 b.可以获取测试用例的结果,yield,返回一个result对象 """
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport():
# 可以获取测试用例的执行结果,yield,返回一个result对象
out = yield
""" 从返回一个result对象(out)获取调用结果的测试报告,返回一个report对象 report对象的属性 包括when(setup,call,teardown三个值)、nodeid(测试用例的名字)、 outcome(用例的执行结果:passed,failed) """
report = out.get_result()
# 仅仅获取用例call阶段的执行结果,不包含setup和teardown
if report.when == 'call':
# 获取用例call执行结果为结果为失败的情况
xfail = hasattr(report,"wasfail")
if(report.skipped and xfail) or (report.failed and not xfail):
# 添加allure报告截图
with allure.step("添加失败截图。。"):
# 使用allure自带的添加附件的方法:三个参数分别为:源文件、文件名 、 文件类型
allure.attach(driver.get_screenshot_as_png(),"失败截图",allure.attachment_type.PNG)
import os
import pytest
def run():
pytest.main(['-v','--alluredir','./result','--clean-alluredir','--allure-no-capture'])
os.system('allure generate ./result -o ./report_allure/ --clean')
if __name__ == '__main__':
run()
4.PO模式优化,元素定位分离
#长流程复杂业务,页面元素封装界定不清晰以页面为单位,存储页面元素定位代码
# 定义关键字驱动类/工具类/基类
from selenium import webdriver
class WebKeys:
# 构造方法,用于接收driver对象
def __init__(self,driver):
self.driver = driver
# 编代码用,写完删掉
# self.driver = webdriver.Chrome()
# 打开浏览器
def open(self,url):
self.driver.get(url)
# 元素定位
def locator(self,name,value):
""" :param name: 元素定位的方式 :param value: 元素定位的路径 :return: """
el = self.driver.find_element(name,value)
# 将定位的元素框出来
self.locate_station(el)
return el
# 显示定位的地方,方便确认定位的位置
def locate_station(self,element):
self.driver.execute_script(
"arguments[0].setAttribute('style',arguments[1]);",
element,
"border: 2px solid green" #边框,green绿色
)
""" 淘宝首页登录 https://login.taobao.com/ page_indexLogin """
# 用户名
page_indexLogin_user = ['id','fm-login-id']
# 密码
page_indexLogin_pwd = ['id','fm-login-password']
# 登录按钮
page_indexLogin_loginBtn = ['xpath', "//div[@class='fm-btn']"]
from class36.p05.key_world.keyword import WebKeys
from class36.p05.page import allPages
class CatLoginPage(WebKeys):
#登录操作
def login(self,username,password):
self.open("https://cart.taobao.com/cart.htm")
# 定位到账户名文本框
# self.locator(allPages.page_indexLogin_user[0],allPages.page_indexLogin_user[1]).send_keys(username)
self.locator(*allPages.page_indexLogin_user).send_keys(username)
# 定位到密码框
self.locator(*allPages.page_indexLogin_pwd).send_keys(password)
# 点击方式
self.locator(*allPages.page_indexLogin_loginBtn).click()
# 购物车登录
import pytest
# 跳过用例
from class36.p05.key_world.keyword import WebKeys
from class36.p05.page.cartLogin_page import CatLoginPage
@pytest.mark.skip
def test_cart_login(browser):
# 购物车登录
# 打开淘宝购物车的登陆页面
browser.get("https://cart.taobao.com/cart.htm")
# 定位到账户名文本框
browser.find_element('id','fm-login-id').send_keys('云纱璃英')
# 定位到密码
browser.find_element('id','fm-login-password').send_keys('xxx')
# 点击登陆按钮
browser.find_element('xpath',"//div[@class='fm-btn']").click()
# 使用关键字驱动模式实现购物车登录
@pytest.mark.skip
def test_case_login2(browser):
# 初始化工具类,传递浏览器对象
wk = WebKeys(browser)
wk.open("https://cart.taobao.com/cart.htm")
# 定位到账户名文本框
wk.locator('id','fm-login-id').send_keys('云纱璃英')
# 定位到密码框
wk.locator('id','fm-login-password').send_keys('xxx')
# 点击方式
wk.locator('xpath',"//div[@class='fm-btn']").click()
# 使用PO模式实现购物车登录
def test_case_login3(browser):
# 初始化页面对象
cartLogin = CatLoginPage(browser)
cartLogin.login('云纱璃英','xxx')
import os
from time import sleep
import allure
import pytest
# 每个用例如果引用了这个fixture,都会在用例执行前执行一下这个fixture
from selenium import webdriver
@pytest.fixture(scope='function')
def browser():
""" 全局定义浏览器驱动,方便下面的hook函数引用driver :return: """
global driver
os.popen("d:/chrome.bat")
sleep(3)
# 加载浏览器驱动
options = webdriver.ChromeOptions()
options.debugger_address = '127.0.0.1:9222'
driver = webdriver.Chrome(options=options)
sleep(2)
# 隐式等待10秒
driver.implicitly_wait(10)
""" yield之前的代码是用例前置 yield之后的代码是用例后置 """
yield driver
# 浏览器退出
""" 这种debug模式,下面的方法无法退出浏览器 driver.quit() driver.close() """
# 通过命令杀死进程
os.system('taskkill /im chromedriver.exe /F')
os.system('taskkill /im chrome.exe /F')
""" 装饰器@pytest.hookimpl(hookwrapper=True)等价于@pytest.mark.hookwrapper的作用: a.可以获取测试用例的信息,比如用例函数的描述 b.可以获取测试用例的结果,yield,返回一个result对象 """
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport():
# 可以获取测试用例的执行结果,yield,返回一个result对象
out = yield
""" 从返回一个result对象(out)获取调用结果的测试报告,返回一个report对象 report对象的属性 包括when(setup,call,teardown三个值)、nodeid(测试用例的名字)、 outcome(用例的执行结果:passed,failed) """
report = out.get_result()
# 仅仅获取用例call阶段的执行结果,不包含setup和teardown
if report.when == 'call':
# 获取用例call执行结果为结果为失败的情况
xfail = hasattr(report,"wasfail")
if(report.skipped and xfail) or (report.failed and not xfail):
# 添加allure报告截图
with allure.step("添加失败截图。。"):
# 使用allure自带的添加附件的方法:三个参数分别为:源文件、文件名 、 文件类型
allure.attach(driver.get_screenshot_as_png(),"失败截图",allure.attachment_type.PNG)
import os
import pytest
def run():
pytest.main(['-v','--alluredir','./result','--clean-alluredir','--allure-no-capture'])
os.system('allure generate ./result -o ./report_allure/ --clean')
if __name__ == '__main__':
run()
5.PO模式优化,流程复用和流程备注
# 定义关键字驱动类/工具类/基类
from selenium import webdriver
class WebKeys:
# 构造方法,用于接收driver对象
def __init__(self,driver):
self.driver = driver
# 编代码用,写完删掉
# self.driver = webdriver.Chrome()
# 打开浏览器
def open(self,url):
self.driver.get(url)
# 元素定位
def locator(self,name,value):
""" :param name: 元素定位的方式 :param value: 元素定位的路径 :return: """
el = self.driver.find_element(name,value)
# 将定位的元素框出来
self.locate_station(el)
return el
# 显示定位的地方,方便确认定位的位置
def locate_station(self,element):
self.driver.execute_script(
"arguments[0].setAttribute('style',arguments[1]);",
element,
"border: 2px solid green" #边框,green绿色
)
# 获取title,获取网页标题栏
def get_title(self):
return self.driver.title
""" 淘宝首页登录 https://login.taobao.com/ page_indexLogin """
# 用户名
page_indexLogin_user = ['id','fm-login-id']
# 密码
page_indexLogin_pwd = ['id','fm-login-password']
# 登录按钮
page_indexLogin_loginBtn = ['xpath', "//div[@class='fm-btn']"]
""" 购物车详情页 https://cart.taobao.com/cart.htm page_cartDetail """
# 全选按钮
page_cartDetail_selectAll = ["id","J_SelectAll1"]
# 结算按钮
page_cartDetail_payBtn = ["xpath","//div[@class='btn-area']"]
# 提交订单按钮
page_cartDetail_submitBtn = ["link text","提交订单"]
# 删除按钮
page_cartDetail_delBtn = ["link text","删除"]
# 关闭按钮
page_cartDetail_closeBtn = ["partial link text","闭"]
# 确定按钮
page_cartDetail_confirmBtn = ["partial link text","确"]
# 颜色分类栏
page_cartDetail_colorClum = "//p[@class='sku-line']"
# SKU修改按钮
page_cartDetail_modifyBtn = ["xpath","//span[text()='修改']"]
# 颜色分类
page_cartDetail_colors = ["xpath","//div[@class='sku-edit-content']/div/dl/dd/ul/li[2]"]
# 颜色分类确认按钮
page_cartDetail_colorsConfirmBtn = ["xpath","//div[@class='sku-edit-content']/div/p/a"]
# 移入收藏夹
page_cartDetail_favorites = ["link text","移入收藏夹"]
# 单一商品信息栏
page_cartDetail_singleGoodsClum = "//div[@class='order-content']"
# 相似宝贝超链接
page_cartDetail_similarGoodsLink = ["link text","相似宝贝"]
# 翻页按钮
# page_cartDetail_simlarGoddsNext = ["xpath","//a[@class='fss-navigator fss-next J_fs_next ']"]
page_cartDetail_simlarGoddsNext = ["xpath","//a[contains(@class,'J_fs_next')]"]
# 相似宝贝
page_cartDetail_simlarGoods = ["xpath","//div[@class='find-similar-body']/div/div/div[2]"]
from time import sleep
import allure
from class36.p06.key_world.keyword import WebKeys
# 购车页面
from class36.p06.page import allPages
from class36.p06.page.cartLogin_page import CatLoginPage
class CartPage(WebKeys):
# 商品结算
@allure.step("商品结算")
def pay(self):
# 在报告里展示代码路径,方便查找
with allure.step("流程代码路径: %s" % __file__):
pass
# 复用登录流程
loginPage = CatLoginPage(self.driver)
loginPage.login('云纱璃英','xxx')
with allure.step("点击全选按钮"):
self.locator(*allPages.page_cartDetail_selectAll).click()
sleep(1)
with allure.step("点击结算按钮"):
self.locator(*allPages.page_cartDetail_payBtn).click()
sleep(1)
# 以页面为单位,封装常用的行为操作代码和元素定位代码
from time import sleep
import allure
from class36.p06.key_world.keyword import WebKeys
from class36.p06.page import allPages
class CatLoginPage(WebKeys):
#登录操作
@allure.step("登录")
def login(self,username,password):
# 在报告里展示代码路径,方便查找
with allure.step("流程代码路径: %s"%__file__):
pass
with allure.step("打开网页")
self.open("https://cart.taobao.com/cart.htm")
with allure.step("输入账户"):
# 定位到账户名文本框
# self.locator(allPages.page_indexLogin_user[0],allPages.page_indexLogin_user[1]).send_keys(username)
self.locator(*allPages.page_indexLogin_user).send_keys(username)
with allure.step("输入密码"):
# 定位到密码框
self.locator(*allPages.page_indexLogin_pwd).send_keys(password)
with allure.step("点击登录"):
# 点击方式
self.locator(*allPages.page_indexLogin_loginBtn).click()
sleep(2)
# 登录后的结果检查
result = self.get_title()
expect = "购物车"
with allure.step("结果检查:(预期){1} in 实际{0}".format(result,expect)):
assert expect in result
# 购物车登录
import pytest
# 跳过用例
from class36.p06.key_world.keyword import WebKeys
from class36.p06.page.cartLogin_page import CatLoginPage
from class36.p06.page.cart_page import CartPage
@pytest.mark.skip
def test_cart_login(browser):
# 购物车登录
# 打开淘宝购物车的登陆页面
browser.get("https://cart.taobao.com/cart.htm")
# 定位到账户名文本框
browser.find_element('id','fm-login-id').send_keys('云纱璃英')
# 定位到密码
browser.find_element('id','fm-login-password').send_keys('xxx')
# 点击登陆按钮
browser.find_element('xpath',"//div[@class='fm-btn']").click()
# 使用关键字驱动模式实现购物车登录
@pytest.mark.skip
def test_case_login2(browser):
# 初始化工具类,传递浏览器对象
wk = WebKeys(browser)
wk.open("https://cart.taobao.com/cart.htm")
# 定位到账户名文本框
wk.locator('id','fm-login-id').send_keys('云纱璃英')
# 定位到密码框
wk.locator('id','fm-login-password').send_keys('xxx')
# 点击方式
wk.locator('xpath',"//div[@class='fm-btn']").click()
# 使用PO模式实现购物车登录
def test_case_login3(browser):
# 初始化页面对象
cartLogin = CatLoginPage(browser)
cartLogin.login('云纱璃英','xxx')
# 实现购物车的结算
def test_case_pay(browser):
# 初始化购物车页面对象
cartPage = CartPage(browser)
cartPage.pay()
import os
from time import sleep
import allure
import pytest
# 每个用例如果引用了这个fixture,都会在用例执行前执行一下这个fixture
from selenium import webdriver
@pytest.fixture(scope='function')
def browser():
""" 全局定义浏览器驱动,方便下面的hook函数引用driver :return: """
global driver
os.popen("d:/chrome.bat")
sleep(3)
# 加载浏览器驱动
options = webdriver.ChromeOptions()
options.debugger_address = '127.0.0.1:9222'
driver = webdriver.Chrome(options=options)
sleep(2)
# 隐式等待10秒
driver.implicitly_wait(10)
""" yield之前的代码是用例前置 yield之后的代码是用例后置 """
yield driver
# 浏览器退出
""" 这种debug模式,下面的方法无法退出浏览器 driver.quit() driver.close() """
# 通过命令杀死进程
os.system('taskkill /im chromedriver.exe /F')
os.system('taskkill /im chrome.exe /F')
""" 装饰器@pytest.hookimpl(hookwrapper=True)等价于@pytest.mark.hookwrapper的作用: a.可以获取测试用例的信息,比如用例函数的描述 b.可以获取测试用例的结果,yield,返回一个result对象 """
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport():
# 可以获取测试用例的执行结果,yield,返回一个result对象
out = yield
""" 从返回一个result对象(out)获取调用结果的测试报告,返回一个report对象 report对象的属性 包括when(setup,call,teardown三个值)、nodeid(测试用例的名字)、 outcome(用例的执行结果:passed,failed) """
report = out.get_result()
# 仅仅获取用例call阶段的执行结果,不包含setup和teardown
if report.when == 'call':
# 获取用例call执行结果为结果为失败的情况
xfail = hasattr(report,"wasfail")
if(report.skipped and xfail) or (report.failed and not xfail):
# 添加allure报告截图
with allure.step("添加失败截图。。"):
# 使用allure自带的添加附件的方法:三个参数分别为:源文件、文件名 、 文件类型
allure.attach(driver.get_screenshot_as_png(),"失败截图",allure.attachment_type.PNG)
import os
import pytest
def run():
pytest.main(['-v','--alluredir','./result','--clean-alluredir','--allure-no-capture'])
os.system('allure generate ./result -o ./report_allure/ --clean')
if __name__ == '__main__':
run()
边栏推荐
- What is distributed and clustered?What is the difference?
- addressable in Golang
- MultipartFile file upload
- 浅识Flutter 基本组件之CheckBox组件
- LeetCode简单题之找到和最大的长度为 K 的子序列
- els block to the left to move the conditional judgment
- Problems that need to be solved in distributed system architecture
- 遗留系统的自动化策略
- els 方块向右移动边界判断、向下加速
- [Dynamic programming] Maximum sum of consecutive subarrays
猜你喜欢

大小端模式

【C语言】预处理操作

Detailed explanation of TCP (2)

The application and practice of mid-to-platform brand advertising platform

Several common errors when using MP

IIR filter and FIR filter

Know the showTimePicker method of the basic components of Flutter

品牌广告投放平台的中台化应用与实践

LeetCode中等题之分数加减运算

12 Disk related commands
随机推荐
自己的一些思考
Key Technologies of Interface Testing
VS QT——ui不显示新添加成员(控件)||代码无提示
【编译原理】递归下降语法分析设计原理与实现
Redis implements distributed locks
Local area network computer hardware information collection tool
SIP Protocol Standard and Implementation Mechanism
原子操作 CAS
What is a distributed lock?Three ways of implementing distributed lock
IIR filter and FIR filter
Know the showTimePicker method of the basic components of Flutter
LeetCode简单题之找到和最大的长度为 K 的子序列
els block to the left to move the conditional judgment
IDEA comment report red solution
Day32 LeetCode
Modbus on AT32 MCUs
TCP详解(二)
[Compilation principle] Lexical analysis program design principle and implementation
Automation strategies for legacy systems
安全20220715