Groovy Testing
Groovy is a scripting language that is executed as Java Byte Code. Groovy Home
This allows a Mix & Match between Groovy and Java Code.
Groovy comes with an AntBuilder that allows execution of Ant scripts from usual Groovy code. This capability allows executing WebTests, since WebTests are usual Ant tasks.
By executing WebTest from a Groovy script, you can fully leverage both the variety of WebTest steps and the sophisticated language means of Groovy for structuring your tests.
Sample use
This example fetches a page and verifies that it contains the numbers 1 through 10.
def webtest_home = System.properties.'webtest.home'
ant.taskdef(resource:'webtest.taskdef'){
classpath(){
pathelement(location:"$webtest_home/lib")
fileset(dir:"$webtest_home/lib", includes:"**/*.jar")
}
}
def config_map = [:]
['protocol','host','port','basepath','resultfile',
'resultpath', 'saveresponse','defaultpropertytype'].each{
config_map[it] = System.properties['webtest.'+it]
}
ant.testSpec(name:'groovy: Test Groovy Scripting at creation time'){
config(config_map)
steps(){
invoke(url:'linkpage.html')
for (i in 1..10){
verifyText(description:"verify number ${i} is on pages", text:"${i}")
}
}
}
Like in ANT, there is a two-phase approach for TaskContainers like 'testSpec'.
- first
- steps are constructed and stored (creation time)
- second
- steps are executed (runtime)
Start with groovy -Dwebtest.home=your_webtest_home test.groovy
More examples
Please find more examples of how to use Canoo WebTest through Groovy under Grails.