Comparing Playwright Test Automation Framework with Fixtures to Selenium and Py.test with Fixtures
Published
Updated
Originally posted on LinkedIn
Introduction
When it comes to test automation, there are various tools and frameworks available, making it challenging to choose the right one for your project. In this blog post, we will compare Playwright, a relatively new test automation framework, with the widely popular Selenium and Py.test combination. Our focus will be on their respective use of fixtures, which are essential components in the world of test automation.
Playwright Test Automation Framework using Fixtures
Playwright is a relatively new test automation framework developed by Microsoft that enables fast, reliable, and capable testing across all modern web browsers. With Playwright, you can write tests in JavaScript, TypeScript, or Python.
Playwright’s Fixture API is designed to handle test setup and teardown in a consistent and scalable manner. Fixtures provide a way to share test resources and configurations across multiple test files, enabling better organization and maintainability.
Some key features of Playwright’s fixtures include:
- Easy test resource management: Playwright’s fixtures simplify resource management by automatically initializing and disposing of resources for each test.
- Parameterized fixtures: You can create fixtures with parameters, allowing different configurations or variations of a resource to be used in multiple tests.
- Test isolation: Playwright ensures that each test is isolated from others by creating a new browser context for each test. This ensures that tests don’t interfere with each other, reducing the chances of flaky tests.
- Improved test organization: Playwright’s fixtures allow you to separate test resources from test logic, improving the overall organization and readability of your test suite.
Selenium and Py.test using Fixtures
Selenium is a popular and widely-used browser automation library, and Py.test (now known as “pytest”) is a versatile testing framework for Python. When used together, they provide a powerful solution for web testing.
Pytest’s fixtures are a core feature of the framework, providing a way to create reusable test resources, configurations, and dependencies. Fixtures in pytest can be shared across multiple test files or kept local to a specific test module.
Some key features of pytest’s fixtures include:
- Dependency injection: Pytest fixtures allow you to define and manage dependencies for your tests, ensuring that resources are created, configured, and disposed of correctly.
- Scoping: Pytest fixtures can be defined with different scopes, such as function, class, module, or session, allowing you to control the lifespan of the fixture and when it’s initialized and torn down.
- Parameterized fixtures: Similar to Playwright, pytest allows you to create fixtures with parameters, enabling you to run tests with different configurations or variations of a resource.
- Autouse fixtures: In pytest, you can define fixtures that are automatically used by all tests in a module or an entire test suite, without the need to explicitly request the fixture in each test function.
In this section, we will provide code examples for both Playwright (using TypeScript) and Selenium with pytest (using Python). These examples demonstrate how to navigate to a website and perform a simple search using fixtures.
Playwright Test Automation Framework using TypeScript
First, install the necessary dependencies:
npm install --save-dev @playwright/test
npx playwright install chromium
Create a playwright.config.ts file to configure Playwright:
// playwright.config.ts
import { defineConfig } from '@playwright/test';
export default defineConfig({
testDir: 'tests',
use: {
browserName: 'chromium',
},
});
Create a tests/search.spec.ts file for the test:
// tests/search.spec.ts
import { test, expect } from '@playwright/test';
test.describe('Search functionality', () => {
test.beforeEach(async ({ page }) => {
await page.goto('https://example.com');
});
test('search for a keyword', async ({ page }) => {
await page.locator('input[name="search"]').fill('playwright');
await page.locator('input[type="submit"]').click();
const searchResults = page.locator('.search-results');
await expect(searchResults).toBeVisible();
});
});
To run the test, execute the following command:
npx playwright test
Selenium and pytest using Python
First, install the necessary dependencies:
pip install selenium pytest
Create a conftest.py file to set up the fixture:
# conftest.py
import pytest
from selenium import webdriver
@pytest.fixture(scope="function")
def driver():
driver = webdriver.Chrome()
driver.maximize_window()
yield driver
driver.quit()
Create a test_search.py file for the test:
# test_search.py
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
def test_search_for_keyword(driver):
driver.get("https://example.com")
search_box = driver.find_element(By.NAME, "search")
search_box.send_keys("selenium" + Keys.RETURN)
search_results = driver.find_element(By.CSS_SELECTOR, ".search-results")
assert search_results.is_displayed()
To run the test, execute the following command:
pytest
These examples demonstrate the basic setup and usage of Playwright and Selenium with pytest to perform a simple search operation. Although the syntax and structure differ between the two, both frameworks effectively provide the tools necessary to create efficient and maintainable test suites.
Pros and Cons
Playwright Test Automation Framework (TypeScript)
Pros:
- Modern API: Playwright offers a modern and clean API that simplifies test writing and maintenance.
- Multiple language support: Playwright supports JavaScript, TypeScript, and Python out of the box, giving you flexibility in choosing your preferred language.
- Better performance: Playwright is designed to be faster and more reliable than Selenium, reducing the likelihood of flaky tests.
- Improved browser support: Playwright has built-in support for Chromium, Firefox, and WebKit, allowing you to test across multiple browsers with ease.
- Auto-waiting: Playwright automatically waits for elements to be available and visible before interacting with them, reducing the need for explicit waits and simplifying test code.
- Native support for network interception and mocking: Playwright offers built-in support for network interception, allowing you to mock API responses or monitor network requests during testing.
Cons:
- Smaller community: Playwright is relatively new compared to Selenium, and its community is smaller, which may impact the availability of support and resources.
- Less mature: As a newer framework, Playwright may have fewer integrations with third-party tools and libraries compared to Selenium.\
Selenium and pytest (Python)
Pros:
- Wide adoption: Selenium is a widely-used browser automation tool, and pytest is a popular testing framework in the Python community. This means there are abundant resources, tutorials, and community support available.
- Extensive browser support: Selenium supports a wide range of web browsers, including older and less popular browsers.
- Language bindings: Selenium provides language bindings for Java, C#, Ruby, and Python, allowing you to choose your preferred programming language.
- Third-party integrations: Selenium has a large ecosystem of third-party libraries and integrations, making it easier to extend its functionality.
- Mature and battle-tested: Selenium has been around for a long time and is considered a reliable and stable solution for web testing.
Cons:
- Slower performance: Selenium is generally slower compared to Playwright, which may impact test execution times and contribute to flaky tests.
- More complex API: Selenium’s API can be more complex and verbose, making it harder to write and maintain tests.
- Explicit waiting: Selenium often requires explicit waits to ensure elements are available before interacting with them, which can make test code more complex and prone to errors.
Both Playwright and Selenium with pytest have their own advantages and drawbacks. The choice between the two will depend on your specific requirements, team familiarity, and desired programming language. While Playwright offers a modern approach and better performance, Selenium with pytest has a larger community and more extensive browser support.
Conclusion
Both Playwright and Selenium with pytest offer powerful testing capabilities and support for fixtures, allowing you to create maintainable, organized, and efficient test suites. While Playwright offers a more modern approach and better browser support, Selenium with pytest has a longer history and a larger user community.
Ultimately, the choice between these two testing solutions will depend on your specific project requirements, team familiarity, and desired programming language. If you prefer working with JavaScript, TypeScript, or desire better support for modern web technologies, Playwright may be the better choice. On the other hand, if your team is already familiar with Selenium and Python or if you require extensive browser support, Selenium with pytest may be the better option.
Updated November 19, 2026: the code samples now run on current versions. Selenium 4 finds the browser driver itself, so webdriver-manager is gone (its old call fails on Selenium 4.10+); the Playwright test uses locators, and ts-node is no longer needed.