Skip to content

Commit

Permalink
FluentControl now extends Configuration
Browse files Browse the repository at this point in the history
Close #380
  • Loading branch information
Toilal committed Oct 6, 2016
1 parent d40577a commit 5f674c9
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 50 deletions.
Expand Up @@ -16,7 +16,7 @@
/**
* Generic adapter to {@link FluentDriver}.
*/
public class FluentAdapter implements FluentControl, ConfigurationProperties {
public class FluentAdapter implements FluentControl {

private final FluentControlContainer driverContainer;

Expand All @@ -30,12 +30,13 @@ public FluentAdapter(final FluentControlContainer driverContainer) {
this.driverContainer = driverContainer;
}

@Delegate(types = ConfigurationProperties.class)
@Delegate
public Configuration getConfiguration() {
return configuration;
}

@Delegate(types = FluentControl.class, excludes = SeleniumDriverControl.class) // We want getDriver to be final.
@Delegate(types = FluentControl.class, excludes = {SeleniumDriverControl.class, Configuration.class})
// We want getDriver to be final.
private ContainerFluentControl getFluentControl() {
return (ContainerFluentControl) getDriverContainer().getFluentControl();
}
Expand Down
@@ -1,5 +1,7 @@
package org.fluentlenium.core;

import org.fluentlenium.configuration.Configuration;
import org.fluentlenium.configuration.ConfigurationProperties;
import org.fluentlenium.core.action.InputControl;
import org.fluentlenium.core.alert.AlertControl;
import org.fluentlenium.core.components.ComponentInstantiator;
Expand All @@ -15,6 +17,7 @@

public interface FluentControl
extends SearchControl<FluentWebElement>, AwaitControl, InputControl, JavascriptControl, AlertControl, SnapshotControl,
EventsControl, NavigationControl, SeleniumDriverControl, CssControl, FluentInjectControl, ComponentInstantiator {
EventsControl, NavigationControl, SeleniumDriverControl, CssControl, FluentInjectControl, ComponentInstantiator,
Configuration {

}
Expand Up @@ -3,7 +3,7 @@
import lombok.experimental.Delegate;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.fluentlenium.configuration.ConfigurationProperties;
import org.fluentlenium.configuration.Configuration;
import org.fluentlenium.core.action.KeyboardActions;
import org.fluentlenium.core.action.MouseActions;
import org.fluentlenium.core.action.WindowAction;
Expand Down Expand Up @@ -46,9 +46,8 @@
* Util Class which offers some shortcut to webdriver methods
*/
public class FluentDriver implements FluentControl {
private String baseUrl;

private final ConfigurationProperties configuration;
@Delegate
private final Configuration configuration;

@Delegate(types = ComponentInstantiator.class)
private final ComponentsManager componentsManager;
Expand All @@ -73,7 +72,7 @@ public class FluentDriver implements FluentControl {

private final WindowAction windowAction;

public FluentDriver(final WebDriver driver, final ConfigurationProperties configuration, final FluentControl adapter) {
public FluentDriver(final WebDriver driver, final Configuration configuration, final FluentControl adapter) {
this.configuration = configuration;
this.componentsManager = new ComponentsManager(adapter);
this.driver = driver;
Expand Down Expand Up @@ -118,18 +117,7 @@ private void configureDriver() {
.setScriptTimeout(this.configuration.getScriptTimeout(), TimeUnit.MILLISECONDS);
}
}

if (this.configuration.getBaseUrl() != null) {
String configBaseUrl = this.configuration.getBaseUrl();
if (configBaseUrl != null) {
if (configBaseUrl.endsWith("/")) {
configBaseUrl = configBaseUrl.substring(0, configBaseUrl.length() - 1);
}
this.baseUrl = configBaseUrl;
}
}
}

}

@Override
Expand Down Expand Up @@ -232,15 +220,6 @@ public WindowAction window() {
return windowAction;
}

/**
* Get the base URL to use when visiting relative URLs, if one is configured
*
* @return The base URL, or null if none configured
*/
public String getBaseUrl() {
return baseUrl;
}

@Override
public FluentWait await() {
return new FluentWait(this);
Expand All @@ -256,10 +235,35 @@ public Cookie getCookie(final String name) {
return getDriver().manage().getCookieNamed(name);
}

private String buildUrl(String url) {
String baseUrl = getBaseUrl();
if (baseUrl != null) {
String configBaseUrl = baseUrl;
if (configBaseUrl != null) {
if (configBaseUrl.endsWith("/")) {
configBaseUrl = configBaseUrl.substring(0, configBaseUrl.length() - 1);
}
baseUrl = configBaseUrl;
}
}
if (baseUrl != null) {
final URI uri = URI.create(url);
if (!uri.isAbsolute()) {
url = baseUrl + url;
}
}
if (url == null) {
url = baseUrl;
}
return url;
}

@Override
public String url() {
String currentUrl = getDriver().getCurrentUrl();

String baseUrl = buildUrl(null);

if (currentUrl != null && baseUrl != null && currentUrl.startsWith(baseUrl)) {
currentUrl = currentUrl.substring(baseUrl.length());
}
Expand All @@ -286,13 +290,8 @@ public void goTo(String url) {
if (url == null) {
throw new IllegalArgumentException("Url is mandatory");
}
if (baseUrl != null) {
final URI uri = URI.create(url);
if (!uri.isAbsolute()) {
url = baseUrl + url;
}
}
getDriver().get(url);

getDriver().get(buildUrl(url));
}

@Override
Expand All @@ -301,17 +300,10 @@ public void goToInNewTab(String url) {
throw new IllegalArgumentException("Url is mandatory");
}

if (baseUrl != null) {
final URI uri = URI.create(url);
if (!uri.isAbsolute()) {
url = baseUrl + url;
}
}

final String newTab;
synchronized (getClass()) {
final Set<String> initialTabs = getDriver().getWindowHandles();
executeScript("window.open('" + url + "', '_blank');");
executeScript("window.open('" + buildUrl(url) + "', '_blank');");
final Set<String> tabs = getDriver().getWindowHandles();
tabs.removeAll(initialTabs);
newTab = tabs.iterator().next();
Expand Down
Expand Up @@ -2,6 +2,7 @@

import com.google.common.collect.ImmutableSet;
import org.assertj.core.api.Assertions;
import org.fluentlenium.configuration.Configuration;
import org.fluentlenium.configuration.ConfigurationProperties;
import org.fluentlenium.core.FluentControl;
import org.fluentlenium.core.FluentDriver;
Expand Down Expand Up @@ -128,9 +129,9 @@ public void openNewAndSwitch() {

final FluentWait fluentWait = mock(FluentWait.class);
final FluentWaitWindowMatcher fluentWaitWindowMatcher = mock(FluentWaitWindowMatcher.class);
final ConfigurationProperties configurationProperties = mock(ConfigurationProperties.class);
final Configuration configuration = mock(Configuration.class);

final FluentDriver currentFluentDriver = new FluentDriver(driver, configurationProperties, fluentControl);
final FluentDriver currentFluentDriver = new FluentDriver(driver, configuration, fluentControl);
final FluentDriver fluentDriverSpied = spy(currentFluentDriver);

when(jsDriver.getWindowHandles()).thenReturn(ImmutableSet.of(windowHandle, windowHandle1),
Expand Down Expand Up @@ -199,9 +200,9 @@ public void clickAndOpenNewTest() throws InterruptedException {
final FluentWebElement fluentWebElement = mock(FluentWebElement.class);
final FluentWait fluentWait = mock(FluentWait.class);
final FluentWaitWindowMatcher fluentWaitWindowMatcher = mock(FluentWaitWindowMatcher.class);
final ConfigurationProperties configurationProperties = mock(ConfigurationProperties.class);
final Configuration configuration = mock(Configuration.class);

final FluentDriver currentFluentDriver = new FluentDriver(driver, configurationProperties, fluentControl);
final FluentDriver currentFluentDriver = new FluentDriver(driver, configuration, fluentControl);
final FluentDriver fluentDriverSpied = spy(currentFluentDriver);

when(driver.getWindowHandles()).thenReturn(ImmutableSet.of(windowHandle, windowHandle1),
Expand Down
Expand Up @@ -17,8 +17,8 @@ public class JavascriptTest extends IntegrationFluentTest {
@Before
public void before() {
goTo(JAVASCRIPT_URL);
getConfiguration().setScreenshotMode(TriggerMode.MANUAL);
getConfiguration().setHtmlDumpMode(TriggerMode.MANUAL);
setScreenshotMode(TriggerMode.MANUAL);
setHtmlDumpMode(TriggerMode.MANUAL);
}

@Test
Expand Down

0 comments on commit 5f674c9

Please sign in to comment.