RIFE logo
This page last changed on Apr 03, 2008 by gbevin.

RIFE web applications can be fully tested outside of a servlet container. This offers much more flexibility and makes it possible to assert conditions based on the site structure and the templates that have been used. To provide HTML parsing and DOM manipulation, RIFE relies on NekoHTML, which in turn relies on Xerces. You need these libraries in your classpath to perform some of the testing functionalities.

To test your web application, you need to have an instance of a Site that contains the elements and all the other declarations that you want to test. Typically this will be exactly the same site as the one that is setup for your real web application in the repository.

It can be obtained like this:

Site site = Site.getRepInstance();

It's however possible to build a site on the fly with only certain elements or sub-sites, if they need to be tested in isolation or in different situations (very useful for reusable components).

For example:

Site site = new SiteBuilder("test")
        .setArrival("Home")
        .addGlobalExit("home", "Home")
        .enterElement("pub/home.xml")
            .setId("Home")
            .setUrl("home")
        .leaveElement()
    .getSite();

Once the site instance has been obtained or created, you can start a MockConversation and perform all the test tasks. For more information, it's best to look at the javadocs of the {{com.uwyn.rife.test}} package.

Below is an example test case:

MockConversation conversation = new MockConversation(Site.getRepInstance());
MockResponse response;
ParsedHtml parsed;

// go to the homepage
response = conversation.doRequest("/");
parsed = response.getParsedHtml();

// get the link to the admin site and click on it
MockLink link_admin = parsed.getLinkWithText("here");
response = link_admin.click();
parsed = response.getParsedHtml();

// check that the authentication page is shown
assertEquals("RIFE/Jumpstart - Admin - Authentication", parsed.getTitle());

// fill in the form on the authentication page and submit it with wrong credentials
MockForm form_credentials = parsed.getFormWithName("credentials");
response = form_credentials
    .parameter("login", "wronglogin")
    .parameter("password", "wrongpassword")
    .submit();
parsed = response.getParsedHtml();

// check that the authentication page is still shown
assertEquals("RIFE/Jumpstart - Admin - Authentication", parsed.getTitle());
assertEquals("The login and the password that you provided are not valid.", 
             response.xpathString("//*[@id='error_area']/text()"));

// fill in the form on the authentication page and submit it with correct credentials
form_credentials = parsed.getFormWithName("credentials");
response = form_credentials
    .parameter("login", "admin")
    .parameter("password", "password")
    .submit();
parsed = response.getParsedHtml();

// check that the administration homepage is shown
assertEquals("RIFE/Jumpstart - Admin - Home", parsed.getTitle());

// check that the authentication cookie has been added
assertEquals(1, response.getNewCookieNames().size());
assertEquals("authid", response.getNewCookieNames().get(0));

RIFE's advanced features, for instance continuations, require a custom classloader that loads all the classes in the application. To ensure that this classloader is used right from the start, it's recommended to prefix your test framework main class with the RunWithEngineClassLoader class on the command line. For example for JUnit (all on one line):

java -Drife.webapp.path=../src:../tests:../build/classes:../build/tests:../lib
     com.uwyn.rife.test.RunWithEngineClassLoader junit.textui.TestRunner
     com.uwyn.rife.jumpstart.TestApplication

Note the rife.webapp.path JVM property, it should contain all the paths with the classes and resources that are part of this application (not the JDK classes, but those that typically end up in the WEB-INF directory). The classloader will only process these classes.

Document generated by Confluence on Oct 19, 2010 14:57