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

Remove dependency upon internet URLs for selenium tests #3271

Merged
merged 1 commit into from
Sep 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,62 +1,62 @@
package org.testcontainers.junit;

import org.jetbrains.annotations.NotNull;
import org.junit.ClassRule;
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 org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.Network;
import org.testcontainers.containers.wait.strategy.HttpWaitStrategy;
import org.testcontainers.utility.DockerImageName;

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

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

/**
*
*/
public class BaseWebDriverContainerTest {

protected void doSimpleWebdriverTest(BrowserWebDriverContainer rule) {
@ClassRule
public static Network NETWORK = Network.newNetwork();

@ClassRule
public static GenericContainer<?> HELLO_WORLD = new GenericContainer<>(DockerImageName.parse("testcontainers/helloworld:1.0.0"))
.withNetwork(NETWORK)
.withNetworkAliases("helloworld")
.withExposedPorts(8080, 8081)
.waitingFor(new HttpWaitStrategy());

protected static void doSimpleExplore(BrowserWebDriverContainer<?> rule) {
RemoteWebDriver driver = setupDriverFromRule(rule);
System.out.println("Selenium remote URL is: " + rule.getSeleniumAddress());
System.out.println("VNC URL is: " + rule.getVncAddress());

driver.get("http://www.google.com");
WebElement search = driver.findElement(By.name("q"));
search.sendKeys("testcontainers");
search.submit();
driver.get("http://helloworld:8080");
WebElement title = driver.findElement(By.tagName("h1"));

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")));
assertEquals("the index page contains the title 'Hello world'",
"Hello world",
title.getText().trim()
);
}

protected void assertBrowserNameIs(BrowserWebDriverContainer rule, String expectedName) {
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
private static RemoteWebDriver setupDriverFromRule(BrowserWebDriverContainer rule) {
private static RemoteWebDriver setupDriverFromRule(BrowserWebDriverContainer<?> rule) {
RemoteWebDriver driver = rule.getWebDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
return driver;
}

protected static void doSimpleExplore(BrowserWebDriverContainer rule) {
RemoteWebDriver driver = setupDriverFromRule(rule);
driver.get("http://en.wikipedia.org/wiki/Randomness");

// Oh! The irony!
assertTrue("Randomness' description has the word 'pattern'", driver.findElementByPartialLinkText("pattern").isDisplayed());
}
Comment on lines -54 to -60
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no real sense in having two ways to test that Selenium is working, so just removed this altogether.


}
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@ public void recordingTestThatShouldBeRecordedAndRetained() {
try (
// recordAll {
// To do this, simply add extra parameters to the rule constructor:
BrowserWebDriverContainer chrome = new BrowserWebDriverContainer()
BrowserWebDriverContainer<?> chrome = new BrowserWebDriverContainer<>()
.withCapabilities(new ChromeOptions())
.withRecordingMode(RECORD_ALL, target)
// }
.withRecordingFileFactory(new DefaultRecordingFileFactory())
.withNetwork(NETWORK)
) {
chrome.start();

Expand Down Expand Up @@ -69,12 +70,13 @@ public static class ChromeThatRecordsFailingTests {
public void recordingTestThatShouldBeRecordedButNotPersisted() {
try (
// withRecordingFileFactory {
BrowserWebDriverContainer chrome = new BrowserWebDriverContainer()
BrowserWebDriverContainer<?> chrome = new BrowserWebDriverContainer<>()
// }
.withCapabilities(new ChromeOptions())
// withRecordingFileFactory {
.withRecordingFileFactory(new CustomRecordingFileFactory())
// }
// }
.withNetwork(NETWORK)
) {
chrome.start();

Expand All @@ -88,11 +90,12 @@ public void recordingTestThatShouldBeRecordedAndRetained() {
try (
// recordFailing {
// or if you only want videos for test failures:
BrowserWebDriverContainer chrome = new BrowserWebDriverContainer()
BrowserWebDriverContainer<?> chrome = new BrowserWebDriverContainer<>()
.withCapabilities(new ChromeOptions())
.withRecordingMode(RECORD_FAILING, target)
// }
.withRecordingFileFactory(new DefaultRecordingFileFactory())
.withNetwork(NETWORK)
) {
chrome.start();

Expand All @@ -115,6 +118,7 @@ public String getFilesystemFriendlyName() {

}

private static class CustomRecordingFileFactory extends DefaultRecordingFileFactory { }
private static class CustomRecordingFileFactory extends DefaultRecordingFileFactory {
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,16 @@ public class ChromeWebDriverContainerTest extends BaseWebDriverContainerTest {

// junitRule {
@Rule
public BrowserWebDriverContainer chrome = new BrowserWebDriverContainer()
.withCapabilities(new ChromeOptions());
public BrowserWebDriverContainer<?> chrome = new BrowserWebDriverContainer<>()
.withCapabilities(new ChromeOptions())
// }
.withNetwork(NETWORK);

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

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

@Test
public void simpleExploreTest() {
doSimpleExplore(chrome);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
public class ContainerWithoutCapabilitiesTest extends BaseWebDriverContainerTest{

@Rule
public BrowserWebDriverContainer chrome = new BrowserWebDriverContainer();
public BrowserWebDriverContainer<?> chrome = new BrowserWebDriverContainer<>()
.withNetwork(NETWORK);

@Test
public void chromeIsStartedIfNoCapabilitiesProvided() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
public class CustomWaitTimeoutWebDriverContainerTest extends BaseWebDriverContainerTest {

@Rule
public BrowserWebDriverContainer chromeWithCustomTimeout = new BrowserWebDriverContainer<>()
.withCapabilities(new ChromeOptions())
.withStartupTimeout(Duration.of(30, SECONDS));
public BrowserWebDriverContainer<?> chromeWithCustomTimeout = new BrowserWebDriverContainer<>()
.withCapabilities(new ChromeOptions())
.withStartupTimeout(Duration.of(30, SECONDS))
.withNetwork(NETWORK);

@Test
public void simpleTest() {
doSimpleWebdriverTest(chromeWithCustomTimeout);
public void simpleExploreTest() {
doSimpleExplore(chromeWithCustomTimeout);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,16 @@ public class FirefoxWebDriverContainerTest extends BaseWebDriverContainerTest {

// junitRule {
@Rule
public BrowserWebDriverContainer firefox = new BrowserWebDriverContainer()
.withCapabilities(new FirefoxOptions());
public BrowserWebDriverContainer<?> firefox = new BrowserWebDriverContainer<>()
.withCapabilities(new FirefoxOptions())
// }
.withNetwork(NETWORK);

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

@Test
public void simpleTest() {
doSimpleWebdriverTest(firefox);
}

@Test
public void simpleExploreTest() {
doSimpleExplore(firefox);
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,8 @@ public class SpecificImageNameWebDriverContainerTest extends BaseWebDriverContai

@Rule
public BrowserWebDriverContainer<?> firefox = new BrowserWebDriverContainer<>(FIREFOX_IMAGE)
.withCapabilities(new FirefoxOptions());

@Test
public void simpleTest() {
doSimpleWebdriverTest(firefox);
}
.withCapabilities(new FirefoxOptions())
.withNetwork(NETWORK);

@Test
public void simpleExploreTest() {
Expand Down