Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to set #string into value having the same regex in karate

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!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

My expected result as:

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

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading