Skip to content

Commit 9039acb

Browse files
stsefanenkobsideup
authored andcommitted
"exposing host port" example. Closes testcontainers#1136 (testcontainers#1152)
* added spring boot webapp test with exposed host port for BrowserWebDriverContainer * fixes after review * fixes after review update to random port * fixes after review update to static resource * fixes after review update initializer * fixes after review remove unused dependency
1 parent 9712ac1 commit 9039acb

File tree

4 files changed

+81
-14
lines changed

4 files changed

+81
-14
lines changed

examples/selenium-container/pom.xml

+27-10
Original file line numberDiff line numberDiff line change
@@ -11,40 +11,57 @@
1111

1212
<artifactId>selenium-container</artifactId>
1313

14+
<dependencyManagement>
15+
<dependencies>
16+
<dependency>
17+
<groupId>org.springframework.boot</groupId>
18+
<artifactId>spring-boot-dependencies</artifactId>
19+
<version>2.1.2.RELEASE</version>
20+
<type>pom</type>
21+
<scope>import</scope>
22+
</dependency>
23+
</dependencies>
24+
</dependencyManagement>
25+
1426
<dependencies>
1527
<dependency>
1628
<groupId>org.seleniumhq.selenium</groupId>
1729
<artifactId>selenium-remote-driver</artifactId>
18-
<version>3.141.59</version>
1930
</dependency>
2031
<dependency>
2132
<groupId>org.seleniumhq.selenium</groupId>
2233
<artifactId>selenium-firefox-driver</artifactId>
23-
<version>3.141.59</version>
2434
</dependency>
2535
<dependency>
2636
<groupId>org.seleniumhq.selenium</groupId>
2737
<artifactId>selenium-chrome-driver</artifactId>
28-
<version>3.141.59</version>
2938
</dependency>
3039
<dependency>
3140
<groupId>${testcontainers.group}</groupId>
3241
<artifactId>selenium</artifactId>
3342
<version>${testcontainers.version}</version>
3443
</dependency>
44+
3545
<dependency>
36-
<groupId>org.slf4j</groupId>
37-
<artifactId>slf4j-api</artifactId>
38-
<version>1.7.25</version>
39-
<scope>provided</scope>
46+
<groupId>org.springframework.boot</groupId>
47+
<artifactId>spring-boot-starter-web</artifactId>
4048
</dependency>
49+
4150
<dependency>
42-
<groupId>ch.qos.logback</groupId>
43-
<artifactId>logback-classic</artifactId>
44-
<version>1.2.3</version>
51+
<groupId>org.springframework.boot</groupId>
52+
<artifactId>spring-boot-starter-test</artifactId>
4553
<scope>test</scope>
4654
</dependency>
4755
</dependencies>
4856

57+
<build>
58+
<plugins>
59+
<plugin>
60+
<groupId>org.springframework.boot</groupId>
61+
<artifactId>spring-boot-maven-plugin</artifactId>
62+
<version>2.1.2.RELEASE</version>
63+
</plugin>
64+
</plugins>
65+
</build>
4966

5067
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.example;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class DemoApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(DemoApplication.class, args);
11+
}
12+
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE HTML>
2+
<html>
3+
<head>
4+
<title>Getting Started: Serving Web Content</title>
5+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
6+
</head>
7+
<body>
8+
<h>Hello World</h>
9+
</body>
10+
</html>

examples/selenium-container/src/test/java/SeleniumContainerTest.java

+31-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
1+
import com.example.DemoApplication;
12
import org.junit.Rule;
23
import org.junit.Test;
4+
import org.junit.runner.RunWith;
35
import org.openqa.selenium.WebElement;
4-
import org.openqa.selenium.remote.RemoteWebDriver;
56
import org.openqa.selenium.chrome.ChromeOptions;
7+
import org.openqa.selenium.remote.RemoteWebDriver;
8+
import org.springframework.boot.test.context.SpringBootTest;
9+
import org.springframework.boot.web.context.WebServerInitializedEvent;
10+
import org.springframework.boot.web.server.LocalServerPort;
11+
import org.springframework.context.ApplicationContextInitializer;
12+
import org.springframework.context.ApplicationListener;
13+
import org.springframework.context.ConfigurableApplicationContext;
14+
import org.springframework.test.context.ContextConfiguration;
15+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
16+
import org.testcontainers.Testcontainers;
617
import org.testcontainers.containers.BrowserWebDriverContainer;
718

819
import java.io.File;
@@ -14,8 +25,14 @@
1425
/**
1526
* Simple example of plain Selenium usage.
1627
*/
28+
@RunWith(SpringJUnit4ClassRunner.class)
29+
@SpringBootTest(classes = DemoApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
30+
@ContextConfiguration(initializers = SeleniumContainerTest.Initializer.class)
1731
public class SeleniumContainerTest {
1832

33+
@LocalServerPort
34+
private int port;
35+
1936
@Rule
2037
public BrowserWebDriverContainer chrome = new BrowserWebDriverContainer()
2138
.withCapabilities(new ChromeOptions())
@@ -25,9 +42,19 @@ public class SeleniumContainerTest {
2542
public void simplePlainSeleniumTest() {
2643
RemoteWebDriver driver = chrome.getWebDriver();
2744

28-
driver.get("https://wikipedia.org");
29-
List<WebElement> searchInput = driver.findElementsByName("search");
45+
driver.get("http://host.testcontainers.internal:" + port + "/foo.html");
46+
List<WebElement> hElement = driver.findElementsByTagName("h");
3047

31-
assertTrue("The search input box is found", searchInput != null && searchInput.size() > 0);
48+
assertTrue("The h element is found", hElement != null && hElement.size() > 0);
3249
}
50+
51+
public static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
52+
@Override
53+
public void initialize(ConfigurableApplicationContext applicationContext) {
54+
applicationContext.addApplicationListener((ApplicationListener<WebServerInitializedEvent>) event -> {
55+
Testcontainers.exposeHostPorts(event.getWebServer().getPort());
56+
});
57+
}
58+
}
59+
3360
}

0 commit comments

Comments
 (0)