kgress / scaffold

A Java based Selenium WebDriver abstraction
MIT License
4 stars 7 forks source link

verifyIsOnPage() not throwing error when passing a nonexistent element #130

Closed atari-xia closed 2 years ago

atari-xia commented 2 years ago

Bug

When calling verifyIsOnPage() with a nonexistent element, the lookup fails but then the test continues In BasePage.verifyIsOnPage() we see:

    // Wait until the page is loaded then look for the elements
    var isPageLoaded = getAutomationWait().waitUntilPageIsLoaded();
    if (isPageLoaded) {
      listOfElements.forEach(elementOnPage -> {
        try {
          elementOnPage.isDisplayed();
        } catch (TimeoutException e) {
          throw new TimeoutException(
              String.format("Page verification failed. Could not find the element " +
                  "%s for the intended page: %s", elementOnPage, getClass().getSimpleName()));
        }
      });

and in BaseWebElement:

  public boolean isDisplayed() {
    try {
      var element = getRawWebElement();
      return element != null && element.isDisplayed();
    } catch (WebDriverException e) {
      return false;
    }
  }

I think the issue is that the WebDriverException here is being caught, and the return value of elementOnPage.isDisplayed() is never being verified.

Expected

A TimeoutException should be thrown and the test execution should stop

Repro

package com.demo;

import io.github.kgress.scaffold.BasePage;
import io.github.kgress.scaffold.webelements.DivWebElement;
import lombok.Getter;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.TimeoutException;

import static org.junit.jupiter.api.Assertions.assertThrows;

class MyTest extends BaseTest {
  @Test
  void demo() {
    getWebDriverWrapper().get("https://www.saucedemo.com");
    assertThrows(TimeoutException.class, Demo::new);
  }

  @Getter
  class Demo extends BasePage {
    private final DivWebElement nonexistent = new DivWebElement("#nonexistent #element");
    public Demo() { verifyIsOnPage(getNonexistent()); }
  }
}