3

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()&& f.name.endsWith('.xml'))
{
 def filename = f.name[0..-5]
 fileList.add(filename)
 log.info filename

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

def f = new File("C:\\Users\\Documents\\Groovy Scripts\\requests\\${context.fileList}.last().text")
log.info 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
  • 20,781
  • 11
  • 57
  • 77
user3212324
  • 163
  • 1
  • 6
  • 23
  • Are you doing `data-driven` tests? Can you show the structure of your test case? – Rao Nov 08 '16 at 01:49
  • Possible duplicate of [How to read a file in Groovy into a string?](http://stackoverflow.com/questions/7729302/how-to-read-a-file-in-groovy-into-a-string) – Rao Nov 08 '16 at 01:51
  • 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. – user3212324 Nov 08 '16 at 03:38
  • Not sure if you understand the earlier question. Because you did not reply to the point. Any way, how many files in the folder? – Rao Nov 08 '16 at 03:48
  • File size varies in the folder... I know I am bit wrong based on my requirements and the code that I have posted... Please correct me for the same. – user3212324 Nov 08 '16 at 04:01
  • You still did not answer for the question from first comment and question from second comment as well. Are all files having requests for the same soap operation? – Rao Nov 08 '16 at 04:07
  • Hi Rao,Yes all files belongs to one soap request operations. However the data inside varies. To answer first question, yes I am trying to automate the process meaning I need to run n number of xmls and save the response – user3212324 Nov 08 '16 at 05:34
  • looks like you previously had similar question [here](http://stackoverflow.com/questions/40402086/groovy-script-automatic-request-and-saving-response-from-soap-ui). Now is that you need set entire request from file instead of just order id? If so, there would be change in the first only in the previously provided answer. Is that fine? Can you clarify? – Rao Nov 08 '16 at 05:55
  • Hi Rao, Yes, that is what my requirement is.. Since few of the xml's have few more parameters other than order id. So i need to pass the entire xml rather than one parameter. – user3212324 Nov 08 '16 at 06:15
  • Can you please check the answer provide below and see if that is helpful. – Rao Nov 08 '16 at 06:18

1 Answers1

3

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:

import groovy.io.FileType

//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
   log.info "New request is set for step2 : ${request}"
   //Run the step2
   step.run(testRunner, 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
testRunner.gotoStep(2)

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

Community
  • 1
  • 1
Rao
  • 20,781
  • 11
  • 57
  • 77
  • 1
    Glad to know that it is helpful. If you wish you can upvote the answers that are helpful to you to appreciate. – Rao Nov 08 '16 at 08:22
  • I am trying to add few more features to my automation. Here I am trying to add the fields in excel sheet. How ever only the last file response is getting recorded. Could you please help me how to write the response for all the file responses. Note: Each file response must be in new row – user3212324 Nov 09 '16 at 12:10
  • `code` import jxl.* import jxl.write.* def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context ) def holder = groovyUtils.getXmlHolder("Step2#Response") def request=groovyUtils.getXmlHolder("Step2#Request") WritableWorkbook workbook = Workbook.createWorkbook(new File("C:\\Users\\Documents\\ Groovy Scripts\\response\\output.xls")) WritableSheet sheet = workbook.createSheet("Worksheet 1", 0) `code` – user3212324 Nov 09 '16 at 12:12
  • xPath1 = "//*:description/text()" xPath2 = "//*:commonOrderId/text()" xPath3 = "//*:m/text()" def row=1 Label orderid = new Label(0,row ,request.getNodeValue(xPath2)); sheet.addCell(orderid); Label mode = new Label(1,row ,request.getNodeValue(xPath3)); sheet.addCell(m); Label description = new Label(2,row , holder.getNodeValue(xPath1)); sheet.addCell(description); Label response = new Label(3, row, context.expand('${Step2#Response}')); sheet.addCell(response); row=row+1 workbook.write(); workbook.close(); – user3212324 Nov 09 '16 at 12:13
  • ,I have used the above codes inside step 2. I believe everytime to create a new excel sheet is creating the issue. Kindly help out – user3212324 Nov 09 '16 at 12:14
  • That is out scope in the question, may be you can open a new question with details. – Rao Nov 09 '16 at 12:18
  • Could you please help me out. I have created a new question. Issue is I am able to write all the responses in excel sheet. Howvere each response is writing twice. https://stackoverflow.com/questions/40507209/how-to-write-the-response-in-existing-excel-sheet-using-groovy-script – user3212324 Nov 10 '16 at 10:57