Setup Pytest-Playwright in PyCharm

Tutorial​​ 6​​ ​​ Setup​​ Pytest-Playwright​​ in​​ PyCharm

What you will Learn:

  • Install pytest framework in playwright

  • Create pytest code and execute the same

  • Code snippets

Install pytest framework in playwright

Execute​​ the command:​​ pip install pytest-playwright​​ 


Execute the command to ensure​​ pytest-playwright​​ is installed:​​ pip list

Execute below command​​ to launch the inspector:

playwright codegen​​ https://sso.teachable.com/secure/673/identity/login​​ 

Click Library dropdown. Pytest should now be seen ​​ 

Select Pytest to convert the code into pytest format

Note: If you do not see 'Pytest' in the​​ inspector​​ dropdown, make sure you have playwright version 1.22 or higher. You can find out the playwright version by executing the 'pip list' command. You can upgrade playwright by executing the commands:​​ 

'pip install -U playwright' followed by 'playwright install'

Now, coming back, enter any email and pwd in the chromium browser​​ 

Notice that the corresponding pytest code gets auto-generated

Copy the code by clicking the ‘Copy’ icon

Create pytest package

Create python file starting with 'test' keyword, example test_pytest.py

Note: We will get error​​ (while execution)​​ if the filename does not start with 'test' keyword.

Paste the code, see below. Remember that this code is in pytest framework format

Close inspector and chromium browser.

Execute​​ the above​​ pytest file by executing​​ -->​​ pytest

Notice​​ above​​ that the test gets passed. By default, the execution happens in headless mode and we do not see the browser.

Let us add a ‘print’ statement in the code

Save.

Execute​​ pytest --headed -s​​ command if we want to launch the browser. The ‘-s’ flag is for displaying the print o/p in console. Notice below the o/p gets printed and the browser gets launched​​ 

This is how we setup the pytest framework in pycharm and execute the pytest file.​​ 

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!

Share On

Share on facebook
Share on twitter
Share on linkedin
Share on whatsapp
Share on tumblr
Share on email

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top

Lifetime Membership Club

LIFETIME MEMBERSHIP BIG SALE - ALL LIVE COURES JUST - 7000 RS/ 99 USD
Attend All Live courses in just 7000 rs / $99 - offer ends 10th Nov 2024