Skip to content

Commit

Permalink
Use ChromeOptions and FirefoxOptions instead of DesiredCapabilities (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelpp authored and kiview committed Dec 4, 2018
1 parent a8e1708 commit 3ad8d80
Show file tree
Hide file tree
Showing 16 changed files with 136 additions and 58 deletions.
14 changes: 12 additions & 2 deletions examples/selenium-container/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,17 @@
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-remote-driver</artifactId>
<version>2.45.0</version>
<version>3.141.59</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-firefox-driver</artifactId>
<version>3.141.59</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-chrome-driver</artifactId>
<version>3.141.59</version>
</dependency>
<dependency>
<groupId>${testcontainers.group}</groupId>
Expand All @@ -37,4 +47,4 @@
</dependencies>


</project>
</project>
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import org.junit.Rule;
import org.junit.Test;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.testcontainers.containers.BrowserWebDriverContainer;

import java.io.File;
Expand All @@ -17,7 +17,7 @@ public class SeleniumContainerTest {

@Rule
public BrowserWebDriverContainer chrome = new BrowserWebDriverContainer()
.withDesiredCapabilities(DesiredCapabilities.chrome())
.withCapabilities(new ChromeOptions())
.withRecordingMode(RECORD_ALL, new File("target"));

@Test
Expand Down
5 changes: 4 additions & 1 deletion modules/selenium/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ description = "Testcontainers :: Selenium"
dependencies {
compile project(':testcontainers')

provided 'org.seleniumhq.selenium:selenium-remote-driver:2.52.0'
provided 'org.seleniumhq.selenium:selenium-remote-driver:3.141.59'
provided 'org.seleniumhq.selenium:selenium-chrome-driver:3.141.59'
testCompile 'org.seleniumhq.selenium:selenium-firefox-driver:3.141.59'
testCompile 'org.seleniumhq.selenium:selenium-support:3.141.59'

testCompile 'org.mortbay.jetty:jetty:6.1.25'
testCompile project(':nginx')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package org.testcontainers.containers;

import com.github.dockerjava.api.command.InspectContainerResponse;
import com.google.common.collect.ImmutableSet;

import com.github.dockerjava.api.command.InspectContainerResponse;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.BrowserType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
Expand Down Expand Up @@ -45,7 +49,7 @@ public class BrowserWebDriverContainer<SELF extends BrowserWebDriverContainer<SE
private static final int VNC_PORT = 5900;

@Nullable
private DesiredCapabilities desiredCapabilities;
private Capabilities capabilities;
private boolean customImageNameIsSet = false;

@Nullable
Expand Down Expand Up @@ -75,7 +79,7 @@ public BrowserWebDriverContainer() {

/**
* Constructor taking a specific webdriver container name and tag
* @param dockerImageName
* @param dockerImageName Name of the docker image to pull
*/
public BrowserWebDriverContainer(String dockerImageName) {
this();
Expand All @@ -86,8 +90,21 @@ public BrowserWebDriverContainer(String dockerImageName) {
}


public SELF withDesiredCapabilities(DesiredCapabilities desiredCapabilities) {
this.desiredCapabilities = desiredCapabilities;
public SELF withCapabilities(Capabilities capabilities) {
this.capabilities = capabilities;
return self();
}

/**
* @deprecated Use withCapabilities(Capabilities capabilities) instead:
* withCapabilities(new FirefoxOptions())
*
* @param capabilities DesiredCapabilities
* @return SELF
* */
@Deprecated
public SELF withDesiredCapabilities(DesiredCapabilities capabilities) {
this.capabilities = capabilities;
return self();
}

Expand All @@ -105,8 +122,16 @@ protected Set<Integer> getLivenessCheckPorts() {
@Override
protected void configure() {

if (desiredCapabilities == null) {
throw new IllegalStateException();
String seleniumVersion = SeleniumUtils.determineClasspathSeleniumVersion();

if (capabilities == null) {
if (seleniumVersion.startsWith("2.")) {
logger().info("No capabilities provided, falling back to DesiredCapabilities.chrome()");
capabilities = DesiredCapabilities.chrome();
} else {
logger().info("No capabilities provided, falling back to ChromeOptions");
capabilities = new ChromeOptions();
}
}

if (recordingMode != VncRecordingMode.SKIP) {
Expand All @@ -120,7 +145,7 @@ protected void configure() {
}

if (!customImageNameIsSet) {
super.setDockerImageName(getImageForCapabilities(desiredCapabilities));
super.setDockerImageName(getImageForCapabilities(capabilities, seleniumVersion));
}

String timeZone = System.getProperty("user.timezone");
Expand All @@ -140,11 +165,9 @@ protected void configure() {
setStartupAttempts(3);
}

public static String getImageForCapabilities(DesiredCapabilities desiredCapabilities) {
public static String getImageForCapabilities(Capabilities capabilities, String seleniumVersion) {

String seleniumVersion = SeleniumUtils.determineClasspathSeleniumVersion();

String browserName = desiredCapabilities.getBrowserName();
String browserName = capabilities.getBrowserName();
switch (browserName) {
case BrowserType.CHROME:
return String.format(CHROME_IMAGE, seleniumVersion);
Expand Down Expand Up @@ -184,7 +207,7 @@ protected void containerIsStarted(InspectContainerResponse containerInfo) {
driver = Unreliables.retryUntilSuccess(30, TimeUnit.SECONDS,
Timeouts.getWithTimeout(10, TimeUnit.SECONDS,
() ->
() -> new RemoteWebDriver(getSeleniumAddress(), desiredCapabilities)));
() -> new RemoteWebDriver(getSeleniumAddress(), capabilities)));

if (vncRecordingContainer != null) {
LOGGER.debug("Starting VNC recording");
Expand Down Expand Up @@ -282,6 +305,4 @@ public SELF withRecordingFileFactory(RecordingFileFactory recordingFileFactory)
public enum VncRecordingMode {
SKIP, RECORD_ALL, RECORD_FAILING
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public static String determineClasspathSeleniumVersion() {

/**
* Read Manifest to get Selenium Version.
* @param manifest
* @param manifest manifest
* @return Selenium Version detected
*/
public static String getSeleniumVersionFromManifest(Manifest manifest) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@

import org.jetbrains.annotations.NotNull;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testcontainers.containers.BrowserWebDriverContainer;

import java.util.List;
import java.util.concurrent.TimeUnit;

import static org.rnorth.visibleassertions.VisibleAssertions.assertEquals;
import static java.lang.String.format;
import static org.rnorth.visibleassertions.VisibleAssertions.assertTrue;

/**
Expand All @@ -20,12 +24,24 @@ protected void doSimpleWebdriverTest(BrowserWebDriverContainer rule) {
System.out.println("Selenium remote URL is: " + rule.getSeleniumAddress());
System.out.println("VNC URL is: " + rule.getVncAddress());

//Runtime.getRuntime().exec("open " + rule.getVncUrl(driver)); // For debugging, on a Mac

driver.get("http://www.google.com");
driver.findElement(By.name("q")).sendKeys("testcontainers");
driver.findElement(By.name("q")).submit();
assertEquals("the word 'testcontainers' appears in the search box", "testcontainers", driver.findElement(By.name("q")).getAttribute("value"));
WebElement search = driver.findElement(By.name("q"));
search.sendKeys("testcontainers");
search.submit();

List<WebElement> results = new WebDriverWait(driver, 15)
.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.cssSelector("#search h3")));

assertTrue("the word 'testcontainers' appears in search results",
results.stream()
.anyMatch(el -> el.getText().contains("testcontainers")));
}

protected void assertBrowserNameIs(BrowserWebDriverContainer rule, String expectedName) {
RemoteWebDriver driver = setupDriverFromRule(rule);
String actual = driver.getCapabilities().getBrowserName();
assertTrue(format("actual browser name is %s", actual),
actual.equals(expectedName));
}

@NotNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import org.junit.Test;
import org.junit.experimental.runners.Enclosed;
import org.junit.runner.RunWith;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.chrome.ChromeOptions;
import org.testcontainers.containers.BrowserWebDriverContainer;
import org.testcontainers.containers.DefaultRecordingFileFactory;

Expand All @@ -19,7 +19,7 @@ public static class ChromeThatRecordsAllTests {

@Rule
public BrowserWebDriverContainer chrome = new BrowserWebDriverContainer()
.withDesiredCapabilities(DesiredCapabilities.chrome())
.withCapabilities(new ChromeOptions())
.withRecordingMode(RECORD_ALL, new File("./build/"))
.withRecordingFileFactory(new DefaultRecordingFileFactory());

Expand All @@ -33,7 +33,7 @@ public static class ChromeThatRecordsFailingTests {

@Rule
public BrowserWebDriverContainer chrome = new BrowserWebDriverContainer()
.withDesiredCapabilities(DesiredCapabilities.chrome());
.withCapabilities(new ChromeOptions());

@Test
public void recordingTestThatShouldBeRecordedButDeleted() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
package org.testcontainers.junit;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.chrome.ChromeOptions;
import org.testcontainers.containers.BrowserWebDriverContainer;

import java.io.IOException;

/**
*
*/
public class ChromeWebDriverContainerTest extends BaseWebDriverContainerTest {

@Rule
public BrowserWebDriverContainer chrome = new BrowserWebDriverContainer()
.withDesiredCapabilities(DesiredCapabilities.chrome());
.withCapabilities(new ChromeOptions());

@Before
public void checkBrowserIsIndeedChrome() {
assertBrowserNameIs(chrome, "chrome");
}

@Test
public void simpleTest() throws IOException {
public void simpleTest() {
doSimpleWebdriverTest(chrome);
}

@Test
public void simpleExploreTest() throws IOException {
public void simpleExploreTest() {
doSimpleExplore(chrome);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.testcontainers.junit;

import org.junit.Rule;
import org.junit.Test;
import org.testcontainers.containers.BrowserWebDriverContainer;

public class ContainerWithoutCapabilitiesTest extends BaseWebDriverContainerTest{

@Rule
public BrowserWebDriverContainer chrome = new BrowserWebDriverContainer();

@Test
public void chromeIsStartedIfNoCapabilitiesProvided() {
assertBrowserNameIs(chrome, "chrome");
}

@Test
public void simpleExploreTestWhenNoCapabilitiesProvided(){
doSimpleExplore(chrome);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

import org.junit.Rule;
import org.junit.Test;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.chrome.ChromeOptions;
import org.testcontainers.containers.BrowserWebDriverContainer;

import java.io.IOException;
import java.time.Duration;

import static java.time.temporal.ChronoUnit.SECONDS;
Expand All @@ -17,11 +16,11 @@ public class CustomWaitTimeoutWebDriverContainerTest extends BaseWebDriverContai

@Rule
public BrowserWebDriverContainer chromeWithCustomTimeout = new BrowserWebDriverContainer<>()
.withDesiredCapabilities(DesiredCapabilities.chrome())
.withCapabilities(new ChromeOptions())
.withStartupTimeout(Duration.of(30, SECONDS));

@Test
public void simpleTest() throws IOException {
public void simpleTest() {
doSimpleWebdriverTest(chromeWithCustomTimeout);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package org.testcontainers.junit;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.testcontainers.containers.BrowserWebDriverContainer;

/**
Expand All @@ -12,7 +13,12 @@ public class FirefoxWebDriverContainerTest extends BaseWebDriverContainerTest {

@Rule
public BrowserWebDriverContainer firefox = new BrowserWebDriverContainer()
.withDesiredCapabilities(DesiredCapabilities.firefox());
.withCapabilities(new FirefoxOptions());

@Before
public void checkBrowserIsIndeedFirefox() {
assertBrowserNameIs(firefox, "firefox");
}

@Test
public void simpleTest() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import org.junit.Ignore;
import org.junit.Test;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testcontainers.containers.BrowserWebDriverContainer;

Expand All @@ -17,7 +17,7 @@ public class FlakyContainerCreationTest {
public void testCreationOfManyContainers() {
for (int i = 0; i < 50; i++) {
BrowserWebDriverContainer container = new BrowserWebDriverContainer()
.withDesiredCapabilities(DesiredCapabilities.chrome())
.withCapabilities(new ChromeOptions())
.withRecordingMode(BrowserWebDriverContainer.VncRecordingMode.RECORD_FAILING, new File("build"));

container.start();
Expand Down
Loading

0 comments on commit 3ad8d80

Please sign in to comment.