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 (getNetwork() == null) { withNetwork(Network.SHARED); super.setDockerImageName(getImageForCapabilities(capabilities, seleniumVersion)); addExposedPorts(SELENIUM_PORT, VNC_PORT); addEnv("TZ", timeZone); addEnv("no_proxy", "localhost"); setCommand("/opt/bin/entry_point.sh"); setStartupAttempts(3);
/** * Remember any other containers this needs to link to. We have to pass these down to the container so that * the other containers will be initialized before linking occurs. * * @param otherContainer the container rule to link to * @param alias the alias (hostname) that this other container should be referred to by * @return this * * @deprecated Links are deprecated (see <a href="https://github.com/testcontainers/testcontainers-java/issues/465">#465</a>). Please use {@link Network} features instead. */ @Deprecated public SELF withLinkToContainer(LinkableContainer otherContainer, String alias) { addLink(otherContainer, alias); return self(); }
@Override public String getVncAddress() { return "vnc://vnc:secret@" + getContainerIpAddress() + ":" + getMappedPort(VNC_PORT); }
@Test @Ignore public void testCreationOfManyContainers() { for (int i = 0; i < 50; i++) { BrowserWebDriverContainer container = new BrowserWebDriverContainer() .withCapabilities(new ChromeOptions()) .withRecordingMode(BrowserWebDriverContainer.VncRecordingMode.RECORD_FAILING, new File("build")); container.start(); RemoteWebDriver driver = container.getWebDriver(); driver.get("http://www.google.com"); container.stop(); } } }
protected void doSimpleWebdriverTest(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(); 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"))); }
@Override protected void containerIsStarted(InspectContainerResponse containerInfo) { driver = Unreliables.retryUntilSuccess(30, TimeUnit.SECONDS, Timeouts.getWithTimeout(10, TimeUnit.SECONDS, () -> () -> new RemoteWebDriver(getSeleniumAddress(), capabilities))); if (vncRecordingContainer != null) { LOGGER.debug("Starting VNC recording"); vncRecordingContainer.start(); } }
@Test public void testConnection() { RemoteWebDriver driver = chrome.getWebDriver(); // Construct a URL that the browser container can access String hostIpAddress = chrome.getTestHostIpAddress(); driver.get("http://" + hostIpAddress + ":" + localPort); String headingText = driver.findElement(By.cssSelector("h1")).getText().trim(); assertEquals("The hardcoded success message was found on a page fetched from a local server", "It worked", headingText); } }
@NotNull private static RemoteWebDriver setupDriverFromRule(BrowserWebDriverContainer rule) { RemoteWebDriver driver = rule.getWebDriver(); driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); return driver; }
@NotNull @Override protected Set<Integer> getLivenessCheckPorts() { Integer seleniumPort = getMappedPort(SELENIUM_PORT); if (recordingMode == VncRecordingMode.SKIP) { return ImmutableSet.of(seleniumPort); } else { return ImmutableSet.of(seleniumPort, getMappedPort(VNC_PORT)); } }
@Override protected void containerIsStarted(InspectContainerResponse containerInfo) { driver = Unreliables.retryUntilSuccess(30, TimeUnit.SECONDS, Timeouts.getWithTimeout(10, TimeUnit.SECONDS, () -> () -> new RemoteWebDriver(getSeleniumAddress(), capabilities))); if (vncRecordingContainer != null) { LOGGER.debug("Starting VNC recording"); vncRecordingContainer.start(); } }
@Test public void testContainerRunningAgainstExposedHostPort() { // useHostExposedPort { final String rootUrl = String.format("http://host.testcontainers.internal:%d/", localServerPort); final RemoteWebDriver webDriver = browser.getWebDriver(); webDriver.get(rootUrl); // } final String pageSource = webDriver.getPageSource(); assertTrue(pageSource.contains("Hello World!")); } }
@NotNull @Override protected Set<Integer> getLivenessCheckPorts() { Integer seleniumPort = getMappedPort(SELENIUM_PORT); if (recordingMode == VncRecordingMode.SKIP) { return ImmutableSet.of(seleniumPort); } else { return ImmutableSet.of(seleniumPort, getMappedPort(VNC_PORT)); } }
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 (getNetwork() == null) { withNetwork(Network.SHARED); super.setDockerImageName(getImageForCapabilities(capabilities, seleniumVersion)); addExposedPorts(SELENIUM_PORT, VNC_PORT); addEnv("TZ", timeZone); addEnv("no_proxy", "localhost"); setCommand("/opt/bin/entry_point.sh"); setStartupAttempts(3);
public URL getSeleniumAddress() { try { return new URL("http", getContainerIpAddress(), getMappedPort(SELENIUM_PORT), "/wd/hub"); } catch (MalformedURLException e) { e.printStackTrace();// TODO return null; } }
/** * Remember any other containers this needs to link to. We have to pass these down to the container so that * the other containers will be initialized before linking occurs. * * @param otherContainer the container rule to link to * @param alias the alias (hostname) that this other container should be referred to by * @return this * * @deprecated Links are deprecated (see <a href="https://github.com/testcontainers/testcontainers-java/issues/465">#465</a>). Please use {@link Network} features instead. */ @Deprecated public SELF withLinkToContainer(LinkableContainer otherContainer, String alias) { addLink(otherContainer, alias); return self(); }
@Test public void testWebDriverToNginxContainerAccessViaContainerLink() { RemoteWebDriver driver = chrome.getWebDriver(); driver.get("http://nginx/"); assertEquals("Using selenium, an HTTP GET from the nginx server returns the index.html from the custom content directory", "This worked", driver.findElement(By.tagName("body")).getText()); } }
@Override public String getVncAddress() { return "vnc://vnc:secret@" + getContainerIpAddress() + ":" + getMappedPort(VNC_PORT); }
public URL getSeleniumAddress() { try { return new URL("http", getContainerIpAddress(), getMappedPort(SELENIUM_PORT), "/wd/hub"); } catch (MalformedURLException e) { e.printStackTrace();// TODO return null; } }