简体   繁体   中英

Is it possible to inject '<' and '>' characters into SoapUI request XML literally using Groovy script?

I'm working with SoapUI for web services functional testing. I'm using SpecFlow to facilitate parameterised data driven testing.

As part of my process, I have a groovy script which executes prior to the test case request. This script reads arguments passed in the command line call and injects them into the request XML parameters.

My request looks something like this:

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" >
  <soap:Header/>
  <soap:Body>
    <soap:TestArg1>TestArgValue1</soap:TestArg1>
    <soap:TestArg2>TestArgValue2</soap:TestArg2>
    <soap:TestArg3>TestArgValue3</soap:TestArg3>
  </soap:Body>
</soap:Envelope>

My script works fine and the string values are injected correctly as expected.

However, during testing, we discovered that many of the test input args will contain special characters (hyphens, slashes etc). In order to work around this issue, I tested the service tests with each argument in the request XML enclosed in a CDATA directive like so:

<![CDATA[TestArg-Hyphen]]>

Testing in the SoapUI GUI was successful; I updated my Groovy script to concatenate

<[CDATA[

before and

]]>

after the variable.

However, when the concatenated value is injected into the request XML, the markup appears like so:

&lt;![CDATA[TestArg-Hyphen]]&gt;

I've been searching in vain for quite a while online for a solution to this problem; it appears the the '<' & '>' characters cannot simply be escaped as with others special ('-', '/' etc) characters.

I've tried enclosing my string in single, double, triple, slashy, dollar-slashy quotes: all produce the same result.

I'm certain there is a way to do this (just not prominently featured in Groovy documentation) but, as yet, I have been unable to identify it.

I'd very much appreciate any help or advice on this. Thanks for looking..

Update: The relevant section of my Groovy script looks like this:

// Get next test step info
def nextStep = testRunner.testCase.getTestStepAt(context.currentStepIndex + 1)

// Create the XmlHolder object for the request step
def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
def holder = groovyUtils.getXmlHolder(nextStep.name + "#Request")

// Inject arg values into request XML
for (i=0; i < argsNamesArray.length; i++) {
    argNode = "//" + argsNamesArray[i]
    def argValue = "<![CDATA[" + argsValuesArray[i] + "]]>"
    log.info argValue
    holder[argNode] = argValue
}
holder.updateProperty()

Note that the

log.info argValue

returns the argument value as I would like to inject into my request:

<![CDATA[TestArg-Hyphen]]>

However, when the script has executed, the request XML is still updated to show:

&lt;![CDATA[TestArg-Hyphen]]&gt;

Is there really no simple, straightforward way to do this??

IMHO, this is not a Groovy problem, but a XML problem instead.

The only char that needs to be escaped its the "<" itself.

You just need to replace "<" by lt; in XML.

Example:

def param1 = "<foo-bar>"
println param1.replace("<", "&lt;")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM