如何在 python pytest html 中添加屏幕截图
How can I add a screenshot in python pytest html
首先我尝试这个 post 的解决方案
我得到了这段代码
import os
import selenium
from selenium import webdriver
import time
from datetime import datetime
import pytest
from selenium import webdriver as selenium_webdriver
from selenium.webdriver.chrome.options import Options
@pytest.fixture()
def setup():
driver = webdriver.Chrome(executable_path=r'E:\Python Projects\StackField\testCases\chromedriver.exe')
return driver
@pytest.mark.hookwrapper
def pytest_runtest_makereport(item, call):
timestamp = datetime.now().strftime('%H-%M-%S')
pytest_html = item.config.pluginmanager.getplugin('html')
outcome = yield
report = outcome.get_result()
extra = getattr(report, 'extra', [])
if report.when == 'call':
feature_request = item.funcargs['request']
driver = feature_request.getfuncargvalue('browser')
driver.save_screenshot('D:/report/scr'+timestamp+'.png')
extra.append(pytest_html.extras.image('D:/report/scr'+timestamp+'.png'))
# always add url to report
extra.append(pytest_html.extras.url('http://www.example.com/'))
xfail = hasattr(report, 'wasxfail')
if (report.skipped and xfail) or (report.failed and not xfail):
# only add additional html on failure
extra.append(pytest_html.extras.image('D:/report/scr.png'))
extra.append(pytest_html.extras.html('<div>Additional HTML</div>'))
report.extra = extra
当我 运行 使用此命令时:
pytest --html=report.html --self-contained-html testCases/test_basic.py
它 returns 我是一个内部错误:
内部错误> feature_request = item.funcargs['request']
内部错误> KeyError: 'request'
然后我尝试了这个方法:
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(item, call):
pytest_html = item.config.pluginmanager.getplugin("html")
outcome = yield
report = outcome.get_result()
extra = getattr(report, "extra", [])
if report.when == "call":
# always add url to report
extra.append(pytest_html.extras.url("https://sprint.moravis.com/run/val/adchef/a0bfkgb"))
xfail = hasattr(report, "wasxfail")
if (report.skipped and xfail) or (report.failed and not xfail):
# only add additional html on failure
report_directory = os.path.dirname(item.config.option.htmlpath)
# file_name = str(int(round(time.sleep() *1000)))+".png"
file_name = report.nodeid.replace(": :", "_") + ".png"
destinationFile = os.path.join(report_directory, file_name)
driver.save_screenshot(destinationFile)
if file_name:
html = '<div><img src ="%s" alt="screenshot" style="width:300px:height=200px"' '\'onclick ="window.open(this.src)" align="right"/></div>' % file_name
extra.append(pytest_html.extras.html(html))
report.extra = extra
def pytest_html_report_title(report):
report.title = "Stackfield report"
我收到了这个错误:
内部错误> driver.save_screenshot(目标文件)
INTERNALERROR> NameError:名称 'driver' 未定义
从这里我不知道如何继续。
在此先感谢您!
只需将安装程序中的驱动程序设置为全局即可
@pytest.fixture()
def setup():
global driver
driver = webdriver.Chrome(executable_path=r'E:\Python Projects\StackField\testCases\chromedriver.exe')
return driver
您不能将固定装置传递给挂钩。
你需要做什么:
- 调用请求夹具。
- 调用启动驱动程序的夹具。
应该是这样的:
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(item, call):
pytest_html = item.config.pluginmanager.getplugin("html")
outcome = yield
report = outcome.get_result()
extra = getattr(report, "extra", [])
if report.when == "call":
if (report.skipped and xfail) or (report.failed and not xfail):
feature_request = item.funcargs["request"]
driver = feature_request.getfixturevalue("setup")
img_name = "name.png"
img_path = os.path.join("path/to/image", img_name)
driver.save_screenshot(img_path)
首先我尝试这个 post 的解决方案 我得到了这段代码
import os
import selenium
from selenium import webdriver
import time
from datetime import datetime
import pytest
from selenium import webdriver as selenium_webdriver
from selenium.webdriver.chrome.options import Options
@pytest.fixture()
def setup():
driver = webdriver.Chrome(executable_path=r'E:\Python Projects\StackField\testCases\chromedriver.exe')
return driver
@pytest.mark.hookwrapper
def pytest_runtest_makereport(item, call):
timestamp = datetime.now().strftime('%H-%M-%S')
pytest_html = item.config.pluginmanager.getplugin('html')
outcome = yield
report = outcome.get_result()
extra = getattr(report, 'extra', [])
if report.when == 'call':
feature_request = item.funcargs['request']
driver = feature_request.getfuncargvalue('browser')
driver.save_screenshot('D:/report/scr'+timestamp+'.png')
extra.append(pytest_html.extras.image('D:/report/scr'+timestamp+'.png'))
# always add url to report
extra.append(pytest_html.extras.url('http://www.example.com/'))
xfail = hasattr(report, 'wasxfail')
if (report.skipped and xfail) or (report.failed and not xfail):
# only add additional html on failure
extra.append(pytest_html.extras.image('D:/report/scr.png'))
extra.append(pytest_html.extras.html('<div>Additional HTML</div>'))
report.extra = extra
当我 运行 使用此命令时: pytest --html=report.html --self-contained-html testCases/test_basic.py
它 returns 我是一个内部错误: 内部错误> feature_request = item.funcargs['request'] 内部错误> KeyError: 'request'
然后我尝试了这个方法:
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(item, call):
pytest_html = item.config.pluginmanager.getplugin("html")
outcome = yield
report = outcome.get_result()
extra = getattr(report, "extra", [])
if report.when == "call":
# always add url to report
extra.append(pytest_html.extras.url("https://sprint.moravis.com/run/val/adchef/a0bfkgb"))
xfail = hasattr(report, "wasxfail")
if (report.skipped and xfail) or (report.failed and not xfail):
# only add additional html on failure
report_directory = os.path.dirname(item.config.option.htmlpath)
# file_name = str(int(round(time.sleep() *1000)))+".png"
file_name = report.nodeid.replace(": :", "_") + ".png"
destinationFile = os.path.join(report_directory, file_name)
driver.save_screenshot(destinationFile)
if file_name:
html = '<div><img src ="%s" alt="screenshot" style="width:300px:height=200px"' '\'onclick ="window.open(this.src)" align="right"/></div>' % file_name
extra.append(pytest_html.extras.html(html))
report.extra = extra
def pytest_html_report_title(report):
report.title = "Stackfield report"
我收到了这个错误: 内部错误> driver.save_screenshot(目标文件) INTERNALERROR> NameError:名称 'driver' 未定义
从这里我不知道如何继续。
在此先感谢您!
只需将安装程序中的驱动程序设置为全局即可
@pytest.fixture()
def setup():
global driver
driver = webdriver.Chrome(executable_path=r'E:\Python Projects\StackField\testCases\chromedriver.exe')
return driver
您不能将固定装置传递给挂钩。 你需要做什么:
- 调用请求夹具。
- 调用启动驱动程序的夹具。
应该是这样的:
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(item, call):
pytest_html = item.config.pluginmanager.getplugin("html")
outcome = yield
report = outcome.get_result()
extra = getattr(report, "extra", [])
if report.when == "call":
if (report.skipped and xfail) or (report.failed and not xfail):
feature_request = item.funcargs["request"]
driver = feature_request.getfixturevalue("setup")
img_name = "name.png"
img_path = os.path.join("path/to/image", img_name)
driver.save_screenshot(img_path)