Script Runner

PDF Print E-mail
User Rating: / 11  |  Rate this article: PoorBest 
GroovyRunnerScreenshot

0.1. Description

This Runner Component runs a Groovy script and output the result of the script. When running a distributed test, the script content is automatically distributed to all assigned Agents.
- Parameters in the trigger message are made available to the script, unless configured not to.
- If the result of the script is a Map, then each entry will be set in the output message. Otherwise, the result will be in the Result column.

0.2. Example scripts

0.3. FTP

The example script below downloads a file from an FTP server and outputs the size and hash of the downloaded file.

// ftp.groovy
@Grab(group='commons-net', module='commons-net', version='2.0')
import org.apache.commons.net.ftp.FTPClient
new FTPClient().with {
   connect("download.acme-enterprises.com")
   enterLocalPassiveMode()
   login("anonymous", "anonymous")
   changeWorkingDirectory("pub/foobar")
   fileType = FTPClient.BINARY_FILE_TYPE
   inFile = new File("c:/temp/install.exe")
   inFile.withOutputStream { ostream -> retrieveFile "install.exe", ostream }
   disconnect()
}

return [ "size":inFile.length(), "hash":Integer.toHexString( inFile.hashCode() ) ]

0.4. HTTP POST

The following example script sends a HTTP POST request:

@Grab(group='org.apache.httpcomponents', module='httpclient', version='4.1.1')

import org.apache.http.client.methods.HttpPost
import org.apache.http.message.BasicNameValuePair
import org.apache.http.impl.client.DefaultHttpClient
import org.apache.http.client.entity.UrlEncodedFormEntity 
import org.apache.http.protocol.HTTP

post = new HttpPost("http://search.yahoo.com/search")
parameters = new ArrayList() 
parameters.add(new BasicNameValuePair("p", "loadui")) 
sendentity = new UrlEncodedFormEntity(parameters, HTTP.UTF_8)
post.setEntity(sendentity)
client = new DefaultHttpClient()   
response = client.execute(post)

return ["responseBody":response.entity.content.text]

0.5. Geb (WebDriver)

@Grab(group='org.codehaus.geb', module='geb-core', version='latest.release')
@Grab(group='org.seleniumhq.selenium', module='selenium-htmlunit-driver', version='latest.release')

import geb.Browser

Browser.drive {
  go "http://google.com/ncr"
}
}