Forum Discussion

TestTestTest's avatar
TestTestTest
New Contributor
6 years ago

Groovy: Read Response using xpath in a soapui property

Hi,

 

I'm trying to replace the xpath with a soapui parameter that eventually will come from a csv, but errors keep showing up. This is the code.

 

 

testRunner.testCase.setPropertyValue( "var_xpath_old", "//person/sex/") // will be in a csv

//Response is here

testRunner.testCase.setPropertyValue( "var_old", response["//person/sex") //<-- this is where is want to inject var_xpath_old

 

any ideas?

 

 

  • TestTestTest's avatar
    TestTestTest
    6 years ago

    Hi,

     

    Thanks to another tester we found the solution. I was missing a step.

    I was not able to use the ${TestCase#property} due to error messages in the groovy script. 

    But is was not aware that you have to declare al local variable first if you want to use the property in the groovy script. So my solution was: 

     

    step 1: fill the csv file

    ----csv file contents

    //person/sex

     

    step 2: put csv into array

    FIle filereader..etc..

    testcase.setproperty("xpath_from_csv", propdata[0])

     

    step 3: declare the property as a local variable in groovy

    xpath =  context.expand ( '${#TestCase#xpath_from_csv} ')

     

    step 4: get the value from the response

    testcase.setproperty("sex_from_response", response[$xpath] )     //xpath comes from csv fil

     

    thank for all the help.

     

     

     

  • Hi TestTestTest,

     

    What is 

    response["//person/sex"

     

    you mean to say this value you want to retrieve from response and use in another groovy.

     

    If this is the scenario, in your send groovy code use get data to retrieve the value from particular node and use here(using ReadAPI)

     

    If you are using SoapUI, then you can use groovyUtils class to get node value.

     

     

     

     

    • TestTestTest's avatar
      TestTestTest
      New Contributor

      Hi,

       

      <quote>response["//person/sex"]</quote> is the xpath to extract te value from the response for the soap call. 

       

      I want to put the text  //person/sex in a csv file so i can create a generic building block so i can use this function wherever i want. 

       

      what i know: 

      - //person/sex retrieves the value i want (groovyutils)

       

      what i dont't know

      - how to replace the text //person/sex with an input(xpath) from the csv file.

       

      in short

      step 1: fill the csv file

      ----csv file contents

      //person/sex

       

      step 2: put csv into array

      FIle filereader..etc..

      testcase.setproperty("xpath_from_csv", propdata[0])

       

      step3: get the value from the response

      testcase.setproperty("sex_from_response", response[$xpath] )     //xpath comes from csv file

       

      Could you help me to replace the response xpath value so i can put this value into a csv file?

       

      thx

       

       

      • nmrao's avatar
        nmrao
        Champion Level 3
        Sorry. unclear.

        What are you trying to achieve?
        Are you using SOAP with data driven test?
        What do you want to do with extracted value from the response?