Java Reference
In-Depth Information
Let's work through this example. We create the web client, get the page containing
the form, and get the form. Next, we get the input text field from the form, emulate
the user typing in a value b , and then get and click the Submit button C . We get a
page back from clicking the button, and we make sure it's the expected page.
If at any step, the framework doesn't find an object, the API throws an exception
and the test automatically fails. This allows you to focus on the test and let the frame-
work handle failing your test if the page or form isn't as expected. The section “Test-
ing JavaScript alerts” completes this example.
12.3.13 Testing frames
HTML frame support is built into the HtmlPage API , where frames can be accessed
with getFrames (returns List<FrameWindow> ) to get all iframes and frames and get-
FrameByName to get the first iframe or frame with a given name. You then call
FrameWindow getEnclosedPage to get the HTML page in that frame. Listing 12.7 navi-
gates through the Java 6 Javadoc.
Listing 12.7
Page navigation through frames
@Test
public void testFramesByNames() throws IOException {
WebClient webClient = new WebClient();
HtmlPage mainPage = (HtmlPage)
webClient.getPage(" http://java.sun.com/javase/6/docs/api/index.html");
// Gets page of the first Frame (upper left)
HtmlPage packageListPage = (HtmlPage)
mainPage.getFrameByName("packageListFrame").getEnclosedPage();
packageListPage.getAnchorByHref("java/lang/package-
frame.html").click();
// get page of the Frame named 'packageFrame' (lower left)
HtmlPage packagePage = (HtmlPage)
mainPage.getFrameByName("packageFrame").getEnclosedPage();
packagePage.getAnchors().get(1).click();
// get page of the Frame named 'classFrame' (right)
HtmlPage classPage = (HtmlPage)
mainPage.getFrameByName("classFrame").getEnclosedPage();
webClient.closeAllWindows();
}
This example uses getFrameByName to get frames and then calls getEnclosedPage .
Unit tests can use the list API getFrames as well, but we point you to the issues discussed
in section 12.3.7, “Accessing elements by name versus index,” earlier in this chapter.
The intermediary FrameWindow returned by getFrameByName isn't used in this
example. Note that it represents the actual web window for a frame or iframe and pro-
vides API s to dig deeper through the GUI such as getFrameElement , which returns a
BaseFrame . BaseFrame in turn provides access to attributes like longdesc , noresize ,
scrolling , and so on.
 
 
 
 
 
 
 
Search WWH ::




Custom Search