Groovy in SOAP UI

Using Script Assertion in SOAP UI

Posted by: devakara on: November 27, 2008

  • In: Groovy|SOAP UI
  •  
  • 5 Comments

Script assertion in SOAP UI helps the developer to immediately run some primary tests after the current messageExchange.

It feels/sounds similar to Groovy Script test step but, in a lot ways it’s more handy. If say we want to validate on the response time and proceed further for succeeding test steps, it feels heavy to have a Groovy step to do that; instead, the Script Assertion implicitly does that job (can be done using “assert messageExchange.timeTaken < 1000”). Using this we could make the Test Suite look more credible and modular.

Script Assertion has access to messageExchange, context and log objects.

We can access the request and response xml as below -

def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )

def requsetHolder = groovyUtils.getXmlHolder( messageExchange.requestContent )

def responseHolder = groovyUtils.getXmlHolder( messageExchange.responseContent )

using the above holders we can get hold of the all elements in response and request xml of the latest messageExchange.

While accessing the elements of request/response xml, using the corresponding namespace(s) for every element is very important. If the response xml is simple (that is, mostly all the elements are referenced by one namespace), then xpath would be simple to trace. But if various elements are referenced by various namespaces, then we should be cautious while framing the xpath.

Here are my observations regarding this:

Lets say following is the response xml obtained after running the Test Request step(which has the script assertion)

Sample Response xml - I

In the Script Assertion step, to access RefNum field (of the above response xml), we write

def refNum =responseHolder.getNodeValue(“//trans:ProcessMessageResponse/content/Response/RefNum”)

And say if the response xml has various namespaces to refer its elements as below

then to access the same RefNum value, the xpath used should be

def refNum =responseHolder.getNodeValue(“//trans:ProcessMessageResponse/ns1:content/ns2:Response/ns2:RefNum”)

or

def refNum = responseHolder.getNodeValue(“//ns2:Response/ns2:RefNum”)

This might raise some ambiguity regarding ‘ns2‘ namespace ref. which I have used to access the RefNum value, though it is nowhere present in the response xml.

What I figured out is default namespaces are named as ns1, ns2 and so on (as applicable) in the order of their occurrences. In the above xml, ‘content‘ element is referred with ‘ns1’; and ‘Response‘ element is referred with ‘ns2’. And thus the xpath is so.

We can verify which namespace is given which name, by clicking the ‘Declare’ button in XPath Assertion window (though this can be done to know the names of different namespaces present in response xml only). But to know, the names of different namespaces present in request xml, ordering logic (what i have mentioned above) should be employed.

Not only while applying Script Assertion, the above explanation (that is, building precise xpath with appropriate namespace references) applies to all the situations where developer needs to access any element of request/response xmls.

Tags:  Script Assertion,  Script assertion in SOAP UI,  messageExchange, messageExchange Script Assertion,  messageExchange.timeTaken,  script assertion for validating response time,  context in script assertion,  access request xml in script assertion,  access response xml in script assertion, request xml script assertion,  response xml script assertion,  GroovyUtils Script Assertion,  namespaces Script Assertion,  getXmlHolder script assertion,  accessing elements under default namespace,  accessing SOAP request in Script Assertion,  accessing SOAP response in Script Assertion, accessing request xml in soap ui,  accessing response xml in soap ui, accessing request xml with groovy in soap ui,  accessiong response xml with groovy in soap ui

Operations with sql instance

Posted by: devakara on: October 17, 2008

  • In: Groovy|SOAP UI
  •  
  • 4 Comments

Groovy provides very robust methods with sql instance to retrieve and manipulate the ResultSet. 

There are methods for retrieving the first row,  the ResultSet of some sql query, directly executing insert/update/delete queries.

Prerequisite: Obtain sql instance using

import groovy.sql.Sql

def sql = Sql.newInstance(dbpath, dbusr, dbpwd, dbdriver)

 

Below are some comprehensive examples which use the above sql instance.

firstRow( sqlQuery ) : This method returns the first row entity out of the ResultSet that is obtained by executing the query passed to it as argument.

def  res = sql.firstRow(“SELECT * FROM TEST_TABLE WHERE USERID=’12345′”)

Result can accessed from res as

println( res[0]  )   OR

println(  res.COLUMN_NAME  )

 

eachRow( sqlQuery, {closure} ) : This method is generally used to retain/modify the whole ResultSet based on some condition.  The second argument of this method i.e., clousure actually consists as set of statements to be executed for each of the result set’s entity. For example

sql.eachRow( “SELECT * FROM TEST_TABLE WHERE USERID=’12345′”,

                      {

                        println( it.COLUMN_1 );

                        println( it[2] );

                      }  )

The println statements (present as a clousure) above are executed for each entity while iterating over the ResultSet, and it refers to the entity of ResultSet that is currently being iterated over. Clousure can have any number of statements.

 

execute( sqlQuery ) : Generally this method is used to INSERT/UPDATE/DELETE records, as it doesn’t return any ResultSet as such. 

sql.execute( “DELETE FROM TEST_TABLE WHERE USERID=’12345′ & USERNAME=’SOMENAME’ ” ) 

                                                             OR

sql.execute( “DELETE FROM TEST_TABLE WHERE USERID = ? & USERNAME = ? “,

                         [ "12345", "SOMENAME" ]   )

Second type of usage is to some extent similar to PreparedStatement right? And the query could also be a INSERT/UPDATE statement, if developer wants to log some value(s) to database during execution of the TestStep.

 

Was the post informative?
Yes
No
Other:
View Results
Polldaddy.com

 

(Please feel free to get back if u have any trouble…as i’m just a mail away…otherwise leave a comment)
Tags:  DB Operations with Groovy,  groovy DB Select,  groovy INSERT into DB,  groovy SELECT from database,  groovy DELETE from database,  DB Select in Groovy,  DB Insertion in Groovy,  sql methods in Groovy,  groovy sql.firstRow(),  groovy sql.eachRow(),  groovy sql.execute(),  DB operations SOAP UI,  sql connection SOAPUI,  sql.eachRow() SOAPUI,  sql.firstRow() SOAPUI,  sql.execute() SOAPUI,  sql firstRow() Groovy SOAPUI,  sql eachRow() Groovy SOAPUI,  sql execute() Groovy SOAPUI,  sql execute groovy,  sql firstRow groovy,  sql eachRow groovy

Property Transfer in SOAP UI

Posted by: devakara on: October 8, 2008

  • In: SOAP UI
  •  
  • 17 Comments

Property transfers become crucial steps in scenarios where validations are done mostly using derived data.

Lets say after some Groovy script execution we end up setting a property value in Properties step while testing an application’s functionality. And most of the times we would be in need of the obtained result out of that groovy step. As we have it in Properties Step we can access it using Property Expansion technique and also using Property Transfer Step.

Property Transfer Step shall be in between the Properties Step and SOAP request Step, but before placing the Transfer Step have Properties and SOAP request Steps in place. Property Transfer window would something like this:

Property Transfer Step

After creating a new Property Transfer in the above window say ‘RefNumTransfer‘, select source, in this case it would be the ‘Properties’ Step, this inturn provides the list of all properties defined under that Step, select the property you wish to transfer.

Now come to Target block, where we would have our SOAP request to catch the property transfer. Initially declare all the namespaces that your request would use, separating each of them with ‘;’ and then provide the XPath of the target node which should capture the transfered value. 

For example if the SOAP request as is below

SOAP request to catch tranfered Property

then the target block of Property Transfer should contain details like this:

Declaring namespaces and adding target XPath

Try running the Property Transfer step, you see the ‘Transfer Name’ and ‘Transfered Value’ in the Transfer Log, and also correspondingly that value is reflected in the SOAP request at the target XPath.

 

(Please feel free to get back if u have any trouble…as I am just a mail away…leave otherwise a comment)
Tags:  declaring namespaces in Property Transfer,  property transfer, property transfer in SOAP UI,  Property Transfer Step,  property transfer to request xml,  property transfer to SOAP request,  target xpath,  target xpath in Property Transfer,  target xpath in SOAP UI,  transfer,  transfer in SOAP UI,  Transfer properties,  Transfer Property in SOAPUI

你可能感兴趣的:(xml,properties,Access,SOAP,groovy,NameSpaces)