8.1. Using pages in a step library

When you need to use a page object in one of your steps, you just ask for one from the Page factory, providing the class of the page object you need, e.g.

FindAJobPage page = getPages().get(FindAJobPage.class);

If you want to make sure you are one the right page, you can use the currentPageAt() method. This will check the page class for any @At annotations present in the Page Object class and, if present, check that the current URL corresponds to the URL pattern specified in the annotation. For example, when you invoke it using currentPageAt(), the following Page Object will check that the current URL is precisely http://www.apache.org.

@At("http://www.apache.org")
public class ApacheHomePage extends PageObject {
    ...
}

The @At annotation also supports wildcards and regular expressions. The following page object will match any Apache sub-domain:

@At("http://.*.apache.org")
public class AnyApachePage extends PageObject {
    ...
}

More generally, however, you are more interested in what comes after the host name. You can use the special #HOST token to match any server name. So the following Page Object will match both http://localhost:8080/app/action/login.form an http://staging.acme.com/app/action/login.form. It will also ignore parameters, so http://staging.acme.com/app/action/login.form?username=toto&password=oz will work fine too.

@At(urls={"#HOST/app/action/login.form"})
public class LoginPage extends PageObject {
   ...
}