There is no problem to save response sheet to file. Smth like
def xmlFile = "C:/.../Try.xml"
def response = context.expand( '${Request#Response}' )
def f = new File(xmlFile)
f.write(response, "UTF-8")
BUT.
I re-run my request in groovy script with new parameters (using while), and I need to ADD info to result file, not to re-write it. Now it's just re-writing each time:(. File created outside the cycle.
Thanks in advance,
Dmitry
straight from the ref-doc
new File('TestFile1.txt').withWriterAppend( 'UTF-8' ){ w->
w << 'abcdefghij'
}
Related
I am trying to replace a test in pom.xml using a groovy script. These are my two approaches. The text should be replaced is {env.AM_$environment.toUpperCase()_SERVER_CREDS_USR}
Approach one
File mainPomXml = new File(rootDir,'/pom.xml')
mainPomXml.text.replace('{env.AM_$environment.toUpperCase()_SERVER_CREDS_USR}','${env.AM_$environment.toUpperCase()_SERVER_CREDS_USR}');
Approach two
def mainPomXml = new File(rootDir,'/pom.xml')
def mainPom = mainPomXml.text.replace('{env.AM_$environment.toUpperCase()_SERVER_CREDS_USR}','${env.AM_$environment.toUpperCase()_SERVER_CREDS_USR}');
mainPomXml.write(mainPom);
But none of these approaches work. That means both executes but the test is not get replaced. How to fix this issue?
Change the mainPom part as below.
def mainPomXml = new File(rootDir, '/pom.xml')
def mainPom = mainPomXml.text.replace('AM_SERVER_CREDS_USR', '${env.AM_'+ env.toUpperCase() +'_SERVER_CREDS_USR}')
mainPomXml.write(mainPom)
I don't know hardly anything about XML. I have successfully gotten a SOAP response (using things like SOAPUi and Boomerang) from an asmx web service. It's a large file.
Now I need to get it to regular delimited columns. Is there a simple way to do this?
My file is attached here
Not sure if it is required one time transformation or do this job quite frequently.
So, adding the answer here with some more details.
Approach #1: Using on-line
As mentioned in the comments, you can use the on-line site to convert your xml data into csv.
Even it requires to do some pre-process with the message / response that you have i.e.,
save the data into file
remove headers or unwanted data etc or make it ready to be usable in the above mentioned online site.
The disadvantages in this approaches
requires some manual work
expose data on public, but at times may be possible to share
time taking
can not use it an automated fashion
difficult to repeat
Approach #2: Using Groovy Script
So, this approach addresses the disadvantages of #1 approach.
Here is the Groovy Script which reads previous soap request step's response, and gives the data into a csv file.
In your test case, add a new groovy script test step right after the soap request step which gives you the data and copy below script content into it. i.e., (Test Case -> Step 1: Soap Request where you are getting responseStep 2: Groovy Script (with below script))
Add a test case custom property, say OUTPUT_FILE_NAME and provide the file path for csv to be saved at. Even, if you do not provide this property, it will automatically saves the csv file chargedata.csv under System temp directory.
You may find the comments in-line
/**
* this script will read the previous step response
* extract the cdata at the given xpath
* read all the records and transfroms into csv file
**/
import com.eviware.soapui.support.XmlHolder
import groovy.xml.*
/**Define the output file name in test case custom property say OUTPUT_FILE_NAME and value as absolute file path
* otherwise, it write a file chargedata.csv in system temp directory
**/
def outputFileName = context.testCase.getPropertyValue('OUTPUT_FILE_NAME') ?: System.getProperty("java.io.tmpdir")+ '/chargedata.csv'
//csv field separator - change it if needed
def delimiter = ','
/**
* Below statement will fetch the previous request step response.
*/
def response = context.testCase.testStepList[context.currentStepIndex - 1].testRequest.response.responseContent
//Create the xml holder object to get the xpath value which cdata in this case
def responseHolder = new XmlHolder(response)
def xpath = '//*:Charges_FileResponse/*:Charges_FileResult'
//Get the cdata part from above xpath which is a string
def data = responseHolder.getNodeValue(xpath)
//This again parses the xml inside of cdata
def chargeRecords = new XmlParser().parseText(data)
//This is going hold all the data from ChargeRecords
def chargeRecordsDataStructure = []
//This is to hold all the headers
def headers = [] as Set
/**
* This is to create Charge data
**/
def buildChargeDataStructure = { charge ->
def chargeDataStructure = new Expando()
charge.children().each {
def elementName = it.name()
def elementText = it.value().join()
chargeDataStructure[elementName] = elementText
//Add to field name to the list if not already added
(elementName in headers) ?: headers << elementName
}
chargeDataStructure
}
/**
* this is to create a csv row in string format
**/
def createRow = { recordDataStructure ->
def row = new StringBuffer()
headers.each {
if (row) {
row += delimiter + recordDataStructure[it] ?: ''
} else {
row += recordDataStructure[it] ?: ''
}
}
row.toString()+'\n'
}
//Build the whole data structure of Charge Records
chargeRecords.Charge.each { charge ->
chargeRecordsDataStructure << buildChargeDataStructure( charge )
}
//Build the rows
def rows = new StringBuffer()
rows << headers.join(',') +'\n'
chargeRecordsDataStructure.each { rows << createRow (it)}
//Write the rows into file
new File(outputFileName).text = rows
i'm using soapui for web service test and i want to find a specific word in my response file which is text file.
i found codes for find and replace but i want only find and display some specific word from my text file.
i tried to modify this code and i need some help please.
suppose i have a text file with Hello world and i want to find only "world" and create anothe with only "word"
def copy(source, dest, Closure replaceText){
dest.write(find(source.text))
}
def source = new File('source.txt') //Hello World
find(source){
it.findAll ('World')
}
Try:
def copy(source, dest, term){
if(source.getText("UTF-8").find(term)){
dest.write(term)
}
}
def source = new File('source.txt') //Hello World
def dest = new File('dest.txt')
copy(source, dest, "World")
your code sample is not very clear, but I guess you'll find your solution when you take a look at the regexp features of groovy:
http://mrhaki.blogspot.de/2009/09/groovy-goodness-matchers-for-regular.html
I have file stored in the website, I want to read the file and store each line in array list.
Currently using this code, it works. Is there a better way of doing this part of the code.
def data = u.toURL().text
def keys = new ArrayList()
data.eachLine {
keys.add(it)
}
Why not:
def keys = u.toURL().readLines()
?
As in the docs.
you could simplify it a bit:
def keys = []
u.toURL().text.eachLine{ keys << it }
and for such simple kind of job, it's just fine
I am doing soapui testing, wherein i want to save request & response suffixed with todaydate.
here's my code:
// Create a File object representing the folder 'A/B'
def folder = new File( 'C:/Project/SOAPUI' )
// If it doesn't exist
if( !folder.exists() ) {
// Create all folders up-to and including B
folder.mkdirs()
}
def myOutFile = "C:/Project/SOAPUI/test_request.txt"
def request = context.expand( '${test#Request}' )
def f = new File(myOutFile)
f.write(request, "UTF-8")
=====================
I want the file name to be test_request(+today's date)
Can you suggest me some options?
Here's an option:
def myOutFile = "C:/Project/SOAPUI/test_request_${(new Date()).format('yyyy-MM-dd')}.txt"
The output looks like C:/Project/SOAPUI/test_request_2015-08-30.txt