Selenium Grid
Playwright can connect to Sauce Labs remotely via its Selenium Grid support to launch the Google Chrome or Microsoft Edge browser.
Playwright connects to the browser using the Chrome DevTools Protocol (CDP). Selenium 4 currently exposes this capability.
Playwright support via Selenium Grid utilizes the Chrome DevTools Protocol (CDP). While this provides immediate compatibility, we are actively developing a native WebSocket integration for enhanced performance. For the most reliable experience today, follow the guidelines shown below.
What You'll Need
- A Sauce Labs account (if you don't have one, start a free trial)
- Your Sauce Labs Username and Access Key
- Playwright installed in your project
- Supported browsers: Google Chrome or Microsoft Edge
Connect Playwright to Sauce Labs
Playwright can connect to Sauce Labs using environment variables or direct configuration in your test code. The recommendations below demonstrate both approaches across multiple programming languages.
Using Fixtures/Extensions (Recommended)
We have crafted code setups and fixtures that will improve how your Playwright tests perform and look on Sauce Labs. These examples include best practices that help you get more information from your tests, making them easier to debug. The enhanced test metadata, proper configuration, and structured approach ensure better visibility into test execution and results in the Sauce Labs dashboard.
The following examples demonstrate how to configure Playwright to connect to Sauce Labs in different programming languages.
- JavaScript/TypeScript
- Java
- Python
- .NET
The fixture builds the Sauce session capabilities and metadata (name, build). You can tweak the capabilities to fit your needs in terms of browser, platform, and other options.
loading...
page API locally and on Sauce.To use the fixture, place it in your project and import it in your test files:
loading...
The extension builds the Sauce session capabilities and metadata (name, build). You can tweak the capabilities to fit your needs in terms of browser, platform, and other options.
loading...
SaucePlaywrightSession
to manage the lifecycle, annotate, and add tags to the Sauce session. The extension
also helps updating the job status based on the test result, it also logs a direct link to the job in
the Sauce Labs UI . Your test code remains unchanged as they use the same page API locally and on Sauce.To use the extension, add it to your base test class and use the
page in your test methods:loading...
The fixture builds the Sauce session capabilities and metadata (name, build). You can tweak the capabilities to fit your needs in terms of browser, platform, and other options.
loading...
page API locally and on Sauce.To use the fixture, place it in your project and import it in your test files:
loading...
The fixture builds the Sauce session capabilities and metadata (name, build). You can tweak the capabilities to fit your needs in terms of browser, platform, and other options.
loading...
page API locally and on Sauce.To use the fixture, place it in your project and import it in your test files:
loading...
Using Environment Variables
Set the SELENIUM_REMOTE_URL and SELENIUM_REMOTE_CAPABILITIES environment variables to connect
Playwright to Sauce Labs:
- JavaScript/TypeScript
- Java
- Python
- .NET
export SELENIUM_REMOTE_URL=https://ondemand.us-west-1.saucelabs.com:443/wd/hub
export SELENIUM_REMOTE_CAPABILITIES='{"platformName":"Windows 11","browserName":"chrome","sauce:options":{"devTools":true,"username":"'$SAUCE_USERNAME'","accessKey":"'$SAUCE_ACCESS_KEY'"}}'
npx playwright test --headed --project "chromium"
export SELENIUM_REMOTE_URL=https://ondemand.us-west-1.saucelabs.com:443/wd/hub
export SELENIUM_REMOTE_CAPABILITIES='{"platformName":"Windows 11","browserName":"chrome","sauce:options":{"devTools":true,"username":"'$SAUCE_USERNAME'","accessKey":"'$SAUCE_ACCESS_KEY'"}}'
mvn test
export SELENIUM_REMOTE_URL=https://ondemand.us-west-1.saucelabs.com:443/wd/hub
export SELENIUM_REMOTE_CAPABILITIES='{"platformName":"Windows 11","browserName":"chrome","sauce:options":{"devTools":true,"username":"'$SAUCE_USERNAME'","accessKey":"'$SAUCE_ACCESS_KEY'"}}'
pytest --headed --browser chromium
export SELENIUM_REMOTE_URL=https://ondemand.us-west-1.saucelabs.com:443/wd/hub
export SELENIUM_REMOTE_CAPABILITIES='{"platformName":"Windows 11","browserName":"chrome","sauce:options":{"devTools":true,"username":"'$SAUCE_USERNAME'","accessKey":"'$SAUCE_ACCESS_KEY'"}}'
HEADED=1 dotnet test
Best Practices
If you're using our recommended fixtures/extensions, many of these best practices are already implemented for you, including test metadata, session management, and job status updates.
Use Headed Mode
Playwright must run in headed mode for Sauce Labs to capture video recordings. Ensure your test configuration or command includes the headed flag:
npx playwright test --headed
Set Meaningful Test Metadata
Always include descriptive test names and build identifiers to make results easier to find and debug:
"sauce:options": {
"name": "Login Flow - Valid Credentials",
"build": "build-2024-01-15-abc123",
"tags": ["playwright", "login", "smoke"]
}
Limitations
Due to the nature of CDP-based communication, there are some limitations when running Playwright via Selenium Grid:
-
Command Display: The commands shown in the Sauce Labs UI may differ from what you typically see when running Playwright locally, this is because the browser communicates with Playwright via CDP. Rest assured that your test is still running successfully and every command is being executed properly. We're actively working on improving how Playwright commands are displayed.
-
Browser Support: Only Google Chrome and Microsoft Edge are supported, as they provide CDP endpoints.
-
Headed Mode Required: Tests must run in headed mode for video capture to work.