This repository has been archived by the owner on Jan 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
HelloControllerIT.java
93 lines (76 loc) · 3.17 KB
/
HelloControllerIT.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package schulte.markus.seleniumstandalonechromespringboot.controller;
import com.palantir.docker.compose.DockerComposeRule;
import com.palantir.docker.compose.connection.DockerPort;
import com.palantir.docker.compose.connection.waiting.HealthChecks;
import io.github.bonigarcia.wdm.ChromeDriverManager;
import java.net.MalformedURLException;
import java.net.URL;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class HelloControllerIT {
/**
* IP of docker0 network on docker-host.
*
* In network_mode=bridge, this value should be fixed, see <a href="https://docs.docker.com/engine/userguide/networking/#the-default-bridge-network">docs.docker.com</a>
*/
private static final String DOCKER0_IP = "172.17.0.1";
private static final String DOCKER_COMPOSE_YML_FILE
= "src/test/resources/docker-compose-selenium-standalone-chrome.yml";
private static final int SELENIUM_HUB_PORT = 4444;
private static final String SELENIUM_STANDALONE_CHROME_SERVICE_NAME
= "selenium-standalone-chrome";
@ClassRule
public static final DockerComposeRule DOCKER_COMPOSE_RULE = DockerComposeRule.builder()
.file(DOCKER_COMPOSE_YML_FILE)
.pullOnStartup(true)
.waitingForService(SELENIUM_STANDALONE_CHROME_SERVICE_NAME,
HealthChecks.toRespond2xxOverHttp(SELENIUM_HUB_PORT,
(port) -> port.inFormat("http://$HOST:$EXTERNAL_PORT")))
.build();
private static WebDriver driver;
@LocalServerPort
private int port;
private URL base;
@BeforeClass
public static void initWebDriver() throws MalformedURLException {
ChromeDriverManager.getInstance().setup();
final DockerPort seleniumStandaloneDockerPort = DOCKER_COMPOSE_RULE.containers()
.container(SELENIUM_STANDALONE_CHROME_SERVICE_NAME).port(SELENIUM_HUB_PORT);
final String remoteSeleniumUrl
= seleniumStandaloneDockerPort.inFormat("http://$HOST:$EXTERNAL_PORT/wd/hub");
driver = new RemoteWebDriver(new URL(remoteSeleniumUrl), DesiredCapabilities.chrome());
}
@AfterClass
public static void closeWebDriver() {
driver.close();
}
@Before
public void setUp() throws MalformedURLException {
/*
* Chrome runs within a docker-container, but the actual Spring Boot application is executed
* on host. So, Chrome has to test against host-address.
*/
this.base = new URL("http://" + DOCKER0_IP + ":" + port);
}
@Test
public void getHello() {
driver.get(base.toString());
final WebElement h1Element = driver.findElement(By.id("h1-hello"));
Assert.assertEquals("Hello world!", h1Element.getText());
}
}