Refresh Page until element appears (or doesnt)

Hi Guys

Loving Katalon and the helpful forums so far, but I hope you can help as I am stumped!

I am trying to verify that an element appears on a page within a certain period of time. I want to use periodic page refreshes for a set time until the object either appears or does not.

Here are my test parameters

  1. Wait for the page to load
    2a) If an object containing specific text appears on the page, click this object.
    Record the object has appeared.
    Close test.
    2b) If the object does not appear, then wait 30 seconds and click Refresh.
    Repeat step (2b) until the text appears and click the object.
    Close test.
  2. If the object does not appear in 5 minutes, close the test and record the object has not appeared.

**Basic Code
**Is it even possible to loop the below IF/THEN statement over a set period of time/minutes or do I need to revise my approach?

WebUI.delay(30)

if (WebUI.verifyElementPresent(findTestObject(‘Objects/Test Object’), FailureHandling.OPTIONAL)) {

WebUI.click(findTestObject('Objects/Test Object))

} else {

WebUI.click(findTestObject('Objects/Refresh Button'))

}

Thanks in advance
J

Hello jdm,

this code should work for you - I used while loop instead of if-else statement.

long timestart// timeout set to 300 secondsint timeout = 300WebUI.waitForPageLoad(30)timestart = System.currentTimeMillis() / 1000L// while element not present, click refresh and wait 30 seconds and check timeoutwhile (!WebUI.verifyElementPresent(findTestObject("Objects/Test Object"), 1, FailureHandling.OPTIONAL)) {	long currenttime = System.currentTimeMillis() / 1000L	// if timeout is reached, mark test failed	if(currenttime > timestart + timeout) {		KeywordUtil.markFailed("Timeout reached.")	}	WebUI.click(findTestObject("Objects/Refresh Button"))	WebUI.delay(30)}// click object as soon as you're outside of the loopWebUI.click(findTestObject("Objects/Test Object"))

3 Likes

While copy pasting the code … I got error, so I am pasting the code with imports.

import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import com.kms.katalon.core.model.FailureHandling as FailureHandling
import com.kms.katalon.core.util.KeywordUtil as KeywordUtil
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

long timestart

// timeout set to 300 seconds
int timeout = 300

WebUI.waitForPageLoad(30)

timestart = System.currentTimeMillis() / 1000L

// while element not present, click refresh and wait 30 seconds and check timeout

while (!WebUI.verifyElementPresent(findTestObject(“Objects/Test Object”), 1, FailureHandling.OPTIONAL))
{ long currenttime = System.currentTimeMillis() / 1000L

// if timeout is reached, mark test failed
if(currenttime > timestart + timeout) {
KeywordUtil.markFailed(“Timeout reached.”)
}

WebUI.click(findTestObject(“Objects/Refresh Button”))
WebUI.delay(30)}

// click object as soon as you’re outside of the loop
WebUI.click(findTestObject(“Objects/Test Object”))