Ask a Question

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

SOLVED
Flo
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>

 

9 REPLIES 9
nmrao
Community Hero

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.

@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.

nmrao
Community Hero

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.

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>

 

nmrao
Community Hero

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.

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.

nmrao
Community Hero

@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.

@nmrao 

This is exactly what I need.

Thanks a lot

nmrao
Community Hero

Glad to know that helped.


Regards,
Rao.
cancel
Showing results for 
Search instead for 
Did you mean: