Multi-Domain Testing in Playwright: One Browser vs. Two Browsers
Multi-domain testing is essential when working with applications that involve authentication, cross-domain interactions, or different user roles. Playwright offers two main approaches to handle multi-domain testing:
- Using a single browser with separate contexts
- Launching two independent browser instances
Each approach has its pros and cons. This post will walk through both strategies with JavaScript examples.
Solution 1: One Browser, Separate Contexts
A single browser instance can create multiple isolated contexts, allowing separate sessions for different domains. This approach is lightweight and efficient.
Example: One Browser, Separate Contexts
const { chromium } = require('playwright');
Solution 2: Two Browsers
For maximum isolation, you can launch two independent browser instances. This ensures completely separate environments for each domain or user role.
Example: Two Browsers
const { chromium } = require('playwright');
Comparison Table
Summary
Both methods have their advantages. If you need to test different sessions in a lightweight way, using a single browser with multiple contexts is the best option. However, if you require complete isolation, using two browser instances is the way to go.
Choose the approach that best fits your testing needs!
---
Guy Arieli is the Chief Technology Officer at BlinqIO, a company building AI-powered test engineering tools for Playwright. With a deep passion for developer productivity and test automation, Guy shares practical insights from real-world use cases.
Want to contribute your own tips? Reach out to us at tips@playwright-user-event.org.