方法代码
#将这个文件放到目录就行
conftest.py
#! /usr/bin/python
# -*- coding: utf8 -*-
import pytest
from selenium import webdriver
import os
import allure
_driver = None
@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
'''
获取每个用例状态的钩子函数
:param item:
:param call:
:return:
'''
# 获取钩子方法的调用结果
outcome = yield
rep = outcome.get_result()
# 仅仅获取用例call 执行结果是失败的情况, 不包含 setup/teardown
if rep.when == "call" and rep.failed:
mode = "a" if os.path.exists("failures") else "w"
with open("failures", mode) as f:
# let's also access a fixture for the fun of it
if "tmpdir" in item.fixturenames:
extra = " (%s)" % item.funcargs["tmpdir"]
else:
extra = ""
f.write(rep.nodeid + extra + "\n")
# 添加allure报告截图
if hasattr(_driver, "get_screenshot_as_png"):
with allure.step('添加失败截图...'):
allure.attach(_driver.get_screenshot_as_png(), "失败截图", allure.attachment_type.PNG)
@pytest.fixture(scope='session')
def browser():
# 设置options参数
option = webdriver.ChromeOptions()
# 浏览器不提供可视化界面
option.add_argument('--headless')
# 隐藏正受到自动测试软件的控制
option.add_experimental_option("excludeSwitches", ["enable-automation"])
# 指定浏览器分辨率
option.add_argument('window-size=1920x1080')
# 最大化运行/全屏窗口,不要使用全屏options=option
# option.add_argument('--start-maximized'
global _driver
if _driver is None:
_driver = webdriver.Chrome(options=option)
yield _driver
print("添加失败截图1111111")
_driver.quit()
测试用例代码
#! /usr/bin/python
# -*- coding: utf8 -*-
import time
from src.common import sllogin
import allure
from selenium.webdriver.common.by import By
def test_1(browser):
with allure.step("step1:打开登录首页"):
browser.get('http://192.168.22.31/WWWW/login.html')
with allure.step("step2:输入账号:admin"):
sllogin.golongin(browser) # 登录系统
browser.get('http://www.baidu.com')
time.sleep(0.5)
# 故意断言失败,看是否会截图
#assert browser.title == "模拟登录"
def test_2(browser):
with allure.step("step2:输入密码:123456"):
browser.find_element(By.NAME, "cerno").send_keys(u"LT7759557")
time.sleep(0.5)
# 故意断言失败,看是否会截图
assert browser.title == ""
作个记录,感谢
https://blog.csdn.net/qq_27371025/article/details/118384363