Your First Playwright Test in Python: Complete Tutorial for Beginners India 2026

May 9, 2026

Your First Playwright Test in Python: Complete Tutorial for Beginners India 2026

Want to write your first Playwright test in Python but do not know where to start? You can write and run a working Playwright test in Python in under 15 minutes with zero prior automation experience — and this tutorial shows you exactly how, from installation to running your first test and understanding every line of code.

Key Takeaway: Playwright Python has the simplest setup of any browser automation framework. One command installs everything. Your first test is 8 lines of code. This tutorial will have you running a real automation script in 15 minutes.

What is Playwright and Why Learn It in 2026?

Playwright is a browser automation framework created by Microsoft in 2020. It is used by QA engineers to write automated tests that control a browser — clicking buttons, filling forms, verifying page content — just like a user would.

  • 2,496+ Playwright QA jobs on Naukri India — growing 180% year-over-year
  • Playwright engineers earn Rs1.5-3 LPA more than Selenium-only engineers
  • Built-in auto-wait eliminates the most common cause of flaky tests
  • Supports Chromium, Firefox, and WebKit (Safari) with identical API
  • Native Python support with pytest integration

Step 1: Install Python and Playwright

Prerequisites: Python 3.8 or higher installed. Check with:

python --version
# Should show Python 3.8 or higher

Install Playwright and the Pytest plugin:

pip install playwright pytest-playwright

# Install browsers (Chromium, Firefox, WebKit)
playwright install

That is it. Total installation time: 3-5 minutes on a standard connection.

Step 2: Your First Playwright Test

Create a file called test_first.py in any folder:

from playwright.sync_api import Page, expect

def test_page_title(page: Page):
    # Navigate to the website
    page.goto("https://playwright.dev/")

    # Verify the page title contains "Playwright"
    expect(page).to_have_title("Fast and reliable end-to-end testing for modern web apps | Playwright")

    # Click on the Docs link
    page.click("text=Docs")

    # Verify we navigated to the docs page
    expect(page).to_have_url("https://playwright.dev/docs/intro")
    print("Test passed! Playwright automation is working.")

Run the test:

# Run in headless mode (no browser window)
pytest test_first.py

# Run with browser visible (helpful for learning)
pytest test_first.py --headed

# Run and see what happens step by step
pytest test_first.py --headed --slowmo 1000

Step 3: Understanding Every Line

Code What It Does
from playwright.sync_api import Page, expect Import the synchronous Playwright API and assertion helper
def test_page_title(page: Page): Function starting with “test_” is auto-detected by pytest. “page” is a Playwright fixture providing a browser page
page.goto("url") Navigate to a URL — browser opens the page and waits until fully loaded
expect(page).to_have_title(...) Assert the page has this exact title — test fails with clear message if not
page.click("text=Docs") Find element with text “Docs” and click it — auto-waits until element is visible and clickable
expect(page).to_have_url(...) Assert the current URL matches — Playwright auto-waits for navigation to complete
Key Takeaway: Playwright auto-waits for elements to be visible, clickable, and stable before acting on them. This eliminates the most common cause of flaky tests in Selenium: timing issues and manual waits.

Step 4: Real-World Test — Login Automation

Now write a test for a real practice application (SauceDemo):

from playwright.sync_api import Page, expect

def test_valid_login(page: Page):
    page.goto("https://www.saucedemo.com/")

    # Fill username and password
    page.fill("#user-name", "standard_user")
    page.fill("#password", "secret_sauce")

    # Click login button
    page.click("#login-button")

    # Verify we reached the products page
    expect(page).to_have_url("https://www.saucedemo.com/inventory.html")
    expect(page.locator(".inventory_list")).to_be_visible()
    print("Login successful - inventory page loaded")

def test_invalid_login(page: Page):
    page.goto("https://www.saucedemo.com/")
    page.fill("#user-name", "wrong_user")
    page.fill("#password", "wrong_password")
    page.click("#login-button")

    # Verify error message appears
    error_msg = page.locator("[data-test='error']")
    expect(error_msg).to_be_visible()
    expect(error_msg).to_contain_text("Username and password do not match")

Step 5: Run on Multiple Browsers

# Run on Chromium (default)
pytest test_login.py

# Run on Firefox
pytest test_login.py --browser firefox

# Run on WebKit (Safari engine)
pytest test_login.py --browser webkit

# Run on ALL browsers simultaneously
pytest test_login.py --browser chromium --browser firefox --browser webkit

Step 6: Playwright Locator Strategies

Playwright has multiple ways to find elements. Prefer these in order:

# 1. By test ID (most reliable - ask developers to add data-testid)
page.locator("[data-testid='submit-button']").click()

# 2. By role (accessibility-based, recommended by Playwright)
page.get_by_role("button", name="Login").click()
page.get_by_role("textbox", name="Username").fill("user@test.com")

# 3. By text
page.get_by_text("Add to Cart").click()

# 4. By label (for form inputs)
page.get_by_label("Email Address").fill("test@gmail.com")

# 5. By CSS selector (familiar from Selenium)
page.locator("#login-button").click()
page.locator(".product-title").first.click()

# 6. By XPath (fallback only)
page.locator("//button[@type='submit']").click()

Common Errors and Fixes

Error Fix
playwright: command not found Run: python -m playwright install
TimeoutError: Locator not found Element not on page. Check locator with --headed and inspect element
Error: Test exceeded 30000ms Slow network. Add page.set_default_timeout(60000) in conftest.py
ModuleNotFoundError: playwright Run in correct virtual environment: pip install playwright

Next Steps After Your First Test

  1. Add 5 more test cases for SauceDemo: cart, checkout, product sorting
  2. Create a conftest.py with shared fixtures
  3. Run pytest --html=report.html to generate an HTML report
  4. Push your test project to GitHub (your portfolio begins here)
  5. Add a GitHub Actions workflow to run tests automatically on push

Frequently Asked Questions

Do I need coding experience to start Playwright Python?

Basic Python is needed — variables, functions, and if/else statements. You do not need to know OOP or advanced Python concepts to write your first test. The Playwright API is designed to be readable and logical even for beginners.

What is the difference between sync and async Playwright Python?

Playwright has two Python APIs: synchronous (sync_api) and asynchronous (async_api). For beginners, always use the synchronous API — it is simpler and works directly with pytest. The async version is for advanced users building concurrent test runners.

How do I debug a Playwright test that is failing?

Run with --headed to see the browser. Add page.pause() to stop execution at any point and inspect the browser state interactively. Use page.screenshot(path="debug.png") to capture what the page looks like when the test fails.

Is Playwright free to use?

Yes. Playwright is completely free and open-source (MIT license), maintained by Microsoft. All three browsers (Chromium, Firefox, WebKit) are included at no cost. The only paid option is cloud browser services like BrowserStack if you need real device testing at scale.

Leave a Comment