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

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

      @CanIgnoreReturnValue 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

      @CanIgnoreReturnValue 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

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

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

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

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

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

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

      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

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

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

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

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

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

      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

      public List<@Nullable 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

      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

      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

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

      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

      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

      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

      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

      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

      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

      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.
      See Also:
    • asDynamicIterable

      Returns a "dynamic" Iterable which reloads web elements during iteration. It's slower than asFixedIterable(), but helps to avoid StaleElementReferenceException etc.
      See Also:
    • as

      @CanIgnoreReturnValue 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
    • toString

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

      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: