1

I have below web service request. I am trying to update a property using groovy script.

Webservice:

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:web="http://www.webserviceX.NET/">
   <soap:Header/>
   <soap:Body>
      <web:ChangeLengthUnit>
         <web:LengthValue>${=(int)(Math.random()*9999)}</web:LengthValue>
         <web:fromLengthUnit>Inches</web:fromLengthUnit>
         <web:toLengthUnit>Centimeters</web:toLengthUnit>
      </web:ChangeLengthUnit>
   </soap:Body>
</soap:Envelope>

Groovy script:

def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )  

// get XmlHolder for request message def 
holder = groovyUtils.getXmlHolder( "ChangeLengthUnit#RawRequest" ) 

// change password using XPath 
holder["//*:LengthValue"] = "1234"  

// write updated request back to teststep 
holder.updateProperty()
context.requestContent = holder.xml

I am unable to update Lengthvalue tag using above script. I an getting below error

org.apache.xmlbeans.XmlException: error: Unexpected element: CDATA org.apache.xmlbeans.XmlException: error: Unexpected element: CDATA error at line: 4 
7
  • You are already using dynamic value. Why do you want to update again? Commented Oct 27, 2017 at 0:04
  • @Rao, I have some special characters to be passed in the request xml. Above example is similar to my issue. I am not using random value in my actual project. Commented Oct 27, 2017 at 0:20
  • Ok, with what value you want to update the request? A static value each time or dynamic one? Are you using free version or pro? Commented Oct 27, 2017 at 0:25
  • I am using free version. I will be updating request xml with a dyamic value from previous service response. Normal property expansion failing as the tag has special characters. Hence I am using groovy script property transfer. Commented Oct 27, 2017 at 0:33
  • 1
    @Rao, I found out the issue. I shouldnt be updating rawrequest, it should be request only. Thanks you rao Commented Oct 27, 2017 at 1:23

1 Answer 1

2

I think you can do that in a simpler way

Lets say your xml request was

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:web="http://www.webserviceX.NET/">
<soap:Header/>
<soap:Body>
  <web:ChangeLengthUnit>
     <web:LengthValue>1234</web:LengthValue>
     <web:fromLengthUnit>Inches</web:fromLengthUnit>
     <web:toLengthUnit>Centimeters</web:toLengthUnit>
     <web:name>Sherly &amp; ' ^ " * 123456 </web:name>
  </web:ChangeLengthUnit>

you can get the value of name with the below script

def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )  

// get XmlHolder for request message def 
holder = groovyUtils.getXmlHolder( "First Step#Request" ) 

def x= holder.getNodeValue("//*:name")

 log.info x

 testRunner.testCase.setPropertyValue("Prop",x)

So what we have done in above code is got the value "Sherly & ' ^ " * 123456 " and created a custom property and added value in it.

Now you could easily use that value in the next request

<web:LengthValue>${#TestCase#Prop}</web:LengthValue>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.