cancel
Showing results for 
Search instead for 
Did you mean: 

Groovy - How to automatically modify/set operation test step request

SOLVED
Highlighted
Contributor

Groovy - How to automatically modify/set operation test step request

Hello,

I want to automatically change content of a WSDL request of several requests in test steps of a test case. The name of the service changed in the WS I used to test, but the contents still the same.

I add the idea to do this using a groovy test step script to launch manually. In order to reuse it, I created it in a separated TestSuite.

 

def testCase = testRunner.testCase.testSuite.project.getTestSuiteByName("testSuiteName").getTestCaseByName("testCaseName")
def request, holder

testCase.testStepList.each { step ->
	request = context.expand( '${#[testSuiteName#testCaseName#'+step.name+']#Request}')
	if(request.contains("<soap:serviceNameBefore>")){ 
		request = request.replaceAll("soap:serviceNameBefore>","soap:serviceNameAfter>")
		// At this time, the request is not updated on expected test step
                // I guess I missing something here
	}
}

I don't find the correct method to update request according to the variable I modified (a string with expected request)

 

Do you have an idea ? Is there another way to do that ?

 

To help understand, here is the example of what I want to do. Change this request :

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soap="http://XX/">
   <soapenv:Header/>
   <soapenv:Body>
      <soap:serviceNameBefore>
         <request>
            <name>?</name>
            <price>?</price>
         </request>
      </soap:serviceNameBefore>
   </soapenv:Body>
</soapenv:Envelope>

in all test step to that one

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soap="http://XX/">
   <soapenv:Header/>
   <soapenv:Body>
      <soap:serviceNameAfter>
         <request>
            <name>?</name>
            <price>?</price>
         </request>
      </soap:serviceNameAfter>
   </soapenv:Body>
</soapenv:Envelope>

 

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Community Hero

Re: Groovy - How to automatically modify/set operation test step request

@Flo 

Below should help you:

 

import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequestStep

//Define element names in below two statements
def elementNameBefore = 'DefineOldName'
def elementNameAfter = 'DefineNameName'
//No change required beyond this
def testCase = testRunner.testCase.testSuite.project.getTestSuiteByName("testSuiteName").getTestCaseByName("testCaseName") testCase.testStepList.findAll{it instanceof WsdlTestRequestStep}.each { step -> def content = step.httpRequest.requestContent def changedContent = content.replaceAll(elementNameBefore, elementNameAfter) step.httpRequest.requestContent = changedContent }


Regards,
Rao.

View solution in original post

9 REPLIES 9
Highlighted
Community Hero

Re: Groovy - How to automatically modify/set operation test step request

I am afraid you could do that or it may work after the change.

Each operation supports fixed element, and changing that may not be suggested.

Or missing the details or context in the question.


Regards,
Rao.
Highlighted
Contributor

Re: Groovy - How to automatically modify/set operation test step request

@nmrao  I edited my question with this :"The name of the operation changed in the WS I used to test, but the contents still the same."

You are right regarding the risks but in this case I know it is possible.

Highlighted
Community Hero

Re: Groovy - How to automatically modify/set operation test step request

Not sure if I am confused or you.

Operation name and Element name in the request are two different things. Is it only Element name changed in your case? And not the operation name?


Regards,
Rao.
Highlighted
Contributor

Re: Groovy - How to automatically modify/set operation test step request

The name of the operation changed, not the element. I may have not well explained.

I give you details. I want to change this request :

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soap="http://XX/">
   <soapenv:Header/>
   <soapenv:Body>
      <soap:OperationBefore>
         <request>
            <name>?</name>
            <price>?</price>
         </request>
      </soap:OperationBefore>
   </soapenv:Body>
</soapenv:Envelope>

to that one

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soap="http://XX/">
   <soapenv:Header/>
   <soapenv:Body>
      <soap:OperationAfter>
         <request>
            <name>?</name>
            <price>?</price>
         </request>
      </soap:OperationAfter>
   </soapenv:Body>
</soapenv:Envelope>

 

Highlighted
Community Hero

Re: Groovy - How to automatically modify/set operation test step request

Hope the following link helps to understand what I mean by operation. You again referred to Element name in the above sample.

https://www.soapui.org/soap-and-wsdl/operations-and-requests.html


Regards,
Rao.
Highlighted
Contributor

Re: Groovy - How to automatically modify/set operation test step request

Thanks for the links. You are right, I mean changing the element of the request.

I edited my original post to try to make it more understandable.

Highlighted
Community Hero

Re: Groovy - How to automatically modify/set operation test step request

@Flo 

Below should help you:

 

import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequestStep

//Define element names in below two statements
def elementNameBefore = 'DefineOldName'
def elementNameAfter = 'DefineNameName'
//No change required beyond this
def testCase = testRunner.testCase.testSuite.project.getTestSuiteByName("testSuiteName").getTestCaseByName("testCaseName") testCase.testStepList.findAll{it instanceof WsdlTestRequestStep}.each { step -> def content = step.httpRequest.requestContent def changedContent = content.replaceAll(elementNameBefore, elementNameAfter) step.httpRequest.requestContent = changedContent }


Regards,
Rao.

View solution in original post

Highlighted
Contributor

Re: Groovy - How to automatically modify/set operation test step request

@nmrao 

This is exactly what I need.

Thanks a lot

Highlighted
Community Hero

Re: Groovy - How to automatically modify/set operation test step request

Glad to know that helped.


Regards,
Rao.
New Here?
Join us and watch the welcome video:
Announcements
TechCorner Leaderboard
Want a fun and easy way to learn ReadyAPI and show off your skills? Try solving weekly TechCorner challenges and get into the Leaderboard!


Challenge Status

Compare an expected JSON value and actual response in Events

Participate!

Fetch value/data from JSON response using Groovy Script

See replies!

Filtering data retrieved from a DataSource

See replies!

Get data from Petstore and add it to Excel sheets

Participate!
Top Kudoed Authors