Forum Discussion

JohanF's avatar
JohanF
New Contributor
2 months ago

How to escape ampersand in Property Expansion?

Hi,

In our TestCases, we use properties as values for SOAP requests in the following way:

    <com:name>${#TestCase#currentName}</com:name>

This mostly works fine, but in some cases the property values contain an ampersand (&) character. This then leads to an incorrect XML and thus invalid SOAP Request:

    <com:name>Fencing & Security</com:name>

The expected behaviour would be that SoapUI escapes the invalid character to create the following element:

    <com:name>Fencing &amp; Security</com:name>

When assigning the value to the element using a separate Property Transfer test step, this is correctly done (the value is escaped).
The problem is that we set all the properties using a Groovy Script before running the request, which makes using property expansion in the request by far the easiest way to assign all values.

Is there a way to use property-expansion that does escape special characters when used in XML?

2 Replies

  • Hello JohanF​ ,

    Your xml example shows the name of a property... I was getting a little confused by your question because it sounded like you want to set the value of a property (not property name)... 

    As a minor rule of thumb, it is wise not to allow property names to contain special characters as you are witnessing...  These names are being utilized by internal workings of SoapUI and you are begging for issues if you must include special characters for a property name.  Property values can have special characters without much issue.   Before setting a property name maybe you could scrub it for special characters and eliminate them or replace with inert character (underscore or dash or something).  Just my thought... I don't think there is a native solution to what you are encountering.

    Regards,

    Todd

  • JohanF's avatar
    JohanF
    New Contributor

    Hi Todd,

    Thanks for your reply! Your initial idea was correct, in the SOAP Request test step we want to retrieve the value of the property.
    The property name is "currentName", hence to do the property expansion we refer to it as "${#TestCase#currentName}" in the respective element in the SOAP Request step:

        <com:name>${#TestCase#currentName}</com:name>

    The problem arises when this property has a value containing an ampersand, for example when the value is: "Fencing & Security".
    When sending the SOAP Request this value is then placed literally (non-escaped) in the SOAP Request, which makes it invalid XML:

        <com:name>Fencing & Security</com:name>

    What we would expect is the property expansion leading to the following request:

        <com:name>Fencing &amp; Security</com:name>

    Best regards,
    Johan