cancel
Showing results for 
Search instead for 
Did you mean: 

Renaming a file using groovy

Highlighted
Contributor

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.

10 REPLIES 10
Highlighted
Super Contributor

Re: Renaming a file using groovy

You can use java code also in soap ui. You can follow below link. There are lot many examples :-

 

https://stackoverflow.com/questions/32587062/name-a-file-in-java-to-include-date-and-time-stamp

Highlighted
Contributor

Re: Renaming a file using groovy

Hi,

 

Thanks for the reply.I'm readinng file name from properties. Is it possible to read properties of soapUI using Java?

Highlighted
Super Contributor

Re: Renaming a file using groovy

Read the properties step using groovy..

def myProps = testRunner.testCase.getTestStepByName("Properties")

You can write groovy and java code together in soapui. There won't be any problem.

Highlighted
Contributor

Re: Renaming a file using groovy

Hi,

 

I tried with below code  and getting an error:

Code:

import java.text.SimpleDateFormat
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()=="outputFileName"){
outputFileName=prop.getValue()
}
}
}
Workbook old_WB=Workbook.getWorkbook(new File(outputFileName))
DateFormat df = new SimpleDateFormat("yyyyMMddhhmmss");
String filename = old_WB.split(".")[0] + df.format(new Date()) + old_WB.split(".")[1];

 

Error:


org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'java.text.SimpleDateFormat@88c7dd60' with class 'java.text.SimpleDateFormat' to class 'jxl.write.DateFormat' error at line: 18

 

I'm not familiar with coding. Please bear with me if there are any blunders.

Thank you in advance

Highlighted
Super Contributor

Re: Renaming a file using groovy

Try the below code for the Workbok :-

 

String FILE = "C:\\Desktop\\data.xls";
Workbook old_wb = Workbook.getWorkbook(new File(FILE));
DateFormat df = new SimpleDateFormat("yyyy-MM-dd:hh:mm:ss");
String filename = old_wb + df.format(new Date()) + ".";

And if you have the excel path then directly use :-

 

String FILE = "C://Desktop//data";
String FILE_EXTENSION = "xls";
DateFormat df = new SimpleDateFormat("yyyy-MM-dd:hh:mm:ss");
String filename = FILE + df.format(new Date()) + "." + FILE_EXTENSION;
System.out.println(filename);
		
Highlighted
Contributor

Re: Renaming a file using groovy

Hi, 

I have used the below code as suggested but still getting error:

Code:

import java.text.SimpleDateFormat
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()=="outputFileName"){
outputFileName=prop.getValue()
}
}
}
Workbook old_WB=Workbook.getWorkbook(new File(outputFileName))
DateFormat df = new SimpleDateFormat("yyyy-MM-dd:hh:mm:ss");
String filename = old_wb + df.format(new Date()) + ".";

error:

org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'java.text.SimpleDateFormat@f7573bba' with class 'java.text.SimpleDateFormat' to class 'jxl.write.DateFormat' error at line: 18

Highlighted
Super Contributor

Re: Renaming a file using groovy

@nmrao Could you please help here? i am also not able to get this,.. I gave the solution. its not working as expected.

Highlighted
Community Hero

Re: Renaming a file using groovy

Could not go thru all the all posts.

First of all why to re-invent the wheel while SoapUI can generate Junit style reports which is a standard reporting?

Have a look at here. You just need to have docker intsalled and working.
All you need is to provide your project path in command line
https://community.smartbear.com/t5/SoapUI-Open-Source/Docker-Image-for-SoapUI-5-4-0/td-p/154860


Regards,
Rao.
Highlighted
Contributor

Re: Renaming a file using groovy

Hi @nmrao ,

 

I have created an excel file for reporting and now I want to append timestamp to the created file.

 

Thank you in advance

New Here?
Join us and watch the welcome video:
Top Kudoed Authors