0

I am running a Selenium script. I want to halt the execution of the script for a while. I do not want to use selenium implicit or explicit wait, as I am not waiting for a page transition or an element to appear or a condition to be satisfied. As far as I know,

    Thread.sleep();

is generally used in such cases. Is there any other way to do this other than Thread.sleep()?

Yuga
  • 49
  • 1
  • 2
  • 10
  • 2
    What are you wanting to accomplish? Are you wanting to wait for user input? Are you wanting to wait without exception handling? – Bill Hileman Oct 26 '17 at 20:20
  • Possible duplicate of [Is there a alternative to Thread.sleep in java](https://stackoverflow.com/questions/37938676/is-there-a-alternative-to-thread-sleep-in-java) – timbre timbre Oct 26 '17 at 20:20
  • @BillHileman I just want to see that the page is properly loaded and all the elements are as expected, before any other operation is done. Using Thread.sleep() is giving some performance issues often. So I just want to know if there is an alternative. – Yuga Oct 26 '17 at 20:32

3 Answers3

0

If you want to wait just for the sake of waiting (for example throttling the speed of your test), and not expecting any other condition to happen, then Thread.sleep() is all you have.

If you are looking for an alternative: in the old days, before .sleep(), you would just create a for loop that did nothing:

for(int i = 0; i < 1000000; i++) {
    // wait
}

However, this is not exactly reliable, as you cannot guarantee the duration from one machine to the next. And such code might even get optimized away.

SiKing
  • 10,003
  • 10
  • 39
  • 90
0

This has always worked for me:

public static void waitForPageToLoad() {

        WebDriverWait wait = new WebDriverWait(driver, 15);

        wait.until(new ExpectedCondition<Boolean>() {
            public Boolean apply(WebDriver wdriver) {
                return ((JavascriptExecutor) driver).executeScript(
                    "return document.readyState"
                ).equals("complete");
            }
        }); 

    }
Bill Hileman
  • 2,798
  • 2
  • 17
  • 24
  • So this explicit wait waits until the page is completely loaded. Is that right? Or am i missing anything? – Yuga Oct 26 '17 at 20:42
  • It waits until the document returns the status of :ready" which is not perfect, but it's about as close as you can get. About the only thing that can interfere with it is ajax, which is another story. – Bill Hileman Oct 26 '17 at 20:44
  • 1
    Did this solution I offered work for you? If so, could you please mark it as the accepted answer? – Bill Hileman Nov 14 '17 at 18:46
0

Thread.sleep() seems the only way to accomplish what you specifically requested for.

Here is the appropriate way to implement it:

/**
 * Pause the test to wait for the page to display completely.
 * This is not normally recommended practice, but is useful from time to time.
 */
public void waitABit(final long delayInMilliseconds) {
    try {
        Thread.sleep(delayInMilliseconds);
    } catch (InterruptedException e) {
        LOGGER.warn("Wait a bit method was interrupted.", e);
    }
}
Zeeshan S.
  • 2,041
  • 2
  • 21
  • 40