Forum Discussion
Hi Tanya
From your post: Have you received it? If you still have a question, I suggest that you reply to that message to Yuri to clarify the details.
Actually to be honest, I didn't received any response from Yuri.
Can you please ask to Yuri to clarify on same. Actually I am struggling on this issue.
Actually I am very new to Ready API. I want Groovy script to write custom properties value into xml file.
Here is my simple API request. Which is put into external xml file. Becasue my project requirement is that if xml request changes in future, we need to change in one external request file instead of changing into each test case. So I need to write this ${#TestSuite#usergroup} custom property value nto xml. And call this .xml file request .
<test">
<addug>
<charging-id type="msisdn">${#Project#MSISDN_GB}</charging-id>
<ug>
<usergroup>${#TestSuite#usergroup}</usergroup>
</ug>
</addug>
</test>
Can you please help on same
Regards
Vishal
This shouldn't be too hard using groovy script. Instead of adding property expansion inside your file, you should add a Setup Script to the test case to edit the xml file to update what ever value you need in your nodes. Set up script will run before execution of test steps and ensure correct data in your XML file itself.
Here is an example script which edits and saves the sample xml in your first post in your thread. If you want to know more I would suggest to read any online documentation available for groovy.util.XmlParser
def filePath = '/path/to/your/file.xml' // Replace with the correct path. def xml = new XmlParser().parse(new FileInputStream(new File(filePath))) xml.get('payload').getAt('modify-UG').getAt('charging-id')[0].value = context.expand('${#TestCase#MSISDN}') new XmlNodePrinter(new PrintWriter(new FileWriter(filePath))).print(xml)
Cheers!
Gilu Gopi
- vpachpute18 years agoFrequent Contributor
Hi Gilu Gopi
Thanks for your solution. It worked out. I am able to add property value into xml.
But facing one issue that while writing property value, it adds the new lines each time.
As xml basic formatting changes, test case fails.
Can you please suggest a way so that basic xml formatting will not change.
Thanks !!!
My Basic request
<abc-request client-application-id="JUnit client" client-domain="JUnit client Domain">
<payload>
<modify-UG>
<charging-id type="msisdn">${#Project#MSISDN}</charging-id>
</modify-UG>
</payload>
</abc-request>After writing property value first time: (Added one extra line)
<abc-request client-application-id="JUnit client" client-domain="JUnit client Domain">
<payload>
<modify-UG>
<charging-id type="msisdn">
447717148597
</charging-id>
</modify-UG>
</payload>
</abc-request>After writing property value Second time : (Added 2 extra lines)
<abc-request client-application-id="JUnit client" client-domain="JUnit client Domain">
<payload>
<modify-UG>
<charging-id type="msisdn">
447717148597
</charging-id>
</modify-UG>
</payload>
</abc-request>Regards
Vishal
- vpachpute18 years agoFrequent Contributor
Hi Gilu Gopi
Can you please suggest any solution.
Regards
Vishal