Baccou Bonneville Blogs Eclipse Blog Process Improvement Blog Java Blog Web Design Blog Miscellaneous .NET Blog

12/04/05

English (US)   HtmlUnit tests with Eclipse and Ant  -  Categories: Eclipse Platform, Ant, Eclipse plug-ins  -  @ 03:10:11 pm

HTMLThis note explains how to perform HTML unit tests within eclipse using HtmlUnit. HtmlUnit is a java unit testing framework for testing web based applications. It is a member of the JUnit family. This method work for any kind of web development project, not only Java ones but also PHP projects for example, as we are testing the HTML as it appears on the client side.

In this article we will also see how to integrate these tests with Ant.

[More:]

My first HtmlUnit test

  1. First, you must download HtmlUnit. Uncompress the zip file under c:\htmlunit.

  2. In Eclipse, create a new Java project using File | New | Project | Java | Java project. You must create a Java project only because we will use Java for unit tests. Enter HelloHtml for the name of the project. For the JDK Compliance, choose 5.0. Choose Create separate source and output folders and click on Configure default... Enter src/test for the source folder name and bin for the ouput folder name. Click on next.

  3. Choose Libraries Path tab. Click on Add External JARs and include every JARs from c:\htmlunit\jars. Then, click again on Add External JARs and include the junit.jar JAR from c:\eclipse\plugins\org.junit_3.8.1. Click on Finish.

  4. Select the test directory and choose New | Other | JUnit Test Case. In the package name, enter com.yourcompany. In the name field, enter IndexTest. Click on Finish.

At this step, an IndexTest.java file should have been created with this content:

package com.yourcompany;

import junit.framework.TestCase;

public class IndexTest extends TestCase {

}

Then, edit this file with this content:

package com.yourcompany;

import junit.framework.TestCase;
import java.net.URL;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

public class IndexTest extends TestCase {

  public void testHomepageTitle() throws Exception {
    final WebClient webClient = new WebClient();
    final URL url = new URL("http://localhost");
    final HtmlPage page = (HtmlPage)webClient.getPage(url);
    // System.out.println(page.asXml());
    assertEquals( "Test Page for Apache Installation",
                  page.getTitleText() );		 
  }	

}

This piece of code will test the title of the default page of an Apache freshly installed. Start your Apache server. To run the test, select IndexTest.java, right click and choose Run As | JUnit test. You should have the following JUnit view with a green bar indicating that the test has run successfuly.

JUnit results

If you encounter a problem, just uncomment the line System.out.println(page.asXml());: you will see the page as XML nodes in the console. This is very useful to debug.

The use of the HtmlUnit API documentation is of course mandatory to code HtmlUnit tests.

Create a test suite
Now, you can create a test suite by selecting the test direcotry and by choosing File | New | Other | JUnit Test Suite. You can leave the default settings and click on Finish.

Integration of HtmlUnit tests with Ant
Next step is to launch HtmlUnit tests using Ant.

First, you must add a JAR for JUnit Ant task. Choose Window | Preferences | Ant | Runtime, click on Add External JARs and go in C:\eclipse\plugins\org.apache.ant_1.6.5\lib and choose ant-junit.jar. You must also add a tools.jar using Add External JARs. For example if you are using Java 5.0, you can take the following JAR file: c:\Program Files\java\jdk1.5.0_06\lib\tools.jar (you should download JDK 5.0 if you do not already done).

Then, create a build.xml file on the root of the project with the following content:

<project name="HelloHtml.Project" default="test" basedir=".">

<property name="bin"
             location="${basedir}/bin"/>
<property name="tests.src"
             location="${basedir}/src/test/com/yourcompany"/>

<path id="test.class.path">
<!-- the html unit classes -->
<pathelement path="C:/htmlunit/lib/xmlParserAPIs-2.2.1.jar"/>
<pathelement path="C:/htmlunit/lib/commons-beanutils-1.7.0.jar"/>
<pathelement path="C:/htmlunit/lib/commons-cli-1.0.jar"/>
<pathelement path="C:/htmlunit/lib/commons-codec-1.3.jar"/>
<pathelement path="C:/htmlunit/lib/commons-collections-3.1.jar"/>
<pathelement path="C:/htmlunit/lib/commons-httpclient-3.0-rc3.jar"/>
<pathelement path="C:/htmlunit/lib/commons-io-1.0.jar"/>
<pathelement path="C:/htmlunit/lib/commons-jelly-1.0.jar"/>
<pathelement path="C:/htmlunit/lib/commons-jelly-tags-log-1.0.jar"/>
<pathelement path="C:/htmlunit/lib/commons-jexl-1.0.jar"/>
<pathelement path="C:/htmlunit/lib/commons-lang-2.0.jar"/>
<pathelement path="C:/htmlunit/lib/commons-logging-1.0.4.jar"/>
<pathelement path="C:/htmlunit/lib/dom4j-1.5.jar"/>
<pathelement path="C:/htmlunit/lib/htmlunit-1.7.jar"/>
<pathelement path="C:/htmlunit/lib/jaxen-1.1-beta-6.jar"/>
<pathelement path="C:/htmlunit/lib/js-1.6R1.jar"/>
<pathelement path="C:/htmlunit/lib/nekohtml-0.9.5.jar"/>
<pathelement path="C:/htmlunit/lib/saxpath-1.0-FCS.jar"/>
<pathelement path="C:/htmlunit/lib/xercesImpl-2.6.2.jar"/>
<pathelement path="C:/eclipse/plugins/org.junit_3.8.1/junit.jar"/>	  	  	
	
<!-- the test classes are all in here -->
<pathelement location="${bin}"/>
</path>

<!-- Build of the HtmlUnit tests -->
<target name="build-tests" description="Build the HtmlUnit tests">
  <javac srcdir="${tests.src}" destdir="${bin}">
    <classpath refid="test.class.path"/>
  </javac>
</target>
	
<!-- Run of the HtmlUnit tests -->
<target name="test" description="Run the HtmlUnit tests"
           depends="build-tests">
  <junit printsummary="yes" haltonfailure="yes">
    <classpath refid="test.class.path"/>
      <formatter type="plain"/>
      <test name="com.mycompany.AllTests" outfile="result"
               todir="${bin}"/>
  </junit>
</target>  
	
</project>

Now, you can launch the tests using Ant: right click on build.xml and choose Run As | Ant Build. You should have the following result:

Buildfile: C:\Mes documents\eclipse\HelloHtml\build.xml
test:
    [junit] Running com.yourcompany.AllTests
    [junit] Tests run: 1, Failures: 0, Errors: 0,
            Time elapsed: 1,359 sec
BUILD SUCCESSFUL
Total time: 1 second

Thank you for your attention and have good tests!

1 comment

Comments:

Comment from: Zoli [Visitor]
It was a quite good tutorial, I could start using htmlunit very easily. Now I will search for some advanced stuff or read the htmlunit API.
Just one comment: Ant build failed for me with some quite long but unuseful message. For some reason the classpath in the build file was not enough I think (I checked and corrected the file names/paths in the build file).
Solution: I added all the libraries written in htmlunit homepage (little bit more than htmlunit archive contains) to Run menu/External tools/External tools/HelloHtml build.xml/Classpath (Add external dirs) and it worked well after it.
Thanks for the Author, have a nice day and successful build for everyone.

Zoli


PermalinkPermalink 05/04/06 @ 13:54

Leave a comment:

Your email address will not be displayed on this site.
Your URL will be displayed.

Allowed XHTML tags: <p, ul, ol, li, dl, dt, dd, address, blockquote, ins, del, span, bdo, br, em, strong, dfn, code, samp, kdb, var, cite, abbr, acronym, q, sub, sup, tt, i, b, big, small>
(Line breaks become <br />)
(Set cookies for name, email and url)
(Allow users to contact you through a message form (your email will NOT be displayed.))
This is a captcha-picture. It is used to prevent mass-access by robots.

Please enter the characters from the image above. (case insensitive)

Pingbacks:

No Pingbacks for this post yet...

powered by
b2evolution

Credits: blog software | web hosting | monetize