Groovy Script: XmlNodePrinter adds one extra line while writing into xml file.
Hi
Actually I want to write parameter value into xml file. It writes values perfectly but adds one extra line. Following is my groovy script. Can you please provide solution on same.
def filePath = '/home/vishalpachpute/API/Requests/AddUG.xml'
def xml = new XmlParser().parse(new FileInputStream(new File(filePath)))
xml.get('payload').getAt('modify-usergroup').getAt('charging-id')[0].value = context.expand('${#TestCase#MSISDN_GB}')
new XmlNodePrinter(new PrintWriter(preserveWhitespace:false(new FileWriter(filePath)))).print(xml)
My Basic External xml request file (In this file I want to write charging-id value )
<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
In that case, I would suggest the following:
Have the original file with data as below which uses property expansion as mentioned earlier, you may call it template request.
<abc-request client-application-id="JUnit client" client-domain="JUnit client Domain"> <payload> <modify-UG> <charging-id type="msisdn">${#TestCase#MSISDN}</charging-id> </modify-UG> </payload> </abc-request>
Before your json step, have a groovy step which does the transformation of the data and saves into a file in the temporary directory by replacing the MSISDN value.
This way, we always only read the template file(remains same) and create a new temporary file and that is sent as attachment each time. So, no overwriting of that template file.
def tmp = System.getProperty('java.io.tmpdir') context.testCase.setPropertyValue('FINAL_DIR', tmp) def originalFilePath = context.expand('${#TestCase#ORIGINAL_FILE_PATH}') def fileName = context.expand('${#TestCase#FINAL_FILE}') def content = new File(originalFilePath).text new File("${tmp}/${fileName}").write(context.expand(content))
Note: If you are using ReadyAPI, then you may avoid additional groovy step, instead use Events feature, TestStep.beforeSubmit and have the above script in it
In the attachment, you define the custom properties as below: