Home > Software design >  How to set #string into value having the same regex in karate
How to set #string into value having the same regex in karate

Time:03-30

I am trying to set #string into value having the same regex in karate, as following: I have sampleXML:

<Addresses>
    <Address Id="AD1">
       <StreetNum>0001</StreetNum>                   
    </Address>
    <Address Id="BR9">
       <StreetNum>0002</StreetNum>                   
    </Address>
    ...
</Addresses>

From the above xml example, I will set #string into value having the same regex, as following:

* string temp = sampleXML
* xml report = temp.replaceAll('<Address Id="#regex [A-Z0-9]{3}">', '<Address Id="#string">')

But it doesn't work, so please help me if you know that how to set #string into value having the same regex in karate? Thanks!

My expected result as:

<Addresses>
        <Address Id="#string">
           <StreetNum>0001</StreetNum>                   
        </Address>
        <Address Id="#string">
           <StreetNum>0002</StreetNum>                   
        </Address>
        ...
 </Addresses>

CodePudding user response:

This is trickier than I thought, you have to use regexes in JavaScript:

* text payload =
"""
<Addresses>
    <Address Id="AD1">
       <StreetNum>0001</StreetNum>                   
    </Address>
    <Address Id="BR9">
       <StreetNum>0002</StreetNum>                   
    </Address>
</Addresses>
"""
* def temp = payload.replaceAll(/Id="[^"] "/g, 'Id="#string"')
* print temp

For more advanced needs, you also have the option of calling Java code: https://github.com/karatelabs/karate#calling-java

  • Related