Skip to content

Selenium

Install

1
npm install @testcontainers/selenium --save-dev

Examples

These examples use the following libraries:

  • selenium-webdriver
    1
    2
    npm install selenium-webdriver
    npm install @types/selenium-webdriver --save-dev
    

Choose an image from the container registry and substitute IMAGE:

1
2
3
4
5
6
7
await using container = await new SeleniumContainer(image).start();

const driver = await new Builder().forBrowser(Browser[browser]).usingServer(container.getServerUrl()).build();
await driver.get("https://testcontainers.com");
expect(await driver.getTitle()).toEqual("Testcontainers");

await driver.quit();

Record a video

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
const container = await new SeleniumContainer(image).withRecording().start();

const driver = await new Builder().forBrowser(Browser[browser]).usingServer(container.getServerUrl()).build();
await driver.get("https://testcontainers.com");

await driver.quit();
const stoppedContainer = await container.stop();

const videoFilePath = tmp.fileSync({ keep: false, prefix: `video-${browser}`, postfix: ".mp4" }).name;
const videoFileName = path.basename(videoFilePath);
await stoppedContainer.saveRecording(videoFilePath);

await using ffmpegContainer = await new GenericContainer(SELENIUM_VIDEO_IMAGE)
  .withCommand(["sleep", "infinity"])
  .start();
await ffmpegContainer.copyFilesToContainer([{ source: videoFilePath, target: `/tmp/${videoFileName}` }]);
const { exitCode } = await ffmpegContainer.exec(["ffprobe", `/tmp/${videoFileName}`]);
expect(exitCode).toBe(0);