This week I received one of that lovely and tricky tasks: to learn Canoo webtest, test it and prove its usefulness to the project in three days - convincing the managers that it should be part of the project. The goal of the project is to produce a finance report with ~200 pages and that report should be validated through azero-errors acceptance test. Several tools were considered, including Selenium and others, but Canoo was choosen due to its PDF test features.

Testing PDF documents with Maven & Canoo webtest

Canoo webtest can be used in three different ways:

  • Ant task: this is the original and very well documented webtest distribution. There is only one gotcha: the ant task depends on a library which you need to copy locally somewhere.

  • Groovy + GMaven: there is an entry in the Marc Guillemot's blog explaining how you can code your webtest in Groovy instead of using ANT. It is a perfect choice if you are already using Groovy but in my case an extra language would cost unnecessary explanations in the technical meeting :)

  • WebTest Maven2 Plugin: the webtest-maven-plugin is a pure Maven plugin that enables your application to test the generated PDF files without any dependencies to local artifacts and without the need to add Groovy to the technology set of your project. That's the way I was looking for and that's my prime choice.

After a first hour of euphoria the things started to get muddy with the configuration of the maven plugin. I am not sure if due to the tight deadline or just because of the scarce documentation on the web, but nevertheless after joining the mailing list of Canoo I started to find my way.

Maven configuration

    

<dependencies>
    <dependency>
        <groupId>com.canoo.webtest</groupId>
        <artifactId>webtest</artifactId>
        <version>3.1-SNAPSHOT</version>
        <scope>integration-test</scope>
    </dependency>
</dependencies>
<repositories>
    <repository>
        <id>webtest_dependencies_snapshot</id>
        <name>WebTest dependencies</name>
        <url>http://webtest.canoo.com/webtest/m2-repo-snapshots</url>
    </repository>
    <repository>
        <id>codehaus</id>
        <name>codehaus</name>
        <url>http://repository.codehaus.org</url>
    </repository>
    <repository>
        <id>ibiblio.mirrors</id>
        <url>http://mirrors.ibiblio.org/pub/mirrors/maven2</url>
    </repository>
</repositories>
<reporting>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>webtest-maven-plugin</artifactId>
        </plugin>
    </plugins>
</reporting>
http://www.canoo.com/images/logos/logo-webtest.png

The webtest configuration

The configuration of the test was a bit complicated because I couldn't find a runnable example on the web (the main motivation to this blog entry). After two days translating the ant samples to maven I finally got the below configuration. I believe other enhacements can be done in order to make the whole solution more elegant, but so far it does the job and I have my presentation ready for the managers ;)

<project name="Canoo Webtest Sample" basedir="." default="all">
    <target name="all">
        <ant antfile="checkPageCount.xml" /> <!-- 1 test per PDF page -->
        <ant antfile="checkWatermark.xml" />
    </target>
</project>

A test example, a simple test to check if the PDF contains a watermark with the word java.net

<project default="test">
    <property name="pdf.file" value="${basedir}/foo.pdf" />

    <taskdef resource="webtestTaskdefs.properties" />
    <target name="test">
        <webtest name="watermark">
            <config protocol="file" summary="${summary}" saveresponse="false"
                resultpath="${resultpath}" resultfile="${resultfile}"
                haltonfailure="${haltonfailure}" haltonerror="${haltonerror}"
                showhtmlparseroutput="${showhtmlparseroutput}"
                autorefresh="${autorefresh}" />
            
            <steps>
                <invoke description="get PDF document" url="${pdf.file}" />
                <pdfVerifyText description="Check watermark" 
                    text="java.net" startPage="1" endPage="1" />
            </steps>
        </webtest>
    </target>
</project>

Download the complete example

Here you can download the complete example, it only requires Maven 2 and should work out of the box. any problems trying that, please let me know. In order to run the example, please follow the below steps:

  1. Download and unzip the sample application.
  2. Open a console, go to the folder you unziped the application and type:
  3. mvn install eclipse:eclipse
  4. mvn webtest:clean webtest:test webtest:report

Done, you will find the report in the file.\myWebTestApp\target\site\webtest\index.html

Acknowledgments

I received a helpful support from the Webtest mailing lists, with special thanks to Christoph Lipp, nodjeand Marc Guillemot. My task is not yet finished but the support of those community members was fundamental to my first success using Canoo webtest.

Blog UPDATE: Canoo published a set of new samples, available here. Take the latest version, unzip and look in the foldersrc/test, specially the example it8 about PDF testing.