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.find_element_by_xpath("//ul/li[2]/div/div[2]/div[1]/a")
handls = driver.window_handles
print(handls)
driver.switch_to.window(handls[1])
driver.find_element_by_xpath('//ul[@data-property="颜色分类"]/li').click()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
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():
out = yield
"""
从返回一个result对象(out)获取调用结果的测试报告,返回一个report对象
report对象的属性
包括when(setup,call,teardown三个值)、nodeid(测试用例的名字)、
outcome(用例的执行结果:passed,failed)
"""
report = out.get_result()
if report.when == 'call':
xfail = hasattr(report,"wasfail")
if(report.skipped and xfail) or (report.failed and not xfail):
with allure.step("添加失败截图。。"):
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()

- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
2.关键字驱动
from selenium import webdriver
class WebKeys:
def __init__(self,driver):
self.driver = driver
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"
)
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
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)
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():
out = yield
"""
从返回一个result对象(out)获取调用结果的测试报告,返回一个report对象
report对象的属性
包括when(setup,call,teardown三个值)、nodeid(测试用例的名字)、
outcome(用例的执行结果:passed,failed)
"""
report = out.get_result()
if report.when == 'call':
xfail = hasattr(report,"wasfail")
if(report.skipped and xfail) or (report.failed and not xfail):
with allure.step("添加失败截图。。"):
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()

- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
3.PO模式
from selenium import webdriver
class WebKeys:
def __init__(self,driver):
self.driver = driver
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"
)
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()
def test_case_login3(browser):
cartLogin = CatLoginPage(browser)
cartLogin.login('云纱璃英','xxx')
import os
from time import sleep
import allure
import pytest
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)
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():
out = yield
"""
从返回一个result对象(out)获取调用结果的测试报告,返回一个report对象
report对象的属性
包括when(setup,call,teardown三个值)、nodeid(测试用例的名字)、
outcome(用例的执行结果:passed,failed)
"""
report = out.get_result()
if report.when == 'call':
xfail = hasattr(report,"wasfail")
if(report.skipped and xfail) or (report.failed and not xfail):
with allure.step("添加失败截图。。"):
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()

- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
4.PO模式优化,元素定位分离
from selenium import webdriver
class WebKeys:
def __init__(self,driver):
self.driver = driver
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"
)
"""
淘宝首页登录
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).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()
def test_case_login3(browser):
cartLogin = CatLoginPage(browser)
cartLogin.login('云纱璃英','xxx')
import os
from time import sleep
import allure
import pytest
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)
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():
out = yield
"""
从返回一个result对象(out)获取调用结果的测试报告,返回一个report对象
report对象的属性
包括when(setup,call,teardown三个值)、nodeid(测试用例的名字)、
outcome(用例的执行结果:passed,failed)
"""
report = out.get_result()
if report.when == 'call':
xfail = hasattr(report,"wasfail")
if(report.skipped and xfail) or (report.failed and not xfail):
with allure.step("添加失败截图。。"):
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()

- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
5.PO模式优化,流程复用和流程备注
from selenium import webdriver
class WebKeys:
def __init__(self,driver):
self.driver = driver
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"
)
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']"
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[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).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()
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
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)
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():
out = yield
"""
从返回一个result对象(out)获取调用结果的测试报告,返回一个report对象
report对象的属性
包括when(setup,call,teardown三个值)、nodeid(测试用例的名字)、
outcome(用例的执行结果:passed,failed)
"""
report = out.get_result()
if report.when == 'call':
xfail = hasattr(report,"wasfail")
if(report.skipped and xfail) or (report.failed and not xfail):
with allure.step("添加失败截图。。"):
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()

- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
- 243
- 244
- 245
- 246
- 247
- 248
- 249
- 250
- 251
- 252
- 253
- 254
- 255
- 256
- 257
- 258
- 259
- 260
- 261
- 262
- 263
- 264
- 265
- 266
- 267
- 268
- 269
- 270
- 271
- 272
- 273
- 274
- 275
- 276
- 277
- 278
- 279
- 280
- 281
- 282
- 283
- 284
- 285
- 286
- 287
- 288
- 289
- 290
- 291