Submit and vote on feature ideas.

Welcome to the new Parasoft forums! We hope you will enjoy the site and try out some of the new features, like sharing an idea you may have for one of our products or following a category.

Managing wait conditions in created tests

As a human, when I use a website, I look around for specific elements then I click on those buttons when they are “ready”. I do this because I am able to see when elements are available to be interacted with in the proper way. In an automated framework it’s a little more difficult for the machine to know when to do things vs when to wait. The machine is going to execute the Selenium test as a series of steps. If it is executing a shopping cart test against Amazon it may come to a point where the next step is to, “add an item to cart”, and then click on the cart button. The problem is that the cart button might not be available immediately after the item is added there may be a moment where the icon changes. The test script needs to know exactly the right amount of time to wait or conditions to look for in order to feel confident moving forward. Recorders have a very hard time understanding and applying the proper wait conditions in our test scripts, so we have to manually build in sophisticated wait conditions.

Here is how to make that less painful

Selenium supports implicit and explicit waits

  • Implicit waits – a global setting that tells the WebDriver to wait for a specific period of time when finding an element. This is generally applied to all elements found by the driver
  • Explicit waits – wait conditions that are set on individual element that instruct WebDriver to wait until a specific condition is met. Such as waiting for an element to be present or visible along wit a maximum time to wait for the condition to be met

Try to use explicit waits whenever possible. This gives you greater control over your tests by fine tuning wait conditions to page events.
Even when using explicit waits, sometimes particular exceptions are thrown by Selenium that interrupt the wait condition - but you actually want the wait condition to continue in those cases. You handle this by teaching the explicit waits to ignore particular exceptions. Some common ones are StaleElementReferenceException, NoSuchElementException, ElementClickInterceptedException. Here is a good example of an explicit wait

WebDriverWait wait = new WebDriverWait(driver, DEFAULT_WAIT_FOR_ELEMENT_TIMEOUT); wait.ignoring(StaleElementReferenceException.class); wait.until(webdriver -> new Select(element).getFirstSelectedOption().getText().trim().length() > 0);