【问题标题】:How to add a screenshot to allure report with python?如何使用python添加屏幕截图来诱惑报告?
【发布时间】:2015-04-28 20:25:43
【问题描述】:

我有这个代码:

# coding: utf-8
from selenium import webdriver
import pytest
import allure


@pytest.yield_fixture(scope='session')
def driver():
    _driver = webdriver.PhantomJS()
    yield _driver
    _driver.quit()


def test_ya(driver):
    with allure.step('open ya.ru and take screenshot'):
        driver.get('http://ya.ru/')            
        allure.attach('screenshot', driver.get_screenshot_as_png(), type='png')

我尝试截取屏幕截图并将其保存到诱惑报告中, 执行后我有:

>       with self._attachfile("%s-attachment.%s" % (uuid.uuid4(), attach_type.extension)) as f:
            if isinstance(body, text_type):
E           AttributeError: 'str' object has no attribute 'extension'

我该如何解决这个问题?

【问题讨论】:

    标签: python webdriver pytest allure


    【解决方案1】:

    诱惑2

    from allure_commons.types import AttachmentType
    
    allure.attach(driver.get_screenshot_as_png(), name="Screenshot", attachment_type=AttachmentType.PNG)
    

    【讨论】:

    • 我认为导入应该是from allure import attachment_type
    • 我的错。这是from allure_commons.types import AttachmentType
    • 这不起作用。我在一个 Behave 框架中,所以这可能是问题所在,但在任何失败的步骤之后,我都有以下代码:allure.attach(context.browser.driver.get_screenshot_as_png(), name='Screenshot', attachment_type=AttachmentType.PNG) 并且魅力报告中没有显示图像文件
    • @TuanChau 我已经尝试了所有可用的“get_screenshot”和save_screenshot。我正在调试以确保正在创建我的屏幕截图,但附件根本没有显示在报告中。这就是我目前所拥有的:allure.attach(context.browser.driver.get_screenshot_as_png(), name=screen_path, attachment_type=AttachmentType.PNG) 其中context.browser.driver 引用了我已经声明的 Behave chromedriver。另外,screen_path 是新创建图像的路径
    • @TuanChau 在我们运行get_screenshot_as_png之后,所附的屏幕截图应该在哪里??
    【解决方案2】:

    您需要使用allure module attachment type constant,而不是将type 设置为字符串png,这是一个定义了extension 属性的Enum

    from allure.constants import AttachmentType
    
    allure.attach('screenshot', driver.get_screenshot_as_png(), type=AttachmentType.PNG)
    

    【讨论】:

    • 那么这个屏幕截图在哪里?自动在 Allure-Results 目录下?
    • 我可以在魅力报告中附加一些路径的现有屏幕截图吗?
    【解决方案3】:

    在您的 conftest.py 中添加以下内容,确保您也已经有一个驱动程序夹具:

    codeblock

    【讨论】:

    • 您好!仅供参考,您可以通过用三个 ` 标记将代码块添加到您的答案中。这可能比看图像更容易阅读。我认为还有一种嵌入图像的方法,但不知道该怎么做。
    • 不,请不要嵌入代码图像。那时没有人可以测试它。
    猜你喜欢
    • 1970-01-01
    • 2021-12-27
    • 1970-01-01
    • 1970-01-01
    • 2018-11-28
    • 1970-01-01
    • 2013-01-20
    • 2019-08-16
    • 1970-01-01
    相关资源
    最近更新 更多