-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added remoteWebDriverUrl property to TestMojo (enables phantomjs 1.8+…
… to be used in lieu of HtmlUnit)
- Loading branch information
1 parent
175f5f7
commit 4974ccc
Showing
4 changed files
with
165 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
src/main/java/com/github/searls/jasmine/WebDriverConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
src/test/java/com/github/searls/jasmine/WebDriverConfigurationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |