Is it best practice to use Thread.sleep() or explicit wait before click on any element in selenium web driver
Asked Answered
L

4

5

I am new to web driver and I wrote a selenium script for web application which contains backbone.js and select2.

I used to get NosuchElementException and Element is not clickable exceptions often. So I have decided to script as below, - before clicking on any element, it will wait for existence of element using explicit wait. i.e before clicking any element , it will wait until the element is loaded.

Is it best practice to wait for each element before clicking?

Lorrainelorrayne answered 22/6, 2015 at 17:52 Comment(0)
V
12

Explicitly waiting for a certain element and its certain state is the best practice in selenium-webdriver. Sleeps are never a good idea since your sleep timeout could be less or more and therefore makes your test inconsistent and non-deterministic.

Using WebDriver wait until is the best solution for synchronizing issues. So in JS something like this,

var until = webdriver.until;
var searchBox = 
driver.wait(until.elementIsEnabled(driver.findElement(webdriver.By.name('q'))),5000,'Search button is not enabled');  
Vibration answered 22/6, 2015 at 18:21 Comment(0)
D
6

Using explicit wait/implicit wait is the best practice, Lets check what actually the explicit wait,Thread.sleep(), Implicit wait's working logic

Explicit wait: An explicit waits is a kind of wait for a certain condition to occur before proceeding further in the code.

Implicit wait: An implicit wait is to tell WebDriver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available. The default setting is 0

Thread.sleep() In sleep code will always wait for mentioned seconds in side the parentheses even in case working page is ready after 1 sec. So this can slow the tests.

Disloyal answered 6/10, 2015 at 6:57 Comment(0)
C
2

Using Explicit wait is suggested for waiting to perform any action on webelement instead of Thread.sleep() as best practice.

Explicit wait in Selenium is equivalent to Thread.sleep() with a specified condition. That means even if you used either Explicit or Implicit wait, you indirectly used Thread.sleep(). The difference is that you specify the conditions for your wait and know when to throw error if your wait is timeout.

If you know the exact time for your wait Thread.sleep() can be used but better avoid using it. You test may slow down if your wait time is more and may fail if your wait time is less.

Catrinacatriona answered 23/6, 2015 at 7:29 Comment(0)
E
0

  /* waitForTitle
   * Before doing anything, make sure the actual title on the page we are visiting is the expected Title.
   * @param {int} timeout
   * @param {string} expectedTitle
   */
  async waitForTitle(timeout, expectedTitle) {
    let isTitle;
    this.printLog('pageBase: wait for expected Page Title to be present when visiting a Page');
    await this.driver.wait(async () => {
      const actualTitle = await this.driver.getTitle();
      isTitle = actualTitle;
      return actualTitle.includes(expectedTitle);
    }, timeout);
    return isTitle.includes(expectedTitle);
  }

//Now continue your test and do your assertion.




Exergue answered 10/10, 2019 at 16:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.