List Filtering

Example 9

Although the get("id") method in most cases eliminates the need for filtering, there are situations when it is required to sort list elements by a specific attribute — for example, by the presence of a certain CSS class on the element. For such cases, filtering must be applied.

In one of the previous examples, where we clicked on buttons in a list, instead of obtaining a button by its "id", you can, as in pure Selenide, apply filtering:

@Test
@Feature("Simple elements list")
@DisplayName("Walk through the lists of simple elements")
public void fillListOfSimpleInputElements() {
    ...
    simpleListsPage.listBirdNameButtons().get("Sparrow").click();                            // this way we chose a button by "id"
    simpleListsPage.listBirdNameButtons().filter(Condition.text("Sparrow")).get(0).click();  // this way we found it via filtering
}

The result of these steps will appear identical in the report.

But what if you take a more complex case, for example like this:

accordionPage.accordionSections().filter(Condition.cssClass("card")).assertSize(5);

When the assertSize(5) method is called on the list object, an event is intercepted and a step is recorded in the report indicating that the list's size has been checked to be 5. However, in this case, there is a sub-step of filtering in the call chain that changes the nature of the check, which the assertSize method is unaware of.

Thus, one might expect the report to show a standard entry, for example:

Assert that the <Accordion categories> list's size is 5

To prevent this, the filtering method returns a clone of the current list that retains all its attributes, but contains only the filtered entries along with a marker that filtering was applied. After that, when any assertion is called, the framework will know that the list was filtered and will log an entry in the report such as:

Assert that the <Accordion categories, filtered by: css class "card"> list's size is 5

After the list name Accordion categories, records of all applied filters are appended.

Let's see a few more examples with filtering:

@Test
@DisplayName("Filtration examples")
public void listFiltering() {
    UiSteps.openBrowser(accordionPageUrl);
    AccordionPage.AccordionSection accordionSection = accordionPage.accordionSections()
                                                            .filter(Condition.text("Chapter 2")).get(0);
    accordionSection.title().click();
    accordionPage.accordionSections().filter(Condition.visible).assertSize(5);
    accordionPage.accordionSections().filter(Condition.cssClass("card")).assertSize(5);
    accordionPage.accordionSections().filter(Condition.name("some")).assertSize(0);
    accordionPage.accordionSections()
            .filter(Condition.visible)
            .filter(Condition.cssClass("card"))
            .filter(Condition.text("Chapter 2"))
            .assertSize(1);
}
Filtration report

Thus, the filtering capabilities are in no way limited, while remaining easy to read.