Check loading time in WebDriver test during execution

Test is executed in Chrome browser. I need to measure response time of a page in execution time to check it is under a predefined value. If it is greater than this value some additional actions should be done. So I need different solution than _System.currentTimeMillis()_, because check of this value should be done automatically in background.

1 Like

i read that 3 times, i don’t get it…
can you try to write it as example? like how solution should look like?

So, loading of page should be terminated if value is greater than a predefined value. To do this, value should be checked during loading time too.

Maybe something like this:

long ts1 = System.currentTimeMillis() / 1000L
WebUI.openBrowser('www.google.com')
WebUI.waitForPageLoad(60) // You can set your max wait time here
long ts2 = System.currentTimeMillis() / 1000L
if ((ts2-t1)>10){
      WebUI.comment('Loading takes too long!')
}

I see, but in this case it will waits for 60 seconds and value of “if” condition will not be computed in background.

It doesn’t wait 60 seconds. It waits until page loads (for example, when I executed this script on my machine, ts2-ts1 was 4 milliseconds). 60 seconds is the maximal time it will wait and if the page isn’t loaded in that time period, it will fail.

Mate Mrse said:

It doesn’t wait 60 seconds. It waits until page loads (for example, when I executed this script on my machine, ts2-ts1 was 4 milliseconds). 60 seconds is the maximal time it will wait and if the page isn’t loaded in that time period, it will fail.

I see, but I should check waiting time during execution too. So, if it is greater than a predefined value, loading of page should be stopped by script.

you can use waitForPageLoad antwhere in script, or, you can check for particular element to show in time limit and set FailureHandling.STOP_ON_FAILURE

Andrej PodhajskĂ˝ said:

you can use waitForPageLoad antwhere in script, or, you can check for particular element to show in time limit and set FailureHandling.STOP_ON_FAILURE

Window looks like this, with a loading bar.

If loading is not complete for a predefined time, Close button should be clicked and some parametres should be chnaged to start process again.

ajx.jpg

then you can use waitForElementVisible with FailureHandling.OPTIONAL …
waitFor… function returns boolean so you can decite what to do next… check documentation for waitForElement… funcions if they can provide you with functionality you need…
https://docs.katalon.com/display/KD/[WebUI]+Wait+For+Element+Visible

1 Like

I am still not sure, how to wait for a predefined time and take an action with this method.

The following code as test case worked for me. It opens the target URL and measures time taken. If it takes more than 1 second, it fails.

import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import org.apache.commons.lang.time.StopWatch
import com.kms.katalon.core.model.FailureHandling as FailureHandling
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
WebUI.openBrowser('')
StopWatch stopwatch = new StopWatch()
stopwatch.start()
WebUI.navigateToUrl('https://katalon-demo-cura.herokuapp.com/')
WebUI.verifyElementPresent(
    findTestObject('Object Repository/Page_CURA Healthcare Service/a_Make Appointment'), 10)
stopwatch.stop()
long timeTaken = stopwatch.getTime()  // milli seconds
WebUI.comment(">>> timeTaken = ${timeTaken}")
// ensure response time is less than 1 second, otherwise failWebUI.verifyLessThan(timeTaken, 1000, FailureHandling.CONTINUE_ON_FAILURE)

When I tried it took 2.781 seconds, which is greater than the expected 1.0 seconds. Therefore the test case failed.

KatalonDiscussion9876.png

kazurayam said:

The following code as test case worked for me. It opens the target URL and measures time taken. If it takes more than 1 second, it fails.

import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject

import org.apache.commons.lang.time.StopWatch
import com.kms.katalon.core.model.FailureHandling as FailureHandling
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
WebUI.openBrowser(‘’)
StopWatch stopwatch = new StopWatch()
stopwatch.start()
WebUI.navigateToUrl(‘https://katalon-demo-cura.herokuapp.com/’)
WebUI.verifyElementPresent(
findTestObject(‘Object Repository/Page_CURA Healthcare Service/a_Make Appointment’), 10)
stopwatch.stop()
long timeTaken = stopwatch.getTime() // milli seconds
WebUI.comment(“>>> timeTaken = ${timeTaken}”)
// ensure response time is less than 1 second, otherwise failWebUI.verifyLessThan(timeTaken, 1000, FailureHandling.CONTINUE_ON_FAILURE)


  
When I tried it took 2.781 seconds, which is greater than the expected 1.0 seconds. Therefore the test case failed.  
![](https://s3.amazonaws.com/katalon-forum/editor/fy/17qxonq7dd8u.png "Image: https://s3.amazonaws.com/katalon-forum/editor/fy/17qxonq7dd8u.png")  

  

I would like to stop loading of page when loading time is exactly 4 seconds. So for example it should not wait for 5 seconds.