conftest.py 里添加截圖的邏輯
# coding:utf-8
import os
from selenium import webdriver
import pytest
from common.settings import PROXY_SERVER
driver = None
@pytest.mark.hookwrapper
def pytest_runtest_makereport(item):
"""
當測試失敗的時候,自動截圖,展示到html報告中
:param item:
"""
pytest_html = item.config.pluginmanager.getplugin('html')
outcome = yield
report = outcome.get_result()
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):
file_name = report.nodeid.replace("::", "_") + ".png"
screen_img = _capture_screenshot()
if file_name:
html = '<div><img src="data:image/png;base64,%s" alt="screenshot" style="width:600px;height:300px;" ' \
'onclick="window.open(this.src)" align="right"/></div>' % screen_img
extra.append(pytest_html.extras.html(html))
report.extra = extra
def _capture_screenshot():
'''
截圖保存為base64,展示到html中
:return:
'''
return driver.get_screenshot_as_base64()
@pytest.fixture(scope='session', autouse=True)
def browser():
global driver
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--proxy-server={}'.format(PROXY_SERVER))
if os.getenv("DOCKER") == "True":
chrome_options.add_argument("--headless")
chrome_options.add_argument("--disable-gpu")
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--ignore-certificate-errors")
driver = webdriver.Chrome(options=chrome_options)
driver.implicitly_wait(30)
yield driver
driver.quit()
case 級別調用時
from page_object.application.app_create import AppCreatePage
class TestCreateApp():
def 測試創建應用(self, browser):
create_page = AppCreatePage(browser)
create_page.open_create_app()
create_page.create_app()
這里吧driver當做一個參數傳給類,方便於page object模式開發寫UI自動化
