Package com.codeborne.selenide
Class ElementsCollection
- All Implemented Interfaces:
Iterable<SelenideElement>
,Collection<SelenideElement>
,List<SelenideElement>
@ParametersAreNonnullByDefault
public class ElementsCollection
extends AbstractList<SelenideElement>
-
Nested Class Summary
-
Field Summary
Fields inherited from class java.util.AbstractList
modCount
-
Constructor Summary
ConstructorDescriptionElementsCollection
(Driver driver, String cssSelector) ElementsCollection
(Driver driver, Collection<? extends org.openqa.selenium.WebElement> elements) ElementsCollection
(Driver driver, org.openqa.selenium.By seleniumSelector) ElementsCollection
(CollectionSource collection) -
Method Summary
Modifier and TypeMethodDescriptionGive this collection a human-readable nameReturns a "dynamic"Iterable
which reloads web elements during iteration.Returns a "static"Iterable
which doesn't reload web elements during iteration.static String
elementsToString
(Driver driver, Collection<org.openqa.selenium.WebElement> elements) Outputs string presentation of the element's collectionFilters elements excluding those which met the given condition (lazy evaluation)excludeWith
(Condition condition) Filters elements excluding those which met the given condition (lazy evaluation)Filters collection elements based on the given condition (lazy evaluation)Filters collection elements based on the given condition (lazy evaluation)Find the first element which met the given condition (lazy evaluation)Find the first element which met the given condition (lazy evaluation)first()
returns the first element of the collection (lazy evaluation)first
(int elements) returns the first n elements of the collection (lazy evaluation)void
forEach
(Consumer<? super SelenideElement> action) Deprecated.get
(int index) Gets the n-th element of collection (lazy evaluation)iterator()
Deprecated.use methodasFixedIterable()
orasDynamicIterable()
instead.last()
returns the last element of the collection (lazy evaluation)last
(int elements) returns the last n elements of the collection (lazy evaluation)listIterator
(int index) Deprecated.use methodasFixedIterable()
orasDynamicIterable()
instead.Deprecated.Use eitherasFixedIterable()
orasDynamicIterable()
instead.should
(CollectionCondition... conditions) Check if a collection matches given condition(s).should
(CollectionCondition condition, Duration timeout) Check if a collection matches a given condition within the given time period.protected ElementsCollection
should
(String prefix, Duration timeout, CollectionCondition... conditions) shouldBe
(CollectionCondition... conditions) For example:$$(".error").shouldBe(empty)
shouldBe
(CollectionCondition condition, Duration timeout) shouldHave
(CollectionCondition... conditions) For example:$$(".error").shouldHave(size(3))
$$(".error").shouldHave(texts("Error1", "Error2"))
shouldHave
(CollectionCondition condition, Duration timeout) Check if a collection matches given condition within given periodint
size()
return actual size of the collection, doesn't wait on collection to be loaded.snapshot()
Takes the snapshot of current state of this collection.stream()
Deprecated.Use eitherasFixedIterable()
orasDynamicIterable()
instead.texts()
Gets all the texts in elements collectiontexts
(Collection<org.openqa.selenium.WebElement> elements) Fail-safe method for retrieving texts of given elements.Object[]
toArray()
Deprecated.use methodasFixedIterable()
orasDynamicIterable()
instead.toString()
protected void
waitUntil
(CollectionCondition condition, Duration timeout) Methods inherited from class java.util.AbstractList
add, add, addAll, clear, equals, hashCode, indexOf, lastIndexOf, listIterator, remove, removeRange, set, subList
Methods inherited from class java.util.AbstractCollection
addAll, contains, containsAll, isEmpty, remove, removeAll, retainAll, toArray
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.Collection
removeIf, toArray
Methods inherited from interface java.util.List
addAll, contains, containsAll, isEmpty, remove, removeAll, replaceAll, retainAll, sort, spliterator, toArray
-
Constructor Details
-
ElementsCollection
-
ElementsCollection
public ElementsCollection(Driver driver, Collection<? extends org.openqa.selenium.WebElement> elements) -
ElementsCollection
-
ElementsCollection
-
-
Method Details
-
should
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
Check if a collection matches a given condition within the given time period.- Parameters:
timeout
- maximum waiting time
-
shouldBe
For example:$$(".error").shouldBe(empty)
-
shouldBe
-
shouldHave
For example:$$(".error").shouldHave(size(3))
$$(".error").shouldHave(texts("Error1", "Error2"))
-
shouldHave
Check if a collection matches given condition within given period- Parameters:
timeout
- maximum waiting time
-
should
@Nonnull protected ElementsCollection should(String prefix, Duration timeout, CollectionCondition... conditions) -
waitUntil
-
filter
Filters collection elements based on the given condition (lazy evaluation)- Parameters:
condition
- condition- Returns:
- ElementsCollection
- See Also:
-
filterBy
Filters collection elements based on the given condition (lazy evaluation)- Parameters:
condition
- condition- Returns:
- ElementsCollection
- See Also:
-
exclude
Filters elements excluding those which met the given condition (lazy evaluation)- Parameters:
condition
- condition- Returns:
- ElementsCollection
- See Also:
-
excludeWith
Filters elements excluding those which met the given condition (lazy evaluation)- Parameters:
condition
- condition- Returns:
- ElementsCollection
- See Also:
-
find
Find the first element which met the given condition (lazy evaluation)- Parameters:
condition
- condition- Returns:
- SelenideElement
- See Also:
-
findBy
Find the first element which met the given condition (lazy evaluation)- Parameters:
condition
- condition- Returns:
- SelenideElement
- See Also:
-
texts
Gets all the texts in elements collection- See Also:
-
texts
@CheckReturnValue @Nonnull public static List<String> texts(@Nullable Collection<org.openqa.selenium.WebElement> elements) Fail-safe method for retrieving texts of given elements.- Parameters:
elements
- Any collection of WebElements- Returns:
- Texts (or exceptions in case of any WebDriverExceptions)
-
elementsToString
@CheckReturnValue @Nonnull public static String elementsToString(Driver driver, @Nullable Collection<org.openqa.selenium.WebElement> elements) Outputs string presentation of the element's collection- Parameters:
elements
- elements of string- Returns:
- e.g.
"[<h1>foo</h1>, <h2>bar</h2>]"
- See Also:
-
get
Gets the n-th element of collection (lazy evaluation)- Specified by:
get
in interfaceList<SelenideElement>
- Specified by:
get
in classAbstractList<SelenideElement>
- Parameters:
index
- 0..N- Returns:
- the n-th element of collection
- See Also:
-
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
returns the last element of the collection (lazy evaluation)- Returns:
- the last element of the collection
- See Also:
-
first
returns the first n elements of the collection (lazy evaluation)- Parameters:
elements
- number of elements 1..N- See Also:
-
last
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 on collection to be loaded.
- Specified by:
size
in interfaceCollection<SelenideElement>
- Specified by:
size
in interfaceList<SelenideElement>
- Specified by:
size
in classAbstractCollection<SelenideElement>
- Returns:
- actual size of the collection
- See Also:
-
stream
Deprecated.Use eitherasFixedIterable()
orasDynamicIterable()
instead.Not recommended: As a rule, tests should not iterate collection elements. Instead, try to write aCollectionCondition
which verifies the whole collection.- See Also:
-
parallelStream
Deprecated.Use eitherasFixedIterable()
orasDynamicIterable()
instead.Not recommended: As a rule, tests should not iterate collection elements. Instead, try to write aCollectionCondition
which verifies the whole collection.- See Also:
-
forEach
Deprecated.Use eitherasFixedIterable()
orasDynamicIterable()
instead.Not recommended: As a rule, tests should not iterate collection elements. Instead, try to write aCollectionCondition
which verifies the whole collection.- See Also:
-
iterator
Deprecated.use methodasFixedIterable()
orasDynamicIterable()
instead.Does not reload collection elements while iterating it. Not recommended: As a rule, tests should not iterate collection elements. Instead, try to write aCollectionCondition
which verifies the whole collection.- Specified by:
iterator
in interfaceCollection<SelenideElement>
- Specified by:
iterator
in interfaceIterable<SelenideElement>
- Specified by:
iterator
in interfaceList<SelenideElement>
- Overrides:
iterator
in classAbstractList<SelenideElement>
- See Also:
-
listIterator
Deprecated.use methodasFixedIterable()
orasDynamicIterable()
instead.Does not reload collection elements while iterating it. Not recommended: As a rule, tests should not iterate collection elements. Instead, try to write aCollectionCondition
which verifies the whole collection.- Specified by:
listIterator
in interfaceList<SelenideElement>
- Overrides:
listIterator
in classAbstractList<SelenideElement>
- See Also:
-
toArray
Deprecated.use methodasFixedIterable()
orasDynamicIterable()
instead.- Specified by:
toArray
in interfaceCollection<SelenideElement>
- Specified by:
toArray
in interfaceList<SelenideElement>
- Overrides:
toArray
in classAbstractCollection<SelenideElement>
- See Also:
-
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:
-
asFixedIterable
Returns a "static"Iterable
which doesn't reload web elements during iteration. It's faster thanasDynamicIterable()
()}, but can sometimes can causeStaleElementReferenceException
etc. if elements are re-rendered during the iteration.- Since:
- 6.2.0
- See Also:
-
asDynamicIterable
Returns a "dynamic"Iterable
which reloads web elements during iteration. It's slower thanasFixedIterable()
, but helps to avoidStaleElementReferenceException
etc.- Since:
- 6.2.0
- See Also:
-
as
Give this collection a human-readable nameCaution: 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
- Overrides:
toString
in classAbstractCollection<SelenideElement>
-
asFixedIterable()
orasDynamicIterable()
instead.