Tuesday, March 25, 2014

SoapUI, Groovy Script tutorial

Groovy is built on JVM, so does the SoapUI, it’s no surprise that SoapUI offers the Groovy script support by default. Let’s do a simple test, basically send out a Http request with dynamic parameters, and parse the result using the regular expression capability of Groovy and IO capablity to save the result back to a file. for the parameter we can use the groorvy to poll a file and return some randomized keywords.

so the keywords file looks like
image

we create one project with one test suite, then under the suite, we can add test case, and for the test step, let’s add first step called GetKeyword as a groovy script.


image

then we put some script here, which will assign a keyword property to the context randomly.
image

then we can access this keyword using context.keyword in next step, now, let’s create a http request to bing.com

image

Click extract params,now if we run this test

image

the query keyword is hardcoded, we can replace it with a groovy expression

image

then add the 3rd step, that we read the response in step 2 and save it to a file

image

if we run the test case, you will see file named keyword.txt with the http response from bing.com search

also you can just copy and save the following xml as a project file, you open in in SoapUI

<?xml version="1.0" encoding="UTF-8"?>
<con:soapui-project activeEnvironment="Default" name="tst" soapui-version="4.6.4" xmlns:con="http://eviware.com/soapui/config"><con:settings/><con:testSuite name="testgroovy"><con:settings/><con:runType>SEQUENTIAL</con:runType><con:testCase failOnError="true" failTestCaseOnErrors="true" keepSession="false" maxResults="0" name="TestCase 1" searchProperties="true" id="cdd185b8-b6ae-4455-9713-ec70b5404569"><con:settings/><con:testStep type="groovy" name="GetKeyword"><con:settings/><con:config><script>def file=new File("c:\\a.txt")
def lines=file.readLines();
def randomindex=new Random()
def keyword=lines[randomindex.nextInt(lines.size())];
context.keyword=keyword
log.info(keyword)</script></con:config></con:testStep><con:testStep type="httprequest" name="Req2Bing"><con:settings/><con:config method="GET" xsi:type="con:HttpRequest" name="Req2Bing" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><con:settings><con:setting id="com.eviware.soapui.impl.wsdl.WsdlRequest@request-headers">&lt;xml-fragment/></con:setting></con:settings><con:endpoint>http://www.bing.com/search</con:endpoint><con:request/><con:credentials><con:authType>Global HTTP Settings</con:authType></con:credentials><con:jmsConfig JMSDeliveryMode="PERSISTENT"/><con:jmsPropertyConfig/><con:parameters><con:parameter><con:name>q</con:name><con:value>${=context.keyword}</con:value><con:style>QUERY</con:style><con:default>androidyou</con:default><con:path xsi:nil="true"/><con:description xsi:nil="true"/></con:parameter></con:parameters></con:config></con:testStep><con:testStep type="groovy" name="SaveitToFile"><con:settings/><con:config><script>def resp=testRunner.getTestCase().getTestStepByName("Req2Bing").httpRequest.response
def raw=resp.getContentAsString()
//save it fo file
def resultFile=new File("c:\\" + context.keyword + "t.txt")
resultFile.write(raw);</script></con:config></con:testStep><con:properties/></con:testCase><con:properties/></con:testSuite><con:properties/><con:wssContainer/></con:soapui-project>

No comments:

 
Locations of visitors to this page