Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 288
Jugi Avatar asked Jun 22 '15 17:06

Jugi


People also ask

Is it good to use thread sleep in Selenium?

If you use Thread. sleep while performing Selenium test automation, it will stop the execution of the script for the time specified in the script, irrespective of the fact that the element on the web page has been found. Selenium waits do not wait for the complete duration of time.

Why is implicit wait better than thread sleep?

Implicit Wait For Automation Testing with Selenium The key point to note here is, unlike Thread. sleep(), it does not wait for the complete duration of time. In case it finds the element before the duration specified, it moves on to the next line of code execution, thereby reducing the time of script execution.

Why thread sleep is not recommended?

If given a wait of 5000 Milliseconds(5 seconds) and an element just take just 1-2 seconds to load, script will still wait for another 3 seconds which is bad as it is unnecessarily increasing the execution time. So thread. sleep() increases the execution time in cases where elements are loaded in no due time.

Which Selenium wait is better?

The best practice to wait for a change in Selenium is to use the synchronization concept. The implicit and explicit waits can be used to handle a wait. The implicit is a global wait applied to every element on the page. The default value of implicit wait is 0.


3 Answers

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');  
like image 180
nilesh Avatar answered Oct 30 '22 02:10

nilesh


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.

like image 37
Kumrun Nahar Keya Avatar answered Oct 30 '22 02:10

Kumrun Nahar Keya


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.

  • WebDriverWait class uses Sleeper class for that purpose.
  • Sleeper class implementation.
like image 36
Manu Avatar answered Oct 30 '22 03:10

Manu