Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes to Selenium/WebDriver module for ongoing compatibility #4609

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
2 changes: 1 addition & 1 deletion docs/examples/junit4/generic/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ dependencies {
testImplementation project(":mysql")

testImplementation 'mysql:mysql-connector-java:8.0.21'
testImplementation "org.seleniumhq.selenium:selenium-api:3.141.59"
testImplementation 'org.seleniumhq.selenium:selenium-java:4.1.1'
testImplementation 'org.assertj:assertj-core:3.15.0'
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testcontainers.Testcontainers;
import org.testcontainers.containers.BrowserWebDriverContainer;
import org.testcontainers.utility.DockerImageName;

import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.URL;

import static org.junit.Assert.assertTrue;

Expand Down Expand Up @@ -46,7 +48,7 @@ public static void tearDown() throws Exception {
}

@Rule
public BrowserWebDriverContainer browser = new BrowserWebDriverContainer()
public BrowserWebDriverContainer<?> browser = new BrowserWebDriverContainer<>("selenium/standalone-chrome:4")
.withCapabilities(new ChromeOptions());

@Test
Expand All @@ -55,7 +57,8 @@ public void testContainerRunningAgainstExposedHostPort() {
final String rootUrl =
String.format("http://host.testcontainers.internal:%d/", localServerPort);

final RemoteWebDriver webDriver = browser.getWebDriver();
URL seleniumAddress = browser.getSeleniumAddress();
RemoteWebDriver webDriver = new RemoteWebDriver(seleniumAddress, new ChromeOptions());
webDriver.get(rootUrl);
// }

Expand Down
21 changes: 7 additions & 14 deletions docs/modules/webdriver_containers.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@ from SeleniumHQ's [docker-selenium](https://github.com/SeleniumHQ/docker-seleniu

## Benefits

* Fully compatible with Selenium 2/Webdriver tests, by providing a `RemoteWebDriver` instance
* Fully compatible with Selenium 3 & 4 tests, by providing a `RemoteWebDriver` instance
* No need to have specific web browsers, or even a desktop environment, installed on test servers. The only dependency
is a working Docker installation and your Java JUnit test suite.
* Browsers are always launched from a fixed, clean image. This means no configuration drift from user changes or
automatic browser upgrades.
* Compatibility between browser version and the Selenium API is assured: a compatible version of the browser docker
images will be automatically selected to match the version of `selenium-api-*.jar` on the classpath
* Additionally the use of a clean browser prevents leakage of cookies, cached data or other state between tests.
* Additionally, the use of a clean browser prevents leakage of cookies, cached data or other state between tests.
* **VNC screen recording**: Testcontainers can automatically record video of test runs (optionally capturing just
failing tests)

Expand Down Expand Up @@ -80,10 +78,6 @@ If you would like to customise the file name of the recording, or provide a diff

Note the factory must implement `org.testcontainers.containers.RecordingFileFactory`.

## More examples

A few different examples are shown in [ChromeWebDriverContainerTest.java](https://github.com/testcontainers/testcontainers-java/blob/master/modules/selenium/src/test/java/org/testcontainers/junit/ChromeWebDriverContainerTest.java).

## Adding this module to your project dependencies

Add the following dependency to your `pom.xml`/`build.gradle` file:
Expand All @@ -102,18 +96,17 @@ testImplementation "org.testcontainers:selenium:{{latest_version}}"
```

!!! 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:
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:

```groovy tab='Gradle'
compile "org.seleniumhq.selenium:selenium-remote-driver:3.141.59"
implementation 'org.seleniumhq.selenium:selenium-java:4.1.1'
```

```xml tab='Maven'
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-remote-driver</artifactId>
<version>3.141.59</version>
<artifactId>selenium-java</artifactId>
<version>4.1.1</version>
</dependency>
```

Testcontainers will try and match the version of the Dockerized browser to whichever version of Selenium is found on the classpath
4 changes: 1 addition & 3 deletions examples/cucumber/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ repositories {
}

dependencies {
implementation 'org.seleniumhq.selenium:selenium-remote-driver:3.141.59'
implementation 'org.seleniumhq.selenium:selenium-firefox-driver:3.141.59'
implementation 'org.seleniumhq.selenium:selenium-chrome-driver:3.141.59'
testImplementation 'org.seleniumhq.selenium:selenium-java:4.1.1'
testImplementation 'io.cucumber:cucumber-java:7.1.0'
testImplementation 'io.cucumber:cucumber-junit:7.1.0'
testImplementation 'org.testcontainers:selenium'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.RemoteWebDriver;
Expand All @@ -21,7 +22,7 @@

public class Stepdefs {

private BrowserWebDriverContainer container = new BrowserWebDriverContainer()
private final BrowserWebDriverContainer<?> container = new BrowserWebDriverContainer<>("selenium/standalone-chrome:4")
.withCapabilities(new ChromeOptions())
.withRecordingMode(RECORD_ALL, new File("build"));

Expand Down Expand Up @@ -49,20 +50,20 @@ public String getFilesystemFriendlyName() {
}

@Given("^location is \"([^\"]*)\"$")
public void locationIs(String location) throws Exception {
public void locationIs(String location) {
this.location = location;
}

@When("^I ask is it possible to search here$")
public void iAskIsItPossibleToSearchHere() throws Exception {
RemoteWebDriver driver = container.getWebDriver();
public void iAskIsItPossibleToSearchHere() {
RemoteWebDriver driver = new RemoteWebDriver(container.getSeleniumAddress(), new ChromeOptions());
driver.get(location);
List<WebElement> searchInputs = driver.findElementsByTagName("input");
List<WebElement> searchInputs = driver.findElements(By.tagName("input"));
answer = searchInputs != null && searchInputs.size() > 0 ? "YES" : "NOPE";
}

@Then("^I should be told \"([^\"]*)\"$")
public void iShouldBeTold(String expected) throws Exception {
public void iShouldBeTold(String expected) {
assertEquals(expected, answer);
}

Expand Down
6 changes: 2 additions & 4 deletions modules/selenium/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ description = "Testcontainers :: Selenium"
dependencies {
api project(':testcontainers')

provided 'org.seleniumhq.selenium:selenium-remote-driver:3.141.59'
provided 'org.seleniumhq.selenium:selenium-chrome-driver:3.141.59'
testImplementation 'org.seleniumhq.selenium:selenium-firefox-driver:3.141.59'
testImplementation 'org.seleniumhq.selenium:selenium-support:3.141.59'
implementation 'org.seleniumhq.selenium:selenium-chrome-driver:4.1.0'

testImplementation 'org.seleniumhq.selenium:selenium-firefox-driver:4.1.0'
testImplementation 'org.mortbay.jetty:jetty:6.1.26'
testImplementation project(':nginx')
testImplementation 'org.rnorth.visible-assertions:visible-assertions:2.1.2'
Expand Down