kgress / scaffold

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

findElements returns the first child for every child available instead of distinct instances of all children #138

Closed hebbmcclure closed 2 years ago

hebbmcclure commented 2 years ago

Bug

BaseWebElement::findElements(elementClass, by) returns of list of the first child element that matches the selector repeated the number of children times.

Example: Given the following HTML:

<html lang="en">
<body>
<div class="reservation-container">
  <div><h5 class="reservation-header">Reservations</h5>
    <div class="reservation-cards-container">
      <div class="reservation-card-container">A</div>
      <div class="reservation-card-container">B</div>
      <div class="reservation-card-container">C</div>
    </div>
  </div>
</div>
</body>
</html>

When executing someElement.findElements(DivWebElement.class, By.cssSelector("div.reservation-card-container")); a list returned with three DivWebElements that represent the first child <div class="reservation-card-container">A</div>

Expected

Add a clear and concise description of what you expected to happen. Using the same Html quoted above, the following code, should produce a list containing DivWebElements for A, B & C someElement.findElements(DivWebElement.class, By.cssSelector("div.reservation-card-container"));

Repro

Here's a UnitTest that reproduces the bug.

  @Test
  void testFindElementsFix() {
    var mockUnitTest = "Mock Unit Test " + getUniqueString();
    desiredCapabilities.setRunType(RunType.HEADLESS);
    TestWebDriverManager testWebDriverManager = new TestWebDriverManager(desiredCapabilities,
        seleniumGridRestTemplate);
    TestContext.baseContext().setContext(testWebDriverManager, mockUnitTest);
    testWebDriverManager.initDriver_fromParent(mockUnitTest);
    WebDriverWrapper testWebDriverWrapper = testWebDriverManager.getWebDriverWrapper_fromParent();
    WebDriver baseWebDriver = testWebDriverWrapper.getBaseWebDriver();
    assertThat(baseWebDriver).isNotNull();
    testWebDriverWrapper.get(String.format("data:text/html;charset=utf-8,%s",
        "<html lang=\"en\">\n"
            + "<body>\n"
            + "<div class=\"reservation-container\">\n"
            + "  <div><h5 class=\"reservation-header\">Reservations</h5>\n"
            + "    <div class=\"reservation-cards-container\">\n"
            + "      <div class=\"reservation-card-container\">A</div>\n"
            + "      <div class=\"reservation-card-container\">B</div>\n"
            + "      <div class=\"reservation-card-container\">C</div>\n"
            + "    </div>\n"
            + "  </div>\n"
            + "</div>\n"
            + "</body>\n"
            + "</html>"));
    DivWebElement parentElement = new DivWebElement(By.cssSelector("div.reservation-container"));
    assertThat(parentElement).isNotNull();
    // now assert Order of child elements is what we expect...
    List<DivWebElement> reservationCards = parentElement.findElements(DivWebElement.class,
        By.cssSelector("div.reservation-card-container"));
    IntStream.range(0, reservationCards.size()).forEach((i) -> {
      switch(i) {
        case 0:
          assertThat(reservationCards.get(i).getText()).isEqualTo("A");
          break;
        case 1:
          assertThat(reservationCards.get(i).getText()).isEqualTo("B");
          break;
        case 2:
          assertThat(reservationCards.get(i).getText()).isEqualTo("C");
          break;
      }
    });
    baseWebDriver.close();
    desiredCapabilities.setRunType(RunType.UNIT);