Forum Discussion

karthe's avatar
karthe
Occasional Contributor
12 years ago

[R] How to change the next request based on the response

Hi all,

I need to write the request based on the response. Assume two request as two steps in a test case, response of the teststep 1 gets generated as below. Request2 should contain status as "PENDING" if Response contains the status as P. Again, Request2 should contain status as "ACTIVE" if response contains the status as A. Could you please help me how to write the request automatically based on the value of the previous response.

Response 1:
<s0:idCreationResponse xmlns:s0="urn:xxxxxxxx.xxxxx.xxxxx">
<status>P</status>
<ID>23000234 CREATION SUCCESS</ID>
</idCreationResponse>

Request 2:
<search>
<status>Pending</status
<date>xxxx-xx-xx</date>
</search>

4 Replies

  • Hello,
    You can use following groovy script in between your both test steps

    def response = context.expand( '${Request1#Response#declare namespace ns=\'whichever you have'; //ns:Response[1]/ns:status[1]}' )
    teststep = testRunner.testCase.getTestStepByName("Request2")
    if(response == 'P' || response == 'p')
    teststep.setPropertyValue('status', 'Pending')
    else if(response == 'A' || response == 'a')
    teststep.setPropertyValue('status', ‘Active’)

    Hope it helps.
  • karthe's avatar
    karthe
    Occasional Contributor
    Hi Reshma,

    Thanks for reply and script. looks good. I have an another question.

    Is it possible to write the tag in the specified block based on the response. i.e Assume the below response ,

    <s0:idCreationResponse xmlns:s0="urn:xxxxxxxx.xxxxx.xxxxx">
    <status>P</status>
    <ID>23000234 CREATION SUCCESS</ID>
    </idCreationResponse>

    request should be written in the pending block & leaving the active block as blank like below ie, if response contains the status as P.

    <search>
    <active></active>
    <pending>
    <status>Pending</status>
    <date>xxxx-xx-xx</date>
    </pending>
    </search>

    request should be written in the Active block & leaving the pending block as blank like below i.e, if response contains the status as A.

    <search>
    <active>
    <status>Active</status>
    <date>xxxx-xx-xx</date>
    </active>
    <pending></pending>
    </search>

    Could you please advise on this? Thanks in advance!!
  • Hello,
    You can use following script assuming that Active/Pending both nodes have status and date sub nodes in any case:

    def response = context.expand( '${Request1#Response#declare namespace ns=\'whichever you have'; //ns:Response[1]/ns:status[1]}' )

    holder = groovyUtils.getXmlHolder( "Request name to be used#Request" )
    holder.namespaces["ns"] = "Name space to be used or whichever is available"

    // Clearing prev values from both status and date
    path = '//status'
    holder.setNodeValue(path , '')

    path = '//date'
    holder.setNodeValue(path , '')

    // Setting status and date as per response
    if(response == 'P' || response == 'p')
    {
    path = "//search/pending/status"
    holder.setNodeValue(path , "Pending")
    path = "//search/pending/date"
    date = new Date().format('YYYY-MM-DD')
    holder.setNodeValue(path , date)
    }
    else if(response == 'A' || response == 'a')
    {
    path = "//search/active/status"
    holder.setNodeValue(path , "Active")
    path = "//search/active/date"
    date = new Date().format('YYYY-MM-DD')
    holder.setNodeValue(path , date)
    }

    Hope it helps...
  • Seems that this is resolved. I think this kind of posts belong in the SoapUI Community forum, however.

    Cheers!

    Manne