簡體   English   中英

AttributeError: 'NoneType' object 沒有屬性 'get_screenshot_as_file'

[英]AttributeError: 'NoneType' object has no attribute 'get_screenshot_as_file'

import pytest
from selenium import webdriver
driver = None


def pytest_addoption(parser):
    parser.addoption("--browser_name", action="store", default="chrome")
    parser.addoption("--url", action="store", default="staging")


@pytest.fixture(scope="class")
def setup(request):
    global driver
    browser_name = request.config.getoption("browser_name")
    url = request.config.getoption("url")
    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_argument("--disable-notifications")
    if browser_name == 'chrome':
        driver = webdriver.Chrome(executable_path="C:\\Softwares\\ChromeDriver\\chromedriver_win32_91version"
                                                  "\\chromedriver.exe", options=chrome_options)
    elif browser_name == "firefox":
        driver = webdriver.Firefox(executable_path="C:\\Softwares\\GeckoDriver\\geckodriver.exe", options=chrome_options)
    elif browser_name == "IE":
        driver = webdriver.Ie(executable_path="C:\\Softwares\\IEDriver\\IEDriverServer.exe", options=chrome_options)
    driver.maximize_window()
    if url == 'staging':
        url = "URL"
    elif url == 'production':
        url = "URL"
    driver.get(url)
    request.cls.driver = driver
    yield
    driver.close()


@pytest.mark.hookwrapper
def pytest_runtest_makereport(item):
    """
        Extends the PyTest Plugin to take and embed screenshot in html report, whenever test fails.
        :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"
            _capture_screenshot(file_name)
            if file_name:
                html = '<div><img src="%s" alt="screenshot" style="width:304px;height:228px;" ' \
                       'onclick="window.open(this.src)" align="right"/></div>' % file_name
                extra.append(pytest_html.extras.html(html))
        report.extra = extra


def _capture_screenshot(name):
    driver.get_screenshot_as_file(name)

這是我的 conftest.py 文件。 我全局初始化了驅動程序,它將在 setup 夾具下定義。 我有自定義報告代碼(它會在測試失敗時捕獲屏幕截圖)。 我正在使用驅動程序 object 來捕獲屏幕截圖,但由於此“AttributeError: 'NoneType' object has no attribute 'get_screenshot_as_file'”錯誤,我無法運行測試。

提前致謝

也許嘗試將您的 function 捕獲屏幕截圖更改為夾具並將您的驅動程序(在您的情況下設置夾具)放入 function 中,如下所示:

@pytest.fixture(autouse=True)
def _capture_screenshot(name, setup):
    yield
    setup.get_screenshot_as_file(name)

您也可以僅在使用yield運算符完成測試時調用捕獲屏幕截圖。 希望它有點幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM