Java Reference
In-Depth Information
RichFaces button
E
. At
F
we extract the value of the status parameter of the managed
bean and assert its value
G
. On the client side, we also get the value of the
span
element
on the page
H
and assert its content
I
.
The
JSFU
nit project provides tight integration with the RichFaces project; if you
find this treatment brief, you can use the
RichFacesClient
JSFU
nit class.
RichFaces-
Client
provides methods for testing drag-and-drop behavior, sliders, calendars, and
other
JSF
widgets.
15.6
Using HtmlUnit with JSFUnit
In this section, we show how
JSFU
nit and HtmlUnit can work together.
Chapter 12 shows how to get to an HtmlUnit
HtmlPage
to start testing a page,
for example:
WebClient webClient = new WebClient();
webClient.setThrowExceptionOnScriptError(false);
HtmlPage searchPage = (HtmlPage)
webClient.getPage("
http://www.google.com");
Listing 15.13 shows how to use HtmlUnit in a
JSFU
nit test:
Listing 15.13
Using HtmlUnit in a JSFUnit test
[...]
public class
TestListAvailableAlbumsWithHTMLUnit
extends
ServletTestCase {
B
C
public void
testIntialPage()
throws
IOException {
JSFSession jsfSession =
new
JSFSession("/");
JSFClientSession client = jsfSession.getJSFClientSession();
D
HtmlPage page = (HtmlPage)client.getContentPage();
HtmlTable table = (HtmlTable)
page.getFirstByXPath("/html/body/form/table");
E
assertNotNull("table should not be null",table);
assertEquals( 6, table.getRowCount() );
HtmlAnchor link = table.getRow(1).getCell(0)
.getFirstByXPath( "a" );
F
G
H
I
assertNotNull("link should not be null", link);
HtmlPage newPage = link.click();
assertEquals(newPage.getTitleText(), "Album details");
}
}
We start our test case again by extending the Cactus
ServletTestCase
class
B
. In our
test
C
, we create a new
JSFSession
object with the request
URL
that we want to
invoke. From the
jsfSession
object, we get a
JSFClientSession
D
. From the
client
object, we get an
HtmlTable
using an
XP
ath expression reflecting the
DOM
tree struc-
ture of our document
E
. We assert that the table isn't
null
and that the number of