Ask a Question

Accessing results from other steps - How to make context expand short?

SOLVED
rajs2020
Frequent Contributor

Accessing results from other steps - How to make context expand short?

Say we get one row from a database and then we access that row in a groovy script. I see many test scripts which access multiple columns in the row like this:

 

context.expand('${GetRowFromDatabase#ResponseAsXml#//Results[1]/ResultSet[1]/Row[1]/EMPLOYEENAME[1]}')

 


This is very long, ugly and repetitive. So, I would like to do something shorter instead, like this:

 

def row = 'GetRowFromDatabase#ResponseAsXml#//Results[1]/ResultSet[1]/Row[1]'
def employeeName = context.expand('${${row}/EMPLOYEENAME[1]}')
// get more columns here.

 


But, this does not work i.e. employeeName is empty. What is my mistake here? Is it possible to do this in ReadyAPI?

 

3 REPLIES 3
nmrao
Community Hero

Try changing from

 

context.expand('${${row}/EMPLOYEENAME[1]}')

 

to

context.expand('${' + row  + '/EMPLOYEENAME[1]}')


Regards,
Rao.
sonya_m
SmartBear Alumni (Retired)

Thank you Rao!

@rajs2020 Does this help?


Sonya Mihaljova
Community and Education Specialist

aaronpliu
Frequent Contributor

Hi @rajs2020 ,

 

I just recommended you to use JsonSlurper to parse your JSON response or XmlHolder to parse XML response.

 

Database query return a xml-like response, right? then you can try:

(Example)

def holder = new XmlHolder(response)
def node = holder.getNodeValues( "//div/a[@title='APIs']/@href" )

 

 

you would paste your sample response here if don't mind, using XPath to retrieve it

 

Thanks,

/Aaron

 

cancel
Showing results for 
Search instead for 
Did you mean: