简体   繁体   中英

how to generate screenshots with allure - behave in python

I am new to Python and Selenium testing, could some one please clarify why taking screenshot and generating allure report does not work together. Attached is the sample code in my testcase step.

@then('the home page does not open with invalid username')
def assert_login_unsuccessful(context):
    try:
        error_Message = login_error_message(context)
    except:
        assert False, "Test Failed"
        context.driver.close()
    if login_error_message(context) == "Please, enter valid credentials":
        print("***Login Not Successful - Home page is not open***")
        #allure.attach('screenshot2', context.driver.get_screenshot_as_png(), type=allure.attachment_type.PNG)
        #myScreenshot = pyautogui.screenshot()
        #myScreenshot.save(r'C:\Users\lakshmi krishnaswamy\screenshots\assert_login_unsuccessful_error_message_invalid_username.png')
        assert True, "Test Passed"
        context.driver.close()

i am using the below command to run the allure behave and generate the json files

behave -f allure_behave.formatter:AllureFormatter -o reports/ features/d3a-login.feature

when i uncomment the pyautogyi screenshot part, i get the below error

C:\Users\lakshmi krishnaswamy\PycharmProjects\d3aBehaveProjectTestSuite>behave -f allure_behave.formatter:AllureFormatter -o reports/ features/d3a-login.feature

DevTools listening on ws://127.0.0.1:55231/devtools/browser/52989908-98f0-4004-a586-b68ea6d9b82d
[2956:4420:0905/131111.519:ERROR:device_event_log_impl.cc(214)] [13:11:11.518] USB: usb_device_handle_win.cc:1048 Failed to read descriptor from node connection: A device attached to the system is not functioning. (0x1F)

DevTools listening on ws://127.0.0.1:59017/devtools/browser/31cb4a4f-fa22-419b-9f4a-48490751dd7a
[18084:16428:0905/131121.810:ERROR:device_event_log_impl.cc(214)] [13:11:21.810] USB: usb_device_handle_win.cc:1048 Failed to read descriptor from node connection: A device attached to the system is not functioning. (0x1F)

DevTools listening on ws://127.0.0.1:62188/devtools/browser/249a6670-fd77-484a-9552-5dd7f1adcd0a
[948:18280:0905/131128.924:ERROR:device_event_log_impl.cc(214)] [13:11:28.924] USB: usb_device_handle_win.cc:1048 Failed to read descriptor from node connection: A device attached to the system is not functioning. (0x1F)

Failing scenarios:
  features/d3a-login.feature:17  Failure login to D3O with invalid username

0 features passed, 1 failed, 0 skipped
2 scenarios passed, 1 failed, 0 skipped
14 steps passed, 1 failed, 0 skipped, 0 undefined
Took 0m25.668s
Exception ignored in: <function Popen.__del__ at 0x000002B064F6FCA0>
Traceback (most recent call last):
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.2032.0_x64__qbz5n2kfra8p0\lib\subprocess.py", line 1055, in __del__
    self._internal_poll(_deadstate=_maxsize)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.2032.0_x64__qbz5n2kfra8p0\lib\subprocess.py", line 1457, in _internal_poll
    if _WaitForSingleObject(self._handle, 0) == _WAIT_OBJECT_0:
OSError: [WinError 6] The handle is invalid

when i uncomment allure attach, i get a smiliar error as attached below

C:\Users\lakshmi krishnaswamy\PycharmProjects\d3aBehaveProjectTestSuite>behave -f allure_behave.formatter:AllureFormatter -o reports/ features/d3a-login.feature

DevTools listening on ws://127.0.0.1:59812/devtools/browser/b1be990e-c456-4cae-b204-1e0242f55a9a
[8968:6156:0905/131625.417:ERROR:device_event_log_impl.cc(214)] [13:16:25.418] USB: usb_device_handle_win.cc:1048 Failed to read descriptor from node connection: A device attached to the system is not functioning. (0x1F)

DevTools listening on ws://127.0.0.1:49256/devtools/browser/45937e86-f1e4-4a17-a90f-7ca369dde276
[18080:10828:0905/131634.363:ERROR:device_event_log_impl.cc(214)] [13:16:34.364] USB: usb_device_handle_win.cc:1048 Failed to read descriptor from node connection: A device attached to the system is not functioning. (0x1F)

DevTools listening on ws://127.0.0.1:61518/devtools/browser/b31c1d7b-c0a5-465a-824a-82e5a3e3b59c
[23544:9780:0905/131641.853:ERROR:device_event_log_impl.cc(214)] [13:16:41.853] USB: usb_device_handle_win.cc:1048 Failed to read descriptor from node connection: A device attached to the system is not functioning. (0x1F)

Failing scenarios:
  features/d3a-login.feature:17  Failure login to D3O with invalid username

0 features passed, 1 failed, 0 skipped
2 scenarios passed, 1 failed, 0 skipped
14 steps passed, 1 failed, 0 skipped, 0 undefined
Took 0m28.658s
Exception ignored in: <function Popen.__del__ at 0x0000017B2057FCA0>
Traceback (most recent call last):
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.2032.0_x64__qbz5n2kfra8p0\lib\subprocess.py", line 1055, in __del__
    self._internal_poll(_deadstate=_maxsize)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.2032.0_x64__qbz5n2kfra8p0\lib\subprocess.py", line 1457, in _internal_poll
    if _WaitForSingleObject(self._handle, 0) == _WAIT_OBJECT_0:
OSError: [WinError 6] The handle is invalid

Please let me know how i can take screenshots with allure

Method like below should work:

    def capture_screenshot(context, title='captured_screenshot'):
        allure.attach(
            context.driver.get_screenshot_as_png(),
            name=f'{title}', attachment_type=allure.attachment_type.PNG
        )

example method call:

capture_screenshot(context, "my_1st_failed_scenario")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM