Forum Discussion

mkhan031486's avatar
mkhan031486
Contributor
8 years ago

How to transfer a value from one test case to another using groovy

 Hi, 

In my first test case, I have a soap request from which I get an account id in the response. Below is the response that I get.

 

<CreatAccountResponse>
<Account>
<AccountId>ACCT11011294</AccountId>
</Account>
</CreatAccountResponse>

 

In my second test case, I have a request where I have to use the account id created in the first test case.

 

<UpdateAccountRequest>
<Account>
<AccountId>"need to use account id from the first test case</AccountId>
<AccountStatus>ACTIVE</AccountStatus>
</Account>
</UpdateAccountRequest>

so my question is how can I transfer the account id from my first test case soap response to my second test case via Groovy script. 

 

Regards, 

Umair

 

  • mkhan031486,

     

    First of all, thumb rule is that, each test case should be independent. Design your tests so that there is not dependency between the two test cases.

     

    The above is not my statement, which I learned and follow.

    Having said that, of course, it does not mean it is not possible to do so in SoapUI, it is possible.

     

    Use below Script Assertion for the first request:

     

    def accountId = new XmlSlurper().parseText(context.response).'**'.find{it.name() == 'AccountId'}.text()
    
    context.testCase.testSuite.setPropertyValue('ACCOUNT_ID, accountId)

    In your next test step use

     

    <AccountId>${#TestSuite#ACCOUNT_ID}</AccountId>

     

     

  • groovyguy's avatar
    groovyguy
    Community Hero

    You can do this with a groovy script, but it may be easier to set up a Property Transfer step and store the property at the Test Suite or Project level in a custom property. 

    • groovyguy's avatar
      groovyguy
      Community Hero

      I did mock up a groovy script that could be used to transfer a value from a response based on the xpath to the element in question. It'll save it as a property at the TestSuite level, which then can be referenced with Property Expansion.

       

      Groovy Script Assertion: 

      import com.eviware.soapui.support.XmlHolder
      
      
      def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context );
      def holder = groovyUtils.getXmlHolder( messageExchange.responseContentAsXml )
      
      def transferValue = holder["//*:XPATH/*:to/*:Element"]
      
      context.testCase.testSuite.setPropertyValue("PropertyNameHere", transferValue);

      Then, in your request, you can use the following and it should work.

      <UpdateAccountRequest>
      <Account>
      <AccountId>${#TestSuite#PropertyNameHere}</AccountId>
      <AccountStatus>ACTIVE</AccountStatus>
      </Account>
      </UpdateAccountRequest>
  • nmrao's avatar
    nmrao
    Champion Level 3

    mkhan031486,

     

    First of all, thumb rule is that, each test case should be independent. Design your tests so that there is not dependency between the two test cases.

     

    The above is not my statement, which I learned and follow.

    Having said that, of course, it does not mean it is not possible to do so in SoapUI, it is possible.

     

    Use below Script Assertion for the first request:

     

    def accountId = new XmlSlurper().parseText(context.response).'**'.find{it.name() == 'AccountId'}.text()
    
    context.testCase.testSuite.setPropertyValue('ACCOUNT_ID, accountId)

    In your next test step use

     

    <AccountId>${#TestSuite#ACCOUNT_ID}</AccountId>