Testcontainers for browser tests: Selenium and Playwright in throwaway containers
Answer first
Testcontainers starts a browser in a throwaway Docker container from your test code and removes it afterwards. Its Selenium modules for Java, Python and Node.js hand you a WebDriver on a fresh browser; for Playwright, a generic container running the official browser server does the same.
- Published
- Updated
- min
- 4
Testcontainers is an open-source library that starts Docker containers from inside your tests and removes them when the tests end. For browser tests it means each run gets a browser from a fixed image instead of whatever is installed on the machine. The Java documentation puts the benefit plainly: "Browsers are always launched from a fixed, clean image", and "the use of a clean browser prevents leakage of cookies, cached data or other state between tests" (Testcontainers for Java: WebDriver containers, read 2026-11-26).
When is Testcontainers the right tool for browser tests?
Testcontainers fits when the test code itself should own the browser's lifecycle: a developer runs one test file and the browser appears, runs, and disappears. It needs only Docker on the machine.
Table: Testcontainers compared with a standing Selenium Grid or a local browser
| Setup | Who starts the browser | Good for |
|---|---|---|
| Testcontainers | The test, per test class or session | Same browser image everywhere; integration suites that already start a database container |
| A standing Selenium Grid | An operator, once | Many teams and suites sharing a fleet of browsers |
| A local browser install | Nobody; it is just there | Quick local runs where version drift does not matter |
For the standing option, see our guide to Selenium Grid in Docker.
How do I use the Selenium module in Python?
Install the Selenium extra, then open the container in a with block and ask it for a driver.
- Install the packages. The
seleniumextra is listed among the extras of testcontainers 4.15.0 on PyPI (PyPI: testcontainers, read 2026-11-26):
pip install "testcontainers[selenium]"
- Start a browser and use it. The two calls come from the module's example (testcontainers-python Selenium module, read 2026-11-26):
from selenium.webdriver import DesiredCapabilities
from testcontainers.selenium import BrowserWebDriverContainer
with BrowserWebDriverContainer(DesiredCapabilities.CHROME) as chrome:
webdriver = chrome.get_driver()
webdriver.get("https://www.selenium.dev")
print(webdriver.title)
Pass DesiredCapabilities.FIREFOX for Firefox. In pytest, put the with block in a session-scoped fixture so the container starts once per run instead of once per test.
How do I use the Selenium module in Node.js?
Install the module and the WebDriver client, start a SeleniumContainer with an image, and build a driver against its URL. The install commands are from the module's page (Testcontainers for Node.js: Selenium, read 2026-11-26):
npm install @testcontainers/selenium --save-dev
npm install selenium-webdriver
npm install @types/selenium-webdriver --save-dev
The key lines of the module's own example, with an image tag from the docker-selenium project:
import { SeleniumContainer } from "@testcontainers/selenium";
import { Browser, Builder } from "selenium-webdriver";
await using container = await new SeleniumContainer("selenium/standalone-chrome:4.54.0-20261124").start();
const driver = await new Builder().forBrowser(Browser.CHROME).usingServer(container.getServerUrl()).build();
await driver.get("https://testcontainers.com");
console.log(await driver.getTitle());
await driver.quit();
await using stops and removes the container when the block ends. For a video, start the container with .withRecording(), stop it, and call saveRecording(path) on the stopped container (Testcontainers for Node.js: Selenium, read 2026-11-26). The current release of testcontainers on npm was 12.2.2, which declares Node.js 22.22 or newer (npm: testcontainers, read 2026-11-26).
How do I use the WebDriver module in Java?
Create a BrowserWebDriverContainer with a docker-selenium image and connect a RemoteWebDriver to its address. The module's documented calls (Testcontainers for Java: WebDriver containers, read 2026-11-26):
BrowserWebDriverContainer chrome =
new BrowserWebDriverContainer("selenium/standalone-chrome:4.54.0-20261124");
chrome.start();
RemoteWebDriver driver = new RemoteWebDriver(chrome.getSeleniumAddress(), new ChromeOptions());
The dependency is org.testcontainers:testcontainers-selenium:2.0.5 in test scope, plus Selenium itself, which "the Testcontainers JAR doesn't add". To record, add .withRecordingMode(VncRecordingMode.RECORD_FAILING, target) for failing tests only, or RECORD_ALL.
How does the browser reach an app running on my machine?
Expose the app's port to containers, then browse to host.testcontainers.internal. Java: Testcontainers.exposeHostPorts(localPort) and then driver.get("http://host.testcontainers.internal:" + localPort) (Testcontainers for Java: WebDriver containers, read 2026-11-26). Node.js: await TestContainers.exposeHostPorts(8000), after which containers reach the port at host.testcontainers.internal:8000 (Testcontainers for Node.js: networking, read 2026-11-26).
How do I run Playwright browsers with Testcontainers?
Run the official Playwright image as a generic container that starts Playwright's browser server, then connect to it. The example below is ours, assembled from documented calls: GenericContainer, withCommand, withExposedPorts, withIpcMode and getMappedPort from Testcontainers (Testcontainers for Node.js: containers, read 2026-11-26), and run-server and connect from Playwright (Playwright Docker, read 2026-11-26).
import { GenericContainer } from "testcontainers";
import { chromium } from "@playwright/test";
const PW = "1.65.1"; // must equal the @playwright/test version in your lock file
const server = await new GenericContainer(`mcr.microsoft.com/playwright:v${PW}-noble`)
.withCommand(["npx", "-y", `playwright@${PW}`, "run-server", "--port", "3000", "--host", "0.0.0.0"])
.withExposedPorts(3000)
.withIpcMode("host")
.start();
const browser = await chromium.connect(`ws://${server.getHost()}:${server.getMappedPort(3000)}/`);
const page = await browser.newPage();
await page.goto("https://playwright.dev");
console.log(await page.title());
await browser.close();
await server.stop();
Three details make it work:
- Matching versions. The client and the server must be the same Playwright release, which is why one constant sets both.
- Waiting for the server. By default Testcontainers "waits up to 60 seconds for mapped network ports to be bound" (Testcontainers for Node.js: wait strategies, read 2026-11-26). The first start also downloads the package with
npx; bake it into your own image if that time matters. - Shared memory.
withIpcMode("host")follows Playwright's advice for Chromium;withSharedMemorySize(bytes)is the alternative when you would rather keep the container's IPC private.
With Playwright Test, you can instead set PW_TEST_CONNECT_WS_ENDPOINT to the server's address, as Playwright's Docker page describes, and keep your tests unchanged.
How do I make container start-up cheaper across runs?
Start containers once per session rather than once per test, and pin image tags so they are pulled once. Testcontainers for Node.js also offers .withReuse(), which means "Testcontainers will not start a new container" when a matching one is already running; it is controlled globally by TESTCONTAINERS_REUSE_ENABLE (Testcontainers for Node.js: containers, read 2026-11-26). A reused browser container keeps no state between tests only if each test opens a new session or context.
What we read for this article
- Testcontainers for Java: WebDriver containers, read 2026-11-26
- testcontainers-python Selenium module, read 2026-11-26
- Testcontainers for Node.js: Selenium, read 2026-11-26
- Testcontainers for Node.js: containers, read 2026-11-26
- Testcontainers for Node.js: networking, read 2026-11-26
- Testcontainers for Node.js: wait strategies, read 2026-11-26
- Playwright Docker, read 2026-11-26
- PyPI: testcontainers, read 2026-11-26
- npm: testcontainers, read 2026-11-26
Q&A
What is Testcontainers?
Testcontainers is an open-source library, available for Java, Python, Node.js and other languages, that starts Docker containers from test code and cleans them up when the test ends. It is used for databases, message brokers and browsers.
Does Testcontainers have a Playwright module?
The official Selenium modules cover WebDriver. For Playwright, start the official Playwright image as a generic container running Playwright's browser server, and connect to it from your test.
How does a browser in a Testcontainers container reach my app on localhost?
Expose the app's port to containers with exposeHostPorts, then open host.testcontainers.internal and that port from the browser. Both the Java and Node.js libraries document this.
Can Testcontainers record a video of a browser test?
Yes. The Java module records with withRecordingMode, for all tests or only failing ones, and the Node.js Selenium module records with withRecording and saveRecording.