It is best to use Thread.sleep () or an explicit wait before clicking on any element of the selenium web driver

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

I used to make NosuchElementException and Element often not depend on the exception. Therefore, I solved the script as shown below - before clicking on any element, it will wait for the existence of the element with an explicit expectation. before clicking on any item, it will wait until the item is loaded.

Is it better to wait for each item before clicking?

+5
source share
4 answers

Explicitly expecting a specific element and its specific state to be the best practice in selenium-webdriver. Sleeping places are never a good idea, as your sleep timeout can be less or more and therefore makes your test inconsistent and non-deterministic.

Use WebDriver to wait until it becomes the best solution to synchronize problems. 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'); 
+11
source

Using explicit wait / implicit wait is best practice. Let's check that there is actually an explicit wait, Thread.sleep (), Implicit wait working logic

Explicit Expectation: Explicit Expectation is a kind of expectation of some condition before continuing with the code.

Implicit expectation: Implicit expectation means that WebDriver must poll the DOM for some time, trying to find the element or elements if they are not immediately available. Default value: 0

Thread.sleep () In the sleep code, it will always wait for the mentioned seconds in parentheses even if the work page is ready in 1 second. Thus, it can slow down the tests.

+5
source

Using Explicit Expectation is suggested to wait for any webelement to take place instead of Thread.sleep () as best practice.

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

If you know the exact timeout, you can use Thread.sleep (), but it's better to avoid using it. You can slow down if your waiting time is longer and may fail if your waiting time is shorter.

+2
source
 /* 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. 
0
source

All Articles