Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added support for making it easy to make the base URL for relative UR…

…Ls configurable
  • Loading branch information...
commit fe2f4621771af179f54a01682587b7efb606416a 1 parent 67edc38
@jroper jroper authored
View
3  .gitignore
@@ -4,4 +4,5 @@
**.DS_Store
**.class
**/target/**
-.idea/**
+.idea/**
+target
View
2  fluentlenium-core/src/main/java/org/fluentlenium/adapter/FluentTest.java
@@ -51,7 +51,7 @@ public void setSnapshotMode(Mode mode) {
@Override
public void starting(FrameworkMethod method) {
super.starting(method);
- initFluent(getDefaultDriver());
+ initFluent(getDefaultDriver(), getDefaultBaseUrl());
initTest();
}
View
14 fluentlenium-core/src/main/java/org/fluentlenium/adapter/IsolatedTest.java
@@ -23,12 +23,22 @@
public IsolatedTest(){
- initFluent(getDefaultDriver());
+ initFluent(getDefaultDriver(), getDefaultBaseUrl());
initTest();
}
public IsolatedTest(WebDriver webDriver){
- initFluent(webDriver);
+ initFluent(webDriver, getDefaultBaseUrl());
+ initTest();
+ }
+
+ public IsolatedTest(String baseUrl) {
+ initFluent(getDefaultDriver(), baseUrl);
+ initTest();
+ }
+
+ public IsolatedTest(WebDriver webDriver, String baseUrl) {
+ initFluent(webDriver, baseUrl);
initTest();
}
View
38 fluentlenium-core/src/main/java/org/fluentlenium/core/Fluent.java
@@ -27,6 +27,7 @@
import java.io.File;
import java.io.IOException;
+import java.net.URI;
import java.util.Date;
import java.util.List;
import java.util.Set;
@@ -36,6 +37,7 @@
*/
public abstract class Fluent implements SearchActions {
private WebDriver driver;
+ private String baseUrl;
private Search search;
public Fluent(WebDriver driver) {
@@ -44,6 +46,18 @@ public Fluent(WebDriver driver) {
FluentThread.set(this);
}
+ public Fluent(WebDriver driver, String baseUrl) {
+ this.driver = driver;
+ if (baseUrl != null) {
+ if (baseUrl.endsWith("/")) {
+ baseUrl = baseUrl.substring(0, baseUrl.length() - 1);
+ }
+ this.baseUrl = baseUrl;
+ }
+ this.search = new Search(driver);
+ FluentThread.set(this);
+ }
+
public Fluent() {
FluentThread.set(this);
}
@@ -73,8 +87,15 @@ public Fluent takeScreenShot(String fileName) {
return this;
}
- protected final void initFluent(WebDriver driver) {
+ protected final void initFluent(WebDriver driver, String baseUrl) {
this.driver = driver;
+ if (baseUrl != null) {
+ if (baseUrl.endsWith("/")) {
+ baseUrl = baseUrl.substring(0, baseUrl.length() - 1);
+ }
+ this.baseUrl = baseUrl;
+ }
+ this.baseUrl = baseUrl;
this.search = new Search(driver);
}
@@ -83,6 +104,15 @@ public WebDriver getDriver() {
}
/**
+ * 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;
+ }
+
+ /**
* wait for an asynchronous call
*
* @return
@@ -161,6 +191,12 @@ public Fluent goTo(String url) {
if (url == null) {
throw new IllegalArgumentException("Url is mandatory");
}
+ if (baseUrl != null) {
+ URI uri = URI.create(url);
+ if (!uri.isAbsolute()) {
+ url = baseUrl + url;
+ }
+ }
getDriver().get(url);
return this;
}
View
12 fluentlenium-core/src/main/java/org/fluentlenium/core/FluentAdapter.java
@@ -58,9 +58,9 @@ protected void initTest() {
construct.setAccessible(true);
page = (T) construct.newInstance();
Class parent = Class.forName(Fluent.class.getName());
- Method m = parent.getDeclaredMethod("initFluent", WebDriver.class);
+ Method m = parent.getDeclaredMethod("initFluent", WebDriver.class, String.class);
m.setAccessible(true);
- m.invoke(page, getDriver());
+ m.invoke(page, getDriver(), getBaseUrl());
//init fields with default proxies
Field[] fields = cls.getDeclaredFields();
@@ -116,6 +116,14 @@ public WebDriver getDefaultDriver() {
return new FirefoxDriver();
}
+ /**
+ * Override this method to set the base URL to use when using relative URLs
+ *
+ * @return The base URL, or null if relative URLs should be passed to the driver untouched
+ */
+ public String getDefaultBaseUrl() {
+ return null;
+ }
public static void assertAt(FluentPage fluent) {
fluent.isAt();
View
5 fluentlenium-core/src/main/java/org/fluentlenium/core/FluentPage.java
@@ -52,10 +52,7 @@ public void isAt() {
* Go to the url defined in the page
*/
public final void go() {
- if (getUrl() == null) {
- throw new IllegalArgumentException("No URL have been defined for this page");
- }
- getDriver().get(getUrl());
+ goTo(getUrl());
}
View
74 fluentlenium-core/src/test/java/org/fluentlenium.integration/BaseUrlTest.java
@@ -0,0 +1,74 @@
+/**
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+package org.fluentlenium.integration;
+
+import org.fluentlenium.core.FluentPage;
+import org.fluentlenium.core.annotation.Page;
+import org.fluentlenium.integration.localtest.LocalFluentCase;
+import org.junit.Test;
+
+import static org.fest.assertions.Assertions.assertThat;
+
+public class BaseUrlTest extends LocalFluentCase {
+ @Page
+ Page2Relative pageRelative;
+
+ @Page
+ Page2 page;
+
+ @Override
+ public String getDefaultBaseUrl() {
+ return DEFAULT_URL;
+ }
+
+ @Test
+ public void baseUrlShouldBeUsedForRelativeUrlInGoTo() {
+ goTo("/page2.html");
+ assertThat(title()).isEqualTo("Page 2");
+ }
+
+ @Test
+ public void baseUrlShouldNotBeUsedForAbsoluteUrlInGoTo() {
+ goTo(DEFAULT_URL);
+ assertThat(title()).isEqualTo("Fluent Selenium Documentation");
+ }
+
+ @Test
+ public void baseUrlShouldBeUsedForRelativeUrlInPageGo() {
+ goTo(pageRelative);
+ pageRelative.isAt();
+ }
+
+ @Test
+ public void baseUrlShouldNotBeUsedForAbsoluteUrlInPageGo() {
+ goTo(page);
+ page.isAt();
+ }
+
+}
+
+class Page2Relative extends FluentPage {
+
+ @Override
+ public String getUrl() {
+ return "/page2.html";
+ }
+
+ @Override
+ public void isAt() {
+ assertThat($("title").first().getText()).isEqualTo("Page 2");
+ }
+}
+
View
2  fluentlenium-core/src/test/java/org/fluentlenium.integration/PageTest.java
@@ -87,7 +87,7 @@ public void goToNexPage() {
@Override
public String getUrl() {
- return LocalFluentCase.DEFAULT_URL + "/page2.html";
+ return LocalFluentCase.DEFAULT_URL + "page2.html";
}
@Override
View
2  fluentlenium-testng/src/main/java/org/fluentlenium/adapter/FluentTestNg.java
@@ -29,7 +29,7 @@ public FluentTestNg() {
@BeforeClass
public void beforeClass() {
- this.initFluent(getDefaultDriver());
+ this.initFluent(getDefaultDriver(), getDefaultBaseUrl());
initTest();
}
Please sign in to comment.
Something went wrong with that request. Please try again.