Package com.codeborne.selenide
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>
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
-
Constructor Summary
ModifierConstructorDescriptionprotected
BaseElementsCollection
(Driver driver, String cssSelector, T... clazz) protected
BaseElementsCollection
(Driver driver, Collection<? extends org.openqa.selenium.WebElement> elements, T... clazz) protected
BaseElementsCollection
(Driver driver, org.openqa.selenium.By seleniumSelector, T... clazz) protected
BaseElementsCollection
(CollectionSource collection, T... clazz) -
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.attributes
(String attribute) Gets all the specific attribute values in elements collectionprotected abstract SELF
create
(CollectionSource source) describe()
Displays the collection in human-readable format.exclude
(WebElementCondition condition) Filters elements excluding those which met the given condition (lazy evaluation)excludeWith
(WebElementCondition condition) Filters elements excluding those which met the given condition (lazy evaluation)filter
(WebElementCondition condition) Filters collection elements based on the given condition (lazy evaluation)filterBy
(WebElementCondition condition) Filters collection elements based on the given condition (lazy evaluation)find
(WebElementCondition condition) Find the first element which met the given condition (lazy evaluation)findBy
(WebElementCondition condition) 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)get
(int index) Gets the n-th element of collection (lazy evaluation)boolean
isEmpty()
return actual state of the collection, doesn't wait until collection is fully loaded.iterator()
Does not reload collection elements while iterating it.last()
returns the last element of the collection (lazy evaluation)last
(int elements) returns the last n elements of the collection (lazy evaluation)should
(WebElementsCondition... conditions) Check if a collection matches given condition(s).should
(WebElementsCondition condition, Duration timeout) Check if a collection matches a given condition within the given time period.protected SELF
should
(String prefix, Duration timeout, WebElementsCondition... conditions) shouldBe
(WebElementsCondition... conditions) For example:$$(".error").shouldBe(empty)
shouldBe
(WebElementsCondition condition, Duration timeout) shouldHave
(WebElementsCondition... conditions) For example:$$(".error").shouldHave(size(3))
$$(".error").shouldHave(texts("Error1", "Error2"))
shouldHave
(WebElementsCondition condition, Duration timeout) Check if a collection matches given condition within given periodint
size()
return actual size of the collection, doesn't wait until collection is fully loaded.snapshot()
Takes the snapshot of current state of this collection.stream()
Does not reload collection elements while iterating it.texts()
Gets all the texts in elements collectiontoString()
protected void
waitUntil
(WebElementsCondition condition, Duration timeout) Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Constructor Details
-
BaseElementsCollection
-
BaseElementsCollection
@SafeVarargs protected BaseElementsCollection(Driver driver, Collection<? extends org.openqa.selenium.WebElement> elements, T... clazz) -
BaseElementsCollection
-
BaseElementsCollection
@SafeVarargs protected BaseElementsCollection(Driver driver, org.openqa.selenium.By seleniumSelector, T... clazz)
-
-
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
-
waitUntil
-
filter
Filters collection elements based on the given condition (lazy evaluation)- Parameters:
condition
- condition- Returns:
- ElementsCollection
- See Also:
-
create
-
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:
-
- NOT RECOMMENDED
Instead of just getting texts, we highly recommend to verify them with
$$.shouldHave(texts(...));
.
- NOT RECOMMENDED
Instead of just getting texts, we highly recommend to verify them with
-
attributes
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(...));
.
- NOT RECOMMENDED
Instead of just getting attributes, we highly recommend to verify them with
-
get
Gets the n-th element of collection (lazy evaluation)- 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
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
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
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 interfaceIterable<T extends SelenideElement>
- See Also:
-
- NOT RECOMMENDED To make it explicit, we recommend to use method #asFixedIterable() or #asDynamicIterable() instead.
-
stream
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.- 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.- See Also:
-
asDynamicIterable
Returns a "dynamic"Iterable
which reloads web elements during iteration. It's slower thanasFixedIterable()
, but helps to avoidStaleElementReferenceException
etc.- 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
-
toString
-
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:
-