Playwright vs Selenium in 2021

For a decade, Selenium was the king of E2E testing. But modern SPAs (React/Vue) flakiness made it painful. Microsoft’s Playwright (fork of Puppeteer) offers a faster, more reliable alternative by using the DevTools Protocol directly.

Auto-Waiting

Playwright waits for elements to be actionable checks prior to performing actions. No more `Thread.Sleep(5000)`!

// Playwright
await page.ClickAsync("text=Submit"); // Waits for visibility, stability, enabled...

// Selenium (Old way)
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
var btn = wait.Until(ExpectedConditions.ElementToBeClickable(By.Id("submit")));
btn.Click();

Browser Contexts

Run isolated tests in parallel without launching new browser windows. Setup takes milliseconds.

await using var browser = await playwright.Chromium.LaunchAsync();
// Contexts are cheap and isolated (cookies, storage)
var context1 = await browser.NewContextAsync();
var context2 = await browser.NewContextAsync();

Key Takeaways

  • Playwright is **faster** and less flaky.
  • Supports **Codegen**: `playwright codegen wikipedia.org`.
  • Supports Chromium, Firefox, and WebKit (Safari) with one API.

Discover more from C4: Container, Code, Cloud & Context

Subscribe to get the latest posts sent to your email.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.