docs/modules/webdriver_containers.md
Testcontainers can be used to automatically instantiate and manage containers that include web browsers, such as those from SeleniumHQ's docker-selenium project.
RemoteWebDriver instanceselenium-api-*.jar on the classpathCreation of browser containers is fast, so it's actually quite feasible to have a totally fresh browser instance for every test.
The following field in your JUnit UI test class will prepare a container running Chrome:
<!--codeinclude-->Chrome inside_block:junitRule
<!--/codeinclude-->Now, instead of instantiating an instance of WebDriver directly, use the following to obtain an instance inside your test methods:
<!--codeinclude-->RemoteWebDriver inside_block:getWebDriver
<!--/codeinclude-->You can then use this driver instance like a regular WebDriver.
Note that, if you want to test a web application running on the host machine (the machine the JUnit tests are running on - which is quite likely), you'll need to use the host exposing feature of Testcontainers, e.g.:
<!--codeinclude-->Open Web Page inside_block:getPage
<!--/codeinclude-->At the moment, Chrome, Firefox and Edge are supported. To switch, simply change the first parameter to the rule constructor:
<!--codeinclude-->Chrome inside_block:junitRule Firefox inside_block:junitRule Edge inside_block:junitRule
<!--/codeinclude-->By default, no videos will be recorded. However, you can instruct Testcontainers to capture videos for all tests or just for failing tests.
<!--codeinclude-->Record all Tests inside_block:recordAll Record failing Tests inside_block:recordFailing
<!--/codeinclude-->Note that the second parameter of withRecordingMode should be a directory where recordings can be saved.
By default, the video will be recorded in FLV format, but you can specify it explicitly or change it to MP4 using withRecordingMode method with VncRecordingFormat option:
Video Format in MP4 inside_block:recordMp4 Video Format in FLV inside_block:recordFlv
<!--/codeinclude-->If you would like to customise the file name of the recording, or provide a different directory at runtime based on the description of the test and/or its success or failure, you may provide a custom recording file factory as follows:
<!--codeinclude-->CustomRecordingFileFactory inside_block:withRecordingFileFactory
<!--/codeinclude-->Note the factory must implement org.testcontainers.containers.RecordingFileFactory.
A few different examples are shown in ChromeWebDriverContainerTest.java.
Add the following dependency to your pom.xml/build.gradle file:
=== "Gradle"
groovy testImplementation "org.testcontainers:testcontainers-selenium:{{latest_version}}"
=== "Maven"
xml <dependency> <groupId>org.testcontainers</groupId> <artifactId>testcontainers-selenium</artifactId> <version>{{latest_version}}</version> <scope>test</scope> </dependency>
!!! hint Adding this Testcontainers library JAR will not automatically add a Selenium Webdriver JAR to your project. You should ensure that your project also has suitable Selenium dependencies in place, for example:
=== "Gradle"
```groovy
compile "org.seleniumhq.selenium:selenium-remote-driver:3.141.59"
```
=== "Maven"
```xml
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-remote-driver</artifactId>
<version>3.141.59</version>
</dependency>
```
Testcontainers will try and match the version of the Dockerized browser to whichever version of Selenium is found on the classpath