Fall Thursday, November 14, 2013 2013

HttpUnit Quick Start Guide

SOFTWARE TESTING – SWE637 – PROF. JEFF OFFUT HUSAM HILAL HttpUnit Quick Start Guide Husam Hilal © 2013

Downloading HttpUnit: You can download httpunit by visiting the following link: http://sourceforge.net/projects/httpunit/?source=directory

You will see a green download button to the right of the screen as in Figure 1, click on it to start downloading httpunit.

Figure 1: Downloading HttpUnit

The download process should start within 10 seconds. If it did not, try to click on direct link to start downloading it manually as in Figure 2.

Figure 2: Downloading HttpUnit manually

After downloading the archived file httpunit-1.7.zip, you need to extract it to get its content. See Figure 3.

Figure 3: The archive file: httpunit-1.7.zip before and after being extracted to the folder httpunit-1.7

1 HttpUnit Quick Start Guide Husam Hilal © 2013

Integrating HttpUnit with Eclipse and JUnit 4 After you create a new project or on your current project, right click on the project title inside Package Explorer inside Eclipse windows, as in Figure 4. Then, click on Properties.

Figure 4

The properties dialog for that project will be opened as in Figure 5. Click on Java Build Path, and then click on Libraries tab. Now you need to add two main things:

1) httpunit.jar library (found under: C:\...\httpunit-1.7\lib). See Figure 6. 2) The other supportive libraries (found under: C:\...\httpunit-1.7\jars) except -3.8.1.jar. You might not need all of them, but from my experience; adding all of them is better than getting a bunch of exceptions after running your tests. You do not need junit-3.8.1.jar because Eclipse will import JUnit 4 for you automatically in the next steps. See Figures 6, 7 and 8.

2 HttpUnit Quick Start Guide Husam Hilal © 2013

Figure 5: Java Build Path

Figure 6: Adding httpunit.jar library

3 HttpUnit Quick Start Guide Husam Hilal © 2013

Figure 7: Adding other supportive libraries for HttpUnit

Figure 8: All needed libraries for using HttpUnit

4 HttpUnit Quick Start Guide Husam Hilal © 2013

Create new test case Now, you need to go back to your project and add a new JUnit Test Case as in Figure 9.

Figure 9: Adding new JUnit Test Case

In the following dialog, that will appear automatically, select New JUnit 4 test and select setUp() and tearDown() stubs so the wizard will automatically create them, as in Figure 10.

Figure 10: New JUnit Test Case

5 HttpUnit Quick Start Guide Husam Hilal © 2013

A message will show up as in Figure 11 giving you the automatic option of adding JUnit4 library to the build path. Select Perform the following action and hit OK.

Figure 11: Adding JUnit 4 library

Now the test case file “TestSample.java” is ready. You need to add the boilerplate “import com.meterware.httpunit.*;” so you will be using httpunit without any issues. I will leave the rest for you to study the following example at Figure 12. import static org.junit.Assert.*; import java.io.IOException; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.xml.sax.SAXException; import com.meterware.httpunit.*; public class TestSample {

String WEBPAGE_URL = "http://www.cs.gmu.edu/~offutt/classes/637/sched.html"; WebConversation wc; WebRequest request; WebResponse responce; WebForm form;

@Before public void setUp() throws Exception { wc = new WebConversation(); request = new GetMethodWebRequest(WEBPAGE_URL); responce = wc.getResponse( request ); }

6 HttpUnit Quick Start Guide Husam Hilal © 2013

@After public void tearDown() throws Exception { wc = null; request = null; responce = null; }

//Notice the test success by matching the intended Webpage title //with the expected value @Test public void test_Success() throws SAXException, IOException { WebLink link = responce.getLinkWith("My home page"); link.click(); responce = wc.getCurrentPage(); String pageTitle = responce.getTitle(); assertEquals("Jeff Offutt home page",pageTitle); }

//Notice that the Webpage title is incorrect @Test public void test_Fail() throws SAXException, IOException { WebLink link = responce.getLinkWith("My home page"); link.click(); responce = wc.getCurrentPage(); String pageTitle = responce.getTitle(); assertEquals("home page",pageTitle); } } Figure 12: TestSample.java

The output for executing TestSample.java:

Figure 13: HttpUnit and JUnit test results

7 HttpUnit Quick Start Guide Husam Hilal © 2013

Figure 14: Why test_Fail() test failed Appendix You can find HttpUnit API references under this directory: “..\httpunit-1.7\doc\api”. Open index.html or index-all.html for the documentation.

TestSample.java file is attached with this documentation.

All the libraries used in the project are listed in Figure 15.

Figure 15: Libraries

8 TestSample.java

1 package sample; 2 import static org.junit.Assert.*; 3 4 import java.io.IOException; 5 import org.junit.After; 6 import org.junit.Before; 7 import org.junit.Test; 8 import org.xml.sax.SAXException; 9 10 import com.meterware.httpunit.*; 11 12 public class TestSample { 13 14 String WEBPAGE_URL = "http://www.cs.gmu.edu/~offutt/classes/637/sched.html"; 15 WebConversation wc; 16 WebRequest request; 17 WebResponse responce; 18 WebForm form; 19 20 @Before 21 public void setUp() throws Exception { 22 wc = new WebConversation(); 23 request = new GetMethodWebRequest(WEBPAGE_URL); 24 responce = wc.getResponse( request ); 25 } 26 27 @After 28 public void tearDown() throws Exception { 29 wc = null; 30 request = null; 31 responce = null; 32 } 33 34 //Notice the test success by matching the intended WebPage title 35 //with the expected value 36 @Test 37 public void test_Success() throws SAXException, IOException { 38 WebLink link = responce.getLinkWith("My home page"); 39 link.click(); 40 responce = wc.getCurrentPage(); 41 String pageTitle = responce.getTitle(); 42 assertEquals("Jeff Offutt home page",pageTitle); 43 } 44 45 //Notice that the WebPage title is incorrect 46 @Test 47 public void test_Fail() throws SAXException, IOException { 48 WebLink link = responce.getLinkWith("My home page"); 49 link.click(); 50 responce = wc.getCurrentPage(); 51 String pageTitle = responce.getTitle(); 52 assertEquals("home page",pageTitle); 53 } 54 } 55

Page 1