Skip to content

Instantly share code, notes, and snippets.

@imshaiknasir
Created June 7, 2025 18:06
Show Gist options
  • Save imshaiknasir/9f27f4f60ffef8d4f4f80fba78b679f4 to your computer and use it in GitHub Desktop.
Save imshaiknasir/9f27f4f60ffef8d4f4f80fba78b679f4 to your computer and use it in GitHub Desktop.

1. The Architecture of Selenium

To understand Playwright's innovation, we first need to understand the traditional architecture of Selenium. It has several distinct layers and uses the standard W3C WebDriver protocol.

The Flow of a Command in Selenium:

  1. Your Test Script (Client): You write your code using Selenium's language bindings (e.g., selenium-python, selenium-java).
  2. JSON Wire Protocol over HTTP: Your script sends a command (e.g., "click this button") as an HTTP request to a local server. This is a standard REST API call.
  3. The WebDriver (e.g., ChromeDriver, GeckoDriver): This is a separate, standalone executable file. It acts as a server and a translator. It receives the HTTP request from your script.
  4. Internal Protocol: The WebDriver then translates that HTTP command into a command that the browser can understand, using its own internal communication protocol (often built on the Chrome DevTools Protocol or a similar vendor-specific API).
  5. The Browser: The browser executes the command and sends the result back.
  6. The Return Trip: The response travels all the way back through the WebDriver via HTTP to your script.

Visualizing Selenium's Architecture:

[Your Code] <--> [HTTP Request] <--> [WebDriver Executable] <--> [Internal Protocol] <--> [Browser]
 (Python/Java)      (JSON Wire)           (e.g., ChromeDriver)                        (Chrome/Firefox)

Key Architectural Characteristics of Selenium:

  • HTTP-Based: Communication is stateless. Each command is a separate HTTP request/response cycle, which has inherent overhead.
  • Multiple Hops: The command goes from your code -> WebDriver -> Browser. The WebDriver is a necessary middleman.
  • Standardization: It relies on the W3C WebDriver standard, which ensures it works with any browser that has a compliant driver. This is its greatest strength but also a source of slowness.

2. The Architecture of Playwright

Playwright was designed from the ground up by Microsoft (hiring a team that originally created Puppeteer at Google) to overcome the architectural limitations of tools like Selenium.

The Flow of a Command in Playwright:

  1. Your Test Script (Client/SDK): You write your code using the Playwright SDK (e.g., @playwright/test, playwright-python).
  2. WebSocket Connection: When you start Playwright, your script establishes a single, persistent WebSocket connection to a Playwright server process (which runs in the background, usually hidden from you).
  3. Playwright Server: This server receives commands from your script over the WebSocket. Unlike Selenium's WebDriver, this is part of the Playwright package itself, not a separate executable you manage.
  4. Browser DevTools Protocol: The Playwright server communicates directly with the browser using its native debugging protocol (like the Chrome DevTools Protocol). There is no translation layer like a separate WebDriver.
  5. Patched Browsers: This is crucial. Playwright doesn't use the standard versions of Chrome, Firefox, and Safari that you have on your machine. When you install Playwright, it downloads its own "patched" versions of Chromium, WebKit (the engine for Safari), and Firefox. These browsers are slightly modified to be more automatable and expose more information reliably.

Visualizing Playwright's Architecture:

[Your Code] <--> [WebSocket Connection] <--> [Playwright Server] <--> [DevTools Protocol] <--> [Patched Browser]
 (Python/JS)        (Persistent)               (Node.js process)                          (Chromium/WebKit/FF)

Key Architectural Characteristics of Playwright:

  • WebSocket-Based: The connection is persistent and bidirectional. This drastically reduces the overhead for sending commands.
  • Fewer Hops: The communication is more direct: Your Code -> Playwright Server -> Browser.
  • Event-Driven: Because it's connected directly to the browser's engine, Playwright doesn't just send commands; it can listen for events from the browser. This is fundamental to its auto-waiting capabilities.

3. How Playwright is Faster Than Selenium

The architectural differences are the direct cause of Playwright's speed and reliability advantages.

1. Communication Protocol: WebSocket vs. HTTP

  • Selenium (HTTP): Imagine sending a series of text messages. For each message (command), you have to find the person's number, send the text, wait for their phone to receive it, get a "delivered" receipt, and then wait for their reply. It's slow and has a lot of setup/teardown for each interaction.
  • Playwright (WebSocket): Imagine having an open phone call. Once the call is connected, you can talk back and forth instantly with very little delay. This is what the persistent WebSocket connection provides. It eliminates the overhead of creating new HTTP requests for every single command.

2. Auto-Waiting Mechanism

This is arguably the biggest real-world performance gain.

  • Selenium: When you tell Selenium to click an element, it tries immediately. If the element isn't there yet (e.g., it's loading due to a network request), the test fails. To prevent this, you must write explicit waits (WebDriverWait, expected_conditions). This adds code complexity and can slow tests down if you use fixed delays like time.sleep().
  • Playwright: Playwright has a powerful Auto-Waiting feature built-in. When you tell it page.click('#myButton'), it doesn't just try to click. It automatically performs a series of checks and waits until the element is:
    • Attached to the DOM
    • Visible
    • Stable (not animating)
    • Enabled and able to receive events It does this by listening to events from the browser. Instead of asking "Are you ready yet?" over and over (polling), it tells the browser, "Let me know when you are ready." This is far more efficient and makes tests both faster (it waits the minimum necessary time) and much more reliable.

3. Efficient Parallelism with Browser Contexts

  • Selenium: To run tests in parallel, you typically have to launch a completely new browser instance for each parallel thread. Launching a browser is a slow, resource-intensive operation.
  • Playwright: Playwright introduces the concept of Browser Contexts. A browser context is like a new, isolated incognito window within a single browser instance. They are extremely lightweight and fast to create. You can run many tests in parallel, each in its own isolated context, all within one single browser instance. This saves a massive amount of time and system resources when running a large test suite.

4. Direct and Privileged Browser Control

By using patched browsers and the DevTools protocol, Playwright has a more "privileged" connection to the browser's rendering engine. This allows it to do things that are difficult or slow in Selenium, such as:

  • Intercepting and modifying network traffic with high precision.
  • Precisely controlling page rendering for stable screenshots.
  • Listening to internal browser events for smarter waiting.

Summary Table

Feature Selenium Playwright Why it Matters for Speed
Communication HTTP (Stateless) WebSocket (Persistent) Massive reduction in command latency.
Browser Interaction Via separate WebDriver executable Direct via DevTools protocol Fewer "hops" and translation layers.
Waiting Explicit waits required (WebDriverWait) Auto-waiting is built-in Tests are faster and more reliable. No unnecessary delays.
Parallelism Usually requires new browser instances Uses lightweight Browser Contexts Drastic reduction in test suite execution time and resource usage.
Browsers Standard, installed browsers Patched, self-contained browsers More reliable and direct control over the browser's internal state.

Conclusion:

Playwright's speed isn't magic; it's the result of a modern architecture designed specifically for the complexities of today's web applications. By replacing the slow HTTP protocol with WebSockets, eliminating the middleman WebDriver, and building in intelligent, event-driven auto-waits, Playwright fundamentally reduces the time spent waiting—both for network latency and for elements to appear on the page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment