# Wait commands doesn't wait enough time

**URL:** <https://forum.katalon.com/t/wait-commands-doesnt-wait-enough-time/16875>\
**Category:** Web Testing\
**Tags:** katalon-studio\
**Created:** [January 4, 2019, 12:47pm UTC](https://forum.katalon.com/t/wait-commands-doesnt-wait-enough-time/16875 "2019-01-04T12:47:41Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Robert\_C](https://avatars.discourse-cdn.com/v4/letter/r/d78d45/32.png) [@Robert\_C](https://forum.katalon.com/u/Robert_C)\
**Post date:** [January 4, 2019, 12:47pm UTC](https://forum.katalon.com/t/wait-commands-doesnt-wait-enough-time/16875/1 "2019-01-04T12:47:41Z")

</div>

I have following loop:

while (WebUI.verifyElementPresent(findTestObject(‘TestObject\_1’), 5, FailureHandling.OPTIONAL) != true) {

```
WebUI.click(findTestObject('TestObject_2'))	

WebUI.waitForElementVisible(findTestObject('TestObject_2'), 40, FailureHandling.CONTINUE_ON_FAILURE)

```

}

Loading whole page after TestObject\_2 takes from 10 to even 40 seconds (loading list of records). I’ve tried to use waitForElementVisible and waitForElementClickable for wait until TestObject\_1 is visible on the page after reload. In both cases wait takes only 5 seconds so then test is failed because it can’t click on TestObject\_2 (page is still loading). I can use delay with 40 second but obviously test cases takes much more time as page is sometimes reloaded after 10-15 seconds and this loop sometimes is running 10 times.  
Any ideas why it happening?

---

<div class="post-metadata">

**Author:** ![Taylor\_Russi](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/taylor_russi/32/812_2.png) [@Taylor\_Russi](https://forum.katalon.com/u/Taylor_Russi)\
**Post date:** [January 4, 2019, 3:04pm UTC](https://forum.katalon.com/t/wait-commands-doesnt-wait-enough-time/16875/2 "2019-01-04T15:04:38Z")

</div>

I don’t know why this happens, but I’ve run into similar things. My workaround would be another loop with a try+catch, that repeatedly tries to click on Object\_2 and stops when the case passes.

---

<div class="post-metadata">

**Author:** ![Russ\_Thomas](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/russ_thomas/32/11884_2.png) [@Russ\_Thomas](https://forum.katalon.com/u/Russ_Thomas)\
**Post date:** [January 4, 2019, 3:26pm UTC](https://forum.katalon.com/t/wait-commands-doesnt-wait-enough-time/16875/3 "2019-01-04T15:26:10Z")

</div>

> [@Robert\_C](#):
>
> Any ideas why it happening?

Not really, no. The problem here is:

You have not described clearly what your website is meant to be doing. It’s possible to _infer_ what it _might_ be doing from some of what you said, but I would be as likely to hinder you as help you. Please, write out a clear paragraph describing how the site is meant to behave.

With that said, I’m guessing you don’t need the loop. Clicking TO\_2 causes a page (or section of a page) to load wherein TO\_1 resides. Once TO\_1 is present then your test (not shown) is meant to proceed - correct?

Statements:

FailureHandling works.  
waitFor\* APIs work.

Your logic though is still under question.

Follow the advice I gave here, then perhaps we can help you:

> [@\[TIP\] How To Help Us Help You!](http://forum.katalon.com/t/tip-my-test-is-broken-nothing-works-please-help-me/12919):
>
> My test is BROKEN! NOTHING WORKS! HELP ME! Relax. We get it. We really do. There are some pretty smart and clever people on this forum, but you must realize, they are not magicians or wizards or gods. They cannot see your code, they can’t see your errors and they don’t even know what your AUT is meant to do even though it might seem perfectly obvious to you. So please relax and follow the advice below, before you start writing your post. The advice below will tell you the exact information …

---

<div class="post-metadata">

**Author:** ![Robert\_C](https://avatars.discourse-cdn.com/v4/letter/r/d78d45/32.png) [@Robert\_C](https://forum.katalon.com/u/Robert_C)\
**Post date:** [January 7, 2019, 7:56am UTC](https://forum.katalon.com/t/wait-commands-doesnt-wait-enough-time/16875/4 "2019-01-07T07:56:41Z")

</div>

Thanks Taylor! would you give me some simple example how to use loop with try catch.

---

<div class="post-metadata">

**Author:** ![Robert\_C](https://avatars.discourse-cdn.com/v4/letter/r/d78d45/32.png) [@Robert\_C](https://forum.katalon.com/u/Robert_C)\
**Post date:** [January 7, 2019, 8:14am UTC](https://forum.katalon.com/t/wait-commands-doesnt-wait-enough-time/16875/5 "2019-01-07T08:14:07Z")

</div>

Hi Russ,  
My script previously is simple search which gives me list of the results (10 records per 1 page). There are 2 kind of results - highlighted in green and red, I need to search for first red record and click it to open details.  
My loop firstly is checking first if red record is displayed on the list of results(TestObject\_1’), if not it’s clicking next page button (TestObject\_2) and it works if I use delay command. I would like to use wait command to check if TestObject\_2 is displayed on the page (it’s displaying when new page finished loading) to save time of execution because sometimes red record is presenting on 10th page and loading each page with results takes from 10 to even 40 seconds.  
I hope it’s more clear now.

---

<div class="post-metadata">

**Author:** ![Antoine\_Bulteau](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/antoine_bulteau/32/2177_2.png) [@Antoine\_Bulteau](https://forum.katalon.com/u/Antoine_Bulteau)\
**Post date:** [January 7, 2019, 11:00am UTC](https://forum.katalon.com/t/wait-commands-doesnt-wait-enough-time/16875/6 "2019-01-07T11:00:01Z")

</div>

Hi Robert,  
I tried to reproduce your case. As far a I can see with your loading scenario, you can optimize your loop, because if your result is on the first page, you wait only 5 seconds, which might not be enought. And later, when you wait until your page is fully loaded, you again wait 5 seconds. Please see the following :

> // wait for the first result page to be fully loaded  
> WebUI.waitForElementVisible(findTestObject(‘next’), 40, FailureHandling.OPTIONAL)
> 
> // search for your element with shorter failed timeout (as we already ensure page is loaded)  
> while (WebUI.verifyElementPresent(findTestObject(‘objetRecherche’), 1, FailureHandling.OPTIONAL) != true) {
> 
> // if not, click next  
> WebUI.click(findTestObject(‘next’))
> 
> // wait for the page to be fully loaded  
> WebUI.waitForElementVisible(findTestObject(‘next’), 40, FailureHandling.OPTIONAL)
> 
> // add this extra comment so your logs will look nicer (for the next check loop result)  
> WebUI.comment(‘next while’)  
> }
> 
> WebUI.click(findTestObject(‘objetRecherche’))

Also, please pay intention to the extra comment at the end of the while loop : this will help you debug your script.

---

<div class="post-metadata">

**Author:** ![Loan\_Tran](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/loan_tran/32/11091_2.png) [@Loan\_Tran](https://forum.katalon.com/u/Loan_Tran)\
**Post date:** [January 8, 2019, 10:22am UTC](https://forum.katalon.com/t/wait-commands-doesnt-wait-enough-time/16875/7 "2019-01-08T10:22:59Z")

</div>

Update:

> [@Loan\_Tran](#):
>
> Confirm that if we don’t set a timeout for waitElementPresent, the default timeout will be taken.

> **Summary**
>
> ## Please ignore the following message:
> 
> Hi @Robert_C,  
> Try seting Default timeout in Project Setting to 40 seconds.  
> If you set 5 seconds in waitElementPresent() but 40 seconds for default timeout then it will be waited up to 5 seconds. However, if 5 seconds is set in waitElementPresent() but 1 second set for default timeout setting, it will be waited 1 second maximum
> 
> ![image](https://europe1.discourse-cdn.com/katalon/original/1X/454a3dc92c403597def8784cc75180d907d4d01d.png)

---

<div class="post-metadata">

**Author:** ![Antoine\_Bulteau](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/antoine_bulteau/32/2177_2.png) [@Antoine\_Bulteau](https://forum.katalon.com/u/Antoine_Bulteau)\
**Post date:** [January 8, 2019, 10:47am UTC](https://forum.katalon.com/t/wait-commands-doesnt-wait-enough-time/16875/8 "2019-01-08T10:47:36Z")

</div>

> [@Loan\_Tran](#):
>
> Try seting Default timeout in Project Setting to 40 seconds.  
> If you set 5 seconds in waitElementPresent() but 40 seconds for default timeout then it will be waited up to 5 seconds. However, if 5 seconds is set in waitElementPresent() but 1 second set for default timeout setting, it will be waited 1 second maximum

Are you saying that setting a value higher than the default value will never be taken into account ? Isn’t it a bug ?

---

<div class="post-metadata">

**Author:** ![Robert\_C](https://avatars.discourse-cdn.com/v4/letter/r/d78d45/32.png) [@Robert\_C](https://forum.katalon.com/u/Robert_C)\
**Post date:** [January 8, 2019, 12:06pm UTC](https://forum.katalon.com/t/wait-commands-doesnt-wait-enough-time/16875/9 "2019-01-08T12:06:18Z")

</div>

Here is my solution - I’ve created new object for spinner which is displaying during searching (TestObject\_3) so now my loop looks like that:

while (WebUI.verifyElementPresent(findTestObject(‘TestObject\_1’), 5, FailureHandling.OPTIONAL) != true) {

WebUI.click(findTestObject(‘TestObject\_2’))

WebUI.waitForElementNotPresent(findTestObject(‘TestObject\_3’), 40, FailureHandling.CONTINUE\_ON\_FAILURE)

}  
and it’s waiting until this spinner disappear to continue looping.

Thanks for all your suggestions!

---

<div class="post-metadata">

**Author:** ![Brandon\_Hein](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/brandon_hein/32/1092_2.png) [@Brandon\_Hein](https://forum.katalon.com/u/Brandon_Hein)\
**Post date:** [January 8, 2019, 8:03pm UTC](https://forum.katalon.com/t/wait-commands-doesnt-wait-enough-time/16875/10 "2019-01-08T20:03:32Z")

</div>

Sounds like you have a working solution, but just wanted to toss an idea to solve this more generically, as I’ve grappled with similar challenges. What it _sounds_ like is happening is that most or all of your page has loaded, but there’s some AJAX going on in the background, and thus your web developer puts up a ‘spinner’ to prevent user interaction with the page until it’s complete.

This would explain why your wait conditions end after ~5 seconds, even though there’s a spinner. As far as the DOM is concerned, your elements are available and ready to be interacted with.

First, check if your page is jQuery enabled by executing the following in the browser console:

`!!window.jQuery`

If you get ‘true’, try using a custom wait condition like the following one:

```
new WebDriverWait(driver, 300, 100).until(new ExpectedCondition<Boolean>() {
						@Override
						public Boolean apply(WebDriver d) {
							JavascriptExecutor js = (JavascriptExecutor)d;
							return (Boolean)js.executeScript(
									"return !!window.jQuery && window.jQuery.active == 0");
						}
					});

```

This will work, spinner or no spinner, for any application that makes heavy use of AJAX.

---

<div class="post-metadata">

**Author:** ![Mate\_Mrse](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/mate_mrse/32/345_2.png) [@Mate\_Mrse](https://forum.katalon.com/u/Mate_Mrse)\
**Post date:** [January 9, 2019, 7:35am UTC](https://forum.katalon.com/t/wait-commands-doesnt-wait-enough-time/16875/11 "2019-01-09T07:35:01Z")

</div>

> [@Antoine\_Bulteau](#):
>
> > Try seting Default timeout in Project Setting to 40 seconds.  
> > If you set 5 seconds in waitElementPresent() but 40 seconds for default timeout then it will be waited up to 5 seconds. However, if 5 seconds is set in waitElementPresent() but 1 second set for default timeout setting, it will be waited 1 second maximum
> 
> Are you saying that setting a value higher than the default value will never be taken into account ? Isn’t it a bug ?

Default timeout will be used only if no timeout is set in `waitElementPresent()`.

---

<div class="post-metadata">

**Author:** ![Loan\_Tran](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/loan_tran/32/11091_2.png) [@Loan\_Tran](https://forum.katalon.com/u/Loan_Tran)\
**Post date:** [January 9, 2019, 10:13am UTC](https://forum.katalon.com/t/wait-commands-doesnt-wait-enough-time/16875/12 "2019-01-09T10:13:53Z")

</div>

I’m sorry for misunderstanding default timeout. Confirm that if we don’t set a timeout for waitElementPresent, the default timeout will be taken.

---

<div class="post-metadata">

**Author:** ![focis.automation](https://avatars.discourse-cdn.com/v4/letter/f/90ced4/32.png) [@focis.automation](https://forum.katalon.com/u/focis.automation)\
**Post date:** [February 26, 2019, 2:22pm UTC](https://forum.katalon.com/t/wait-commands-doesnt-wait-enough-time/16875/13 "2019-02-26T14:22:34Z")

</div>

Hi Brandon, Could you please tell … How to use this in Katalon and the imports required --\> Using @Keyword.

Thanks.

---

<div class="post-metadata">

**Author:** ![Brandon\_Hein](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/brandon_hein/32/1092_2.png) [@Brandon\_Hein](https://forum.katalon.com/u/Brandon_Hein)\
**Post date:** [February 26, 2019, 5:37pm UTC](https://forum.katalon.com/t/wait-commands-doesnt-wait-enough-time/16875/14 "2019-02-26T17:37:42Z")

</div>

Sure:

```
package util

import org.openqa.selenium.JavascriptExecutor
import org.openqa.selenium.WebDriver
import org.openqa.selenium.support.ui.ExpectedCondition
import org.openqa.selenium.support.ui.WebDriverWait

import com.kms.katalon.core.annotation.Keyword
import com.kms.katalon.core.webui.driver.DriverFactory

public class Util {

	@Keyword
	public void waitUntilJQueryReady() {
		new WebDriverWait(DriverFactory.getWebDriver(), 300, 100).until(new ExpectedCondition<Boolean>() {
					@Override
					public Boolean apply(WebDriver d) {
						JavascriptExecutor js = (JavascriptExecutor)d;
						return (Boolean)js.executeScript(
								"return !!window.jQuery && window.jQuery.active == 0");
					}
				});
	}
}

```

Usage would just be:

`CustomKeywords.‘util.Util.waitUntilJQueryReady’()`

---

<div class="post-metadata">

**Author:** ![focis.automation](https://avatars.discourse-cdn.com/v4/letter/f/90ced4/32.png) [@focis.automation](https://forum.katalon.com/u/focis.automation)\
**Post date:** [February 27, 2019, 4:57am UTC](https://forum.katalon.com/t/wait-commands-doesnt-wait-enough-time/16875/15 "2019-02-27T04:57:20Z")

</div>

Thanks a ton Brandon.🙂

_I have quick doubt, Hope you don’t mind my ignorance …_

a. How is it different from built in keyword **_WebUI.waitForJQueryLoad(10)_**  
b. what does **300, 100** mean in new WebDriverWait(DriverFactory.getWebDriver(), 300, 100).

---

<div class="post-metadata">

**Author:** ![Russ\_Thomas](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/russ_thomas/32/11884_2.png) [@Russ\_Thomas](https://forum.katalon.com/u/Russ_Thomas)\
**Post date:** [February 27, 2019, 12:28pm UTC](https://forum.katalon.com/t/wait-commands-doesnt-wait-enough-time/16875/16 "2019-02-27T12:28:15Z")

</div>

While we’re waiting for Brandon to surface…

> [@focis.automation](#):
>
> a. How is it different from built in keyword _**WebUI.waitForJQueryLoad(10)**_

The jQuery.active property is a counter. Each time jQuery starts an asynchronous process, it increments the counter. Each time a process completes (regardless of a success status) it decrements the counter. The code is waiting for the the counter to reach zero, meaning, “it’s finished and ready”.

I don’t know the detail behind waitForJQueryLoad but it certainly doesn’t do what Brandon has given you.

> [@](#):
>
> b. what does **300, 100** mean in new WebDriverWait(DriverFactory.getWebDriver(), 300, 100).

The selenium APIs are documented here:

> **[WebDriverWait](https://www.selenium.dev/selenium/docs/api/java/org/openqa/selenium/support/ui/WebDriverWait.html)**
>
> declaration: package: org.openqa.selenium.support.ui, class: WebDriverWait

---

<div class="post-metadata">

**Author:** ![focis.automation](https://avatars.discourse-cdn.com/v4/letter/f/90ced4/32.png) [@focis.automation](https://forum.katalon.com/u/focis.automation)\
**Post date:** [February 27, 2019, 1:53pm UTC](https://forum.katalon.com/t/wait-commands-doesnt-wait-enough-time/16875/17 "2019-02-27T13:53:02Z")

</div>

Thanks Russ 👍

---

<div class="post-metadata">

**Author:** ![Brandon\_Hein](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/brandon_hein/32/1092_2.png) [@Brandon\_Hein](https://forum.katalon.com/u/Brandon_Hein)\
**Post date:** [February 27, 2019, 5:07pm UTC](https://forum.katalon.com/t/wait-commands-doesnt-wait-enough-time/16875/18 "2019-02-27T17:07:11Z")

</div>

> [@focis.automation](#):
>
> a. How is it different from built in keyword _**WebUI.waitForJQueryLoad(10)**_

You can check the source for the WebUI implementation of this here: [https://github.com/katalon-studio/katalon-studio-testing-framework/blob/0e936ccef5035367f00ffa262e8515cbd533ea9c/Include/scripts/groovy/com/kms/katalon/core/webui/keyword/builtin/WaitForJQueryLoadKeyword.groovy](https://github.com/katalon-studio/katalon-studio-testing-framework/blob/0e936ccef5035367f00ffa262e8515cbd533ea9c/Include/scripts/groovy/com/kms/katalon/core/webui/keyword/builtin/WaitForJQueryLoadKeyword.groovy)

> [@focis.automation](#):
>
> b. what does **300, 100** mean in new WebDriverWait(DriverFactory.getWebDriver(), 300, 100).

Russ has already linked you the WebDriverWait documentation, but to reiterate, the constructor that I used is:

 ![image](https://europe1.discourse-cdn.com/katalon/original/2X/c/ca31e35432c89656b20878c77027af34c7103a35.png)

So the 300 is the timeout in seconds (5 minutes) and the 100 is the polling frequency (aka the sleep time between checking the active jQuery count) in milliseconds.

---

<div class="post-metadata">

**Author:** ![focis.automation](https://avatars.discourse-cdn.com/v4/letter/f/90ced4/32.png) [@focis.automation](https://forum.katalon.com/u/focis.automation)\
**Post date:** [February 28, 2019, 9:21am UTC](https://forum.katalon.com/t/wait-commands-doesnt-wait-enough-time/16875/19 "2019-02-28T09:21:18Z")

</div>

Thanks Brandon 🙂

---

<div class="post-metadata">

**Author:** ![focis.automation](https://avatars.discourse-cdn.com/v4/letter/f/90ced4/32.png) [@focis.automation](https://forum.katalon.com/u/focis.automation)\
**Post date:** [February 28, 2019, 1:22pm UTC](https://forum.katalon.com/t/wait-commands-doesnt-wait-enough-time/16875/20 "2019-02-28T13:22:52Z")

</div>

In case if I use this CustomKeywords.‘util.Util.waitUntilJQueryReady’() for non-Jqery page, the application is running in an infinite loop.

[Next page](https://forum.katalon.com/t/wait-commands-doesnt-wait-enough-time/16875.md?page=2)
