Skip to content

Commit

Permalink
Merge pull request #331 from FluentLenium/remote-driver
Browse files Browse the repository at this point in the history
Add support for "remote" webDriver
  • Loading branch information
Toilal committed Sep 15, 2016
2 parents 319fec7 + 81b4a6d commit 67acde4
Show file tree
Hide file tree
Showing 22 changed files with 286 additions and 64 deletions.
6 changes: 5 additions & 1 deletion README.md
Expand Up @@ -1011,11 +1011,15 @@ FluentLenium can be configured in many ways through configuration properties.

Default value: ```null```.

Possible values are ```firefox```, ```marionette```, ```chrome```, ```ie```, ```safari```,
Possible values are ```remote```, ```firefox```, ```marionette```, ```chrome```, ```ie```, ```safari```,
```phantomjs```, ```htmlunit```, or any class name implementing ```WebDriver```.

If not defined, FluentLenium will use the first value for which WebDriver is available in classpath.

- **remoteUrl**

Sets the remote URL for ```remote``` *webDriver*. This should be the URL to access Selenium-Grid server.

- **capabilities**

Sets the [Capabilities](https://github.com/SeleniumHQ/selenium/wiki/DesiredCapabilities) to use with the WebDriver.
Expand Down
7 changes: 6 additions & 1 deletion fluentlenium-core/pom.xml
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.fluentlenium</groupId>
Expand All @@ -18,6 +19,10 @@
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-support</artifactId>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-remote-driver</artifactId>
</dependency>
<dependency>
<groupId>org.atteo.classindex</groupId>
<artifactId>classindex</artifactId>
Expand Down

This file was deleted.

Expand Up @@ -116,7 +116,7 @@ public void releaseFluent() {
* @see #getDriver()
*/
public WebDriver newWebDriver() {
WebDriver webDriver = WebDrivers.INSTANCE.newWebDriver(getWebDriver(), getCapabilities());
WebDriver webDriver = WebDrivers.INSTANCE.newWebDriver(getWebDriver(), getCapabilities(), this);
if (Boolean.TRUE.equals(getEventsEnabled())) {
webDriver = new EventFiringWebDriver(webDriver);
}
Expand Down
Expand Up @@ -134,6 +134,11 @@ public String getWebDriver() {
return getStringProperty("webDriver");
}

@Override
public String getRemoteUrl() {
return getStringProperty("remoteUrl");
}

@Override
public Capabilities getCapabilities() {
return getCapabilitiesProperty("capabilities");
Expand Down
Expand Up @@ -11,6 +11,7 @@
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;

/**
* {@link ConfigurationProperties} based on {@link FluentConfiguration} annotation.
Expand Down Expand Up @@ -54,7 +55,7 @@ private Capabilities getCapabilitiesValue(String property) {
InputStream stream = null;
try {
stream = url.openStream();
property = IOUtils.toString(stream);
property = IOUtils.toString(stream, Charset.defaultCharset());
} catch (IOException e) {
throw new ConfigurationException("Can't read Capabilities defined at " + url);
} finally {
Expand Down Expand Up @@ -102,6 +103,12 @@ public String getWebDriver() {
return getStringValue(configuration.webDriver());
}

@Override
public String getRemoteUrl() {
if (configuration == null) return null;
return getStringValue(configuration.remoteUrl());
}

@Override
public Capabilities getCapabilities() {
if (configuration == null) return null;
Expand Down
Expand Up @@ -48,6 +48,15 @@ public String getWebDriver() {
return null;
}

@Override
public String getRemoteUrl() {
for (ConfigurationProperties configuration : configurations) {
String remoteUrl = configuration.getRemoteUrl();
if (remoteUrl != null) return remoteUrl;
}
return null;
}

@Override
public Capabilities getCapabilities() {
for (ConfigurationProperties configuration : configurations) {
Expand Down
Expand Up @@ -23,6 +23,11 @@ public String getWebDriver() {
return null;
}

@Override
public String getRemoteUrl() {
return null;
}

@Override
public Capabilities getCapabilities() {
return null;
Expand Down
Expand Up @@ -16,6 +16,14 @@ public interface ConfigurationMutator {
*/
void setWebDriver(String webDriver);

/**
* Sets the value of <pre>remoteUrl</pre> property.
*
* @param remoteUrl property value
* @see ConfigurationProperties#getRemoteUrl()
*/
void setRemoteUrl(String remoteUrl);

/**
* Sets the value of <pre>capabilities</pre> property.
*
Expand Down
Expand Up @@ -111,7 +111,8 @@ enum DriverLifecycle {
* Sets the WebDriver type to use.
*
* When FluentLenium needs to create a new {@link WebDriver} instance, it calls {@link FluentAdapter#newWebDriver()}
* which delegates to {@link org.fluentlenium.configuration.WebDrivers.Impl#newWebDriver(String, Capabilities)} registry using the value stored in
* which delegates to
* {@link org.fluentlenium.configuration.WebDriversRegistryImpl#newWebDriver(String, Capabilities, ConfigurationProperties)} registry using the value stored in
* webDriver and capabilities property.
*
* Possible values are "firefox", "chrome", "ie", "htmlunit", or any class name implementing {@link WebDriver}.
Expand All @@ -123,6 +124,16 @@ enum DriverLifecycle {
*/
String getWebDriver();

/**
* <pre>remoteUrl</pre> property.
*
* Sets the remoteUrl for "remote" webDriver.
*
* @return remoteUrl property value
* @see org.openqa.selenium.remote.RemoteWebDriver
*/
String getRemoteUrl();

/**
* <pre>capabilities</pre> property.
*
Expand Down
@@ -1,6 +1,15 @@
package org.fluentlenium.configuration;

import org.fluentlenium.utils.ReflectionUtils;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.Augmenter;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

import java.lang.reflect.InvocationTargetException;
import java.net.MalformedURLException;
import java.net.URL;

public class DefaultWebDriverFactories {
public static class FirefoxWebDriverFactory extends ReflectiveWebDriverFactory {
Expand Down Expand Up @@ -80,6 +89,45 @@ public int getPriority() {
}
}

public static class RemoteWebDriverFactory extends ReflectiveWebDriverFactory {
public RemoteWebDriverFactory() {
super("remote", RemoteWebDriver.class);
}

@Override
protected WebDriver newInstance(Class<? extends WebDriver> webDriverClass, ConfigurationProperties configuration, Object[] args) throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
URL url = null;
if (configuration != null) {
String remoteUrl = configuration.getRemoteUrl();


if (remoteUrl != null) {
try {
url = new URL(remoteUrl);
} catch (MalformedURLException e) {
throw new ConfigurationException("remoteUrl configuration property is not a valid URL.", e);
}
}
}

Object[] urlArgs = new Object[2];
urlArgs[0] = url;
urlArgs[1] = args.length > 0 ? args[0] : new DesiredCapabilities();

return newRemoteWebDriver(urlArgs);
}

protected WebDriver newRemoteWebDriver(Object[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
WebDriver webDriver = ReflectionUtils.getConstructor(webDriverClass, URL.class, Capabilities.class).newInstance(args);
return new Augmenter().augment(webDriver);
}

@Override
public int getPriority() {
return 0;
}
}

public static class HtmlUnitWebDriverFactory extends ReflectiveWebDriverFactory {
public HtmlUnitWebDriverFactory() {
super("htmlunit", "org.openqa.selenium.htmlunit.HtmlUnitDriver");
Expand Down
Expand Up @@ -57,6 +57,14 @@ Boolean asBoolean() {
*/
String webDriver() default "";

/**
* <i>remoteUrl</i> property.
*
* @return remoteUrl
* @see ConfigurationProperties#getRemoteUrl()
*/
String remoteUrl() default "";

/**
* <i>capabilities</i> property.
*
Expand Down
Expand Up @@ -15,6 +15,8 @@ public class ProgrammaticConfiguration implements Configuration {

private String webDriverName;

private String remoteUrl;

private Capabilities capabilities;

private DriverLifecycle driverLifecycle;
Expand Down Expand Up @@ -49,6 +51,16 @@ public void setWebDriver(String webDriverName) {
this.webDriverName = webDriverName;
}

@Override
public String getRemoteUrl() {
return remoteUrl;
}

@Override
public void setRemoteUrl(String remoteUrl) {
this.remoteUrl = remoteUrl;
}

@Override
public Capabilities getCapabilities() {
return capabilities;
Expand Down
Expand Up @@ -14,11 +14,11 @@
* A simple {@link WebDriverFactory} that create {@link WebDriver} instances using reflection.
*/
public class ReflectiveWebDriverFactory implements WebDriverFactory, ReflectiveFactory, AlternativeNames {
private String name;
private Object[] args;
private String webDriverClassName;
private Class<? extends WebDriver> webDriverClass;
private boolean available;
protected String name;
protected Object[] args;
protected String webDriverClassName;
protected Class<? extends WebDriver> webDriverClass;
protected boolean available;

public ReflectiveWebDriverFactory(String name, String webDriverClassName, Object... args) {
this.name = name;
Expand Down Expand Up @@ -53,7 +53,7 @@ protected DesiredCapabilities newDefaultCapabilities() {
}

@Override
public WebDriver newWebDriver(Capabilities capabilities) {
public WebDriver newWebDriver(Capabilities capabilities, ConfigurationProperties configuration) {
if (!available) {
throw new ConfigurationException("WebDriver " + webDriverClassName + " is not available.");
}
Expand All @@ -69,17 +69,21 @@ public WebDriver newWebDriver(Capabilities capabilities) {
ArrayList<Object> argsList = new ArrayList<>(Arrays.asList(args));
argsList.add(0, capabilities);
try {
return ReflectionUtils.newInstance(webDriverClass, argsList.toArray());
return newInstance(webDriverClass, configuration, argsList.toArray());
} catch (NoSuchMethodException e) {
// Ignore capabilities.
}
}
return ReflectionUtils.newInstance(webDriverClass, args);
return newInstance(webDriverClass, configuration, args);
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
throw new ConfigurationException("Can't create new WebDriver instance", e);
}
}

protected WebDriver newInstance(Class<? extends WebDriver> webDriverClass, ConfigurationProperties configuration, Object[] args) throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
return ReflectionUtils.newInstance(webDriverClass, args);
}

@Override
public String getName() {
return name;
Expand Down
Expand Up @@ -13,7 +13,8 @@ public interface WebDriverFactory extends Factory {
* Creates a new instance of {@link WebDriver}.
*
* @param desiredCapabilities Desired capabilities for the web driver
* @param configuration Configuration
* @return new instance of web driver
*/
WebDriver newWebDriver(Capabilities desiredCapabilities);
WebDriver newWebDriver(Capabilities desiredCapabilities, ConfigurationProperties configuration);
}
@@ -1,5 +1,6 @@
package org.fluentlenium.configuration;

import org.openqa.selenium.Capabilities;
import org.openqa.selenium.WebDriver;

import java.util.List;
Expand Down Expand Up @@ -31,11 +32,12 @@ protected void handleNoFactoryAvailable(String name) {
/**
* Creates a new {@link WebDriver} instance from factory of the given name
*
* @param name name of the factory used to create new WebDriver instance
* @param capabilities Desired capabilities for the WebDriver
* @param name name of the factory used to create new WebDriver instance
* @param capabilities Desired capabilities for the WebDriver
* @param configuration Configuration for the WebDriver
* @return a new WebDriver instance
*/
public synchronized WebDriver newWebDriver(String name, org.openqa.selenium.Capabilities capabilities) {
return get(name).newWebDriver(capabilities);
public synchronized WebDriver newWebDriver(String name, Capabilities capabilities, ConfigurationProperties configuration) {
return get(name).newWebDriver(capabilities, configuration);
}
}
Expand Up @@ -73,6 +73,14 @@ public void webDriver() {
Assertions.assertThat(getConfiguration().getWebDriver()).isEqualTo("firefox");
}

@Test
public void remoteUrl() {
Assertions.assertThat(getConfiguration().getRemoteUrl()).isNull();

mockProperty("remoteUrl", "http://localhost:4444");
Assertions.assertThat(getConfiguration().getRemoteUrl()).isEqualTo("http://localhost:4444");
}

@Test
public void capabilities() {
Assertions.assertThat(getConfiguration().getWebDriver()).isNull();
Expand Down

0 comments on commit 67acde4

Please sign in to comment.