How to get elapsed time when running a test case in Katalon?

107 views Asked by At

When running a test case in Katalon then it shows elapsed time of that test case in the log viewer and I want to get that elapsed time automatically to output into a custom report. How can I get this elapsed time?

2

There are 2 answers

1
Mate Mrše On

I'm guessing you are using the script view.

Do this:

import groovy.time.TimeCategory
import groovy.time.TimeDuration

start = new Date()

// execute the test

end = new Date()

TimeDuration td = TimeCategory.minus(end, start)
println td
0
Mike Warren On

To expand on @Mate Mrse's answer, let's do the following:

Create Custom Keyword, or something similar

Let's call this Custom Keyword ElapsedTimeUtil:

import groovy.time.TimeCategory
import groovy.time.TimeDuration

package com.tarang.utils

public class ElapsedTimeUtil { 
    private static final ElapsedTimeUtil _instance;

    private Date startTime;

    public static ElapsedTimeUtil GetInstance() {
        if (_instance == null)
            _instance = new ElapsedTimeUtil();

        return _instance;
    }

    private ElapsedTimeUtil() {
        // this is deliberately left empty, for semantic reasons
    }

    public void startTimer() {
        this.startTime = new Date();
    }

    public TimeDuration getElapsedTime() {
        return TimeCategory.minus(new Date(), this.startTime);
    }
}

Notice that, we are using Groovy's built-in TimeDuration class (here is documentation).

Set this Custom Keyword up with Test Case Listener

Follow the linked instructions to create your Test Listener.

Then, we do the following:

Initialization

In your @BeforeTestCase hook, add the following:

ElapsedTimeUtil.GetInstance().startTimer();

Teardown

This is where you get the elapsed time for your custom report. You can choose to do this one of two ways:

  • in the @AfterTestCase hook, or
  • in the tearDownTestCase hook of your test suite (delete the (skipped = true) from the annotation)

Either way, add the following:

ElapsedTimeUtil.GetInstance().getElapsedTime(); // TODO: pass this to your report util keyword

The former is preferred for completeness, and will happen regardless of test suite or test case