From b2be16f40f44fbc09df0bac53b33401c7caab772 Mon Sep 17 00:00:00 2001 From: hemanik Date: Tue, 24 Jan 2017 11:45:16 +0530 Subject: [PATCH] Adds new arquillian property to include command line args of Selenium Server & tests --- README.adoc | 9 +- .../configuration/MockDroneConfiguration.java | 10 ++ .../process/SeleniumServerExecutor.java | 14 +- .../binary/process/StartSeleniumServer.java | 12 +- .../configuration/WebDriverConfiguration.java | 28 ++-- .../factory/RemoteWebDriverFactory.java | 9 +- .../process/SeleniumServerTestCase.java | 136 ++++++++++++++++++ ...tRemoteWebDriverFactorySessionStoring.java | 3 + 8 files changed, 209 insertions(+), 12 deletions(-) create mode 100644 drone-webdriver/src/test/java/org/jboss/arquillian/drone/webdriver/binary/process/SeleniumServerTestCase.java diff --git a/README.adoc b/README.adoc index 7cf5760e..f47eba3b 100644 --- a/README.adoc +++ b/README.adoc @@ -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, ++ +`-debug -role hub -browserTimeout 1000` ++ +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}+ diff --git a/drone-configuration/src/test/java/org/jboss/arquillian/drone/configuration/MockDroneConfiguration.java b/drone-configuration/src/test/java/org/jboss/arquillian/drone/configuration/MockDroneConfiguration.java index c394dd66..457e574f 100644 --- a/drone-configuration/src/test/java/org/jboss/arquillian/drone/configuration/MockDroneConfiguration.java +++ b/drone-configuration/src/test/java/org/jboss/arquillian/drone/configuration/MockDroneConfiguration.java @@ -52,6 +52,8 @@ public class MockDroneConfiguration implements DroneConfiguration 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) { diff --git a/drone-webdriver/src/main/java/org/jboss/arquillian/drone/webdriver/binary/process/StartSeleniumServer.java b/drone-webdriver/src/main/java/org/jboss/arquillian/drone/webdriver/binary/process/StartSeleniumServer.java index c06c3ab6..2ac8acc9 100644 --- a/drone-webdriver/src/main/java/org/jboss/arquillian/drone/webdriver/binary/process/StartSeleniumServer.java +++ b/drone-webdriver/src/main/java/org/jboss/arquillian/drone/webdriver/binary/process/StartSeleniumServer.java @@ -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() { @@ -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; } diff --git a/drone-webdriver/src/main/java/org/jboss/arquillian/drone/webdriver/configuration/WebDriverConfiguration.java b/drone-webdriver/src/main/java/org/jboss/arquillian/drone/webdriver/configuration/WebDriverConfiguration.java index 533e22cc..9e09fb8e 100644 --- a/drone-webdriver/src/main/java/org/jboss/arquillian/drone/webdriver/configuration/WebDriverConfiguration.java +++ b/drone-webdriver/src/main/java/org/jboss/arquillian/drone/webdriver/configuration/WebDriverConfiguration.java @@ -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; @@ -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. * @@ -55,6 +55,8 @@ public class WebDriverConfiguration implements DroneConfiguration> 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); + } +} diff --git a/drone-webdriver/src/test/java/org/jboss/arquillian/drone/webdriver/factory/remote/reusable/TestRemoteWebDriverFactorySessionStoring.java b/drone-webdriver/src/test/java/org/jboss/arquillian/drone/webdriver/factory/remote/reusable/TestRemoteWebDriverFactorySessionStoring.java index ae19ba05..e527aad5 100644 --- a/drone-webdriver/src/test/java/org/jboss/arquillian/drone/webdriver/factory/remote/reusable/TestRemoteWebDriverFactorySessionStoring.java +++ b/drone-webdriver/src/test/java/org/jboss/arquillian/drone/webdriver/factory/remote/reusable/TestRemoteWebDriverFactorySessionStoring.java @@ -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) { @@ -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()); @@ -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());