Skip to content

Commit

Permalink
Adds new arquillian property to include command line args of Selenium…
Browse files Browse the repository at this point in the history
… Server & tests
  • Loading branch information
hemanik authored and MatousJobanek committed Feb 15, 2017
1 parent b4a2496 commit b2be16f
Show file tree
Hide file tree
Showing 8 changed files with 209 additions and 12 deletions.
9 changes: 8 additions & 1 deletion README.adoc
Expand Up @@ -334,7 +334,14 @@ your changes there first.

Prerequisites:

* running Selenium Server at port 4444, with paths to binaries required for remote browser execution. If you don't do it by yourself, Drone will run an instance of Selenium Server automatically. However in this case the server is started and stopped for each test class separately and only in a standalone mode. So, if you are fine with this automatic behavior you can skip this step, otherwise run your Selenium Server.
* running Selenium Server at port 4444, with paths to binaries required for remote browser execution. If you don't do it by yourself, Drone will run an instance of Selenium Server automatically. However in this case the server is started and stopped for each test class separately and only in a standalone mode with only one parameter `-port` that is taken from defined remoteAddress.
+
In case you want to specify other arguments to the selenium server, you can do so using the property,
+
`<property name="seleniumServerArgs">-debug -role hub -browserTimeout 1000</property>`
+
So, if you are fine with this automatic behavior you can skip this step, otherwise run your Selenium Server.

* installed web browsers you want to test
* you might want to align your layout to be the same as paths to binaries specified within arquillian.xml in _drone-webdriver_ module. Alternatively, you can override properties with
+-Darq.extension.${extensionQualifier}.${propertyName}+
Expand Down
Expand Up @@ -52,6 +52,8 @@ public class MockDroneConfiguration implements DroneConfiguration<MockDroneConfi

private String browserCapabilities;

private String seleniumServerArgs;

/*
* (non-Javadoc)
*
Expand Down Expand Up @@ -153,4 +155,12 @@ public void setBrowserCapabilities(String browserCapabilities) {
this.browserCapabilities = browserCapabilities;
}

public String getSeleniumServerArgs() {
return seleniumServerArgs;
}

public void setSeleniumServerArgs(String seleniumServerArgs) {
this.seleniumServerArgs = seleniumServerArgs;
}

}
@@ -1,6 +1,9 @@
package org.jboss.arquillian.drone.webdriver.binary.process;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
Expand Down Expand Up @@ -43,6 +46,7 @@ public class SeleniumServerExecutor {
*/
public void startSeleniumServer(@Observes StartSeleniumServer startSeleniumServer) {
String browser = startSeleniumServer.getBrowser();
String seleniumServerArgs = startSeleniumServer.getSeleniumServerArgs();
String seleniumServer = startSeleniumServer.getPathToSeleniumServerBinary();
int port = startSeleniumServer.getUrl().getPort();

Expand All @@ -67,8 +71,16 @@ public void startSeleniumServer(@Observes StartSeleniumServer startSeleniumServe
}

try {
Command build = javaCommand.parameters("-jar", seleniumServer, "-port", String.valueOf(port)).build();
List<String> parameterList = new ArrayList<>(Arrays.asList("-jar", seleniumServer, "-port", String.valueOf(port)));

if (seleniumServerArgs != null && !seleniumServerArgs.isEmpty()){
parameterList.addAll(Arrays.asList(seleniumServerArgs.split(" ")));
}

Command build = javaCommand.parameters(parameterList).build();

SeleniumServerExecution execution = new SeleniumServerExecution().execute(build);

seleniumServerExecutionInstanceProducer.set(execution);

} catch (Exception e) {
Expand Down
Expand Up @@ -14,13 +14,15 @@ public class StartSeleniumServer implements Event {
private String browser;
private DesiredCapabilities capabilities;
private URL url;
private String seleniumServerArgs;

public StartSeleniumServer(String pathToSeleniumServerBinary, String browser,
DesiredCapabilities capabilities, URL url) {
DesiredCapabilities capabilities, URL url, String seleniumServerArgs) {
this.pathToSeleniumServerBinary = pathToSeleniumServerBinary;
this.browser = browser;
this.capabilities = capabilities;
this.url = url;
this.seleniumServerArgs = seleniumServerArgs;
}

public String getPathToSeleniumServerBinary() {
Expand All @@ -31,6 +33,14 @@ public void setPathToSeleniumServerBinary(String pathToSeleniumServerBinary) {
this.pathToSeleniumServerBinary = pathToSeleniumServerBinary;
}

public String getSeleniumServerArgs() {
return seleniumServerArgs;
}

public void setSeleniumServerArgs(String seleniumServerArgs) {
this.seleniumServerArgs = seleniumServerArgs;
}

public String getBrowser() {
return browser;
}
Expand Down
Expand Up @@ -16,14 +16,6 @@
*/
package org.jboss.arquillian.drone.webdriver.configuration;

import java.lang.annotation.Annotation;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.jboss.arquillian.config.descriptor.api.ArquillianDescriptor;
import org.jboss.arquillian.drone.configuration.ConfigurationMapper;
import org.jboss.arquillian.drone.spi.DroneConfiguration;
Expand All @@ -32,6 +24,14 @@
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.lang.annotation.Annotation;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* Generic configuration for WebDriver Driver. By default, it uses HtmlUnit Driver.
*
Expand All @@ -55,6 +55,8 @@ public class WebDriverConfiguration implements DroneConfiguration<WebDriverConfi

public static final String DEFAULT_BROWSER_CAPABILITIES = new BrowserCapabilitiesList.HtmlUnit().getReadableName();

public static final String DEFAULT_SELENIUM_SERVER_ARGS = "";

private int iePort;

private URL remoteAddress;
Expand All @@ -65,6 +67,8 @@ public class WebDriverConfiguration implements DroneConfiguration<WebDriverConfi

private boolean remote;

private String seleniumServerArgs;

// ARQ-1206, ability to delete all cookies in reused browsers
private boolean reuseCookies;

Expand Down Expand Up @@ -126,6 +130,10 @@ public Capabilities getCapabilities() {
new DesiredCapabilities(this.capabilityMap));
}

public String getSeleniumServerArgs() {
return seleniumServerArgs;
}

@Override
public String getConfigurationName() {
return CONFIGURATION_NAME;
Expand Down Expand Up @@ -186,4 +194,8 @@ public void setReuseCookies(boolean reuseCookies) {
public void setDimensions(String dimensions) {
this.dimensions = dimensions;
}

public void setSeleniumServerArgs(String seleniumServerArgs) {
this.seleniumServerArgs = seleniumServerArgs;
}
}
Expand Up @@ -95,6 +95,13 @@ public RemoteWebDriver createInstance(WebDriverConfiguration configuration) {

Validate.isEmpty(configuration.getBrowser(), "The browser is not set.");

String seleniumServerArgs = configuration.getSeleniumServerArgs();
if (Validate.empty(seleniumServerArgs)) {
configuration.setSeleniumServerArgs(WebDriverConfiguration.DEFAULT_SELENIUM_SERVER_ARGS);
log.log(Level.INFO, "Property \"seleniumServerArgs\" was not specified, using default value of {0}",
WebDriverConfiguration.DEFAULT_SELENIUM_SERVER_ARGS);
}

// construct capabilities
DesiredCapabilities desiredCapabilities = new DesiredCapabilities(getCapabilities(configuration, true));

Expand All @@ -107,7 +114,7 @@ public RemoteWebDriver createInstance(WebDriverConfiguration configuration) {
new SeleniumServerBinaryHandler(desiredCapabilities).downloadAndPrepare().toString();
if (!Validate.empty(seleniumServer)) {
startSeleniumServerEvent
.fire(new StartSeleniumServer(seleniumServer, browser, desiredCapabilities, remoteAddress));
.fire(new StartSeleniumServer(seleniumServer, browser, desiredCapabilities, remoteAddress, seleniumServerArgs));
}
} catch (Exception e) {
throw new IllegalStateException(
Expand Down
@@ -0,0 +1,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);
}
}
Expand Up @@ -98,6 +98,7 @@ public void setupMocks() {

try {
String browser = System.getProperty("browser");
String seleniumServerArgs = System.getProperty("seleniumServerArgs");
String seleniumServerBinary =
new SeleniumServerBinaryHandler(new DesiredCapabilities()).downloadAndPrepare().toString();
} catch (Exception e) {
Expand All @@ -121,6 +122,7 @@ public void when_reusable_session_is_created_then_is_can_be_pulled_from_session_
when(configuration.isRemoteReusable()).thenReturn(true);
when(configuration.getCapabilities()).thenReturn(desiredCapabilities);
when(configuration.getRemoteAddress()).thenReturn(hubUrl);
when(configuration.getSeleniumServerArgs()).thenReturn("-debug");

// when
fire(new BeforeSuite());
Expand Down Expand Up @@ -149,6 +151,7 @@ public void when_session_is_created_and_persisted_but_nonreusable_then_next_crea
when(configuration.isRemoteReusable()).thenReturn(true);
when(configuration.getCapabilities()).thenReturn(desiredCapabilities);
when(configuration.getRemoteAddress()).thenReturn(hubUrl);
when(configuration.getSeleniumServerArgs()).thenReturn("-debug");

// when
fire(new BeforeSuite());
Expand Down

0 comments on commit b2be16f

Please sign in to comment.