Groovy scripts ,xml holders and regular expressions in Soap-Ui

In our Soap-Ui project lets add a new test ,groovy request.

Type following in groovy request

log.info "hello"
def req  = context.expand('${login#request}')//This will get all data from request of test step named “login” .Login should be in the same test case. i.e we have a test case and in that test case we have a login test step and a groovy request test step.

Context will get data that can be used within all the test steps in the same test case. i.e if we have multiple test steps in same test case we can share the data we get from context.Also note that if we run our test case after typing the above command we will get context data in a alert box as soap shows the last executed request in an alert box . or we can simply print the data using the below command if we don’t want the data to be shown in alert box.

log.info req// we can then print it

def req1  = context.expand('${login#response}') )//This will get all data from response of test step named “login” i.e whether login is successful, what is session id etc.

log.info req1//Then print the response.

Xml holder

def res1  = context.expand('${Addproduct#response}')//Get the response from a soap request Add product.
def response1 =  new XmlHolder(res1); Put that entire response in XML holder

For the XmlHolder to work we need to add this import to our groovy script

//import com.eviware.soapui.support.XmlHolder

//Then we try to get xpath on getNodeValue method i.e from the first item tag get the category tag.Please note that if xpath is wrong an error will be thrown but if xpath is correct but not present i.e let us sat item[3]/category_name ,there is no item[3] then it will print null.

def productName =  response1.getNodeValue('//item[1]/category_name')
log.info productName //print

We can even use the complete xpath to get names of a product etc.We can go to our response for our request and get complete xpath of something. the starting tag must be something like ns1:GetAllProductsResponse then it can be something like return then allproducts etc

productName = response.getNodeValue('//ns1:GetAllProductsResponse/return/allproducts/item[2]/product_name')

//We can also get all the items with a given tagname and print them.
def items = response1.getNodeValues('//product_name');
log.info "The total no of items are" + items.length;

for(x in items){
    log.info x
}

def price = response1.getNodeValues('//price');

for(x=0;x<items.length;x++){
    log.info items[x]+"-----------------------"+ price[x];
    }

But one thing to note is getNodeValue will print null if no tag with that xpath exists.But getNodeValues print an array of items. So if tag name is not present then getNodeValues will print an empty array and its length will be 0.So getNodeValues and then items.length>0 can be used to validate whether a tag name exists in the response.

Below are req expressions

The below reg expression will make sure we have an item tag and under that we have a price tag.And the price tag can have any only digits 1-9 and there can be more than one digits 1,

log.info response1.getNodeValue("matches(//item[1]/price,'[0-9]{1,}')")

The below reg expression will make sure we have an session tag which can have any digits 1-9 or from a to z or A to Z and there can be exactly 10 characters,

log.info response1.getNodeValue("matches(//sessionid,'[0-9a-zA-Z]{20}')")