Using WebDriverWait to verify a performed action versus assertions in JUnit



  • 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?

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


  • 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 visibilityOfElementLocated(
          final By locator) {
        return new ExpectedCondition() {
          @Override
          public WebElement apply(WebDriver driver) {
            try {
              return elementIfVisible(findElement(locator, driver));
            } catch (StaleElementReferenceException e) {
              return null;
            }
          }
        };
      }
    


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2