0% found this document useful (0 votes)
372 views22 pages

Playwright Python Training: Basics to POM

The document outlines a training session on Playwright with Python, covering fundamentals, setup, and testing using Pytest. Key topics include browser automation, installation, writing tests, using fixtures, and implementing the Page Object Model for maintainable code. The training emphasizes cross-browser testing and structured project organization, providing hands-on examples and tasks for participants.

Uploaded by

drctest542
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
372 views22 pages

Playwright Python Training: Basics to POM

The document outlines a training session on Playwright with Python, covering fundamentals, setup, and testing using Pytest. Key topics include browser automation, installation, writing tests, using fixtures, and implementing the Page Object Model for maintainable code. The training emphasizes cross-browser testing and structured project organization, providing hands-on examples and tasks for participants.

Uploaded by

drctest542
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

S e s s io n 1 /3

Playwright Python
Session 1: Fundamentals & First Tests

Duration: 30 minutes | Live Demo Included


What is Playwright?
🎭 Browser Automation – Control Chromium, Firefox, WebKit via Python

⚡ Fast & Reliable – Auto-waiting, smart locators, zero flakiness

🔍 Multi-browser testing – Same test across all browsers

📸 Debugging tools – Screenshots, traces, videos on demand

Playwright Python Training 0-5 min


Playwright Architecture

Playwrigh
Each layer is independent & reusable

Playwright Python Training 5-10 min


Setup & Installation
p i p i n s t a l l p l a y w r i g h t p y t e s t p y t e s t - p l a y w r i g h t
p y t h o n - m p l a y w r i g h t i n s t a l l

✓ Creates virtual env (recommended)


✓ Installs browser binaries (Chromium, Firefox, WebKit)

Playwright Python Training 5-10 min


First Playwright Script
from playwright.sync_api import sync_playwright

with sync_playwright() as p:
browser = [Link]()
page = browser.new_page()
[Link]('[Link]
print([Link]())
[Link]()

Run:

Playwright Python Training 10-20 min (Demo)


Move to Pytest + Fixtures
def test_page_title(page):
[Link]('[Link]
assert [Link]() == 'Example Domain'

def test_click_and_navigate(page):
[Link]('[Link]
[Link]('a')
assert 'new page' in [Link]

Run:

Playwright Python Training 15-25 min (Demo)


Session 1: Key Takeaways
• Playwright = browser control + Python API

Sync API: for simple scripts

• Pytest + fixtures = structured test framework

📝 Task: Create 1 basic test with pytest

Playwright Python Training 25-30 min


S e s s io n 2 /3

Playwright Python
Session 2: Pytest Fixtures & Structure

Duration: 30 minutes | Live Demo Included


Built-in Fixtures
• – Per-test Page instance (function-scoped)

• – Browser instance (session-scoped, reused)

• – Isolated browser context (per test)

• – Browser type as string

Fixtures are injected as test function parameters

Playwright Python Training 0-5 min


Recommended Structure
project/
├── tests/
│ ├── test_login.py
│ └── test_dashboard.py
├── [Link]
├── [Link]
└── [Link]

[Link] = shared fixtures for all tests

Playwright Python Training 5-10 min


Custom Fixture Example
# [Link]
import pytest

@[Link]
def authenticated_page(page):
[Link]('[Link]
[Link]('#email', 'test@[Link]')
[Link]('#password', 'password123')
[Link]('button[type=submit]')
yield page # Use in test
[Link]('[Link]

# test_dashboard.py
def test_view_dashboard(authenticated_page):
# Already logged in!
assert 'Dashboard' in authenticated_page.title()

Playwright Python Training 10-20 min (Demo)


Key Assertions
[Link]('[Link]
assert [Link]() == 'Example'
assert 'success' in [Link]()

locator = [Link]('#element')
assert locator.is_visible()
assert locator.text_content() == 'Expected Text'

Also: API from pytest-playwright

Playwright Python Training 15-25 min


Running Tests
# All tests
pytest

# Specific test file


pytest tests/test_login.py

# Headed mode (see browser)


pytest --headed

# Single browser
pytest -k "test_name"

Playwright Python Training 20-25 min


Session 2: Key Takeaways
• Fixtures = clean setup/teardown per test

• Custom fixtures enable reusable test logic

• Structure tests in folders + [Link]

📝 Task: Refactor tests using custom fixtures

Playwright Python Training 25-30 min


S e s s io n 3 /3

Playwright Python
Session 3: Page Object Model & Advanced

Duration: 30 minutes | Live Demo Included


Page Object Model (POM)
• 📄 Each page = 1 Python class

🎯 Locators encapsulated in class attributes

⚙️Actions as methods (login(), click_button(), etc.)

✨ Benefits: maintainable, readable, easy updates

Playwright Python Training 0-5 min


POM Example: Login Page
# pages/login_page.py
class LoginPage:
def __init__(self, page):
[Link] = page
self.email_input = '#email'
self.password_input = '#password'
self.login_button = 'button[type=submit]'

def navigate(self):
[Link]('[Link]

def login(self, email, password):


[Link](self.email_input, email)
[Link](self.password_input, password)
[Link](self.login_button)

Playwright Python Training 5-15 min (Demo)


Using POM in Tests
# tests/test_login.py
from pages.login_page import LoginPage

def test_login_success(page):
login_page = LoginPage(page)
login_page.navigate()
login_page.login('user@[Link]', 'pass123')
assert 'Dashboard' in [Link]()

def test_login_invalid_email(page):
login_page = LoginPage(page)
login_page.navigate()
login_page.login('invalid', 'pass123')
assert 'error' in [Link]().lower()

Playwright Python Training 10-20 min (Demo)


Full Project Structure
project/
├── pages/
│ ├── base_page.py
│ ├── login_page.py
│ └── dashboard_page.py
├── tests/
│ ├── test_login.py
│ └── test_dashboard.py
├── [Link]
├── [Link]
└── [Link]

Playwright Python Training 15-20 min


Cross-Browser Testing
# p y t e s t . i n i
[ p y t e s t ]
a d d o p t s = - - b r o w s e r c h r o m i u m - - b r o w s e r f i r e f o x

Or run via CLI:

pytest --browser chromium --browser firefox --browser webkit

Tests run on all 3 browsers automatically!

Playwright Python Training 20-27 min


Session 3: Key Takeaways
• POM = scalable, maintainable framework

• Encapsulate locators & actions in page classes

• Cross-browser runs with one command

📝 Assignment: Build POM for real app flows

Playwright Python Training 27-30 min


Resources & Next Steps
• 📖 Playwright Docs:

• 📚 Pytest Docs:

• 💾 Starter Repo: Share on your Git platform

• 🚀 Next: CI Integration (GitHub Actions, Azure


Questions?
Pipelines)

You might also like