/
SeleniumServerTestCase.java
136 lines (101 loc) · 4.28 KB
/
SeleniumServerTestCase.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package org.jboss.arquillian.drone.webdriver.binary.process;
import org.jboss.arquillian.drone.webdriver.binary.handler.SeleniumServerBinaryHandler;
import org.jboss.arquillian.test.spi.event.suite.AfterSuite;
import org.jboss.arquillian.test.test.AbstractTestTestBase;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.URL;
import java.util.List;
import java.util.logging.Handler;
import java.util.logging.Logger;
import java.util.logging.StreamHandler;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static org.assertj.core.api.Assertions.assertThat;
public class SeleniumServerTestCase extends AbstractTestTestBase {
private static Logger log = Logger.getLogger(SeleniumServerExecutor.class.toString());
private OutputStream logCapturingStream;
private StreamHandler customLogHandler;
private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
private String seleniumServerBinary;
private DesiredCapabilities capabilities;
private URL url;
@Override
protected void addExtensions(List<Class<?>> extensions) {
extensions.add(SeleniumServerExecutor.class);
}
@Before
public void initialise() throws Exception {
seleniumServerBinary =
new SeleniumServerBinaryHandler(new DesiredCapabilities()).downloadAndPrepare().toString();
capabilities = new DesiredCapabilities();
url = new URL("http://localhost:5555/wd/hub/");
attachLogCapture();
setUpStreams();
}
@Test
public void should_start_selenium_server_with_serverArgs_debug() throws Exception {
final String browser = "chrome";
final String seleniumServerArgs = "-debug -role node";
fire(new StartSeleniumServer(seleniumServerBinary, browser, capabilities, url, seleniumServerArgs));
final String command = parseLogger();
assertThat(command).contains(seleniumServerArgs);
assertThat(outContent.toString()).contains("DEBUG");
}
@Test
public void should_start_selenium_server_with_no_serverArgs() throws Exception {
final String browser = "chrome";
fire(new StartSeleniumServer(seleniumServerBinary, browser, capabilities, url, null));
final String command = parseLogger();
assertThat(command).endsWith(String.valueOf(url.getPort()));
assertThat(outContent.toString()).contains("Selenium Server is up and running");
}
@Test
public void should_start_selenium_server_as_hub() throws Exception {
final String browser = "chrome";
String seleniumServerArgs = "-role hub -browserTimeout 1000";
fire(new StartSeleniumServer(seleniumServerBinary, browser, capabilities, url, seleniumServerArgs));
String hubCommand = parseLogger();
assertThat(hubCommand).contains(seleniumServerArgs);
assertThat(outContent.toString()).contains("Selenium Grid hub is up and running");
}
@After
public void stopSeleniumServer() {
cleanUpStreams();
fire(new AfterSuite());
}
private String parseLogger() throws IOException {
final String capturedLog = getTestCapturedLog();
final String expectedLogPart = "Running Selenium server process: java .*$";
final Pattern pattern = Pattern.compile(expectedLogPart);
final Matcher matcher = pattern.matcher(capturedLog);
String command;
if (matcher.find()) ;
{
command = matcher.group();
}
return command;
}
private void attachLogCapture() {
logCapturingStream = new ByteArrayOutputStream();
Handler[] handlers = log.getParent().getHandlers();
customLogHandler = new StreamHandler(logCapturingStream, handlers[0].getFormatter());
log.addHandler(customLogHandler);
}
private String getTestCapturedLog() throws IOException {
customLogHandler.flush();
return logCapturingStream.toString();
}
private void setUpStreams() {
System.setOut(new PrintStream(outContent));
}
private void cleanUpStreams() {
System.setOut(System.out);
}
}