Call a method until an element becomes visible

I am trying to call a method until a particular element becomes visible. The element in question is always present on the webpage but it only becomes visible if a particular button is clicked.

I am trying to do something like do…while.

while (true){
			selectFirstAvailableOption();
			if (WebUI.verifyElementVisible(findTestObject('ProductDetailsPage/PickUpAtStoreOption'), FailureHandling.CONTINUE_ON_FAILURE)) {
				break;
			}
		}

I want to continue calling selectFirstAvailableOption() until ProductDetailsPage/PickUpAtStoreOption becomes visible.

Currently my test goes in infinite loop and the test keeps on running. Does someone know what I am doing wrong?

You need to realize that your test code is not connected to the web page. Therefore, when a change in the page does occur, your code needs to be checking at about the same time.

Rather than me give you the code, let me first outline the code in English and you see if you can write it yourself – sound okay?

  1. Inside your loop, call WebUI.delay(1), this will cause your loop to check the element once every second or so.
  2. Make sure your loop has a maximum timeout (e.g. 30 seconds) after which time, you throw an exception which will fail the test because the element di not appear in a reasonable amount of time.
  3. Optionally, and for bonus points, start a counter before the loop. Inside the loop, increment the counter. Print the value of the counter in the exception showing how many times the loop failed to find the element. :sunglasses:

Once your loop does those things, create another version to make it a Keyword. Then you can call your keyword from anywhere any time you need it.

Let me know how you get on.

@Russ_Thomas

I added that WebUI.delay(1) just inside my while loop and it did not fix it. Then I also added the timeout of 10 seconds, but with while(true) the test was in infinite loop. But if I changed that to while(false) then the test failed after 10 seconds and throws an error stating that

Caused by: com.kms.katalon.core.exception.StepFailedException: Object 'Object Repository/ProductDetailsPage/PickUpAtStoreOption' is NOT visible

It never called selectFirstAvailableOption(); after it failed to find 'ProductDetailsPage/PickUpAtStoreOption' the first time.

I also used the following code to perform this and still the test fails immediately if it fails to find the 'ProductDetailsPage/PickUpAtStoreOption'

while ({
			selectFirstAvailableOption();
			WebUI.verifyElementVisible(findTestObject('ProductDetailsPage/PickUpAtStoreOption'))
		}()) continue

How about instead of your “while” loop, you go for a “for” loop for about 10 counts: No infinite loop that way. If you haven’t got your element visible in 10 seconds then…

for (int i = 0; i < 10; i++) {
    selectFirstAvailableOption();
    if (WebUI.verifyElementVisible(findTestObject('ProductDetailsPage/PickUpAtStoreOption'), FailureHandling.OPTIONAL))
       break;
    WebUI.delay(1)
    WebUI.comment("not here at ${i}")
}

Have you tried this?

  1. it takes “int timeout” argument
  2. it returns boolean value; you can check if the element has got finally visible or not by the returned value