How do I perform a static wait in selenium using java?

3.1k Views Asked by At

It's pretty frustrating. Everywhere I look people keep telling me to use explicit, implicit, and fluent waits. These waits make it so you pause based on elements. However, patronizing us and removing tools and options is not a good idea. In my current specific pickle, I have a button that is designed to disappear if someone clicks on it too quickly. However, it is also designed to now show up immediately either. So you have to wait until it shows up, then wait for enough time to pass by, and only then click the button. With the suggestions, I am given it is impossible. A static pause or sleep has to be used in this case. For some reason, I can't even use thread sleep because it seems like it has been deprecated in Java 8 itself or something.

3

There are 3 best solutions below

2
On

What's the source that makes you think, that java.lang.Thread would be deprecated?

So even in the Java 11 docs it is not deprecated.

So if you want to use it, feel free. :-)

2
On

Possibly through the terms static wait and static pause you meant Thread.sleep() which is still useful in many ways.

However, while executing your Tests through Selenium inducing sleep wouldn't be an elegant solution to fix the issue as inducing Thread.sleep(1000); degrades the overall Test Execution Performance. You can find a detailed discussion in Selenium needs a sleep before going to the next page.

If you need to induce waits Implicit Waits is a good way to start with. You can find a detailed discussion in Using implicit wait in selenium. However as the current Web Applications are built through JavaScript, Angular, ReactJS, etc Explicit Waits would be the way forward.

So, moving forward you can make a transition towards Explicit Waits. You can find a detailed discussion in Replace implicit wait with explicit wait (selenium webdriver & java).

At this point, implementing Fluent Wait will be much easier and you can find a detailed discussion in Implicit vs Explicit vs Fluent Wait.

0
On

implicit Wait:

driver.manage().timeouts().implicitlyWait(TimeOut, TimeUnit.SECONDS);   

Explicit Wait:

WebDriverWait wait = new WebDriverWait(WebDriverRefrence,TimeOut);