Run First Test in Playwright Python

Profile picture for user arilio666

In this article, we will run our first test using playwright Python.

1. Once you have Playwright installed, you can create a new Python file and import the necessary modules:

from Playwright.sync_api import sync_playwright

2. Next, you can use the Playwright API to launch a new browser and create a new page:

with sync_playwright() as p:
   browser = p.chromium.launch(headless=False)
   page = browser.new_page()

3. Example

page.goto('https://www.programsbuzz.com/user/login')
   page.locator('#edit-name').type('Luffy')
   page.locator('#edit-pass').type('Gum-Gum')
   page.locator('(//*[@id="edit-submit"])[2]').click()
   errorText = page.locator("(//h2[@class='visually-hidden'])[2]").text_content()
   print(errorText)
  • Here we navigated to the user login page.
  • Using the type command, we fill in the username and password.
  • With the click method, we then click on the login button.
  • Using text_content(), we then get the error message and print it.
Run First Test in Playwright Python