Skip to content

Commit

Permalink
integrates phantomjs directly
Browse files Browse the repository at this point in the history
  • Loading branch information
klieber committed May 10, 2015
1 parent 7c2ee71 commit 8806af6
Show file tree
Hide file tree
Showing 8 changed files with 360 additions and 137 deletions.
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@
</properties>

<prerequisites>
<maven>3.0.4</maven>
<maven>3.1.0</maven>
</prerequisites>

<dependencies>
Expand Down Expand Up @@ -246,6 +246,11 @@
<artifactId>htmlunit</artifactId>
<version>2.14</version>
</dependency>
<dependency>
<groupId>com.github.klieber</groupId>
<artifactId>phantomjs-maven-core</artifactId>
<version>0.6</version>
</dependency>

<!-- Plugin test deps -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.github.searls.jasmine.driver;

import com.github.klieber.phantomjs.locate.PhantomJsLocator;
import com.github.klieber.phantomjs.locate.PhantomJsLocatorOptions;
import com.github.klieber.phantomjs.locate.RepositoryDetails;
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.phantomjs.PhantomJSDriver;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.lang.reflect.Constructor;
Expand All @@ -21,6 +24,8 @@ public class WebDriverFactory {
private String browserVersion;
private String webDriverClassName;
private List<Capability> webDriverCapabilities;
private PhantomJsLocatorOptions phantomJsLocatorOptions;
private RepositoryDetails repositoryDetails;

public WebDriverFactory() {
setWebDriverCapabilities(null);
Expand All @@ -42,9 +47,27 @@ public void setWebDriverCapabilities(List<Capability> webDriverCapabilities) {
this.webDriverCapabilities = Objects.firstNonNull(webDriverCapabilities, Collections.<Capability>emptyList());
}

public PhantomJsLocatorOptions getPhantomJsLocatorOptions() {
return phantomJsLocatorOptions;
}

public void setPhantomJsLocatorOptions(PhantomJsLocatorOptions phantomJsLocatorOptions) {
this.phantomJsLocatorOptions = phantomJsLocatorOptions;
}

public RepositoryDetails getRepositoryDetails() {
return repositoryDetails;
}

public void setRepositoryDetails(RepositoryDetails repositoryDetails) {
this.repositoryDetails = repositoryDetails;
}

public WebDriver createWebDriver() throws Exception {
if (HtmlUnitDriver.class.getName().equals(webDriverClassName)) {
return createDefaultWebDriver();
if (PhantomJSDriver.class.getName().equals(webDriverClassName)) {
return createPhantomJsWebDriver();
} else if (HtmlUnitDriver.class.getName().equals(webDriverClassName)) {
return createHtmlUnitWebDriver();
} else {
return createCustomWebDriver();
}
Expand Down Expand Up @@ -85,6 +108,11 @@ private Object[] getWebDriverConstructorArguments(Constructor<? extends WebDrive

private DesiredCapabilities getCapabilities() {
DesiredCapabilities capabilities = new DesiredCapabilities();
customizeCapabilities(capabilities);
return capabilities;
}

private void customizeCapabilities(DesiredCapabilities capabilities) {
capabilities.setJavascriptEnabled(true);

for (Capability capability : webDriverCapabilities) {
Expand All @@ -97,18 +125,27 @@ private DesiredCapabilities getCapabilities() {
capabilities.setCapability(capability.getName(),capability.getMap());
}
}

return capabilities;
}

private WebDriver createDefaultWebDriver() throws Exception {
DesiredCapabilities capabilities = getCapabilities();
if (StringUtils.isBlank(capabilities.getBrowserName())) {
capabilities.setBrowserName(BrowserType.HTMLUNIT);
}
if (StringUtils.isBlank(capabilities.getVersion())) {
private WebDriver createHtmlUnitWebDriver() throws Exception {
DesiredCapabilities capabilities = DesiredCapabilities.htmlUnitWithJs();
if (StringUtils.isNotBlank(capabilities.getVersion())) {
capabilities.setVersion(browserVersion.replaceAll("(\\D+)_(\\d.*)?", "$1-$2").replaceAll("_", " ").toLowerCase());
}
return new QuietHtmlUnitDriver(getCapabilities(), debug);
customizeCapabilities(capabilities);
return new QuietHtmlUnitDriver(capabilities, debug);
}

private WebDriver createPhantomJsWebDriver() throws Exception {
DesiredCapabilities capabilities = DesiredCapabilities.phantomjs();
customizeCapabilities(getCapabilities());

if (capabilities.getCapability("phantomjs.binary.path") == null) {
PhantomJsLocator locator = new PhantomJsLocator(this.phantomJsLocatorOptions, this.repositoryDetails);
String phantomJsPath = locator.locate();
capabilities.setCapability("phantomjs.binary.path", phantomJsPath);
}

return new PhantomJSDriver(capabilities);
}
}
113 changes: 10 additions & 103 deletions src/main/java/com/github/searls/jasmine/mojo/AbstractJasmineMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,82 +45,6 @@ public abstract class AbstractJasmineMojo extends AbstractMojo implements Jasmin
defaultValue="${project.basedir}${file.separator}src${file.separator}test${file.separator}javascript")
private File jsTestSrcDir;

/**
* Determines the Selenium WebDriver class we'll use to execute the tests. See the Selenium documentation for more details.
* The plugin uses <a href="http://htmlunit.sourceforge.net/">HtmlUnit</a> by default.
*
* <p>Some valid examples:</p>
* <ul>
* <li>org.openqa.selenium.htmlunit.HtmlUnitDriver</li>
* <li>org.openqa.selenium.phantomjs.PhantomJSDriver</li>
* <li>org.openqa.selenium.firefox.FirefoxDriver</li>
* <li>org.openqa.selenium.ie.InternetExplorerDriver</li>
* </ul>
* <p></p>
* For org.openqa.selenium.phantomjs.PhantomJSDriver, see the webDriverCapabilities property.
*
* @since 1.1.0
*/
@Parameter(defaultValue="org.openqa.selenium.htmlunit.HtmlUnitDriver")
protected String webDriverClassName;

/**
* <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 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_17, INTERNET_EXPLORER_9, INTERNET_EXPLORER_10</p>
*
* @since 1.1.0
*/
@Parameter(defaultValue="FIREFOX_17")
protected String browserVersion;

/**
* <p>Determines the format that jasmine:test will print to console.</p>
* <p>Valid options:</p>
* <ul>
* <li>"documentation" - (default) - print specs in a nested format</li>
* <li>"progress" - more terse, with a period for a passed specs and an 'F' for failures (e.g. '...F...')</li>
* </ul>
*
* @since 1.1.0
*/
@Parameter(defaultValue="documentation")
protected String format;

/**
* <p>JavaScript sources (typically vendor/lib dependencies) that need to be loaded
* before other sources (and specs) in a particular order. Each source will first be
Expand Down Expand Up @@ -250,14 +174,6 @@ public abstract class AbstractJasmineMojo extends AbstractMojo implements Jasmin
@Parameter(defaultValue="ManualSpecRunner.html")
protected String manualSpecRunnerHtmlFileName;

/**
* The name of the generated JUnit XML report.
*
* @since 1.1.0
*/
@Parameter(defaultValue="TEST-jasmine.xml")
protected String junitXmlReportFileName;

/**
* The name of the directory the specs will be deployed to on the server.
*
Expand All @@ -282,15 +198,6 @@ public abstract class AbstractJasmineMojo extends AbstractMojo implements Jasmin
@Parameter(defaultValue="${project.build.sourceEncoding}")
protected String sourceEncoding;

/**
* Keep the server alive after the <code>jasmine:test</code> goal exists.
* Useful if you need to run further analysis on your tests, like collecting code coverage.
*
* @since 1.3.1.0
*/
@Parameter(property="keepServerAlive", defaultValue="false")
protected boolean keepServerAlive;

/**
* <p>Allows specifying which source files should be included and in what order.</p>
* <pre>
Expand Down Expand Up @@ -376,16 +283,16 @@ public abstract class AbstractJasmineMojo extends AbstractMojo implements Jasmin
@Parameter(property="jasmine.uriScheme", defaultValue="http")
protected String uriScheme;

/**
* <p>Not used by the <code>jasmine:bdd</code> goal.</p>
*
* <p>The <code>jasmine:test</code> goal to specify hostname where the server is running. Useful when using
* the RemoteWebDriver.</p>
*
* @since 1.3.1.4
*/
@Parameter(property="jasmine.serverHostname", defaultValue = "localhost")
protected String serverHostname;
/**
* <p>Not used by the <code>jasmine:bdd</code> goal.</p>
*
* <p>The <code>jasmine:test</code> goal to specify hostname where the server is running. Useful when using
* the RemoteWebDriver.</p>
*
* @since 1.3.1.4
*/
@Parameter(property="jasmine.serverHostname", defaultValue = "localhost")
protected String serverHostname;

/**
* <p>Determines the strategy to use when generation the JasmineSpecRunner. This feature allows for custom
Expand Down
76 changes: 76 additions & 0 deletions src/main/java/com/github/searls/jasmine/mojo/PhantomJsOptions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.github.searls.jasmine.mojo;

import com.github.klieber.phantomjs.locate.PhantomJsLocatorOptions;

import java.io.File;

public class PhantomJsOptions implements PhantomJsLocatorOptions {

private static final String DEFAULT_PHANTOMJS_VERSION = "2.0.0";
private static final String DEFAULT_OUTPUT_DIRECTORY = "target/phantomjs";

private Source source = Source.REPOSITORY;

private String version;
private boolean checkSystemPath;
private boolean enforceVersion;

private String baseUrl;
private File outputDirectory;

public PhantomJsOptions() {
this.source = Source.REPOSITORY;
this.version = DEFAULT_PHANTOMJS_VERSION;
this.checkSystemPath = true;
this.enforceVersion = true;
this.outputDirectory = new File(DEFAULT_OUTPUT_DIRECTORY);
}

public Source getSource() {
return source;
}

public void setSource(Source source) {
this.source = source;
}

public boolean isCheckSystemPath() {
return checkSystemPath;
}

public void setCheckSystemPath(boolean checkSystemPath) {
this.checkSystemPath = checkSystemPath;
}

public boolean isEnforceVersion() {
return enforceVersion;
}

public void setEnforceVersion(boolean enforceVersion) {
this.enforceVersion = enforceVersion;
}

public String getVersion() {
return version;
}

public void setVersion(String version) {
this.version = version;
}

public String getBaseUrl() {
return baseUrl;
}

public void setBaseUrl(String baseUrl) {
this.baseUrl = baseUrl;
}

public File getOutputDirectory() {
return outputDirectory;
}

public void setOutputDirectory(File outputDirectory) {
this.outputDirectory = outputDirectory;
}
}
Loading

0 comments on commit 8806af6

Please sign in to comment.