Take the 2-minute tour ×
Software Quality Assurance & Testing Stack Exchange is a question and answer site for software quality control experts, automation engineers, and software testers. It's 100% free, no registration required.

Can someone tell me if by using the following code is sufficient enough to verify that the actions performed have taken place. So by using a wait command for certain elements to be visible in the DOM, instead of using Assert.assertEquals() (or similar), is it valid? Or would there be a better approach?

From my understanding, asserting an action is the way to prove the actions performed have been successful. Although if I was to wait for an element to be visible, is this somewhat the same?

@Test
public void openButton(){

    WebDriverWait wait = new WebDriverWait(chrome, 30);

    wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div.menu.navButton")));

    WebElement menuBtn = chrome.findElementByCssSelector("div.menu.navButton");
    menuBtn.click();

    wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div.menuPopup.t-menuPopup-A")));

    WebElement openBtn = chrome.findElementByCssSelector("div.menuItem.f-feature-A.j-openProject");
    openBtn.click();

    wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("input.t-searchInput-A.j-treeFilter-input[placeholder='Search']")));

    WebElement xBtn = chrome.findElementByCssSelector("p.closeButton");
    xBtn.click();

    wait.until(ExpectedConditions.invisibilityOfElementLocated(By.cssSelector("p.closeButton")));
}
share|improve this question
add comment

3 Answers

Yes, using WebDriverWait is sufficent in the case where you want to know if your code can proceed to next steps (ie some button being visible). There is no point in doing something like

wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div.menu.navButton")));
Assert.assertTrue(driver.findElement(By.cssSelector("div.menu.navButton")).isDisplayed());

as the ExpectedConditions class is already checking this (and actually checks even more).

What the until method of the WebDriverWait class is doing: Repeatedly applies this instance's input value to the given function until one of the following occurs:

the function returns neither null nor false,
the function throws an unignored exception,
the timeout expires,
the current thread is interrupted

Also, look into inner working of expected conditions to better understand what they are doing, ie:

  /**
   * An expectation for checking that an element is present on the DOM of a page
   * and visible. Visibility means that the element is not only displayed but
   * also has a height and width that is greater than 0.
   *
   * @param locator used to find the element
   * @return the WebElement once it is located and visible
   */
  public static ExpectedCondition<WebElement> visibilityOfElementLocated(
      final By locator) {
    return new ExpectedCondition<WebElement>() {
      @Override
      public WebElement apply(WebDriver driver) {
        try {
          return elementIfVisible(findElement(locator, driver));
        } catch (StaleElementReferenceException e) {
          return null;
        }
      }
    };
  }
share|improve this answer
add comment

According to the Selenium documentation, the Wait function is actually activated on all element lookups, but by default has a timeout of 0s. This can be changed to some other value, eg 10 seconds like so

WebDriver driver = new FirefoxDriver();
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
driver.Url = "http://somedomain/url_that_delays_loading";
IWebElement myDynamicElement = driver.FindElement(By.Id("someDynamicElement"));

This indicates to me that your thinking is sound as it mimicks the behaviour of the libary itself, and by using the ImplicitlyWait function you'd be able to make your code neater and easier to read, at the possibly expense of waiting for elements that never appear.

share|improve this answer
add comment

We have found this works better than the waits:

    new WebDriverWait(driver, 5).until(new Predicate<WebDriver>() {
        public boolean apply(WebDriver d) {
            return d.findElement(By.id("your id")).isDisplayed();
        }
    });
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.