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
| import pytest from selenium import webdriver from selenium.webdriver.common.desired_capabilities import DesiredCapabilities from py._xmlgen import html
_driver = None
@pytest.fixture(scope='session', autouse=True) def driver(): global _driver print(11111) ip = "远程ip" server = "http://%s:7777/wd/hub" % ip _driver = webdriver.Remote( command_executor="http://%s:7777/wd/hub" % ip, desired_capabilities=DesiredCapabilities.CHROME ) yield _driver _driver.close() _driver.quit()
@pytest.mark.hookwrapper def pytest_runtest_makereport(item): """ 当测试失败的时候,自动截图,展示到html报告中 :param item: """ if not _driver: return pytest_html = item.config.pluginmanager.getplugin('html') outcome = yield report = outcome.get_result() report.description = str(item.function.__doc__) extra = getattr(report, 'extra', [])
if report.when == 'call' or report.when == "setup": xfail = hasattr(report, 'wasxfail') if (report.skipped and xfail) or (report.failed and not xfail): screen_img = _capture_screenshot() if screen_img: html = '<div><img src="data:image/png;base64,%s" alt="screenshot" style="width:1024px;height:768px;" ' \ 'onclick="window.open(this.src)" align="right"/></div>' % screen_img extra.append(pytest_html.extras.html(html)) report.extra = extra
def pytest_html_results_table_header(cells): cells.insert(1, html.th('用例名称')) cells.insert(2, html.th('Test_nodeid')) cells.pop(2)
def pytest_html_results_table_row(report, cells): cells.insert(1, html.td(report.description)) cells.insert(2, html.td(report.nodeid)) cells.pop(2)
def pytest_html_results_table_html(report, data): if report.passed: del data[:] data.append(html.div('通过的用例未捕获日志输出.', class_='empty log'))
def pytest_html_report_title(report): report.title = "pytest示例项目测试报告"
def _capture_screenshot(): """ 截图保存为base64 :return: """ return _driver.get_screenshot_as_base64()
|