forked from testcontainers/testcontainers-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocalServerWebDriverContainerTest.java
More file actions
62 lines (51 loc) · 2.09 KB
/
LocalServerWebDriverContainerTest.java
File metadata and controls
62 lines (51 loc) · 2.09 KB
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
package org.testcontainers.junit;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.bio.SocketConnector;
import org.mortbay.jetty.handler.ResourceHandler;
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testcontainers.Testcontainers;
import org.testcontainers.containers.BrowserWebDriverContainer;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Test that a browser running in a container can access a web server hosted on the host machine (i.e. the one running
* the tests)
*/
public class LocalServerWebDriverContainerTest {
@Rule
public BrowserWebDriverContainer<?> chrome = new BrowserWebDriverContainer<>()
.withAccessToHost(true)
.withCapabilities(new ChromeOptions());
private int localPort;
@Before
public void setupLocalServer() throws Exception {
// Set up a local Jetty HTTP server
Server server = new Server();
server.addConnector(new SocketConnector());
ResourceHandler resourceHandler = new ResourceHandler();
resourceHandler.setResourceBase("src/test/resources/server");
server.addHandler(resourceHandler);
server.start();
// The server will have a random port assigned, so capture that
localPort = server.getConnectors()[0].getLocalPort();
}
@Test
public void testConnection() {
// getWebDriver {
RemoteWebDriver driver = new RemoteWebDriver(chrome.getSeleniumAddress(), new ChromeOptions());
// }
// Construct a URL that the browser container can access
// getPage {
Testcontainers.exposeHostPorts(localPort);
driver.get("http://host.testcontainers.internal:" + localPort);
// }
String headingText = driver.findElement(By.cssSelector("h1")).getText().trim();
assertThat(headingText)
.as("The hardcoded success message was found on a page fetched from a local server")
.isEqualTo("It worked");
}
}