SoapUI: Using the Extended Script Library

Say you’re reusing scripts and also want somewhere you can open the actual groovy script and check it for errors. You can do this in SoapUI Pro by making use of the Groovy Script Library.

So, in SoapUI, you can now reference the script via a Groovy script, like so (this could be all one line but I prefer two):

def cs = new qa.SomeScript()
cs.someMethod(testRunner,4)

Where SomeScript could look like:

package qa

public class SomeScript {

	def someMethod(testRunner,testStepLocation) {

			//properties
			def request = testRunner.testCase.getTestStepAt(testStepLocation).getTestRequest()
			def tcProps = testRunner.testCase.getProperties()
			def map = [:]

			//grab all the properties from the test case and put them in a map as long as they aren't empty
			for (p in tcProps) {
				if (p.value.getValue() != "") {
					map.put(p.key, p.value.getValue())
				}
			}
			def builder = new groovy.json.JsonBuilder(map)
			def json = builder.toPrettyString()
			request.setRequestContent(json)
	}
}
Advertisements

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