Montag, 18. Mai 2009

Custom Webtest steps for the Grails Webtest Plugin

Since version 0.6 of the grails webtest plugin, it is possible to create custom steps (http://www.leebutts.com/2009/01/grails-webtest-plugin-06-released.html):
"You can now extend com.canoo.webtest.steps.Step by placing groovy classes in webtest/tests/step. They will be automatically loaded at runtime and allow for easy testing of complicated scenarios such as JSON interfaces and email integration"
Unfortunately, there is no description or example how to actually create a custom step.

So here is what you have to do:
  • Create directory webtest/tests/step
  • Create Step Class. It is important that the class name ends with 'Step', otherwise it will not be picked up.
  • Class must extend com.canoo.webtest.steps.Step
  • Class must override void doExecute()
  • Put the step's execution logic into the doExecute() method.
  • To pass parameters to the step, define the step's arguments as properties of the step class.
  • To validate the parameters, override void verifyParameters() and check the properties you defined. If property is not valid, throw IllegalArgumentException.

Here is an example of a custom step. The step will log a greeting message. Our goal is to create a step that can be called in a webtest test class as shown below:
def testGreeting() {
 greeting(param:'grails')
}

The implementation of the step is quite simple:
import org.apache.log4j.Logger

class GreetingStep extends com.canoo.webtest.steps.Step {
 private static Logger log = Logger.getLogger(GreetingStep)
 String param

 @Override
 void doExecute(){
  log.info "Hello $param"
 }

 @Override
 protected void verifyParameters(){
  if(!param) {
   throw new IllegalArgumentException('param not set')
  }
 }
}

Place GreetingStep class in directory webtest/tests/step.

1 Kommentar:

  1. Hi Soenke,

    thanks for the tutorial, I probably should have done this when I added the feature!

    thanks

    Lee

    AntwortenLöschen