Forum Discussion

vpachpute1's avatar
vpachpute1
Frequent Contributor
8 years ago
Solved

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 file...
  • nmrao's avatar
    nmrao
    8 years ago

    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: