Skip to content

Commit

Permalink
Merge pull request #233 from FluentLenium/feature/PageUrlAnnotation
Browse files Browse the repository at this point in the history
resolves #231, PageUrl annotation added within tests
  • Loading branch information
filipcynarski committed Apr 2, 2016
2 parents 7b8b8e6 + 1e23910 commit be04464
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 9 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ A Page Object can model the whole page or just a part of it.

To construct a Page, extend [org.fluentlenium.core.FluentPage](https://github.com/FluentLenium/FluentLenium/blob/master/fluentlenium-core/src/main/java/org/fluentlenium/core/FluentPage.java).
In most cases, you have to define the url of the page by overriding the `getUrl` method.
It is also possible to use `@PageUrl` annotation, its value will be passed to the `getUrl` method, overriding is not required.
By doing this, you can then use the `goTo(myPage)` method in your test code.

It may be necessary to ensure that you are on the right page, not just at the url returned by `getUrl` [accessible in your test via the void url() method].
Expand Down Expand Up @@ -378,6 +379,21 @@ public class LoginPage extends FluentPage {
}
```

or using `@PageUrl` annotation instead of overriding `getUrl` method

```java
@PageUrl("myCustomUrl")
public class LoginPage extends FluentPage {
public void isAt() {
assertThat(title()).isEqualTo("MyTitle");
}
public void fillAndSubmitForm(String... paramsOrdered) {
fill("input").with(paramsOrdered);
click("#create-button");
}
}
```

And the corresponding test:

```java
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.fluentlenium.core;

import org.fluentlenium.core.annotation.PageUrl;
import org.openqa.selenium.WebDriver;

/**
Expand All @@ -21,6 +22,12 @@ public FluentPage(WebDriver driver) {
* @return page URL
*/
public String getUrl() {
if (this.getClass().isAnnotationPresent(PageUrl.class)) {
String url = this.getClass().getAnnotation(PageUrl.class).value();
if (!url.isEmpty()) {
return url;
}
}
return null;
}

Expand All @@ -40,6 +47,4 @@ public void isAt() {
public final void go() {
goTo(getUrl());
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.fluentlenium.core.annotation;

import java.lang.annotation.Retention;

import static java.lang.annotation.RetentionPolicy.RUNTIME;

/**
* <b>PageUrl</b> is a class annotation used instead of <b>getUrl</b> method of <b>FluentPage</b> object.
* If <b>PageUrl</b> annotation is used the page class may not override the <b>getUrl</b> method.
*/
@Retention(RUNTIME)
public @interface PageUrl {
/**
* The page URL can be relative or absolute, if the URL is not recognized
* as absolute will be treated as relative.
* <p/>
* For example :
* <code>@PageUrl("/index.html")</code> should redirect to baseUrl + "/index.html"
* <code>@PageUrl("http://example.com")</code> should redirect to "http://example.com"
*
* @return page url
*/
String value();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.fluentlenium.core.FluentPage;
import org.fluentlenium.core.annotation.Page;
import org.fluentlenium.core.annotation.PageUrl;
import org.fluentlenium.integration.localtest.LocalFluentCase;
import org.junit.Test;

Expand All @@ -17,7 +18,7 @@ public class BaseUrlTest extends LocalFluentCase {

@Override
public String getDefaultBaseUrl() {
return DEFAULT_URL;
return DEFAULT_URL_PATH;
}

@Test
Expand Down Expand Up @@ -46,13 +47,8 @@ public void baseUrlShouldNotBeUsedForAbsoluteUrlInPageGo() {

}

@PageUrl("/page2.html")
class Page2Relative extends FluentPage {

@Override
public String getUrl() {
return LocalFluentCase.PAGE_2_URL;
}

@Override
public void isAt() {
assertThat(getDriver().getTitle()).isEqualTo("Page 2");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,20 @@
import org.openqa.selenium.htmlunit.HtmlUnitDriver;

import static org.fluentlenium.integration.util.UrlUtil.getAbsoluteUrlFromFile;
import static org.fluentlenium.integration.util.UrlUtil.getAbsoluteUrlPathFromFile;

public abstract class LocalFluentCase extends FluentTest {

public static final String DEFAULT_URL;
public static final String DEFAULT_URL_PATH;
public static final String JAVASCRIPT_URL;
public static final String PAGE_2_URL;
public static final String IFRAME_URL;
public static final String ANOTHERPAGE_URL;

static {
DEFAULT_URL = getAbsoluteUrlFromFile("index.html");
DEFAULT_URL_PATH = getAbsoluteUrlPathFromFile("index.html");
JAVASCRIPT_URL = getAbsoluteUrlFromFile("javascript.html");
PAGE_2_URL = getAbsoluteUrlFromFile("page2.html");
IFRAME_URL = getAbsoluteUrlFromFile("iframe.html");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.fluentlenium.integration.util;

import java.io.File;
import java.net.URL;

public final class UrlUtil {
Expand Down Expand Up @@ -27,4 +28,15 @@ public static String getAbsoluteUrlFromFile(final String file) {

return url.toString();
}

/**
* Removes file name from URL string
*
* @param file the file String
* @return the URL String
*/
public static String getAbsoluteUrlPathFromFile(final String file) {
String url = getAbsoluteUrlFromFile(file);
return url.substring(0, url.lastIndexOf(File.separator));
}
}

0 comments on commit be04464

Please sign in to comment.