Forum Discussion

roja1's avatar
roja1
Contributor
5 years ago

Renaming a file using groovy

Hi,

 

I have 5 test cases in my test suite. I have written a groovy script which creates an excel file with results of all the test cases. My second test case copies the report created by first test case and writes the result of second test case in a new sheet of the existing excel report. Like wise it happens for all the test cases and a final report gets created with the results of individual test cases in individual sheets. I want my excel file name with timestamp. So I'm writing a script for renaming the file to report name appended by current timestamp. suppose my report name is output_service_api.xls now I want the file name to be output_service_api_2019-12-30 11:22:15.00594+0530.xls. Please help me with this.

 

Below is the code which I have used for report creation:

import com.eviware.soapui.model.testsuite.Assertable.AssertionStatus
import com.eviware.soapui.impl.wsdl.teststeps.RestTestRequestStep
import com.eviware.soapui.support.XmlHolder
import jxl.*
import jxl.write.*
def count=0,i=1,j=1
def inputFileName,outputFileName
def testStep = testRunner.testCase.testSteps["Properties"]
if (testStep.getPropertyCount() > 0){
for (prop in testStep.getPropertyList()){
if(prop.getName()=="inputFileName"){
inputFileName=prop.getValue()
}
else if(prop.getName()=="outputFileName"){
outputFileName=prop.getValue()
}
}
}
Workbook existingInputWB=Workbook.getWorkbook(new File(outputFileName))
WritableWorkbook outputWB=Workbook.createWorkbook(new File(outputFileName),existingInputWB)
testCaseName=testRunner.testCase.name
outputWB.createSheet(testCaseName,2)
WritableSheet sheet_writable = outputWB.getSheet(testCaseName)
jxl.write.Label testStepCell = new jxl.write.Label(0,0 ,"Test Step")
jxl.write.Label reqUrlCell = new jxl.write.Label(2,0 ,"Request URL")
jxl.write.Label reqPayloadCell = new jxl.write.Label(3,0 ,"Request Payload")
jxl.write.Label responseCell = new jxl.write.Label(4,0 ,"Response")
jxl.write.Label statusCell = new jxl.write.Label(1,0 ,"Status")
jxl.write.Label reasonCell = new jxl.write.Label(5,0 ,"Reason")
sheet_writable.addCell(testStepCell)
sheet_writable.addCell(reqUrlCell)
sheet_writable.addCell(reqPayloadCell)
sheet_writable.addCell(responseCell)
sheet_writable.addCell(statusCell)
sheet_writable.addCell(reasonCell)
(0..context.testCase.testStepCount-1).each{
def step = context.testCase.testStepList[it]
if ( step instanceof RestTestRequestStep) {
jxl.write.Label stepName = new jxl.write.Label(0,i ,step.name)
sheet_writable.addCell(stepName)
def tr=testRunner.testCase.getTestStepByName(step.name)
def String endPointUrl= tr.getHttpRequest().getResponse().getURL()
jxl.write.Label reqUrl = new jxl.write.Label(2,i ,endPointUrl)
sheet_writable.addCell(reqUrl)
def payload = context.expand(step.getPropertyValue('Request'))
jxl.write.Label reqPayload = new jxl.write.Label(3,i ,payload)
sheet_writable.addCell(reqPayload)
def response = context.expand(step.getPropertyValue('Response'))
jxl.write.Label reqResponse = new jxl.write.Label(4,i ,response)
sheet_writable.addCell(reqResponse)
count=count+1
i=i+1
}
}

def TestCase = testRunner.getTestCase()
def StepList = TestCase.getTestStepList()
StepList.each{
if(it.metaClass.hasProperty(it,'assertionStatus')){
if(it.assertionStatus == AssertionStatus.FAILED){
jxl.write.Label anotherWritableCell10 = new jxl.write.Label(1,j ,"Failed")
sheet_writable.addCell(anotherWritableCell10)
}else if(it.assertionStatus == AssertionStatus.VALID){
jxl.write.Label anotherWritableCell10 = new jxl.write.Label(1,j ,"Passed")
sheet_writable.addCell(anotherWritableCell10)
}else if(it.assertionStatus == AssertionStatus.UNKNOWN){
jxl.write.Label anotherWritableCell10 = new jxl.write.Label(1,j ,"UNKNOWN (PROBABLY NOT ALREADY EXECUTED)")
sheet_writable.addCell(anotherWritableCell10)
}
j=j+1
}

}
outputWB.write()
outputWB.close()
existingInputWB.close()

 

Appreciate your help in advance.