【问题标题】:How to capture screenshots with Selenium when running tests on a Jenkins agent在 Jenkins 代理上运行测试时如何使用 Selenium 捕获屏幕截图
【发布时间】:2018-12-01 08:05:30
【问题描述】:

现在我正在使用 pytest 和 selenium 在 Jenkins 上运行测试。当我在本地运行下面的代码时,它会在每次测试后创建一个屏幕截图,效果很好。

@pytest.mark.hookwrapper
def pytest_runtest_makereport(item, call):
    pytest_html = item.config.pluginmanager.getplugin('html')
    outcome = yield
    report = outcome.get_result()
    if 'selenium' not in item.fixturenames or 'selenium' not in item.funcargs:
        return

    driver = item.funcargs['selenium']
    extra = getattr(report, 'extra', [])
    if report.when == 'call' or report.when == "setup":
        extra.append(pytest_html.extras.url(driver.current_url, name = "Go To URL Tested"))
        if not report.skipped:
            report_directory = os.path.dirname(item.config.option.htmlpath)
            file_name = f"{item.originalname}.png"
            screenshot_was_saved = driver.get_screenshot_as_file(f"{report_directory}\\{file_name}")
            if file_name:
                report.screenshot = html.a(html.img(src = file_name, onclick = "window.open(this.src)", align = "right", style = "max-height: 150px;"), href = "#")
        report.extra = extra

但是,当我在 Jenkins 上的构建管道中运行相同的代码时,不会创建图像。从get_screenshot_as_file 方法返回的值是False。没有返回异常并且测试通过。 Jenkins 代理有权写入文件,因为创建了我要转储到 HTML 报告的目录。报告的 index.html 也会被创建。除了图像,一切都在那里。

有没有人有类似的经验?我什至尝试在无头模式下运行 chrome 驱动程序,但这并没有什么不同。

任何帮助将不胜感激。

【问题讨论】:

  • 你的 selenium 代码可以在没有 jenkins 的情况下工作吗?
  • 是的,它可以在 Jenkins 之外正常工作。当我 RDP 进入 Jenkins 服务器并在 PowerShell 控制台中运行 pytest 时,它也可以正常工作。
  • 很可能是无头模式的问题。
  • 尝试使用driver.save_screenshot(),甚至创建一个虚拟jenkins 作业,尝试通过不同的库捕获屏幕,例如PIL.ImageGrab.grab()pyautogui.screenshot(),你会得到图片位吗?如果不是,产生的错误信息是什么?
  • @arjabbar 你是在slave上运行你的测试吗?根据我的经验,这可能是问题

标签: python selenium jenkins selenium-webdriver pytest


【解决方案1】:

这对我有用

@pytest.mark.hookwrapper
def pytest_runtest_makereport(item, call):
    """
    Extends the PyTest Plugin to take and embed screenshots 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':
        xfail = hasattr(report, 'wasxfail')
        if (report.skipped and xfail) or (report.failed and not xfail):
            report_directory = os.path.dirname(item.config.option.htmlpath)
            file_name = str(int(round(time.time() * 1000))) + ".png"
            # full_path = os.path.join("C:\Screenshots", file_name)
            full_path = os.path.join(report_directory, file_name)
            if item.funcargs.get('driver'):
                print(f"[INFO] screenshot: {full_path}")
                item.funcargs['driver'].get_screenshot_as_file(full_path)
                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

【讨论】:

  • 它不起作用:“驱动程序”(我尝试使用“硒”)是未知的
  • 它确实有效,你需要放入 conftest.py (pytest)
  • ... 这个条件返回 False : if item.funcargs.get('driver'):
  • 我有一个fixture,它产生一个驱动实例@pytest.fixture() def driver(request): .....
猜你喜欢
  • 2011-10-13
  • 2015-04-03
  • 2017-04-02
  • 2011-10-13
  • 2018-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-26
相关资源
最近更新 更多