We are building a new experience for our SmartBear Community.
This site is now read-only. You can still browse and search as usual. Learn More
This site is now read-only. You can still browse and search as usual. Learn More
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:
I agree with nmrao, do you really need to updating values when the property expansion should do the job.
But addressing your question...
I believe that it is the preserve white space setting that is causing your issue. See the following code based on you initial post, that I had in a Groovy Test step. This will produce two files when with the extra line breaks and one without:
import groovy.xml.*
import groovy.util.XmlParser
def xmlText = '''<abc-request client-application-id="JUnit client" client-domain="JUnit client Domain">
<payload>
<modify-UG>
<charging-id type="msisdn"></charging-id>
</modify-UG>
</payload>
</abc-request>'''
def xml = new XmlParser().parseText(xmlText)
xml.get('payload').getAt('modify-UG').getAt('charging-id')[0].value = context.expand( '${#TestCase#MSISDN_GB}' )
// Exhibits the Extra line breaks issue
def filePath1 = '/home/vishalpachpute/API/Requests/AddUG1.xml'
def printWriter1 = new PrintWriter(new FileWriter(filePath1))
def xmlNodePrinter1 = new XmlNodePrinter(printWriter1)
xmlNodePrinter1.setPreserveWhitespace(false)
xmlNodePrinter1.print(xml)
// Does NOT exhibit the extra line break issue
def filePath2 = '/home/vishalpachpute/API/Requests/AddUG2.xml'
def printWriter2 = new PrintWriter(new FileWriter(filePath2))
def xmlNodePrinter2 = new XmlNodePrinter(printWriter2)
xmlNodePrinter2.setPreserveWhitespace(true)
xmlNodePrinter2.print(xml)
This blog post:
http://mrhaki.blogspot.co.uk/2012/10/groovy-goodness-pretty-print-xml.html
Contains some very useful information about formatting XML data.
Hi
Answer: Actually property expansion does it. It is right. But my project requirement is
1. Keep base xml request file at any external location.
2. Call it in the requests. Change some properties values.
So that going forward, if there any changes in the requests, we have do maintenance in externalized file only. Not inside the scripts.
Thanks very much for your solution. It really helped me.
Ohh. That's really good idea.
But before this workaround, I have tried this.
1. Following is my externalized request.xml file
<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>
2. Call request from external request file (For this, In requests, I am using media type as 'text/xml' and attaching the .xml file)
Issue:
When I passed custom properties(${#TestCase#MSISDN}) inside request.xml file, it is not picking the value of of custom property.
So Can you please correct me if I am missing anything here. This will be very straightforward solution for me.
regards
Vishal