I would like to return the values of the elapsed time of a specific step in my test case into an excel file to include on reporting of how long it takes to execute each step.
Is there any commands or plug ins that can return the value of the elapsed time for a specific test step into an excel sheet?
1 Like
system
August 21, 2024, 6:56pm
2
Hi there,
Thank you very much for your topic. Please note that it may take a little while before a member of our community or from Katalon team responds to you.
Thanks!
Here I would show you an example Test Case, which reports how long in seconds it took to launch a browser and navigate to a URL
import java.time.Duration
import java.time.LocalDateTime
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
String url = 'https://forum.katalon.com/'
// measure the duration
LocalDateTime start = LocalDateTime.now()
WebUI.openBrowser(url)
LocalDateTime end = LocalDateTime.now()
Duration duration = Duration.between(start, end)
// report the duration
int nano = duration.getNano()
String secondsString = String.format("%.2f", nano / 1000000000)
WebUI.comment("it took ${secondsString} second to navigate to $url")
WebUI.closeBrowser()
This gave me the following output
Collecting duration value is as simple as this. Tedious? yes, it is.
2 Likes
Katalon Studio does not provide such feature.
I have developed a class library named “Timekeeper” for me to measure the elapsed time in my Katalon projects:
Timekeeper is a Java/Groovy library that helps tests to compile performance reports in Markdown.
Timekeeper writes a Markdown text in a table format, like this
This library is intended primarily for my own use. But you can use it as is published at the Maven Central .
This library supports saving the report into a CSV file.
Excel (.xls) is not supported. ---- Why? ---- The reason is personal. I am on mac without MS Excel.
If you want to write to Microsoft Office applications, I.E. Excel, you should invest time in learning Apache POI. I have been learning this to create custom Excel reports using an excel file template.
Example of writing text to a cell in a basic manner…Combine this with @kazurayam method for getting duration via start and end times.
input: String to enter into cell
myCell : String reference to cell. I.E. A1
mySheet: name of the Excel sheet.
myPath: File path to excel file.
Example:
setCellText("duration","A2", "Template", ".\\Template\\template - Copy.xlsx")
import org.apache.poi.ss.util.CellReference
import org.apache.poi.xssf.usermodel.XSSFSheet
import org.apache.poi.xssf.usermodel.XSSFWorkbook
public static void setCellText(String input, String myCell, String mySheet, String myPath) {
File file = new File(myPath)
FileInputStream fis = new FileInputStream (file);
XSSFWorkbook workbook = new XSSFWorkbook (fis);
XSSFSheet sheet = workbook.getSheet(mySheet);
CellReference cellRef = new CellReference(myCell)
sheet.getRow(cellRef.getRow()).getCell(cellRef.getCol()).setCellValue(input)
FileOutputStream fos = new FileOutputStream(myPath);
workbook.write(fos);
fos.close();
fis.close();
}
I found that Katalon Studio bundles Super CSV
See How can I write values to a CSV file from Katalon Studio - #8 by kazurayam
I made a sample Test Case that utilized Super CSV
“Test Cases/TCx”
import static com.kms.katalon.core.testcase.TestCaseFactory.findTestCase
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
import java.time.Duration
import java.time.LocalDateTime
import org.supercsv.io.CsvListWriter
import org.supercsv.prefs.CsvPreference
import com.kms.katalon.core.configuration.RunConfiguration
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
LocalDateTime start = LocalDateTime.now()
WebUI.callTestCase(findTestCase("Test Cases/TC1"), [:]) // TC1 takes a few seconds to finish
LocalDateTime end = LocalDateTime.now()
Duration duration = Duration.between(start, end)
// report the duration
int nano = duration.getNano()
String secondsString = String.format("%.2f", nano / 1000000000)
Path projectDir = Paths.get(RunConfiguration.getProjectDir())
Path csvFile = projectDir.resolve("output.csv")
Writer writer = Files.newBufferedWriter(csvFile)
CsvListWriter clw = new CsvListWriter(writer, CsvPreference.EXCEL_PREFERENCE)
clw.write(Arrays.asList("memo","duration"))
clw.write(["calling \"TC1\" took", secondsString])
clw.flush()
clw.close()
output.csv
memo,duration
"calling ""TC1"" took",0.71