Playwright 102: Selenium vs Playwright

Akhil Singh Rawat
3 min readFeb 27, 2023

When it comes to automated testing of web applications, two of the most popular frameworks are Playwright and Selenium. Both of these frameworks offer powerful tools for automating web browsers and interacting with web applications, but they have different approaches and features. In this blog, we will compare Playwright and Selenium, highlighting the key differences between them and providing code examples.

Architecture

One of the key differences between Playwright and Selenium is their architecture. Playwright uses a multi-page, multi-context model, which means that it can simulate multiple browser windows and tabs, each with its own environment and lifecycle. This allows for efficient testing of complex web applications. Selenium, on the other hand, uses a single-threaded model, which can lead to slower test execution times for complex applications.

API

Both Playwright and Selenium offer APIs for automating web applications, but there are differences in the way they work. Playwright’s API is designed to be simple and intuitive, with a focus on modern web application development practices. Selenium’s API is more complex and has a steeper learning curve, but it offers more advanced features for testing.

Here’s an example of how to navigate to a web page using Playwright:

const { chromium } = require('playwright');
(async () => {
const browser = await chromium.launch();
const context = await browser.newContext();
const page = await context.newPage();

await page.goto('https://www.google.com');

await browser.close();
})();

And here’s how to navigate to a web page using Selenium:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class ExampleTest {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
WebDriver driver = new ChromeDriver();

driver.get("https://www.google.com");

driver.quit();
}
}

As you can see, the Playwright code is more concise and easier to read, while the Selenium code requires more setup and boilerplate.

Cross-browser support

Both Playwright and Selenium offer cross-browser support, allowing you to test your web applications on multiple platforms. However, Playwright supports Chromium, Firefox, and WebKit browsers out of the box, while Selenium requires additional drivers to be installed for each browser.

Here’s an example of how to automate Firefox using Playwright:

const { firefox } = require('playwright');

(async () => {
const browser = await firefox.launch();
const context = await browser.newContext();
const page = await context.newPage();

await page.goto('https://www.google.com');

await browser.close();
})();

And here’s how to automate Firefox using Selenium:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class ExampleTest {
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver", "/path/to/geckodriver");
WebDriver driver = new FirefoxDriver();

driver.get("https://www.google.com");

driver.quit();
}
}

As you can see, Playwright provides a more streamlined approach to cross-browser testing.

Playwright vs Selenium

Conclusion

In conclusion, both Playwright and Selenium are powerful tools for automating web applications, but they have different approaches and features. Playwright’s multi-page, multi-context model, simple API, and cross-browser support make it an excellent choice for modern web application development. Selenium’s advanced features and flexibility make it a good choice for complex web applications that require more advanced testing. Ultimately, the choice between Playwright and Selenium will depend on your specific needs and preferences.

--

--