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 package info.magnolia.integrationtests.docker;
35
36 import org.junit.runner.Description;
37 import org.testcontainers.containers.BindMode;
38 import org.testcontainers.containers.GenericContainer;
39 import org.testcontainers.containers.SeleniumUtils;
40 import org.testcontainers.containers.traits.LinkableContainer;
41 import org.testcontainers.containers.wait.LogMessageWaitStrategy;
42
43
44
45
46
47 public class SeleniumGridNodeContainer extends GenericContainer<SeleniumGridNodeContainer> implements LinkableContainer {
48
49 private static final String CHROME_IMAGE = "selenium/node-chrome%s:%s";
50 private static final String FIREFOX_IMAGE = "selenium/node-firefox%s:%s";
51
52 private static final int VNC_PORT = 5900;
53
54 private boolean debug = false;
55
56 private String targetBrowser;
57
58 public SeleniumGridNodeContainer() {
59 this.waitStrategy = new LogMessageWaitStrategy().withRegEx(".*node is registered.*\n");
60 }
61
62 @Override
63 protected Integer getLivenessCheckPort() {
64 return getMappedPort(VNC_PORT);
65 }
66
67 public SeleniumGridNodeContainer withDebugMode(boolean isDebugModeOn) {
68 this.debug = isDebugModeOn;
69 return this;
70 }
71
72 public SeleniumGridNodeContainer withTargetBrowser(String targetBrowser) {
73 this.targetBrowser = targetBrowser;
74 return this;
75 }
76
77 public SeleniumGridNodeContainer withSeleniumHubHost(String seleniumHubHost) {
78 return withEnv("HUB_PORT_4444_TCP_ADDR", seleniumHubHost);
79 }
80
81 public SeleniumGridNodeContainer withSeleniumHubPort(int seleniumHubPort) {
82 return withEnv("HUB_PORT_4444_TCP_PORT", String.valueOf(seleniumHubPort));
83 }
84
85 public SeleniumGridNodeContainer withUploadDirectory(String uploadDirectory) {
86 return withFileSystemBind(uploadDirectory, "/var/upload", BindMode.READ_ONLY);
87 }
88
89 public SeleniumGridNodeContainer withTestNetwork(String testNetwork) {
90 return withCreateContainerCmdModifier(cmd -> cmd.withNetworkMode(testNetwork));
91 }
92
93 @Override
94 protected void configure() {
95
96 String timeZone = System.getProperty("user.timezone");
97
98 if (timeZone == null || timeZone.isEmpty()) {
99 timeZone = "Etc/UTC";
100 }
101
102 addFileSystemBind("/dev/shm", "/dev/shm", BindMode.READ_WRITE);
103 setDockerImageName(getImageForSelectedBrowser());
104
105 addExposedPorts(VNC_PORT);
106 addEnv("TZ", timeZone);
107 addEnv("no_proxy", "localhost");
108 setCommand("/opt/bin/entry_point.sh");
109
110 addEnv("SCREEN_DEPTH", "24");
111 addEnv("SCREEN_WIDTH", "1280");
112 addEnv("SCREEN_HEIGHT", "1024");
113
114
115
116
117 setStartupAttempts(3);
118 }
119
120 public String getImageForSelectedBrowser() {
121
122 final String seleniumVersion = SeleniumUtils.determineClasspathSeleniumVersion();
123 final String debugSuffix = debug ? "-debug" : "";
124
125 switch (targetBrowser) {
126 case "chrome":
127 return String.format(CHROME_IMAGE, debugSuffix, seleniumVersion);
128 case "firefox":
129 return String.format(FIREFOX_IMAGE, debugSuffix, seleniumVersion);
130 default:
131 throw new UnsupportedOperationException(String.format("Browser name must be 'chrome' or 'firefox'; provided '%s' is not supported", targetBrowser));
132 }
133 }
134
135 @Override
136 protected void finished(Description description) {
137 this.stop();
138 }
139 }
140