Advise on how to handle timestamp difference

I am trying to verify that a service history was logged.

In my script I grab the timestamp after hitting execute button

WebUI.click(findTestObject(‘x/x/execute_Btn’))
TimeZone.setDefault(TimeZone.getTimeZone(‘UTC’))
def now = new Date()
String val = now.format(‘yyyy-MM-dd' T 'HH:mm:ss’)
println('Value is: ’ + val)

from console
Value is: 2022-08-16 T 19:49:15

I then click the history tab and need to verify that the latest execute was logged

When I verify that text is present

WebUI.verifyTextPresent(val, false)

it will sometime fail as the timestamp on the history page is off by a few seconds. Even when I remove the seconds from the format, it may fail as the minute may be different.

Does anyone have any suggestions on how to handle this?

Katalon API provides nothing supportive for Date/Time issues. You have to write a fair amount of Groovy codes for yourself.

Please have a look at the following GitHub project

where I created “Test Cases/TC1” as a proposal to your question.

import java.nio.file.Path
import java.nio.file.Paths
import java.time.LocalDateTime
import java.time.ZoneOffset
import java.time.format.DateTimeFormatter
import java.time.temporal.ChronoUnit

import com.kms.katalon.core.configuration.RunConfiguration
import com.kms.katalon.core.testobject.ConditionType
import com.kms.katalon.core.testobject.TestObject
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

/**
 * A proposal to a question raised at
 * https://forum.katalon.com/t/advise-on-how-to-handle-timestamp-difference/68652
 */

// open the web page
Path projectDir = Paths.get(RunConfiguration.getProjectDir())
Path html = projectDir.resolve("page.html")
URL htmlUrl = html.toFile().toURI().toURL()
WebUI.openBrowser(htmlUrl.toExternalForm())

// get the string of timestamp
TestObject tObj = makeTestObject("//*[@id='clock']")
WebUI.verifyElementPresent(tObj, 10)
String ts = WebUI.getText(tObj)
println("displayed value is: " + ts)   // in UTC

// convert the timestamp string into a LocalDateTime object
// see https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html
// see https://www.baeldung.com/java-datetimeformatter
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-M-d' T 'H:m:s.SSS'Z'")
LocalDateTime parsedTimestamp = LocalDateTime.parse(ts, formatter)

// intentionally insert a delay for debugging
WebUI.delay(3)

// get the current timestamp that this test script recognizes
LocalDateTime currentTimestamp = LocalDateTime.now(ZoneOffset.UTC)
println("current timestamp is: " + currentTimestamp.format(formatter))

// calculrate the difference in the seconds unit between the currentTimestamp and the parsedTimestamp
// see https://www.baeldung.com/java-date-difference
long diff = ChronoUnit.SECONDS.between(parsedTimestamp, currentTimestamp);
println("diff is: " + diff)

// make sure the difference is less than 10 seconds
assert diff < 10

// close the page
WebUI.closeBrowser()

/**
 * function that creates a TestObject using an XPath expression
 */
def makeTestObject(String xpath) {
	TestObject tObj = new TestObject(xpath)
	tObj.addProperty("xpath", ConditionType.EQUALS, xpath)
	return tObj
}

TC1 converts a String of timestamp-like format into a java.time.LocalDateTime object.

TC1 also gets a current timestamp as a LocalDateTime object.

TC1 calculrates a difference between the two LocalDateTime objects.

TC1 verifies if the difference is less than 10 seconds.

Output:

...
2022-08-17 10:37:54.188 DEBUG testcase.TC1                             - 5: tObj = makeTestObject("//*[@id='clock']")
2022-08-17 10:37:54.190 DEBUG testcase.TC1                             - 1: tObj = new com.kms.katalon.core.testobject.TestObject(xpath)
2022-08-17 10:37:54.206 DEBUG testcase.TC1                             - 2: tObj.addProperty("xpath", EQUALS, xpath)
2022-08-17 10:37:54.230 DEBUG testcase.TC1                             - 3: return tObj
2022-08-17 10:37:54.236 DEBUG testcase.TC1                             - 6: verifyElementPresent(tObj, 10)
2022-08-17 10:37:54.634 DEBUG testcase.TC1                             - 7: ts = getText(tObj)
2022-08-17 10:37:54.935 DEBUG testcase.TC1                             - 8: println("displayed value is: " + ts)
displayed value is: 2022-8-17 T 1:37:54.176Z
2022-08-17 10:37:54.940 DEBUG testcase.TC1                             - 9: formatter = DateTimeFormatter.ofPattern("yyyy-M-d' T 'H:m:s.SSS'Z'")
2022-08-17 10:37:54.964 DEBUG testcase.TC1                             - 10: parsedTimestamp = LocalDateTime.parse(ts, formatter)
2022-08-17 10:37:54.989 DEBUG testcase.TC1                             - 11: delay(3)
2022-08-17 10:37:58.021 DEBUG testcase.TC1                             - 12: currentTimestamp = LocalDateTime.now(UTC)
2022-08-17 10:37:58.055 DEBUG testcase.TC1                             - 13: println("current timestamp is: " + currentTimestamp.format(formatter))
current timestamp is: 2022-8-17 T 1:37:58.045Z
2022-08-17 10:37:58.066 DEBUG testcase.TC1                             - 14: diff = SECONDS.between(parsedTimestamp, currentTimestamp)
2022-08-17 10:37:58.087 DEBUG testcase.TC1                             - 15: println("diff is: " + diff)
diff is: 3
2022-08-17 10:37:58.089 DEBUG testcase.TC1                             - 16: assert diff < 10
2022-08-17 10:37:58.104 DEBUG testcase.TC1                             - 17: closeBrowser()
2022-08-17 10:37:58.284 INFO  c.k.katalon.core.main.TestCaseExecutor   - END Test Cases/TC1

You shouldn’t use java.util.Date class any longer. The java.util.Date class is not enough in many cases.

You should learn Java Date/Time API supported at Java 8.

See

2 Likes

Thank you, I was able compare the timestamps and move forward with the test case with few seconds differencts

1 Like