HTTPUNIT

12
HTTPUNIT HTTPUNIT

description

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 - PowerPoint PPT Presentation

Transcript of HTTPUNIT

Page 1: HTTPUNIT

HTTPUNITHTTPUNIT

Page 2: HTTPUNIT

What is HTTPUNITWhat is HTTPUNIT

HttpUnit is an open source software testing HttpUnit is an open source software testing framework used to perform testing of web framework used to perform testing of web sites without the need for a web browser.sites without the need for a web browser.

HttpUnit is primarily designed for "black-box" HttpUnit is primarily designed for "black-box" testing of web sites testing of web sites

HttpUnit is free software available from HttpUnit is free software available from [HttpUnitSite] that implements several useful unit [HttpUnitSite] that implements several useful unit testing methods together with classes for connecting testing methods together with classes for connecting to HTTP servers, processing HTML, and maintaining to HTTP servers, processing HTML, and maintaining stateful sessionsstateful sessions. .

Page 3: HTTPUNIT

BSD(Berkeley Software Distribution) BSD(Berkeley Software Distribution) license.license.

Recent Version HttpUnit1.7Recent Version HttpUnit1.7

JavaScript support is very basic at JavaScript support is very basic at present. The near-term goal is full present. The near-term goal is full JavaScript 1.1 support. Currently, we JavaScript 1.1 support. Currently, we do not plan to support browser-do not plan to support browser-specific JavaScript. specific JavaScript.

Page 4: HTTPUNIT

HttpUnit supports :HttpUnit supports :o HTMLHTML form submission form submission o JavaScriptJavaScripto automatic page redirection and automatic page redirection and

cookiescookies. . o Written in Written in JavaJava, HttpUnit allows Java , HttpUnit allows Java

test code to process returned pages test code to process returned pages as text, XML as text, XML DOMDOM, or containers of , or containers of forms, tables and links forms, tables and links

Page 5: HTTPUNIT

It is well suited to be used in It is well suited to be used in combination with combination with JUnitJUnit..

It easily write tests that verify the It easily write tests that verify the proper behaviour of a web site. proper behaviour of a web site.

The use of HttpUnit allows for The use of HttpUnit allows for automated testing of web automated testing of web applications applications

Page 6: HTTPUNIT

The center of HttpUnit is the WebConversation The center of HttpUnit is the WebConversation class, which takes the place of a browser class, which takes the place of a browser talking to a single site. talking to a single site.

It is responsible for maintaining session It is responsible for maintaining session context, which it does via cookies returned by context, which it does via cookies returned by the server.the server.

To use it, one must create a request and ask To use it, one must create a request and ask the WebConversation for a response. the WebConversation for a response.

For Example:For Example:

WebConversation wc = WebConversation wc = newnew WebConversation(); WebConversation(); WebRequest req = WebRequest req = newnew GetMethodWebRequest( GetMethodWebRequest( "http://www.meterware.com/testpage.html" ); "http://www.meterware.com/testpage.html" );

WebResponse resp = wc.getResponse( req );WebResponse resp = wc.getResponse( req );

Page 7: HTTPUNIT

The response may now be The response may now be manipulated either as pure text (via manipulated either as pure text (via the getText() method), as a DOM (via the getText() method), as a DOM (via the getDOM() method)the getDOM() method)

WebConversation wc = WebConversation wc = newnew WebConversation(); WebResponse resp = WebConversation(); WebResponse resp = wc.getResponse( wc.getResponse( "http://www.meterware.com/testpage.html" "http://www.meterware.com/testpage.html" ); );

Page 8: HTTPUNIT

Some Drawbacks to HTTPUnitSome Drawbacks to HTTPUnit:: Tests are tied to page structures.Tests are tied to page structures.

If you change your page (say, reordering If you change your page (say, reordering links or forms), you will break your tests.links or forms), you will break your tests.

Test assertions typically depend on Test assertions typically depend on the content of the page.the content of the page. If content changes, tests may break.If content changes, tests may break. Checking content is not completely Checking content is not completely

sufficient for testing Grid portals.sufficient for testing Grid portals.

Page 9: HTTPUNIT

public WebConversation loginToPortal() public WebConversation loginToPortal() throws Exception {throws Exception {

//WebConversation objects are the cornerstone //WebConversation objects are the cornerstone classclass..

WebConversation wc = new WebConversation();WebConversation wc = new WebConversation(); //We will need to specify the user agent to simulate a //We will need to specify the user agent to simulate a browserbrowser..

ClientProperties cprops = ClientProperties cprops = wc.getClientProperties();wc.getClientProperties();

cprops.setUserAgent("Mozilla/5.0");cprops.setUserAgent("Mozilla/5.0"); WebResponse resp = wc.getResponse(portalUrl);WebResponse resp = wc.getResponse(portalUrl); // Find the login form.// Find the login form.

// It's not named so we pick it out of an array// It's not named so we pick it out of an array String frontPage = resp.getText();String frontPage = resp.getText();

Page 10: HTTPUNIT

//First make sure we are looking at the right web page//First make sure we are looking at the right web page.. assertTrue("Failed to get front page",assertTrue("Failed to get front page", frontPage.indexOf("Please Login") != -1);frontPage.indexOf("Please Login") != -1); WebForm form = resp.getForms()[1];WebForm form = resp.getForms()[1]; assertNotNull("No login form found", form);assertNotNull("No login form found", form); // Fill in form and submit// Fill in form and submit form.setParameter("userName", “admin”); form.setParameter("userName", “admin”); form.setParameter("password", “admin”);form.setParameter("password", “admin”); form.submit();form.submit(); // Get logged in page// Get logged in page WebResponse resp2 = wc.getCurrentPage();WebResponse resp2 = wc.getCurrentPage(); String page = resp2.getText();String page = resp2.getText(); assertFalse("Failed to log in",assertFalse("Failed to log in", page.indexOf("Please Login") != -1);page.indexOf("Please Login") != -1); return wc;return wc; }}

Page 11: HTTPUNIT

Assertion Statement Reference:Assertion Statement Reference: There is the list of the There is the list of the different types of assertion statements that are used to test different types of assertion statements that are used to test your code.your code.

o assertNotNull("url: ", url, response)assertNotNull("url: ", url, response) o assertFalse(message, condition) assertFalse(message, condition) o assertNotNull(object) assertNotNull(object) o assertNotSame(expected, actual) assertNotSame(expected, actual) o assertNotSame(message, expected, actual) assertNotSame(message, expected, actual) o assertNull(object) assertNull(object) o assertNull(message, object) assertNull(message, object) o assertSame(expected, actual) assertSame(expected, actual) o assertSame(message, expected, actual) assertSame(message, expected, actual) o assertTrue(condition) assertTrue(condition) o assertTrue(message, condition) assertTrue(message, condition) o fail() fail() o fail(message) fail(message)

Page 12: HTTPUNIT

Thank YouThank You