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;
response = conversation.doRequest("/");
parsed = response.getParsedHtml();
MockLink link_admin = parsed.getLinkWithText("here");
response = link_admin.click();
parsed = response.getParsedHtml();
assertEquals("RIFE/Jumpstart - Admin - Authentication", parsed.getTitle());
MockForm form_credentials = parsed.getFormWithName("credentials");
response = form_credentials
.parameter("login", "wronglogin")
.parameter("password", "wrongpassword")
.submit();
parsed = response.getParsedHtml();
assertEquals("RIFE/Jumpstart - Admin - Authentication", parsed.getTitle());
assertEquals("The login and the password that you provided are not valid.",
response.xpathString("));
form_credentials = parsed.getFormWithName("credentials");
response = form_credentials
.parameter("login", "admin")
.parameter("password", "password")
.submit();
parsed = response.getParsedHtml();
assertEquals("RIFE/Jumpstart - Admin - Home", parsed.getTitle());
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):
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.