0

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")));
}

4 Answers 4

1

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;
        }
      }
    };
  }
0

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.

0

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();
        }
    });
0
public static void WaitForData (IWebDriver driver)


 {

            var dt1 = DateTime.Now;

            var diff = 0;

            while (diff < 3)
            {

                var dt2 = DateTime.Now;
                var ts = dt2.Subtract(dt1);
                diff = (int) ts.TotalSeconds;

            }
        }
1
  • 1
    Hi, John. On such an old post with other answers, it's better if you can include more than just a code sample. Could you explain why this code will solve the OP's problem in a way that is better than the other answers?
    – Kate Paulk
    Feb 23, 2015 at 11:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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