Tutorial 7 – Taking Screenshots and record videos using Playwright-Python
What you will Learn:
screenshot=on usage
screenshot=only-on-failure usage
video=on
video=retain-on-failure
Code snippets
screenshot=on usage
Go to https://playwright.dev/python/docs/test-runners
As seen below, the --screenshot flag can be used to take screenshots. The default option is ‘off’ (this means that, by default, the screenshot is not taken)
Below script was created in our previous blog
To take the screenshot of the execution, let us execute the below command with ‘on’ option:
pytest --headed --screenshot=on .\pytestpac\test_pytest.py
Notice that ‘test-results’ folder gets created.
Expand this folder and sub-folder. The .png file can be seen (filename starts with ‘test-finished’)
Select and click the .png file, notice the captured screenshot
screenshot=only-on-failure usage
We will now see how to take a screenshot 'only on failure'. Let us introduce an error in script (See line#8)
page.get_by_label("E2mail").press(“Tab”)
So basically, the script should not be able to type anything in the password field.
Execute the below command:
pytest --headed --screenshot=only-on-failure .\pytestpac\test_pytest.py
Notice that the script fails
Notice that, this time, the .png filename starts with ‘test-failed’
Click this file, notice below that script could not locate the password field and hence nothing gets typed in the password field
Let us correct back the script
Video recording
Let us now see how to perform video recording using
--video=on option.
Execute:
pytest --headed --video=on --slowmo=3000 .\pytestpac\test_pytest.py
The video file gets generated
Right click this video and open the explorer path.
Open this file to play the video recording
Close the video file.
video=retain-on-failure
Let us introduce error in line#8
Execute:
pytest --headed --video=retain-on-failure --slowmo=3000 .\pytestpac\test_pytest.py
Code snippet
from playwright.sync_api import Page, expect
def test_example(page: Page) -> None:
page.goto("https://sso.teachable.com/secure/673/identity/login")
page.get_by_label("Email").click()
page.get_by_label("Email").fill("dummy@test.com")
page.get_by_label("Email").press("Tab")
page.get_by_label("Password").fill("test")
print("test execution complete")
Thank you for reading!