Building a Minimalist UI Test Framework with Playwright and Pytest

Vladislav Timashenkov from InfoWatch presents a practical approach to constructing a concise UI automation testing framework using Playwright. The article focuses on leveraging native Playwright features instead of creating additional abstraction layers. It covers launching a Chromium browser once per session, handling authentication via API calls to obtain cookies, and managing isolated BrowserContext instances for test independence. Readers learn how to implement Page Object models that rely on Playwright's built-in locators and auto-waiting mechanisms. The tutorial includes real code examples for fixtures, context factories, and a sample test for tag management functionality. This method reduces maintenance overhead while ensuring tests remain scalable across different environments.

HabrTranslated from Russian

Read at source

Vladislav Timashenkov from InfoWatch shares a streamlined method for building a UI test automation framework. The approach emphasizes direct use of Playwright capabilities rather than wrapping them in custom abstractions.

The framework starts by launching a single Chromium browser process for the entire test session using a session-scoped fixture. Settings such as headless mode and slow motion are stored separately in BROWSER_SETTINGS to allow environment-specific configuration through variables or files.

Authentication is handled without UI interaction. A factory fixture uses the requests library to call the login API endpoint and extracts cookies for later use in browser contexts. This design supports multiple users during a single test run.

Isolated BrowserContext instances provide test separation. A context factory fixture accepts authentication data, applies CONTEXT_SETTINGS including base URL and locale, and ensures all contexts are closed after each test function.

The page fixture combines the authenticated user data with the context factory to deliver a ready-to-use page object. Because the fixture scope is function-level, every test receives a fresh page with pre-applied cookies.

Page Object classes encapsulate business logic only. Locators are exposed as properties that return fresh Locator instances on each access, taking advantage of Playwright’s lazy evaluation and automatic waiting. Methods such as create_tag and delete_tag_by_name perform interactions without additional explicit waits.

A sample test demonstrates creating a tag with generated data from Faker and verifying visibility using the expect assertion API. The article concludes that this minimal infrastructure scales easily as new page objects are added.

Meridian digestThe week's most consequential security stories, distilled into one briefing.See the digest

More in Other

Mail.ru Reduces Lifetime 1TB Cloud Storage Promo to Standard 8GB Quota for Select Users
Kimi K3 Open Model Surpasses Closed AI Leaders with Million-Token Context and 896 Experts
Innostage Completes Functional Testing of PT NGFW 1.10.3 Across 156 Criteria