Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to connect to remote Selenium server #235

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.remote.BrowserType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

import java.lang.reflect.Constructor;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

Expand All @@ -20,6 +24,7 @@ public class WebDriverFactory {
private boolean debug;
private String browserVersion;
private String webDriverClassName;
private String remoteWebDriverUrl;
private List<Capability> webDriverCapabilities;

public WebDriverFactory() {
Expand All @@ -38,18 +43,54 @@ public void setWebDriverClassName(String webDriverClassName) {
this.webDriverClassName = webDriverClassName;
}

public void setRemoteWebDriverUrl(String remoteWebDriverUrl) {
this.remoteWebDriverUrl = remoteWebDriverUrl;
}

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

public WebDriver createWebDriver() throws Exception {
if (HtmlUnitDriver.class.getName().equals(webDriverClassName)) {

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

WebDriver createRemoteWebDriver()
{
// If the user set remoteWebDriverUrl, webDriverClassName must be either RemoteWebDriver or null
boolean ok = webDriverClassName == null || RemoteWebDriver.class.getName().equals(webDriverClassName);
if (!ok)
{
throw new RuntimeException("You can't use 'remoteWebDriverUrl' with drivers other than RemoteWebDriver.");
}

// In case it's null
webDriverClassName = RemoteWebDriver.class.getName(); // do we have to?

return new RemoteWebDriver(toUrl(remoteWebDriverUrl), getCapabilities());
}

URL toUrl(String url)
{
try
{
return new URL(url);
}
catch (MalformedURLException e)
{
throw new RuntimeException(e);
}
}

@SuppressWarnings("unchecked")
private Class<? extends WebDriver> getWebDriverClass() throws Exception {
return (Class<WebDriver>) Class.forName(webDriverClassName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public abstract class AbstractJasmineMojo extends AbstractMojo implements Jasmin
/**
* 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.
*
* If the remoteWebDriverUrl property is included this property will be ignored.
*
* <p>Some valid examples:</p>
* <ul>
Expand All @@ -66,6 +68,14 @@ public abstract class AbstractJasmineMojo extends AbstractMojo implements Jasmin
*/
@Parameter(defaultValue="org.openqa.selenium.htmlunit.HtmlUnitDriver")
protected String webDriverClassName;

/**
* A URL used to create a RemoteWebDriver. If this property is included, webDriverClassName will be ignored.
*
*/
@Parameter
protected String remoteWebDriverUrl;


/**
* <p>Web driver capabilities used to initialize a DesiredCapabilities instance when creating a web driver.</p>
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/github/searls/jasmine/mojo/TestMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ private WebDriver createDriver() throws Exception {
WebDriverFactory factory = new WebDriverFactory();
factory.setWebDriverCapabilities(webDriverCapabilities);
factory.setWebDriverClassName(webDriverClassName);
factory.setRemoteWebDriverUrl(remoteWebDriverUrl);
factory.setDebug(debug);
factory.setBrowserVersion(browserVersion);
return factory.createWebDriver();
Expand Down