Skip to content

Commit

Permalink
Merge pull request #200 from klieber/issue/200_web_driver_capabilities
Browse files Browse the repository at this point in the history
Can't use phantomjs.cli.args in webDriverCapabilities; fixes #80; fixes #81
  • Loading branch information
klieber committed Jan 9, 2014
2 parents c45b873 + 72c52bd commit 5425526
Show file tree
Hide file tree
Showing 8 changed files with 185 additions and 64 deletions.
@@ -1,5 +1,6 @@
package com.github.searls.jasmine.driver;

import org.openqa.selenium.Capabilities;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;

import com.gargoylesoftware.htmlunit.BrowserVersion;
Expand All @@ -11,11 +12,12 @@
* The default web driver - overridden to tweak a few things.
*/
public class QuietHtmlUnitDriver extends HtmlUnitDriver {
private final boolean debug;

public QuietHtmlUnitDriver(BrowserVersion version, boolean debug) {
super(version);
this.debug = debug;
private final boolean debug;

public QuietHtmlUnitDriver(Capabilities capabilities, boolean debug) {
super(capabilities);
this.debug = debug;
this.setJavascriptEnabled(true);
}

Expand Down
@@ -1,15 +1,17 @@
package com.github.searls.jasmine.driver;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.github.searls.jasmine.mojo.Capability;
import com.google.common.base.Objects;
import org.codehaus.plexus.util.StringUtils;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.remote.BrowserType;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.lang.reflect.Constructor;
import java.util.Collections;
import java.util.Map;
import java.util.List;

/**
* Creates a WebDriver for TestMojo using configured properties.
Expand All @@ -18,7 +20,7 @@ public class WebDriverFactory {
private boolean debug;
private String browserVersion;
private String webDriverClassName;
private Map<String, String> webDriverCapabilities;
private List<Capability> webDriverCapabilities;

public WebDriverFactory() {
setWebDriverCapabilities(null);
Expand All @@ -36,8 +38,8 @@ public void setWebDriverClassName(String webDriverClassName) {
this.webDriverClassName = webDriverClassName;
}

public void setWebDriverCapabilities(Map<String, String> webDriverCapabilities) {
this.webDriverCapabilities = Objects.firstNonNull(webDriverCapabilities, Collections.<String, String>emptyMap());
public void setWebDriverCapabilities(List<Capability> webDriverCapabilities) {
this.webDriverCapabilities = Objects.firstNonNull(webDriverCapabilities, Collections.<Capability>emptyList());
}

public WebDriver createWebDriver() throws Exception {
Expand Down Expand Up @@ -74,21 +76,31 @@ private Object[] getWebDriverConstructorArguments(Constructor<? extends WebDrive
return new Object[] {getCapabilities()};
}

private Capabilities getCapabilities() {
private DesiredCapabilities getCapabilities() {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setJavascriptEnabled(true);
for (Map.Entry<String, String> entry : webDriverCapabilities.entrySet()) {
capabilities.setCapability(entry.getKey(), entry.getValue());
}
return capabilities;
}

for (Capability capability : webDriverCapabilities) {
if (StringUtils.isNotBlank(capability.getValue())) {
capabilities.setCapability(capability.getName(),capability.getValue());
} else if (capability.getList() != null && !capability.getList().isEmpty()) {
capabilities.setCapability(capability.getName(),capability.getList());
} else if (capability.getMap() != null && !capability.getMap().isEmpty()) {
capabilities.setCapability(capability.getName(),capability.getMap());
}
}

private BrowserVersion getBrowserVersion() throws Exception {
return (BrowserVersion) BrowserVersion.class.getField(browserVersion).get(BrowserVersion.class);
return capabilities;
}

private WebDriver createDefaultWebDriver() throws Exception {
return new QuietHtmlUnitDriver(getBrowserVersion(), debug);
DesiredCapabilities capabilities = getCapabilities();
if (StringUtils.isBlank(capabilities.getBrowserName())) {
capabilities.setBrowserName(BrowserType.HTMLUNIT);
}
if (StringUtils.isBlank(capabilities.getVersion())) {
capabilities.setVersion(browserVersion.replaceAll("(\\D+)_(\\d.*)?", "$1-$2").replaceAll("_", " ").toLowerCase());
}
return new QuietHtmlUnitDriver(getCapabilities(), debug);
}
}
Expand Up @@ -6,6 +6,7 @@
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
Expand Down Expand Up @@ -65,28 +66,47 @@ public abstract class AbstractJasmineMojo extends AbstractMojo implements Jasmin
protected String webDriverClassName;

/**
* Web driver capabilities used to initialize a DesiredCapabilities instance when creating a web driver.
* <p/>
* This property will be ignored if org.openqa.selenium.htmlunit.HtmlUnitDriver is used; use the browserVersion
* property instead.
* <p/>
* For org.openqa.selenium.phantomjs.PhantomJSDriver, include "phantomjs.binary.path" if phantomJS is not in the
* system command path of the build machine.
* <p>Web driver capabilities used to initialize a DesiredCapabilities instance when creating a web driver.</p>
*
* <p>Capabilities value can be either a String, a List, or a Map.</p>
*
* <p>Example:</p>
* <pre>
* &lt;webDriverCapabilities&gt;
* &lt;capability&gt;
* &lt;name&gt;phantomjs.binary.path&lt;/name&gt;
* &lt;value&gt;/opt/phantomjs/bin/phantomjs&lt;/value&gt;
* &lt;/capability&gt;
* &lt;capability&gt;
* &lt;name&gt;phantomjs.cli.args&lt;/name&gt;
* &lt;list&gt;
* &lt;value&gt;--disk-cache=true&lt;/value&gt;
* &lt;value&gt;--max-disk-cache-size=256&lt;/value&gt;
* &lt;/list&gt;
* &lt;/capability&gt;
* &lt;capability&gt;
* &lt;name&gt;proxy&lt;/name&gt;
* &lt;map&gt;
* &lt;httpProxy&gt;myproxyserver.com:8000&lt;/httpProxy&gt;
* &lt;/map&gt;
* &lt;/capability&gt;
* &lt;/webDriverCapabilities&gt;
* </pre>
*
* @since 1.3.1.1
*/
@Parameter
protected Map<String, String> webDriverCapabilities;
@Parameter
protected List<Capability> webDriverCapabilities = Collections.emptyList();

/**
* <p>Determines the browser and version profile that HtmlUnit will simulate. This setting does nothing if the plugin is configured not to use HtmlUnit.
* This maps 1-to-1 with the public static instances found in {@link com.gargoylesoftware.htmlunit.BrowserVersion}.</p>
*
* <p>Some valid examples: CHROME, FIREFOX_3_6, INTERNET_EXPLORER_7, INTERNET_EXPLORER_8, INTERNET_EXPLORER_9</p>
* <p>Some valid examples: CHROME, FIREFOX_17, INTERNET_EXPLORER_9, INTERNET_EXPLORER_10</p>
*
* @since 1.1.0
*/
@Parameter(defaultValue="FIREFOX_3_6")
@Parameter(defaultValue="FIREFOX_17")
protected String browserVersion;

/**
Expand Down Expand Up @@ -273,6 +293,8 @@ public abstract class AbstractJasmineMojo extends AbstractMojo implements Jasmin
protected boolean keepServerAlive;

/**
* <p>Allows specifying which source files should be included and in what order.</p>
* <pre>
* &lt;sourceIncludes&gt;
* &lt;include&gt;vendor/&#42;&#42;/&#42;.js&lt;/include&gt;
* &lt;include&gt;myBootstrapFile.js&lt;/include&gt;
Expand Down Expand Up @@ -415,6 +437,7 @@ public abstract class AbstractJasmineMojo extends AbstractMojo implements Jasmin

@Override
public void execute() throws MojoExecutionException, MojoFailureException {

this.loadResources();

this.sources = new ScriptSearch(this.jsSrcDir,this.sourceIncludes,this.sourceExcludes);
Expand Down
44 changes: 44 additions & 0 deletions src/main/java/com/github/searls/jasmine/mojo/Capability.java
@@ -0,0 +1,44 @@
package com.github.searls.jasmine.mojo;

import java.util.List;
import java.util.Map;

public class Capability {

private String name;
private String value;
private List<String> list;
private Map<String,String> map;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}

public List<String> getList() {
return list;
}

public void setList(List<String> list) {
this.list = list;
}

public Map<String, String> getMap() {
return map;
}

public void setMap(Map<String, String> map) {
this.map = map;
}
}
82 changes: 53 additions & 29 deletions src/site/markdown/phantomjs.md
@@ -1,5 +1,5 @@
Using with PhantomJS
===================================
====================
Starting with version `1.3.1.1` it is possible to configure the jasmine-maven-plugin to use [PhantomJS](http://phantomjs.org) instead of [HtmlUnit](http://htmlunit.sourceforge.net/) to execute your specs.

Here is an example configuration:
Expand All @@ -25,43 +25,66 @@ Here is an example configuration:
</plugins>
</build>
```
The above configuration assumes that the `phantomjs` binary is on your systems `PATH`.
The above configuration assumes that the `phantomjs` binary is on your systems `PATH`. If you would prefer, you can also specify the location of the binary using a configuration like this:

If you would prefer, you can also use [klieber's phantomjs-maven-plugin](https://github.com/klieber/phantomjs-maven-plugin) to pull down a version of phantomjs:
```
<plugin>
<groupId>com.github.klieber</groupId>
<artifactId>phantomjs-maven-plugin</artifactId>
<version>0.2.1</version>
<executions>
<execution>
<goals>
<goal>install</goal>
</goals>
</execution>
</executions>
<configuration>
<version>1.9.2</version>
</configuration>
</plugin>
<build>
<plugins>
<plugin>
<groupId>com.github.searls</groupId>
<artifactId>jasmine-maven-plugin</artifactId>
<version>${jasmine-plugin-version}</version>
<executions>
<execution>
<goals>
<goal>test</goal>
</goals>
<configuration>
<webDriverClassName>org.openqa.selenium.phantomjs.PhantomJSDriver</webDriverClassName>
<webDriverCapabilities>
<capability>
<name>phantomjs.binary.path</name>
<value>/opt/phantomjs/bin/phantomjs</name>
</capability>
</webDriverCapabilities>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
```
If you use com.github.klieber.phantomjs-maven-plugin edit the jasmine-maven-plugin configuration to point to the phantomjs that gets installed dynamically:

```
<webDriverCapabilities>
<phantomjs.binary.path>${phantomjs.binary}</phantomjs.binary.path>
</webDriverCapabilities>
```
For more information on configuration options for PhantomJSDriver see its [documentation](https://github.com/detro/ghostdriver).

Automatically installing phantomjs
----------------------------------
One of the downsides of using phantomjs instead of HtmlUnit is it requires native binaries be present on the system you are running your build on. The [phantomjs-maven-plugin](https://klieber.github.io/phantomjs-maven-plugin) solves that problem by automatically pulling down phantomjs when needed.

If you would prefer, you can also specify the location of the binary using a configuration like this:
Here's an example using `phantomjs-maven-plugin` with the `jasmine-maven-plugin`:

```
<build>
<plugins>
<plugin>
<groupId>com.github.klieber</groupId>
<artifactId>phantomjs-maven-plugin</artifactId>
<version>${phantomjs-maven-plugin.version}</version>
<executions>
<execution>
<goals>
<goal>install</goal>
</goals>
</execution>
</executions>
<configuration>
<version>1.9.2</version>
</configuration>
</plugin>
<plugin>
<groupId>com.github.searls</groupId>
<artifactId>jasmine-maven-plugin</artifactId>
<version>${jasmine-plugin-version}</version>
<version>${jasmine-maven-plugin-version}</version>
<executions>
<execution>
<goals>
Expand All @@ -70,7 +93,10 @@ If you would prefer, you can also specify the location of the binary using a con
<configuration>
<webDriverClassName>org.openqa.selenium.phantomjs.PhantomJSDriver</webDriverClassName>
<webDriverCapabilities>
<phantomjs.binary.path>/opt/phantomjs/bin/phantomjs</phantomjs.binary.path>
<capability>
<name>phantomjs.binary.path</name>
<value>${phantomjs.binary}</name>
</capability>
</webDriverCapabilities>
</configuration>
</execution>
Expand All @@ -79,5 +105,3 @@ If you would prefer, you can also specify the location of the binary using a con
</plugins>
</build>
```

For more information on configuration options for PhantomJSDriver see its [documentation](https://github.com/detro/ghostdriver).
Expand Up @@ -8,6 +8,7 @@
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.openqa.selenium.remote.DesiredCapabilities;

import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.isA;
Expand All @@ -18,13 +19,18 @@
*/
@RunWith(MockitoJUnitRunner.class)
public class QuietHtmlUnitDriverTest {

@Mock
private WebClient mockWebClient;

@Mock
private DesiredCapabilities capabilities;

private QuietHtmlUnitDriver driver;
private boolean debug = false;

private void createDriver() {
driver = new QuietHtmlUnitDriver(BrowserVersion.FIREFOX_3_6, debug);
driver = new QuietHtmlUnitDriver(capabilities, debug);
}

@Test
Expand Down
@@ -1,5 +1,7 @@
package com.github.searls.jasmine.driver;

import com.github.searls.jasmine.mojo.Capability;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -60,7 +62,10 @@ public void enablesJavascriptOnCustomDriver() throws Exception {

@Test
public void setsCapabilityFromMap() throws Exception {
factory.setWebDriverCapabilities(ImmutableMap.of("foo", "bar"));
Capability capability = new Capability();
capability.setName("foo");
capability.setValue("bar");
factory.setWebDriverCapabilities(ImmutableList.of(capability));

assertEquals("bar", createWebDriverAndReturnCapabilities().getCapability("foo"));
}
Expand Down

0 comments on commit 5425526

Please sign in to comment.