WSO2Con 2013 CFP Banner

Testing the Apache Axis2 Web Module with HttpUnit

This tutorial by Charitha Kankanamge demonstrates how HttpUnit is used to test the Axis2 administration console -Web module. At the end of the tutorial, you will know how to automate a sanity check of the axis2 Webapp module. It can also be extended to test the whole Web application or used in regression testing when Axis2 is deployed on different servlet containers.

Date: Thu, 16th Aug, 2007
Level: Introductory
Reads: 8360
Discuss this article on Stack Overflow View | Add


IntroductionHttpUnit

HttpUnit facilitates agile testing by providing a set of APIs to construct test cases, which can be used to verify the contents of Web applications. It is very useful for black box testing. HttpUnit can emulate user interactions with a Web application. HttpUnit can be combined with a Java unit test framework, such as JUnit, and used effectively for verifying the behavior of a Web application without launching it in a browser.

Background

HttpUnit is an open source testing framework. It helps to implement automated test scripts for Web applications. The HttpUnit API can mimic a number of browser behaviors, including form submission, JavaScript, HTTP authentication, and the use of cookies. It also enables you to analyze the content that is returned when a Web page is loaded. For further information on HttpUnit, refer to the HttpUnit cookbook.

Getting Started

Prerequisites

  • Apache Tomcat (http://tomcat.apache.org/download-60.cgi)
  • axis2.war (http://ws.apache.org/axis2/download.cgi)
  • JDK 1.5 or higher

Download the axis2.war distribution from the above location and deploy it on Apache Tomcat. See the Axis2 Installation Guide for more information on installing the Axis2 Web application. We are going to test the following scenario of the Axis2 Web application. The initial test verifies the functionality of the Axis2 Web module when it is deployed on a servlet container.

  1. Access the Axis2 Web application (Administration page)
  2. Verify the 'HappyAxis' page
  3. Log in to the Administration page using the default user name and password
  4. Verify the available services list
  5. Check the WSDL generated by the default version service

Automating the Axis2 Administration Console with HttpUnit

We will go through each of the above test scenarios and see how HttpUnit is used to automate them. Open the Axis2 Web application, http://localhost:8080/axis2 using any browser, and go through the above steps manually before automating the scenario.

First, download the latest stable version of HttpUnit (1.6.2 at the time of writing)

Unzip the contents into a directory in your local file system. For example, HTTPUNIT_HOME. Now add all the .jar files in HTTPUNIT_HOME/jars and HTTPUNIT_HOME/lib/httpunit.jar in to your class path.

Next, open your favorite IDE and add the following JUnit test case.

public class AxisHttpUnitTest extends TestCase {
public void testAxisAdmin(){
}
public void testHappyAxisPage(){
}
public void testAdminLogin(){
}
public void testServiceList(){
}
public void testVersionServiceWSDL(){
}
}

Download the complete source.

  1. Access the Axis2 Web Application (Administration page)

  2. Open the home page of the Axis2 administration console by pointing your browser to http://localhost:8080/axis2.

    In the first scenario, we will see how the Axis2 administration page can be accessed through HttpUnit. The WebConversation, WebRequest and WebResponse classes are the fundamental constructs of any HttpUnit test. All other tests are built using them. The primary object of an HttpUnit test case is WebConversation. It is used to maintain a session context between server and client communication. In other words, a user session (a sequence of interactions with the Web application) is encapsulated with it. An instance of the WebConversation class is created as follows.

    WebConversation wc = new WebConversation()

    Then, we have to create a WebRequest which is used to send a request to the server.

    WebRequest request = new GetMethodWebRequest("http://localhost:8080/axis2")

    Here we access the Axis2 home page. So we send the HTTP GET request "localhost:8080/axis2".

    Now we need to get the response back. An object of the WebResponse class can be used as follows.

    WebResponse response = wc.getResponse(request)

    We can check whether the correct page is retrieved by adding an assertion as follows.

    assertEquals("Axis 2 - Home", response.getTitle())

    The first test scenario was automated easily using the basic constructs of HttpUnit. Next we will see how page navigation can be performed using HttpUnit.

  3. Verify the Axis2 Happiness page

  4. When you deploy Axis2 in any servlet container, the easiest way to verify the status of the Axis2 installation is to check the Axis2 Happiness page. If your installation is successful, you will see a page as follows.

    We can easily emulate the actions of this scenario with HttpUnit. Click the Validate link on the Axis2-Home page, which was done in the previous step (scenario 1), to access the Axis2 Happiness page. Add the following HttpUnit code to get the Validate link from the Axis2 home page.

    WebLink validatelink = response.getLinkWith("Validate")

    WebLink class represents a link in an HTML page. So, we can call the getLinkWith() method which returns the first link with the specified text and assigns the return value to an instance of the WebLink. We can verify the success of the above statement by adding an assertion as follows.

    assertNotNull(validatelink)

    Next, click the Validate link by calling the click() method of the WebLink instance.

    WebResponse happyaxispage = validatelink.click()

    The Validatelink.click() method returns an instance of the WebResponse which is the Axis2 Happiness page. To check this, the following assertion can be used.

    assertEquals("Axis2 Happiness Page",happyaxispage.getTitle())

    Now we need to check the status of the Axis2 installation as stated on the Axis2 Happiness page. It can be verified by getting the HTML text of the page and checking whether the status message is included in there. With HttpUnit, we can get the raw HTML output via getText().

    assertTrue(happyaxispage.getText().contains("properly."))

    We have completed two simple test scenarios so far.

  5. Log in to the Axis2 Administration Console Using the Default User Name and Password

  6. The login page of the Axis2 administration console can be accessed by clicking the Administration link on the Axis2 home page.

    Add the following HttpUnit statement to click the Administration link, which is same as clicking the Validate link in the previous scenario.

    WebLink adminlink = response.getLinkWith("Administration")

    The output of clicking the Administration link is the Axis2 Log in page, which is an instance of the WebResponse class.

    WebResponse axis2loginpage = adminlink.click()

    We can verify the existence of the log in page with either one of the following assertions.

    assertEquals(200, axis2loginpage.getResponseCode())

    Using the above assertion, we check the response code by clicking on the admin link event. If the HTTP CODE is 200, then we can guarantee that the login page exists. To verify the presence of the login page by checking the page title, the following assertion can be used.

    assertEquals("Login to Axis2 :: Administration page", axis2loginpage.getTitle())

    Login entries are implemented as form objects in most HTML pages. In HttpUnit, the WebForm class represents a form in an HTML page. If you open the HTML source of the Login to Axis2 :: Administration page, you can see the name of our WebForm, which is LoginForm. The GetFormWithName() method of the WebResponse class returns an instance of the WebForm as follows.

    WebForm loginform = axis2loginpage.getFormWithName("LoginForm")

    We can check the availability of the login form with the following assertion.

    assertNotNull(loginform)

    Next, we need to fill this form with the username and password of the Axis2 administration page. The setParameter(String, value) method Sets the value of a parameter in this form.

    loginform.setParameter("userName", "admin");
    loginform.setParameter("password", "axis2");

    Now we can simulate the submission of user credentials via the form's submit() method:

    adminpage = loginform.submit()

    The response of the above submit()method can be verified by following two assertions. First, we can check the title of the page using the getTitle() method.

    assertEquals("Axis2 :: Administration Page", adminpage.getTitle())

    Since the getText() method returns the raw HTML output of the page, we can ensure that we logged in to the Axis2 administration console by checking the availability of the Welcome to Axis2 Web Admin Module !! text as follows.

    assertTrue(adminpage.getText().indexOf("Welcome to Axis2 Web Admin Module !!")>0)

    In the next steps, we will see how some of the features in the Axis2 administration console are verified with HttpUnit.

  7. Verify the Available Services List

  8. You can see the deployed services by navigating to the Available Services page once you log into the Axis2 administration page. This can be done by clicking the Available Services link in the left navigation pane on the welcome page.

    We can get the Available Services link by calling the getLinkWith() method.

    WebLink availableserviceslink = adminpage.getLinkWith("Available Services")

    Then, invoke the call() method of the WebLink to access the Available Services page.

    availableservicepage = availableserviceslink.click()

    The axis2 Version service is deployed by default when installing Axis2 on a servlet container. If the installation is successful, the version service should be available on the Available Services page. It can be verified with a HttpUnit assertion as follows.

    assertEquals("Version",availableservicepage.getLinkWith("Version").getText())
  1. Check the WSDL Generated by the Default Version Service

  2. It is essential to verify the WSDL generated by Axis2 when performing a smoke test in any servlet container, which includes the Axis2 Web module. In a code first scenario, if a new version of Axis2 is deployed or if the servlet container is changed, the Web service author has to verify the generated WSDL by manually inspecting each element. The effort and time required to verify the elements of a generated WSDL can be greatly reduced if this task is automated. Let's see how it can be achieved with HttpUnit. To do this, we just check whether the root element is present in the generated WSDL.

    We can retrieve the WSDL of the default version service by clicking on the service on the 'Available Services' page. We use the getLinkWith(“Version”) method to retrieve the hyperlink of the version service.

    WebLink versionservicelink = availableservicepage.getLinkWith("Version")

    Then we can simply click it by calling the Click() method and we get the WSDL of the version service as the response.

    WebResponse versionservicewsdl = versionservicelink.click()

    In this step, we are expected to validate an XML document. So you can use any XML processing model. Here we get the raw HTML text of a WSDL and process it with DOM.

    String text = versionservicewsdl.getText()
    ByteArrayInputStream bais = new ByteArrayInputStream(text.getBytes())
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance()
    DocumentBuilder db = dbf.newDocumentBuilder()
    Document doc = db.parse(bais)
    Element rootElement = doc.getDocumentElement()
    assertEquals(rootElement.getNodeName(), "wsdl:definitions")

    Also, you can traverse through the all elements in the document as follows.

    NodeList childNodes = rootElement.getChildNodes()
    for (int i = 0; i < childNodes.getLength(); i++) {
    if (childNodes.item(i) instanceof Element) {
    System.out.println(((Element) childNodes.item(i)).getNodeName())
    }
    }

    Since the objective of this tutorial is to discuss the usage of HttpUnit with Axis2, we will not cover the details of DOM and other XML parsing techniques. Please visit w3.org or W3 Schools DOM tutorial to get more information on DOM.

    Finally, run all the above test scenarios as JUnit tests in your IDE and check the results.

Summary

HttpUnit is an efficient way to create automated tests programmatically with more freedom than the visual test scripting environments. Most of the tools which record and playback browser activities tend to capture worthless details with hard-to-modify big test scripts. With HttpUnit, you can easily create flexible and structured code while developing the application. Hence, it can be considered as a useful component in a programmers development tool kit in an agile software process. In this tutorial, we discussed a mechanism of using HttpUnit to automate smoke tests in the Axis2 Web module. When the new Axis2 versions are released, Web service authors or deployment engineers can easily verify the functionality of Axis2 on different servlet containers using an automated HttpUnit test suite.

References

1. Httpunit Home

Author

Charitha Kankanamge, Manager - Software Quality Assurance, WSO2. charitha at wso2 dot com