9

I have this code:

# 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')

and I try to take a screenshot and save it to allure report, after execution I have:

>       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'

How can I fix this?

3 Answers3

14

For allure 2

from allure_commons.types import AttachmentType

allure.attach(driver.get_screenshot_as_png(), name="Screenshot", attachment_type=AttachmentType.PNG)
Tuan Chau
  • 1,243
  • 1
  • 16
  • 30
  • I think the import should be `from allure import attachment_type`. – Jaakko Nov 01 '17 at 08:36
  • My bad. It's `from allure_commons.types import AttachmentType` – Tuan Chau Nov 01 '17 at 09:13
  • 1
    This does not work. I'm in a Behave framework so that may be the issue but after any failed steps I have the following code: `allure.attach(context.browser.driver.get_screenshot_as_png(), name='Screenshot', attachment_type=AttachmentType.PNG)` and no image file shows up on the allure report – partydog Dec 20 '17 at 17:24
  • @TuanChau i've tried every 'get_screenshot' and `save_screenshot` available. I'm debugging to make sure that my screenshot is being created but the attachment simply isn't showing up in the report. here's what I have at the moment: `allure.attach(context.browser.driver.get_screenshot_as_png(), name=screen_path, attachment_type=AttachmentType.PNG)` where `context.browser.driver` is referencing a Behave chromedriver that I've already declared. Also, `screen_path` is the path to the newly created image – partydog Dec 28 '17 at 17:31
  • @TuanChau where is the attached screenshot supposed to live after we run `get_screenshot_as_png`?? – partydog Dec 28 '17 at 18:22
  • It's must be a byte array on memory, I guess. – Tuan Chau Dec 29 '17 at 11:50
7

Instead of setting the type as a string png, you need to use allure module attachment type constant, which is an Enum with extension attribute defined:

from allure.constants import AttachmentType

allure.attach('screenshot', driver.get_screenshot_as_png(), type=AttachmentType.PNG)
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
0

in your conftest.py add the following, make sure you already have a driver fixture as well:

codeblock

Jb254
  • 1
  • 1
  • Hello! FYI, you can add code blocks to your answer by surrounding it with triple ` marks. That might make it a bit easier to read than looking at an image. I think there is also a way to embed images, but not sure how to do that. – erncyp Jun 15 '20 at 09:59
  • No, please don't embed code image. No one could test it then. – dboy Jun 15 '20 at 10:07