user3212324 user3212324 - 1 year ago 197
Groovy Question

Groovy script to Read an xml file and its contents

Requirement: To read the xml file from the folder and pass the contents of the file to Soap request.

Issue I am trying to read the file saved in the folder using groovy script, But unable to read the contents of the file. I am getting Null pointer exception while trying to print the contents of the xml file.

def fileList = []
new File("C:\\Users\\Documents\\Groovy Scripts\\requests").eachFile
{ f ->
if (f.isFile()&&'.xml'))
def filename =[0..-5]
fileList.add(filename) filename

if (fileList.size() <1)
{"No request files found")
context.put('fileList', fileList)

def f = new File("C:\\Users\\Documents\\Groovy Scripts\\requests\\${context.fileList}.last().text") f

Update based on comments, adding to the question.

My test case contains 3 steps. Step 1: to read the xml file from the folder. Step 2: use the xml file content as soap request input. Step 3: save the response of step 2 in output folder as xml.

Rao Rao
Answer Source

It is understand that you need to do the data-driven tests where requests are kept in a directory.

Previously, an approach is provided here to loop thru the data and save responses.

All the change you might need now is in the very first step - which reads the directory, and loops thru your files and set the file content as request and run the soap request step.

Groovy Script for Step1:


//change your input directory name below
def dir = new File('path/to/input/dir')
dir.eachFile (FileType.FILES) { file ->  

   //Get the step
   def step = context.testCase.getTestStepAt(1)
   //Set the file content as test step request
   step.testRequest.requestContent = file.text "New request is set for step2 : ${request}"
   //Run the step2, context)
//By now all the orders got executed, now need to exit the step without additionally running step2
//So, jump to step2, index is 2

You can continue to use the remaining steps as mentioned in the above provided link.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download