HTMLUnit Test Cases
HTMLUnit Test Cases
HTMLUnit Test Cases
http://www.topcoder.com/tc?module=Static&d1=f...
Login
Competitions Forums TopCoder Wiki Event Calendar Press Room Surveys My TopCoder About TopCoder
Overview
Were you the type of child who took apart your parents' appliance to see how they worked? If you like seeing how things work and finding ways to make them stop then testing might be for you. Through TopCoder's Testing Competitions, participants will create test code that will be used to put Assembly Competition submissions through their paces. This article will provide an overview of how the competition works, and what it takes to write a simple functional test.
Member Search:
Handle:
Advanced Search
The testing distribution posted by the TopCoder stuff will contain all the information you need to write the tests. The most important documents are: The specification A high-level overview of the functionality of the application you are going to test. The use cases The use cases for the application. You should be familiar with them. The test scenario This document contains the test scenarios which need to be implemented. It is the minimum you must submit. Feel free to add new scenarios, though, since the score depends on the quality of scenarios you submit. The prototype This is the most important part. It defines the page layout, the navigation, form, inputs, the names of links, etc. - all the critical details that must be used while writing the tests. It is highly recommended to deploy the prototype on the local http server (Tomcat, Apache, IIS, or whatever you feel comfortable with) and try to run the test cases against it. If you have any questions about the prototype, or the scenarios, or anything, don't be afraid to post your thoughts on the forum specific to your contest (the forums will be created for each competition).
1 of 6
http://www.topcoder.com/tc?module=Static&d1=f...
Login True
In the next few paragraphs I will try to show how the test case could be done.
Because in our sample test scenario we are going to test the alert message, an appropriate handler must be registered.
WebClient provides a couple of convenient methods to do that. You may use the URL or String (since 1.9), as well as create your own custom request using the WebRequestSetting object. This method returns the Page object implementation depending on the content type of the header. It may be HtmlPage instance (for text/html), TextPage (for text/plain) and so on. In your tests you expect to receive HtmlPage. If any error occurs with the connection or on the server side (like 404) an exception will be thrown.
Page respPage = client.getPage("http://localhost:8080/tc/sample.html"); assertEquals("Expected HtmlPage", HtmlPage.class, respPage.getClass()); HtmlPage page = (HtmlPage) respPage;
Checking the response. We should check if the returned page is the one we had expected. The HtmlUnit does not offer us any new assertions, so we need to use the one from the JUnit. For the sample test page, we expect the title to be 'Test sample', the page has one table with the caption 'This is a sample form.' and one form. To check this, you use some method offered by the HtmlPage class. First we check the page title:
Then, we check the tables count. Because there is no API call to get the table, we can get the table element by tag name.
Navigation on the page. The Web page usually contains a hyperlink to other resources. Such links are modeled in the HtmlUnit by HtmlAnchor class. The framework offers two straightforward methods to get the link. If you know the href attribute of the anchor tag, you may use:
assertNotNull(page.getAnchorByHref("result.html"));
assertNotNull(page.getAnchorByName("test"));
Things get harder if the anchor does not have name or id attributes and the href value is generated dynamically (for example, if it contains the session id). In such cases you need to use the value of the anchor tag. Because there is no method to do that, we need to do it by hand. First we get the list of all anchors on the page:
Now, iterating thru the list; you check each tag converted to the page representation. Look for the 'Result page' name and create a helper method getLink(HtmlPage page, String linkName);
2 of 6
http://www.topcoder.com/tc?module=Static&d1=f...
private static HtmlAnchor getLink(HtmlPage page, String linkName) { List anchors = page.getAnchors(); for (Iterator it = anchors.iterator(); it.hasNext();) { HtmlAnchor anchor = (HtmlAnchor) it.next(); if (linkName.equals(anchor.asText().trim())) { return anchor; } } return null; // or throw an exception }
The disadvantage of this method is that the value must be unique thru the page or else only the first link will be returned (of course you may code this method to return the 2nd, 3rd, ..n, nth occurrence of the name, but the order of the anchors is not guaranteed). Using the anchor. Clicking on the link usually leads to some action and your current object may become outdated. Use the one returned from the click action; the returned object does not necessarily need to be HtmlPage, but you can assume it will be.
Filling the form out. Now we know how to connect to the server, get the page, check its content, and navigate using hyperlinks. It's time to fill out the form since the straight majority of the test scenarios contain them. The form is represented by the HtmlForm class, which provides some really helpful methods. First of all, you need to get the proper form. It's very simple if we know the 'name' attribute of the form:
If the form doesn't have a name attribute, but has an id, you may use a bit more general method:
Having the form, you may like to check if it has the correct submit method...
or action
The form's inputs have their equivalent in the framework - HtmlInput and the specialized HtmlTextInput, HtmlPasswordInput, HtmlCheckBoxInput, and so on. We may retrieve each of them by the 'name' attribute or by the value (these are not the only possibilities to get the input or any other element on the page, though). To get the login and password input:
You may also check if the password input has type="password" attribute. You may do this in any of the following ways:
assertEquals("Input: password type:", "password", pass.getAttributeValue("type")); assertEquals("Input: password should have type=password", HtmlPasswordInput.class, pass.getClass());
Setting the value of the input meansjust setting its value attribute :)
pass.setAttributeValue("value", "test");
3 of 6
http://www.topcoder.com/tc?module=Static&d1=f...
login.setValueAttribute("TCSTESTER");
The setAttributeValue() method can be used to modify other attributes as well. Select box. The select boxes contain a list of options, one or more of which can be selected. By using the HtmlSelect you may check the option values, the default ones, the options id/value pairs. We may also 'inject' a fake value that was not present on the list. To do that you need to get the HtmlSelect instance from the form:
We may get the option at any position or the name, as well as the list of all options:
There are two methods to select an option (or options for multiple selects). Because there may be some other action behind this, the setSelectedAttribute method returns the page object. It may be the same page, if no action was taken, or a new one - for instance, if the selecting action causes some changes on the page via javascript or a round trip to the Web server. On our sample page, the result of selecting any option is the alert message. You will check if the alert box has been shown to the user.
String alertMessage = handler.popAlertMessage(); assertNull("Unexpected alert: " + alertMessage, alertMessage); actionSelect.setSelectedAttribute("Login", true); assertEquals("Alert message", "Alert message", handler.popAlertMessage());
Check boxes. The next most often used input element is the checkbox. It is represented in the framework by the HtmlCheckBoxInput class and allows you to check its default value and/or change it.
If the button which we need to use does not have the 'name' or id attributes set, you may need to find it in the same ways you do with the anchors, by getting the list of the buttons on the form and iterating over it checking the button asText() values.
4 of 6
http://www.topcoder.com/tc?module=Static&d1=f...
First, we need to get the table. As I said before, there is no API method to get the table. The next method that can be used is getOneHtmlElementByAttribute().This method takes 3 arguments: the tag name, the attribute name and the attribute value. It may be used to get each element from the page that matches. If the element does not exist, an exception will be thrown.
If the table you test contains the <caption> element (and the sample test page does), you may check its value using API call.
HtmlTable provides the access to its rows, as well as to each cell. The indexes of the cells are zero-based. The HtmlTableCell has two concrete implementations: HtmlTableDataCell and HtmlTableHeaderCell which are just the markers for the <td> and <th> tags since they do not provide any new methods.
To check the text value of the cell, we need to use the asText() method.
If the value of the cell is expected to be something more than just a plain text, you may select this element, using the cell as a base, by id or any other attribute.
cell = table.getCellAt(4, 1); HtmlAnchor anchor = (HtmlAnchor) cell.getHtmlElementById("linkId"); anchor = (HtmlAnchor) cell.getHtmlElementsByAttribute("a", "name", "linkName").get(0); assertNotNull("Missing link.", anchor);
Note that the getHtmlElementsByAttribute() will return the list of elements which match the given triple (though, keep in mind, you should check the list size before accessing any element). If none are found, an empty list will be returned.
class MessageHandler implements AlertHandler { private List alerts = new ArrayList(); /** * Handle an alert for the given page. * * @param page The page on which the alert occurred. * @param message The message in the alert. */ public void handleAlert(Page page, String message) { alerts.add(message); } /** * Returns the last alert message or null, if wasn't any. * * @return the last alert message or null, if wasn't any. */ public String popAlertMessage() { if (alerts.size() > 0) { return (String) alerts.remove(0); } return null; } }
That's it - the test case is ready, and you should be ready to participate in the Testing Competition. Good luck!
5 of 6
http://www.topcoder.com/tc?module=Static&d1=f...
Home | About TopCoder | Press Room | Contact Us | Careers | Privacy | Terms Competitions | Software
Copyright 2001-2010, TopCoder, Inc. All rights reserved.
6 of 6