0% found this document useful (1 vote)
371 views14 pages

Groovy Script For Regular Use

The document discusses various ways to access properties and values from the SoapUI test context and manipulate requests and responses using Groovy scripts. 1) The context object can be used to reference properties from the project, test suite, test case, test step, and global levels as well as environment variables and file paths. 2) Properties can be read from the same or different levels and values can be updated at runtime by getting the request/response holder and setting node values. 3) Test steps can be executed and responses validated dynamically by running scripts that import necessary SoapUI classes.

Uploaded by

scribd@123
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
371 views14 pages

Groovy Script For Regular Use

The document discusses various ways to access properties and values from the SoapUI test context and manipulate requests and responses using Groovy scripts. 1) The context object can be used to reference properties from the project, test suite, test case, test step, and global levels as well as environment variables and file paths. 2) Properties can be read from the same or different levels and values can be updated at runtime by getting the request/response holder and setting node values. 3) Test steps can be executed and responses validated dynamically by running scripts that import necessary SoapUI classes.

Uploaded by

scribd@123
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 14

Context

http://webservices-testing.blogspot.de/2012/04/how-to-access-project-test-suitetest.html #Project# #"est#uite# #"est$ase# #%oc&#ervice# #'lobal# ##!stem# #*nv# +"est#tep name,# - re erences a Project propert! - re erences a "est#uite propert! in the containing "est#uite - re erences a "est$ase propert! in the containing "est$ase - re erences a %oc&#ervice propert! in the containing %oc&#ervice - re erences a global propert! (optional) - re erences a s!stem propert! - re erences a environment variable - re erences a "est#tep propert! within the current "est$ase

Example: def myVar = context.expand('${#TestCase#MyName}'

Reading the properties from the Same level !! "et propert#es from test Case$ test %&#te and pro'ect
de test$asePropert! - test.unner.test$ase.getPropert!/alue (0$ountr!0 ) log.in o test$asePropert! de test#uitePropert! - test.unner.test$ase.test#uite.getPropert!/alue( 0$ountr!0) log.in o test#uitePropert! de projectPropert! test.unner.test$ase.test#uite.project.getPropert!/alue( 0$ountr!0 ) log.in o projectPropert! de globalPropert! com.eviware.soapui.#oap12.globalProperties.getPropert!/alue( 03PPass0 ) log.in o globalPropert!

Reading the properties from the different level


de "#-test.unner.test$ase.test#uite.project.test#uites+0"est#uite 20, log.in o "#.getPropert!/alue(0name0) de "#-test.unner.test$ase.test#uite.project.test#uites+0"est#uite 20,.test$ases+0"est$ase 10, log.in o "#.getPropert!/alue(0name0)

Updating Request Node Values at run time

import com.eviware.soapui.support.4 re5uest - conte6t.e6pand(789all'oal3eeper#re5uest:7) de holder-new ;ml<older(re5uest) holder.set=ode/alue(0// oot:s$ountr!=ame0>7?ra@il7) //setting node value o re5uest holder.updatePropert!() test.unner.test$ase.get"est#tep?!=ame(0all'oal3eeper0).setPropert!/alue(0re5uest 0>holder.get;ml()) de runner-test.unner.run"est#tep?!=ame(0all'oal3eeper0) de status-runner.get#tatus() log.in o status

Executing a step at run time


import com.eviware.soapui.support.4 //to get the object o read*6cel $lass testsuite - test.unner.test$ase.test#uite.project.test#uites+0AataAriven0,B testsuite.test$ases+0"est $ase 10,.test#teps+0"est #tep =ame0,.run(test.unner> conte6t)B

To retrieve the values from the response


de groov!1tils - new com.eviware.soapui.support.'roov!1tils( conte6t ) de holder - groov!1tils.get;ml<older(0.e5uest 1#response0) //holder.declare=amespace( 7ns7> 7http://www.webservice;.=*"7) holder.namespaces+7ns7,-7http://www.webservice;.=*"7B log.in o holder.get=ode/alue(0//ns:$it!0) // "o capture single node value or( node in holder+7//ns:$it!7, ) // "o capture multiple node values log.in o node

Validate Response Dynamically Using !path "ssertion#


$ath % http%&&learnsoapui'(ordpress'com&category&assertion)in)soapui&

CD-- e6pected result value should be e5ual to --E 89m!"est#tep.e5uest=ame#.e5uest#//Feather?!$it!/$it!=ame:

To get the data entered in a Text *ox in a dialog


http%&&((('soapui'org&apidocs&com&evi(are&soapui&support&components&+onfigurationDialog'html,add+om-o*ox .ava'lang' String/ .ava'lang'String#

import com.eviware.soapui.support.4 import com.eviware.soapui.support.components.4 $on igurationAialog Aialog-12#upport.create$on igurationAialog(0"est0) %ap values - new <ash%ap() values.put(0*nter =ame0>00)B Aialog.add"e6tGield(0*nter =ame0> 0"ool"ip0) Aialog.show(values) var-values.get(0*nter =ame0) log.in o var

To get the selected value from the +om-o *ox in a Dialog


$on igurationAialog Aialog-12#upport.create$on igurationAialog(0"est0) #tring+, envHoptions-+0"*#"0>02="0>0P.IA0, Aialog.add$ombo?o6(0#elect*=/0> envHoptions> 0#elect *nvironment0) %ap /alue- new <ash%ap() /alue.put(0#elect*=/0>00) Aialog.show(/alue) var-/alue.get(0#elect*=/0) log.in o var

To +apture a pass(ord
import com.eviware.soapui.support.4 import com.eviware.soapui.support.components.4 char+, Password - 12#upport.promptPassword(0*nter Password0> 0Password #creen0) #tring var-new #tring(Password) log.in o var

"ttaching a file in soapui


http://www.soapui.org/ orum/viewtopic.phpJt-12K24 http://stac&over low.com/5uestions/L0MM04L/attaching- ile-in-soapui-with-groov! http://stac&over low.com/5uestions/N4O2PKP/change-soapui-re5uest-with-groov! http://www.soapui.org/Gunctional-"esting/script-assertions.html - it has code chec& this http://www.tech5ues.com/5uestion/1-L0MM04L/Qttaching- ile-in-#oap12-with-groov! -2%P

//get re5uest de re5uest - test.unner.test$ase.get"est#tep?!=ame( 0.e5uest 10 ).test.e5uest // clear e6isting attachments or( a in re5uest.attachments ) 9 re5uest.removeQttachment( a ) : // get ile to attach //de ile=ame - conte6t.e6pand( 789#ource o data#Path"o;.$ Gile data name :7 ) de ile=ame - conte6t.e6pand( 789#"est$ase#;.$H ileHname:7 ) de ilePath - conte6t.e6pand( 789#"est$ase#;.$H ilesHpath:7 ) log.in o 0 ile: 0 R ilePath R ile=ame de ile - new Gile( ilePath R ile=ame ) i ( ile -- null) 9 log.error 0bad ilename0 : else 9 // attach and set properties de attachment - re5uest.attachGile( ile> true ) attachment.content"!pe - 0application/octet-stream0 de list - ile=ame.to&eni@e(0SS0)B attachment.setPart(list.last()) //de holder2 - groov!1tils.get;ml<older( 0.e5uest 1#.e5uest0 ) // 'et .e5uest bod! //de startAate2 holder2.set=ode/alue( 0//blac:Gile?!te#tream0>0cid:0Rlist.last())B //#et 0lin&0 to attachment in re5uest bod! //holder2.updatePropert!() //and update // attachment.setPart(list.last())B //set attachment :

Verifying the response http%&&((('soapui'org&0unctional)Testing&script)assertions'html


The Script-Assertion allows for arbitrary validation of a message, which included message content, HTTP headers, etc. Validation scripts are written in the pro ect scripting language !"roovy or #avaScript$ and are e%ecuted whenever the assertion-status of the containing sampler TestStep is updated. Add a Script Asssertion to your TestStep with the standard &Add Assertion& button'

which brings up the following configuration dialog

The top area is a standard soap() script editor, with corresponding popup menu and actions. The *un button e%ecutes the assertion script against the last received response and displays the result in a popup window. The bottom shows the output of the log variable available in the script.

The message+%change ob ect available in the script e%poses a bunch of properties related to the last re,uest-response messages !chec. out the Javadoc$, lets loo. at some simple e%amples/ 0. Validate the content of an HTTP Header in the response

// check for the amazon id header assert messageExchange.responseHeaders["x-amz-id-1"] != null


Validate a certain response time

// check that response time is less than 400 ms assert messageExchange.timeTaken !""
Validate the e%istence of an attachment in the response

// check that we received 2 attachments assert messageExchange.response#ttachments.length == $


Validate the e%istence of a specific 123 element in the response

// check for RequestId element in response de% holder = ne& 'mlHolder( messageExchange.response)ontent#s'ml * assert holder["//ns1 RequestId"! "# null
This last e%ample is greatly simplified in soap() Pro which adds a corresponding wi4ard to the 5utline +ditor'

capturing Ra(Request 1 Response


testCase.getTestStepByName(myTestStep).getProperty(Request).getValue() or context.testCase.getTestStepAt( ).getProperty(Request).getValue() or testRunner.testCase.getTestStepAt( ).getProperty(Request).getValue()

one of the ways to get the raw request data


message!xc"ange.getRequestContent().toStr#ng()

2essage Exchange http%&&((('soapui'org&apidocs&com&evi(are&soapui&model&iface&2essageExchange'h tml

Methods Modifier and Type Method and Description

Method Summary

+tring +tring[] ,odel-tem

$et%ndpoint(* $et&essa$es(* $et&odelItem(*

.peration $et'peration(* +tringTo+tring,ap $et(roperties(* +tring /0te[] /0te[] #ttachment[] #ttachment[] +tring $et(ropert)(+tring name* $etRawRequest*ata(* $etRawResponse*ata(* $etRequest+ttachments(* $etRequest+ttachments,or(art(+tring part1ame* $etRequest-ontent(*

+tring $etRequest-ontent+s.ml(* +tringTo+trings,ap $etRequest/eaders(* 2esponse #ttachment[] #ttachment[] +tring $etResponse(* $etResponse+ttachments(* $etResponse+ttachments,or(art(+tring part1ame* $etResponse-ontent(*

+tring $etResponse-ontent+s.ml(* +tringTo+trings,ap $etResponse/eaders(* long long /oolean /oolean /oolean /oolean $et0imestamp(* $et0ime0aken(* hasRaw*ata(* hasRequest(/oolean ignoreEmpt0* hasResponse(* is*iscarded(*

SoapUI Pro / Groovy - Scripts and scripted assertions


http://automated-joseph.blogspot.de/2 !!/ "/soapui-pro-groovy-scripts-and-scripted.html

1erif) %lements of %ach 0)pe are *escendin$ 2) 3core import com.e3i&are.soapui.support.'mlHolder import 4a3a.util.5 de% groo306tils = ne& com.e3i&are.soapui.support.7roo306tils( context *8 de% holder = groo306tils.get'mlHolder( messageExchange.response)ontent#s'ml * de% node)ount = holder["count(99ns1:2esponse[1]9ns1:search[1]9ns1:results[1]9ns1:e*"] de% currentT0pe; current+core = "" de% t0pe<ist = [] de% %ound = %alse de% index = " currentT0pe = holder.get1ode=alue( "99ns1:2esponse[1]9ns1:search[1]9ns1:results[1]9ns 1:e[1]9ns1:result[1]9ns1:t0pe[1]" * current+core = holder.get1ode=alue( "99ns1:2esponse[1]9ns1:search[1]9ns1:results[1]9ns 1:e[1]9ns1:result[1]9ns1:score[1]" * t0pe<ist.add( [ currentT0pe; current+core ] * node)ount = node)ount.to-nteger(* %or ( i = $8 i currentT0pe = holder.get1ode=alue( "99ns1:2esponse[1]9ns1:search[1]9ns1:results[1]9ns 1:e[" > i > "]9ns1:result[1]9ns1:t0pe[1]" * current+core = holder.get1ode=alue( "99ns1:2esponse[1]9ns1:search[1]9ns1:results[1]9ns 1:e[" > i > "]9ns1:result[1]9ns1:score[1]" * t0pe+ize = t0pe<ist.size(* %or ( 0 = "8 0 index = 0 %ound = true /reak @ @ i% ( %ound *? pre3ious = t0pe<ist[ index ] t0pe+ize8 0>> *? i% ( t0pe<ist[ 0 ].contains( currentT0pe * *? node)ount8 i>> *?

assert pre3ious[ 1 ] A= current+core %ound = %alse @ else ? t0pe<ist.add( [ currentT0pe; current+core ] * @ @

0he 4niversal 35+ +ssertion 6makes mana$in$ sla assertion values a one7 stop7shop8 de% slaBtimeBtaken = messageExchange.getTimeTaken(*8 de% slaBtime = context.expand( CD?EFro4ectEslaBtime@C *.to-nteger(*8 assert slaBtimeBtaken slaBtime8

/eader Information +ssertion de% headers = messageExchange.get2esponseHeaders(*8 de% mapBke0 = context.expand( CD?EFro4ectEmapBke0@C *8 de% mapB3al = context.expand( CD?EFro4ectEmapB3al@C *8 assert headers.get(mapBke0*.to+tring(* == mapB3al8

1erif)in$ 'rder of Response %lements 2) %lement 1alue +ssertion import com.e3i&are.soapui.support.'mlHolder8 de% /oBhost = context.expand( CD?EFro4ectEpro4ectBhost@C *8 de% groo306tils = ne& com.e3i&are.soapui.support.7roo306tils( context *8 de% holder = groo306tils.get'mlHolder( messageExchange.response)ontent#s'ml *8 de% node)ount = holder["count(99ns1:2esponse[1]9ns1:search[1]9ns1:results[1]9ns1:e*"]8 de% expected)ount = context.expand( CD?EFro4ectEexpectedBreturnBcount@C *8 node)ount = node)ount.to-nteger(*8

assert node)ount == expected)ount8 pre3iousFla0)ount=alue = holder.get1ode=alue( "99ns1:2esponse[1]9ns1:search[1]9ns1:results[1]9ns 1:e[1]9ns1:result[1]9ns1:track[1]9ns1:pla0counts[1]" *.to-nteger(*8 %or( int i = $8 i = node)ount8 >>i *?

currentFla0)ount=alue = holder.get1ode=alue( "99ns1:2esponse[1]9ns1:search[1]9ns1:results[1]9ns 1:e[Di]9ns1:result[1]9ns1:track[1]9ns1:pla0counts[1]" *.to-nteger(*8 assert pre3iousFla0)ount=alue A= currentFla0)ount=alue8 pre3iousFla0)ount=alue = currentFla0)ount=alue8 @

9re$orian *ate 3trin$ :uilder de% g)alendar = ne& 7regorian)alendar(*8 de% return+tring = null8 g)alendar.time-n,illis = +0stem.currentTime,illis(*8 return g)alendar.get()alendar.GE#2* > "-" >H g)alendar.get()alendar.,.1TH* > "-" > ( g)alendar.get()alendar.I#TE* - 1 * >H " " > g)alendar.get()alendar.H.62B.JBI#G* > ":" > g)alendar.get()alendar.,-16TE* >H ":" > g)alendar.get()alendar.+E).1I*8

Random ;on7Repeatin$ I*s 3trin$ :uilder de% standardBcount = context.expand( CD ?FropertiesEstandardBcount@C *.to-nteger(*8 de% trackBrange = context.expand( CD ?FropertiesEtrackBrange@C *.to-nteger(*8 de% generator = ne& 2andom(*8 de% randomBint = generator.next-nt( trackBrange *8 de% randomBintBstring; returnBstring = randomBint.to+tring(*8 de% 4umpBstring = ""8 %or ( i = 18 i standardBcount8 i>> * ?

i% ( ! returnBstring.contains( 4umpBstring * KK returnBstring != ""

KK randomBint @ else ? i--8 @

trackBrange * ?

returnBstring = returnBstring > "; " > 4umpBstring8

randomBint = generator.next-nt(trackBrange*8 4umpBstring = randomBint.to+tring(*8 @ return returnBstring8

Reversin$ I*s 3trin$ :uilder de% original = context.expand( CD?FropertiesEtracks@C *8 de% tokens = original.tokenize("; "*8 de% size = tokens.size(*8 de% ne&=alue = ""8 ne&=alue = tokens[ " ]8 tokens.remo3e( " *8 %or ( current in tokens * ? i% ( current != null KK current != "" * ? ne&=alue = ne&=alue > "; " > current8 @ @ return ne&=alue8

3huffle I*s 3trin$ :uilder de% original = context.expand( CD?FropertiesEtracks@C *8 de% tokens = original.tokenize("; "*8 de% size = tokens.size(* - 18 de% ne&=alue = ""8 de% generator = ne& 2andom(*8 i% ( size A " * ? int r = generator.next-nt(size* > 18

ne&=alue = tokens[r]8 %or ( i in 1..size * ? r = generator.next-nt(size > 1*8 ne&=alue = ne&=alue > "; " > tokens[r]8 tokens.remo3e(r*8 size = tokens.size(* - 18 @ @ else ? ne&=alue = original8 @ return ne&=alue8

Response %lements 1alues 3trin$ :uilder de% groo306tils = ne& com.e3i&are.soapui.support.7roo306tils( context *8 de% holder = groo306tils.get'mlHolder( context.expand( CD?Fu/lish Fla0listE2esponse#s'ml@C **8 de% node=alue = holder.get1ode=alue( "99ns1:2esponse[1]9ns1:&arning[1]9ns1:%ailBtoBadd[ 1]9ns1:e[1]9text(*" *8 de% i = 18 returnBstring = context.expand( CD?FropertiesE/lacklistedBtrackBids@C *8 &hile ( node=alue != null * ? i% ( ! returnBstring.contains( node=alue * * ? returnBstring = returnBstring > "; " > node=alue8 @ i>>8 node=alue = holder.get1ode=alue( "99ns1:2esponse[1]9ns1:&arning[1]9ns1:%ailBtoBadd[ 1]9ns1:e[" > i.to+tring(* > "]9text(*" *8 @ return returnBstring8

Random /e< 3trin$ :uilder

de% generator = ne& 2andom(*8 de% r = generator.nextIou/le(*8 return Iou/le.toHex+tring(r*[!..1L]8

Random -haracters 3trin$ :uilder de% generator = ne& 2andom(*8 de% count = ( context.expand( CD?+earch FropertiesEsearchBstringBlength@C *.to-nteger(* *8 de% r = generator.next-nt( count >1 * >18 de% tokenBstring = <ong.to+tring( ,ath.a/s(generator.next<ong(* *; ML*8 return tokenBstring.get#t( 1..count *8

+reating a (or3-oo3 and (riting into a (or3-oo3


http%&&((('andy3han'com&.excelapi&tutorial'html

import 4xl.5 import 4xl.&rite.5 Nrita/leNork/ook &ork/ook = Nork/ook.createNork/ook(ne& Jile(C):99Test9output.xlsC** Nrita/le+heet sheet = &ork/ook.create+heet("Norksheet 1"; "* 99log.in%o(sheet1.isHidden(** <a/el la/el = ne& <a/el("; $; "Test data in )olumn #; 2o& M"*8 99column="=#;ro&="=1 sheet.add)ell(la/el*8 <a/el la/el1 = ne& <a/el($; $; ")olumn ); 2o& M"*8 sheet.add)ell(la/el1*8 &ork/ook.&rite(* &ork/ook.close(*

Reading Data from a (or3-oo3


http%&&((('andy3han'com&.excelapi&tutorial'html

import 4xl.58 context.setFropert0("countr01ames"; ne& readExcel(**8 pu/lic class readExcel? Nork/ook &=Nork/ook.getNork/ook(ne& Jile(C):99Iocuments and +ettings9amunaga9Iesktop9Fro4ect9Fro4ect9IataOase.xlsC**8 99return allElements o% )olumn 99<ist get=alues(int sheet-ndex;int col-ndex* <ist get=alues(int sheet-ndex;int col-ndex*? de% m0+heet=&.get+heet(sheet-ndex*8 <ist name=[]8 de% count2o&s=m0+heet.get2o&s(*8 %or (de% i=18i count2o&s8i>>*? name[i-1]=m0+heet.get)ell(col-ndex;i*.get)ontents(* log.in%o name[i-1] @ return name @ @

Rest 13 3'+( http:99pradeep/ishnoi./logspot.de9$"11B"$B"1Barchi3e.html

You might also like