Class BaseElementsCollection<T extends SelenideElement,SELF extends BaseElementsCollection<T,SELF>>

java.lang.Object
com.codeborne.selenide.BaseElementsCollection<T,SELF>
All Implemented Interfaces:
Iterable<T>
Direct Known Subclasses:
ElementsCollection, SelenideAppiumCollection

@ParametersAreNonnullByDefault public abstract class BaseElementsCollection<T extends SelenideElement,SELF extends BaseElementsCollection<T,SELF>> extends Object implements Iterable<T>
  • Constructor Details

    • BaseElementsCollection

      @SafeVarargs protected BaseElementsCollection(CollectionSource collection, T... clazz)
    • BaseElementsCollection

      @SafeVarargs protected BaseElementsCollection(Driver driver, Collection<? extends org.openqa.selenium.WebElement> elements, T... clazz)
    • BaseElementsCollection

      @SafeVarargs protected BaseElementsCollection(Driver driver, String cssSelector, T... clazz)
    • BaseElementsCollection

      @SafeVarargs protected BaseElementsCollection(Driver driver, org.openqa.selenium.By seleniumSelector, T... clazz)
  • Method Details

    • should

      @Nonnull public SELF should(WebElementsCondition... conditions)
      Check if a collection matches given condition(s).

      For example:

       
       $$(".text_list").should(containExactTextsCaseSensitive("text1", "text2"));
       $$(".cat_list").should(allMatch("value==cat", el -> el.getAttribute("value").equals("cat")));
       
       
    • should

      @Nonnull public SELF should(WebElementsCondition condition, Duration timeout)
      Check if a collection matches a given condition within the given time period.
      Parameters:
      timeout - maximum waiting time
    • shouldBe

      @Nonnull public SELF shouldBe(WebElementsCondition... conditions)
      For example: $$(".error").shouldBe(empty)
    • shouldBe

      @Nonnull public SELF shouldBe(WebElementsCondition condition, Duration timeout)
    • shouldHave

      @Nonnull public SELF shouldHave(WebElementsCondition... conditions)
      For example: $$(".error").shouldHave(size(3)) $$(".error").shouldHave(texts("Error1", "Error2"))
    • shouldHave

      @Nonnull public SELF shouldHave(WebElementsCondition condition, Duration timeout)
      Check if a collection matches given condition within given period
      Parameters:
      timeout - maximum waiting time
    • should

      @Nonnull protected SELF should(String prefix, Duration timeout, WebElementsCondition... conditions)
    • waitUntil

      protected void waitUntil(WebElementsCondition condition, Duration timeout)
    • filter

      @CheckReturnValue @Nonnull public SELF filter(WebElementCondition condition)
      Filters collection elements based on the given condition (lazy evaluation)
      Parameters:
      condition - condition
      Returns:
      ElementsCollection
      See Also:
    • create

      protected abstract SELF create(CollectionSource source)
    • filterBy

      @CheckReturnValue @Nonnull public SELF filterBy(WebElementCondition condition)
      Filters collection elements based on the given condition (lazy evaluation)
      Parameters:
      condition - condition
      Returns:
      ElementsCollection
      See Also:
    • exclude

      @CheckReturnValue @Nonnull public SELF exclude(WebElementCondition condition)
      Filters elements excluding those which met the given condition (lazy evaluation)
      Parameters:
      condition - condition
      Returns:
      ElementsCollection
      See Also:
    • excludeWith

      @CheckReturnValue @Nonnull public SELF excludeWith(WebElementCondition condition)
      Filters elements excluding those which met the given condition (lazy evaluation)
      Parameters:
      condition - condition
      Returns:
      ElementsCollection
      See Also:
    • find

      @CheckReturnValue @Nonnull public SelenideElement find(WebElementCondition condition)
      Find the first element which met the given condition (lazy evaluation)
      Parameters:
      condition - condition
      Returns:
      SelenideElement
      See Also:
    • findBy

      @CheckReturnValue @Nonnull public SelenideElement findBy(WebElementCondition condition)
      Find the first element which met the given condition (lazy evaluation)
      Parameters:
      condition - condition
      Returns:
      SelenideElement
      See Also:
    • texts

      @CheckReturnValue @Nonnull public List<String> texts()
      Gets all the texts in elements collection
      See Also:
      • NOT RECOMMENDED Instead of just getting texts, we highly recommend to verify them with $$.shouldHave(texts(...));.
    • attributes

      @CheckReturnValue @Nonnull public List<String> attributes(String attribute)
      Gets all the specific attribute values in elements collection
      See Also:
      • NOT RECOMMENDED Instead of just getting attributes, we highly recommend to verify them with $$.shouldHave(attributes(...));.
    • get

      @CheckReturnValue @Nonnull public T get(int index)
      Gets the n-th element of collection (lazy evaluation)
      Parameters:
      index - 0..N
      Returns:
      the n-th element of collection
      See Also:
    • first

      @CheckReturnValue @Nonnull public SelenideElement first()

      returns the first element of the collection (lazy evaluation)

      NOTICE: Instead of $$(css).first(), prefer $(css) as it's faster and returns the same result

      Returns:
      the first element of the collection
      See Also:
    • last

      @CheckReturnValue @Nonnull public SelenideElement last()
      returns the last element of the collection (lazy evaluation)
      Returns:
      the last element of the collection
      See Also:
    • first

      @CheckReturnValue @Nonnull public SELF first(int elements)
      returns the first n elements of the collection (lazy evaluation)
      Parameters:
      elements - number of elements 1…N
      See Also:
    • last

      @CheckReturnValue @Nonnull public SELF last(int elements)
      returns the last n elements of the collection (lazy evaluation)
      Parameters:
      elements - number of elements 1…N
      See Also:
    • size

      @CheckReturnValue public int size()

      return actual size of the collection, doesn't wait until collection is fully loaded.

      Returns:
      actual size of the collection
      See Also:
    • isEmpty

      public boolean isEmpty()

      return actual state of the collection, doesn't wait until collection is fully loaded.

      Returns:
      actual size of the collection
      See Also:
    • snapshot

      @CheckReturnValue @Nonnull public SELF snapshot()
      Takes the snapshot of current state of this collection. Succeeding calls to this object WILL NOT RELOAD collection element from browser.

      Use it to speed up your tests - but only if you know that collection will not be changed during the test.

      Returns:
      current state of this collection
      See Also:
    • iterator

      @CheckReturnValue @Nonnull public Iterator<T> iterator()
      Does not reload collection elements while iterating it. Not recommended: As a rule, tests should not iterate collection elements. Instead, try to write a CollectionCondition which verifies the whole collection.
      Specified by:
      iterator in interface Iterable<T extends SelenideElement>
      See Also:
      • NOT RECOMMENDED To make it explicit, we recommend to use method #asFixedIterable() or #asDynamicIterable() instead.
    • stream

      @CheckReturnValue @Nonnull public Stream<T> stream()
      Does not reload collection elements while iterating it. Not recommended: As a rule, tests should not iterate collection elements. Instead, try to write a CollectionCondition which verifies the whole collection.
      See Also:
    • asFixedIterable

      @CheckReturnValue @Nonnull public BaseElementsCollection.SelenideElementIterable<T> asFixedIterable()
      Returns a "static" Iterable which doesn't reload web elements during iteration. It's faster than asDynamicIterable()}, but can sometimes can cause StaleElementReferenceException etc. if elements are re-rendered during the iteration.
      Since:
      6.2.0
      See Also:
    • asDynamicIterable

      @CheckReturnValue @Nonnull public BaseElementsCollection.SelenideElementIterable<T> asDynamicIterable()
      Returns a "dynamic" Iterable which reloads web elements during iteration. It's slower than asFixedIterable(), but helps to avoid StaleElementReferenceException etc.
      Since:
      6.2.0
      See Also:
    • as

      @CheckReturnValue @Nonnull public SELF as(String alias)
      Give this collection a human-readable name

      Caution: you probably don't need this method. It's always a good idea to have the actual selector instead of "nice" description (which might be misleading or even lying).

      Parameters:
      alias - a human-readable name of this collection (null or empty string not allowed)
      Returns:
      this collection
      Since:
      5.20.0
    • toString

      @CheckReturnValue public String toString()
      Overrides:
      toString in class Object
    • describe

      @CheckReturnValue @Nonnull public String describe()
      Displays the collection in human-readable format. Useful for logging and debugging. Not recommended to use for test verifications. May work relatively slowly because it fetches actual element information from browser.
      Returns:
      e.g. [Order has been confirmed]
      Since:
      7.4.0
      See Also: