Forum Discussion

rajs2020's avatar
rajs2020
Frequent Contributor
4 years ago

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?

 

  • Try changing from

     

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

     

    to

    context.expand('${' + row  + '/EMPLOYEENAME[1]}')
  • nmrao's avatar
    nmrao
    Champion Level 3

    Try changing from

     

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

     

    to

    context.expand('${' + row  + '/EMPLOYEENAME[1]}')
  • aaronpliu's avatar
    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