Skip to content

Commit

Permalink
Added remoteWebDriverUrl property to TestMojo (enables phantomjs 1.8+…
Browse files Browse the repository at this point in the history
… to be used in lieu of HtmlUnit)
  • Loading branch information
johnomalley committed Feb 15, 2013
1 parent 175f5f7 commit 4974ccc
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 48 deletions.
11 changes: 11 additions & 0 deletions src/main/java/com/github/searls/jasmine/AbstractJasmineMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,21 @@ public abstract class AbstractJasmineMojo extends AbstractMojo {
*
* Some valid examples: org.openqa.selenium.htmlunit.HtmlUnitDriver, org.openqa.selenium.firefox.FirefoxDriver, org.openqa.selenium.ie.InternetExplorerDriver
*
* If the remoteWebDriverUrl property is included this property will be ignored.
*
* @parameter default-value="org.openqa.selenium.htmlunit.HtmlUnitDriver"
*/
protected String webDriverClassName;

/**
* A URL used to create a RemoteWebDriver. If this property is included, webDriverClassName will be ignored.
*
* See http://phantomjs.org/release-1.8.html for an example of RemoteWebDriver use.
*
* @parameter
*/
protected String remoteWebDriverUrl;

/**
* 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
Expand Down
55 changes: 7 additions & 48 deletions src/main/java/com/github/searls/jasmine/TestMojo.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,19 @@
package com.github.searls.jasmine;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.net.MalformedURLException;

import org.apache.commons.io.FileUtils;
import org.apache.maven.plugin.MojoFailureException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.IncorrectnessListener;
import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
import com.gargoylesoftware.htmlunit.WebClient;
import com.github.searls.jasmine.format.JasmineResultLogger;
import com.github.searls.jasmine.io.scripts.TargetDirScriptResolver;
import com.github.searls.jasmine.model.JasmineResult;
import com.github.searls.jasmine.runner.ReporterType;
import com.github.searls.jasmine.runner.SpecRunnerExecutor;
import com.github.searls.jasmine.runner.SpecRunnerHtmlGenerator;
import com.github.searls.jasmine.runner.SpecRunnerHtmlGeneratorFactory;
import org.apache.commons.io.FileUtils;
import org.apache.maven.plugin.MojoFailureException;
import org.openqa.selenium.WebDriver;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;

/**
* @component
Expand Down Expand Up @@ -66,42 +58,9 @@ private JasmineResult executeSpecs(File runnerFile) throws MalformedURLException
}

private WebDriver createDriver() {
if (!HtmlUnitDriver.class.getName().equals(webDriverClassName)) {
try {
@SuppressWarnings("unchecked")
Class<? extends WebDriver> klass = (Class<? extends WebDriver>) Class.forName(webDriverClassName);
Constructor<? extends WebDriver> ctor = klass.getConstructor();
return ctor.newInstance();
} catch (Exception e) {
throw new RuntimeException("Couldn't instantiate webDriverClassName", e);
}
}

// We have extra configuration to do to the HtmlUnitDriver
BrowserVersion htmlUnitBrowserVersion;
try {
htmlUnitBrowserVersion = (BrowserVersion) BrowserVersion.class.getField(browserVersion).get(BrowserVersion.class);
} catch (Exception e) {
throw new RuntimeException(e);
}
HtmlUnitDriver driver = new HtmlUnitDriver(htmlUnitBrowserVersion) {
protected WebClient modifyWebClient(WebClient client) {
client.setAjaxController(new NicelyResynchronizingAjaxController());

//Disables stuff like this "com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify WARNING: Obsolete content type encountered: 'text/javascript'."
if (!debug)
client.setIncorrectnessListener(new IncorrectnessListener() {
public void notify(String arg0, Object arg1) {}
});

return client;
};
};
driver.setJavascriptEnabled(true);
return driver;
return new WebDriverConfiguration(webDriverClassName, remoteWebDriverUrl, browserVersion, debug).createWebDriver();
}


private void logResults(JasmineResult result) {
JasmineResultLogger resultLogger = new JasmineResultLogger();
resultLogger.setLog(getLog());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.github.searls.jasmine;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.IncorrectnessListener;
import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
import com.gargoylesoftware.htmlunit.WebClient;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

import java.net.MalformedURLException;
import java.net.URL;

/**
* Creates a WebDriver according to the properties configured.
*/
class WebDriverConfiguration {
private final String webDriverClassName;
private final String remoteWebDriverUrl;
private final String browserVersion;
private final boolean debug;

WebDriverConfiguration(String webDriverClassName, String remoteWebDriverUrl, String browserVersion, boolean debug) {
this.webDriverClassName = webDriverClassName;
this.remoteWebDriverUrl = remoteWebDriverUrl;
this.browserVersion = browserVersion;
this.debug = debug;
}

WebDriver createWebDriver() {
if (remoteWebDriverUrl != null) {
return createRemoteWebDriver();
} else if (webDriverClassName.equals(HtmlUnitDriver.class.getName())) {
return createDefaultWebDriver();
} else {
return createCustomWebDriver();
}
}

private WebDriver createRemoteWebDriver() {
try {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setJavascriptEnabled(true);
return new RemoteWebDriver(new URL(remoteWebDriverUrl), capabilities);
} catch (MalformedURLException e) {
throw new RuntimeException("Malformed remoteWebDriverUrl: " + remoteWebDriverUrl);
}
}

private WebDriver createCustomWebDriver() {
try {
return (WebDriver) Class.forName(webDriverClassName).getConstructor().newInstance();
} catch (Exception e) {
throw new RuntimeException("Couldn't instantiate " + webDriverClassName, e);
}
}

private WebDriver createDefaultWebDriver() {
// We have extra configuration to do to the HtmlUnitDriver
BrowserVersion htmlUnitBrowserVersion;
try {
htmlUnitBrowserVersion = (BrowserVersion) BrowserVersion.class.getField(browserVersion).get(BrowserVersion.class);
} catch (Exception e) {
throw new RuntimeException(e);
}
HtmlUnitDriver driver = new HtmlUnitDriver(htmlUnitBrowserVersion) {
protected WebClient modifyWebClient(WebClient client) {
client.setAjaxController(new NicelyResynchronizingAjaxController());

//Disables stuff like this "com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify WARNING: Obsolete content type encountered: 'text/javascript'."
if (!debug) {
client.setIncorrectnessListener(new IncorrectnessListener() {
public void notify(String arg0, Object arg1) {
}
});
}
return client;
}
};
driver.setJavascriptEnabled(true);
return driver;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.github.searls.jasmine;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.UnreachableBrowserException;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;

public class WebDriverConfigurationTest {
private String webDriverClassName;
private String remoteWebDriverUrl;
private String browserVersion = "FIREFOX_3_6";
@Rule
public ExpectedException expectedException = ExpectedException.none();

@Before
public void setUp() {
webDriverClassName = "org.openqa.selenium.htmlunit.HtmlUnitDriver";

}

private WebDriver createWebDriver() {
return new WebDriverConfiguration(webDriverClassName, remoteWebDriverUrl, browserVersion, false).createWebDriver();
}

@Test
public void createsDefaultWebDriverIfClassNameIsDefault() {
assertTrue(createWebDriver() instanceof HtmlUnitDriver);
}

@Test
public void createsCustomWebDriverIfSpecified() {
WebDriver mockDriver = mock(WebDriver.class);
webDriverClassName = mockDriver.getClass().getName();

assertEquals(mockDriver.getClass(), createWebDriver().getClass());
}

@Test
public void throwsExceptionIfCustomWebDriverCannotBeCreated() {
expectedException.expect(RuntimeException.class);
webDriverClassName = "fooBar";
expectedException.expectMessage("Couldn't instantiate " + webDriverClassName);

createWebDriver();
}

@Test
public void createsRemoteWebDriverIfRemoteUrlIsConfigured() {
expectedException.expect(UnreachableBrowserException.class);
remoteWebDriverUrl = "http://localhost:1234";
RemoteWebDriver remoteWebDriver = (RemoteWebDriver) createWebDriver();

assertEquals(remoteWebDriverUrl, remoteWebDriver.getCurrentUrl());
}
}

0 comments on commit 4974ccc

Please sign in to comment.