WebTest with Groovy, Maven and Eclipse

WebTest is in fact “only” a set of Ant tasks therefore it’s not a surprise that WebTest tests are traditionally written in XML. Grails WebTest plugin has shown that Groovy can be a very pleasant alternative thanks to the AntBuilder. Even if you don’t use Grails, you can write WebTest tests in Groovy using the support provided by WebTest’s distribution but if you plan to write all your tests in Groovy, this is surely not optimal. This post shows step by step a way to write WebTest tests that is near to a traditional setup for unit tests using Groovy and Maven.

Note: I’m not a Maven expert. The proposed code works but it can perhaps be improved.

  1. Create a new Maven project
    mvn archetype:create -DarchetypeGroupId=org.apache.maven.archetypes -DgroupId=my.domain -DartifactId=myWebTestApp
    

  2. Edit the generated pom.xml
    1. Add WebTest as dependency:
          <dependencies>
              ...
              <dependency>
                  <groupId>com.canoo.webtest</groupId>
                  <artifactId>webtest</artifactId>
                  <version>3.1-SNAPSHOT</version>
              </dependency>
          </dependencies>
      
    2. Add reference to WebTest’s Maven snapshot repository
      	    <repositories>
      	        <repository>
      	            <id>webtest_dependencies_snapshot</id>
      	            <name>WebTest dependencies</name>
      	            <url>http://webtest.canoo.com/webtest/m2-repo-snapshots</url>
      	        </repository>
      	    </repositories>
      

      Note that this is needed only if you want to use the latest WebTest snapshot (which is always the best one). You don’t need it if you use a version that is available on the central repository. As of Apr. 30, WebTest 3.0 has not been uploaded to the central repository. The upload request is available here: MAVENUPLOAD-2439.

      Update: WebTest 3.0 is available in Maven central repository since May 19.

    3. Configure the GMaven plugin
      <build>
      	<plugins>
      		<plugin>
      			<groupId>org.codehaus.groovy.maven
      			</groupId>
      			<artifactId>gmaven-plugin</artifactId>
      			<version>1.0-rc-5</version>
      			<executions>
      				<execution>
      					<goals>
      						<goal>compile</goal>
      						<goal>testCompile</goal>
      					</goals>
      				</execution>
      			</executions>
      		</plugin>
      		<plugin>
      			<groupId>org.codehaus.mojo</groupId>
      			<artifactId>build-helper-maven-plugin</artifactId>
      			<executions>
      				<execution>
      					<id>add-test-source</id>
      					<phase>generate-sources</phase>
      					<goals>
      						<goal>add-test-source</goal>
      					</goals>
      					<configuration>
      						<sources>
      							<source>src/test/groovy</source>
      						</sources>
      					</configuration>
      				</execution>
      			</executions>
      		</plugin>
      	</plugins>
      </build>
      
  3. Create the Eclipse project
    mvn -Declipse.downloadSources=true eclipse:eclipse
    

    I guess that something similar exists for other IDEs.

  4. Import the project in your workspace
  5. Add Groovy support in the IDE
    The Groovy support in Eclipse is not as good as in other IDEs but it is better than nothing.
  6. Write your first WebTest in Groovy src/test/groovy/my/domain/FirstWebTest.groovy
    package my.domain
    
    import com.canoo.webtest.WebtestCase
    
    /**
     * My first WebTest in Groovy.
     */
    class FirstWebTest extends WebtestCase {
    
    	void testGoogleSearch() {
    		webtest("My first test") {
    			invoke "http://www.google.com/ncr", description: "Go to Google (in English)"
    			verifyTitle "Google"
    			setInputField name: "q", value: "WebTest"
    			clickButton "I'm Feeling Lucky"
    			verifyTitle "Canoo WebTest"
    		}
    	}
    }
    
  7. Run the test
    • As normal unit test from the IDE
      As it is a normal unit test and you can use the IDE support for that. In Eclipse this can be done with right mouse click / Run As... / Junit Test or with the shortcut Alt + Shift + X Z
    • Or from the command line
      mvn test
      
  8. Enjoy the results!
    Running a WebTest this way produces “normal” JUnit reports as well as the traditional reports of WebTest that contain precise information to quickly find the cause of failed tests.
  9. (optional) Run headless
    Per default WebTest shows a console that informs about the running tests and opens the reports in your browser once the tests are finished. This is often useful on a workstation but it may be really annoying on a build server. To avoid that, you just need to set the wt.headless property:

    mvn test -Dwt.headless
    
About these ads

32 Comments

  1. Dierk said,

    April 30, 2009 at 12:05 pm

    Great write-up! Thanks, Dierk

  2. Dariusch said,

    April 30, 2009 at 1:13 pm

    That’s great. Very nice article.

    Keep at it!

  3. Matt Raible said,

    May 1, 2009 at 2:45 pm

    First of all, it’s great to see a Maven plugin for WebTest finally seeing the light of day!

    Secondly, if you’re trying to use this in a war project, it seems like the best phase to execute it in would be the integration-test phase, not the test phase. Is it possible to override the default?

  4. Marc Guillemot said,

    May 5, 2009 at 10:44 am

    @Matt

    What is presented here is not a Maven plugin for WebTest: no special plugin is needed, the gmaven plugin just does the work (a dedicated plugin would surely make sense to allow a better control).

    Your remark concerning the integration-test-phase makes sense. I have to improve my maven knowledge to find how to do it ;-(

  5. Beat said,

    May 6, 2009 at 12:40 pm

    First of all: Thanks a lot! Maven integration is exactly what I need in my project.

    However, I’m struggling a bit with various issues:

    1. How can I get the example running if I’m behind a proxy? I’ve added
    setproxy proxyhost:”firewall”, proxyport:”8080″ as the first line in the webtest closure, but this has not worked (it’s just been a very wild guess on my part).

    2. I can’t run the test script as JUnit test. In the Run as… menu, Eclipse just displays Maven targets.

    3. I’m running mvn test from a Windows command line. There is a problem at the end with the wt.openResultFile target: It opens the result web-page through cmd but somehow cmd ignores (or does not receive) the /c switch and keeps running –> I must stop mvn test manually with ^C, which has the unfortunate effect that some java process keeps running in the background. How can I check if cmd is really started with the /c option?

    • Sophie de Malet said,

      June 7, 2010 at 10:31 am

      Hello, Did you ever solve the problem of the wt:openResultFile not completing? I am having the same problem – both from NetBeans and the command line. The test report window is opened and the results displayed as expected but I need to use ^C to actually stop the test.
      Thanks.

      • Beat Koch said,

        June 7, 2010 at 4:38 pm

        Hello,

        when running my tests from Eclipse (which is what I mostly do now), I haven’t had this problem. So I have now just tried to execute the tests from the command line with the classpath settings generated by Eclipse and it works without having to press ^C. Why, I don’t know yet, but it seems that the order of the items on the classpath is relevant.

        Posting the complete, working classpath here would be too long; you can send me an email to bidli at hispeed dot ch if you need more information.

      • Sophie de Malet said,

        June 8, 2010 at 2:09 pm

        Thanks. I managed to use the following configuration in my pom file to disable the webtest report so I do not now need to use ^C . This gets around the problem

        maven-surefire-plugin

        -Dwt.headless

        integration-tests
        integration-test

        test

      • Sophie de Malet said,

        June 8, 2010 at 2:14 pm

        Would be even better if my last reponse hadn’t got rid of the control characters….Hope this works!

        <artifactId>maven-surefire-plugin</artifactId>
        <configuration>
        <argLine>-Dwt.headless</argLine>
        <execution>
        <id>integration-tests</id>
        <phase>integration-test</phase>
        <goals>
        <goal>test</goal>
        </goals>
        </execution>
        </configuration>

  6. Marc Guillemot said,

    May 6, 2009 at 4:02 pm

    @Beat

    1- It should work if you call the AntBuilder by yourself before the webtest closure.
    getAnt().setproxy proxyhost: ”firewall”, proxyport: 8080

    2- You need to add the groovy Nature to your Eclipse project

    3- Switch to a better OS ;-)
    No idea. I guess that the same problem may appear when you run the test directly with ant from the command line. Perhaps some space in the path to the results?

  7. ian Homer said,

    May 13, 2009 at 10:58 am

    You’re a star. This is exactly what I needed – this should be fed back to the Canoo WebTest guys because this a much better approach that what is current documented there.

  8. BBCLX said,

    June 2, 2009 at 8:32 am

    Hy there. This article is exactly what I am looking for to integrate in our project. However, when I run the webtest the URL gets invoked and I can check the result page in the resport. But during the invoke the following error occurs:

    JavaScript error loading page http://www.google.com/: Wrapped com.gargoylesoftware.htmlunit.ScriptException: TypeError: Cannot find function inRange in object [object].

    I would appreciate any help, as I can’t wait to run this baby on hudson ;-)

    • BBCLX said,

      June 2, 2009 at 8:45 am

      This might as well be a silly question, but what is the use of a repository when I cannot browse to its url?

      http://webtest.canoo.com/webtest/m2-repo-snapshots

      • Jean Hominal said,

        June 2, 2009 at 3:29 pm

        @BBCLX:
        1. I’ve ran into the exact same exception; however, when I replace “http://www.google.com/ncr” with “http://www.google.com/” the problem seemingly disappears.

        @Marc Guillemot:
        Canoo Webtest has been uploaded since May 19th on Apache’s central repository, you may say it here. ^^

        I greatly appreciated your guide, and I greatly appreciated my first dips of Canoo WebTest.
        Thank you!

      • Marc Guillemot said,

        June 6, 2009 at 5:40 am

        @BBCLX
        You don’t need to browse the repository, only to get the right files ;-)
        In fact this was the easiest way to implement it and it works. If you have a better solution, a patch is welcome

      • Marc Guillemot said,

        June 6, 2009 at 5:42 am

        @Jean Hominal

        good idea. I’ve made the update.

    • Marc Guillemot said,

      June 6, 2009 at 5:44 am

      Google’s home page evolves and it seems that they are now using some new JS code for IE that is not yet supported by HtmlUnit. We will surely work on it. As workaround you can try to simulate Firefox rather than IE.

  9. Jean Hominal said,

    June 4, 2009 at 1:28 pm

    Hi,

    I am working on a setup of this kind. (Eclipse+Groovy+Maven): I have multiple webtest files, all written in Groovy, and launched from Eclipse or from Maven’s command line.

    Everything works fine, but I would like to define custom steps, and I do not know where to put the for them. What would be the best place for that?

    • Marc Guillemot said,

      June 6, 2009 at 5:47 am

      What about using a custom base class that extends WebtestCase for your tests? There you can override createAntBuilder() to add your custom steps to the AntBuilder.

      • Jean Hominal said,

        June 8, 2009 at 8:22 am

        Thank you, it was the perfect solution for me!

        I had found some information (already from you) there, I will just put what worked for me, as reference:

        public abstract class CustomWebtestCase extends WebtestCase
        {
          void createAntBuilder() {
            super.createAntBuilder();
            ant.project.addTaskDefinition("myTask",package.myTask);
          }
        }

        (NB: I had put

        
        

        in my previous comment, without realizing that it would be swallowed by WordPress, and so made my previous comment borderline unintelligible.)

      • Jean Hominal said,

        June 8, 2009 at 8:25 am

        Oh great. I wanted to say that the <webtest> that I had put in my previous posts were swallowed, and so made my *two* previous messages difficult to understand.

  10. June 11, 2009 at 6:56 am

    [...] “WebTest with Groovy, Maven and Eclipse” – artykuł na blogu Marc’a Guillemot’a. [...]

  11. Kavita said,

    June 25, 2009 at 7:34 am

    Hi Marc,

    Thanks for your post.
    I am trying to integrate Webtest into our project. I followed your instructions(used webtest 3.0) and have no compilation problems. I am trying to run the sample Googletest in your post. I get the following error:

    java.io.FileNotFoundException: C:\Documents%20and%20Settings\kab25\.m2\repository\com\canoo\webtest\webtest\3.0\webtest-3.0.jar (The system cannot find the path specified)

    Is it because of the spaces in the path? the jar file is in the directory. Any ideas/help appreciated.

    Kavita

  12. Kavita said,

    June 25, 2009 at 2:51 pm

    Thanks Alex. I moved my repository to directory/path with out spaces and it works. However this would be a temporary work around and have to wait for the bug-fix before I am able to deliver my code.

  13. June 30, 2009 at 4:06 am

    [...] WebTest with Groovy, Maven and Eclipse « Marc Guillemot’s blog (tags: maven groovy testing ant plugin webtest) [...]

  14. Dennis said,

    December 12, 2009 at 7:25 pm

    @Marc Guillemot:

    Don’t know if this is supposed to work, but I’m trying to run the Groovy WebTest from groovy:

    groovy -cp webtest.jar FirstWebTest.groovy

    I get a bizarre error: “java.lang.NullPointerException: Cannot invoke method toURI() on null object”. I get this error even if I comment out everything within webtest closure.

  15. JS said,

    May 7, 2010 at 5:48 pm

    I have set up Groovy web tests, but when I tried to use the data driven feature in the groovy code. WT told me getAnt() returned null.

    My groovy test class extends WebtestCase, and I run the tests on the command line.

    Rings a bell?

  16. June 6, 2010 at 10:41 pm

    [...] “WebTest with Groovy, Maven and Eclipse” – artykuł na blogu Marc’a Guillemot’a. [...]

  17. Sophie de Malet said,

    June 7, 2010 at 1:51 pm

    How do you set the wt.openResultFile.skip property as maven does not seem to use the webtest.xml settings file?
    Thanks.

  18. Caroline said,

    February 16, 2014 at 3:31 pm

    This article was incredibly helpful, specially since I
    was searching for thoughts on this subject last Thursday.
    Keep posting.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: