kgress / scaffold

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

nth-child invalid empty sibling removal in buildComponentList() #137

Closed Proryanator closed 2 years ago

Proryanator commented 2 years ago

Summary

buildComponentList() is super useful, and uses nth-child for creating really nice lists of components. However if a website is designed poorly, the nth-child index logic can pull the wrong elements.

I believe we can do an instant validation on the nth-child index that scaffold would choose to build the component list to filter out indices that would produce empty/null components.

<div class="thing">
   <input class="input">
</div>
<div></div>
<div class="thing">
   <input class="input">
</div>
<div></div>

With the way that things work as is, passing a list of elements with css selector .thing which produces a list of 2 elements via findElements(), scaffold will create the following selectors:

.thing:nth-child(1) .thing:nth-child(2)

Due to how nth-child functions, this will actually select the first and second <div> that does not have that class .thing, breaking the css selector filtering process and giving unusable component results in the list.

This will also make the list builder more robust to bad website design.

A/C

buildComponentList() should do some validation that, for each nth-child component it intends to build, that it does not include siblings that do not match the passed in locator.

Some unit tests around this functionality, with a website that has this bad design would be nice, perhaps a local cached *.html page that has the exact situations we're trying to address.