Skip to content

frquintero/playwright-python-beginner

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Playwright Python Testing Project

A beginner's guide and examples for browser automation testing using Playwright with Python.

Overview

This project demonstrates basic to intermediate Playwright concepts for end-to-end web testing. It includes:

  • Installation setup
  • Basic test examples
  • Browser interaction demonstrations
  • Configuration and best practices

Prerequisites

  • Python 3.8 or higher
  • pip (Python package installer)
  • Git (optional, for version control)

Installation

  1. Clone or download this repository

  2. Create a virtual environment (recommended):

    python3 -m venv venv
  3. Activate the virtual environment:

    source venv/bin/activate  # On Windows: venv\Scripts\activate
  4. Install dependencies:

    pip install pytest-playwright
  5. Install browser binaries:

    playwright install

    Optional: Install system dependencies (Linux/Mac):

    playwright install --with-deps

Project Structure

playwright/
├── venv/                    # Virtual environment
├── playwright-beginner-guide.md  # Comprehensive beginner guide
├── test_example.py         # Basic test examples
├── test_interactions.py    # Browser interaction demonstrations
├── example_screenshot.png  # Screenshot from tests
└── README.md              # This file

Running Tests

Activate environment first:

source venv/bin/activate

Run all tests:

pytest

Run specific test file:

pytest test_example.py

Run single test:

pytest test_example.py::test_has_title

Run with visible browser (headed mode):

pytest --headed

Run on specific browser:

pytest --browser chromium  # or firefox, webkit

Run on all browsers:

pytest --browser all

Interactive UI mode:

pytest --ui

Test Files Description

test_example.py

  • test_has_title: Basic navigation and title assertion
  • test_get_started_link: Clicking links and verifying navigation

test_interactions.py

Demonstrates core Playwright interactions:

  • Navigation (page.goto())
  • Clicking elements (page.click(), role-based locators)
  • Text input (page.fill())
  • Waiting for elements (page.wait_for_selector())
  • Screenshots (page.screenshot())
  • Assertions (expect() statements)

Key Concepts

Browser Contexts

Each test gets an isolated browser context (incognito-like session).

Auto-waiting

Playwright automatically waits for elements to be ready before interactions.

Locators

Multiple ways to find elements:

  • CSS selectors: page.click("button#submit")
  • Role-based: page.get_by_role("button", name="Submit")
  • Text content: page.get_by_text("Welcome")

Assertions

Web-first assertions using expect():

expect(page).to_have_title("My App")
expect(page.get_by_text("Success")).to_be_visible()

Configuration

Create pytest.ini for custom settings:

[tool:pytest]
testpaths = .
addopts = --browser chromium --headed

Troubleshooting

Common Issues

Browser not found:

playwright install

Tests failing:

  • Check internet connection
  • Verify target websites are accessible
  • Use --headed to debug visually

Virtual environment issues:

  • Ensure venv is activated: source venv/bin/activate
  • Reinstall if corrupted: rm -rf venv && python3 -m venv venv

Dependencies Warning

If you see library warnings on Linux, install system dependencies:

# Ubuntu/Debian
sudo apt-get install libnss3 libatk-bridge2.0-0 libdrm2 libxkbcommon0 libxcomposite1 libxdamage1 libxrandr2 libgbm1 libxss1 libasound2

# Or use Playwright's installer
playwright install --with-deps

Learning Resources

Next Steps

  1. Explore the playwright-beginner-guide.md for detailed explanations
  2. Modify existing tests to learn different scenarios
  3. Add new tests for your own applications
  4. Set up CI/CD with GitHub Actions
  5. Learn advanced features: API testing, mobile emulation, visual comparisons

Contributing

Feel free to add more test examples or improve the documentation.

License

This project is for educational purposes. Playwright is licensed under Apache 2.0.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages