140 likes | 311 Views
HTTPUNIT. What is HTTPUNIT. HttpUnit is an open source software testing framework used to perform testing of web sites without the need for a web browser. HttpUnit is primarily designed for "black-box" testing of web sites
E N D
What is HTTPUNIT • HttpUnit is an open source software testing framework used to perform testing of web sites without the need for a web browser. • HttpUnit is primarily designed for "black-box" testing of web sites • HttpUnit is free software available from [HttpUnitSite] that implements several useful unit testing methods together with classes for connecting to HTTP servers, processing HTML, and maintaining stateful sessions.
BSD(Berkeley Software Distribution) license. • Recent Version HttpUnit1.7 • JavaScript support is very basic at present. The near-term goal is full JavaScript 1.1 support. Currently, we do not plan to support browser-specific JavaScript.
HttpUnit supports : • HTML form submission • JavaScript • automatic page redirection and cookies. • Written in Java, HttpUnit allows Java test code to process returned pages as text, XML DOM, or containers of forms, tables and links
It is well suited to be used in combination with JUnit. • It easily write tests that verify the proper behaviour of a web site. • The use of HttpUnit allows for automated testing of web applications
The center of HttpUnit is the WebConversation class, which takes the place of a browser talking to a single site. • It is responsible for maintaining session context, which it does via cookies returned by the server. • To use it, one must create a request and ask the WebConversation for a response. For Example: WebConversation wc = new WebConversation(); WebRequest req = new GetMethodWebRequest( "http://www.meterware.com/testpage.html" ); WebResponse resp = wc.getResponse( req );
The response may now be manipulated either as pure text (via the getText() method), as a DOM (via the getDOM() method) • WebConversation wc = new WebConversation(); WebResponse resp = wc.getResponse( "http://www.meterware.com/testpage.html" );
Some Drawbacks to HTTPUnit: • Tests are tied to page structures. • If you change your page (say, reordering links or forms), you will break your tests. • Test assertions typically depend on the content of the page. • If content changes, tests may break. • Checking content is not completely sufficient for testing Grid portals.
public WebConversation loginToPortal() throws Exception { //WebConversation objects are the cornerstone class. WebConversation wc = new WebConversation(); //We will need to specify the user agent to simulate a browser. ClientProperties cprops = wc.getClientProperties(); cprops.setUserAgent("Mozilla/5.0"); WebResponse resp = wc.getResponse(portalUrl); // Find the login form. // It's not named so we pick it out of an array String frontPage = resp.getText();
//First make sure we are looking at the right web page. assertTrue("Failed to get front page", frontPage.indexOf("Please Login") != -1); WebForm form = resp.getForms()[1]; assertNotNull("No login form found", form); // Fill in form and submit form.setParameter("userName", “admin”); form.setParameter("password", “admin”); form.submit(); // Get logged in page WebResponse resp2 = wc.getCurrentPage(); String page = resp2.getText(); assertFalse("Failed to log in", page.indexOf("Please Login") != -1); return wc; }
Assertion Statement Reference: There is the list of the different types of assertion statements that are used to test your code. • assertNotNull("url: ", url, response) • assertFalse(message, condition) • assertNotNull(object) • assertNotSame(expected, actual) • assertNotSame(message, expected, actual) • assertNull(object) • assertNull(message, object) • assertSame(expected, actual) • assertSame(message, expected, actual) • assertTrue(condition) • assertTrue(message, condition) • fail() • fail(message)