Felipe Gaúcho works as senior software engineer at Netcetera AG in Switzerland. He is a well known Brazilian JUG leader and open-source evangelist. Felipe works with Java since its early versions and has plans to keep that Java tradition as it is. When he is not coding, he prefers to listen reggae and travel around with his lovely wife Alena and his son Rodrigo. Felipe is a DZone MVB and is not an employee of DZone and has posted 29 posts at DZone. View Full User Profile

Testing PDF files with Canoo Webtest and Maven2

11.20.2009
| 5708 views |
  • submit to reddit

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 a zero-errors acceptance tests. 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 that you need to copy locally somwhere.
  • Groovy + GMaven: there is an entry in the Gillemot'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 enable 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 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>

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 day translating the ant samples to maven I finally got the below configuration. It works but it is yet not perfect, because I am not sure about all its details and not sure how to make it modular. I will update this entry as soon I figure out how to make it modular, if you know that already please give me your feedback. I believe other enhacements can be done in order to make the whole solution more elegant, but so far it do the job and I have my presentation ready for the managers ;)

<target name="all">
<webtest name="Test">
<config protocol="file" haltonerror="false" haltonfailure="false" summary="true" saveresponse="true" resultpath="../../../target/webtest/webtest-results" />
<steps>
<invoke description="get PDF document" url="${basedir}/foo.pdf" />
<pdfVerifyPageCount count="1" />
<pdfVerifyText description="Investment Proposal" text="Canoo webtest line #1" startPage="1" endPage="1" />
</steps>
</webtest>
</target>

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, nodje and Marc Guillemot. My task is not yet finished but the support of these community members was fundamental to my first success using Canoo webtest.

 

 

References
Published at DZone with permission of Felipe Gaúcho, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)