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?
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.
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.
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.
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.
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');
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With