Forum Discussion

kirankumar_sk's avatar
kirankumar_sk
Occasional Visitor
9 years ago
Solved

How to extract value from response, compare it, write result as pass/fail to csv with Groovy script?

Hi I am using SOAP UI 4.6.0 - I have a webservice (currency converter from webservicex.net) - I have a TestStep with request XML and a Groovy script.

- The input values to the Request XML are passed from a csv file using a "Set Up Script" of the TestCase.

- The Request XML is parameterized to fetch values from csv.

- A Groovy script is used to Read next (line) set of values from csv until it reaches the end.

 

Problem:

How do I extract a value from Response? After extracting a value from response I want to compare it with a value passed from CSV file (expected value) and then write test result (pass/fail) to a new csv file?

Where should I write script for 'Extract value from Response', 'Compare value in response with an expected value and write result'?

 

Thanks in advance for your help

  • Coming to the issue, you can do it in multiple ways using assertion.

     

    You can select the type of assertion that you are comfortable with.

    Here are the possibles in this case.

     

    1. Contains Assertion: You can provide the current expected value in the expected place holder like ${#TestCase#EXP_PROP} (use appropriate variable that you were setting in the ReadNextLine step
    2. Script Assertion: You can use this if you are comfortable with groovy scripting. Here you will be able to read the response value using
      def holder = new XmlHolder( messageExchange.responseContentAsXml )
      //use xpath according to your response, and expected property name
      assert holder["//ns1:RequestId"] == context.testCase.getPropertyValue('EXP_PROP')
    3. Xpath Assertion: You can this as well using xpath and expected value as
      context.testCase.getPropertyValue('EXP_PROP')

1 Reply

  • nmrao's avatar
    nmrao
    Champion Level 3

    Coming to the issue, you can do it in multiple ways using assertion.

     

    You can select the type of assertion that you are comfortable with.

    Here are the possibles in this case.

     

    1. Contains Assertion: You can provide the current expected value in the expected place holder like ${#TestCase#EXP_PROP} (use appropriate variable that you were setting in the ReadNextLine step
    2. Script Assertion: You can use this if you are comfortable with groovy scripting. Here you will be able to read the response value using
      def holder = new XmlHolder( messageExchange.responseContentAsXml )
      //use xpath according to your response, and expected property name
      assert holder["//ns1:RequestId"] == context.testCase.getPropertyValue('EXP_PROP')
    3. Xpath Assertion: You can this as well using xpath and expected value as
      context.testCase.getPropertyValue('EXP_PROP')