Package com.workfusion.rpa.helpers
Class UiElementCollection
- All Implemented Interfaces:
Iterable<UiElement>,Collection<UiElement>,List<UiElement>,SequencedCollection<UiElement>
This class wraps a list of
UiElement and provides a new functionality.
A UiElementCollection is an immutable, List-compatible view over the
elements matched by a selector (created via RPA.$$(...)). On top of the standard list
operations it adds:
- positional access —
first(),last(),get(int); - assertions over the whole collection —
shouldHave(UiCollectionCondition...),shouldBe(UiCollectionCondition...),shouldHaveSize(int); - fluent waiting —
waitUntil(UiCollectionCondition)/waitWhile(UiCollectionCondition); - filtering —
filter(UiCondition),exclude(UiCondition),find(UiCondition); - bulk text extraction —
getTexts().
When constructed from a By selector, the matching elements are resolved lazily so that
waitUntil/waitWhile can re-query the page on each polling iteration.
- Samples
$$(".errorMessage").first()shouldBe(visible, enabled);$$("td").shouldHaveSize(5);$$(".edit").getTexts();
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic StringelementsToString(List<? extends com.workfusion.rpa.internal.api.element.WebElement> elements) Builds a human-readable, multi-line string representation of an element collection.exclude(com.workfusion.rpa.helpers.conditionsnative.UiCondition condition) Returns a new collection with the elements that match the given condition removed (the inverse offilter(UiCondition)).excludeWith(com.workfusion.rpa.helpers.conditionsnative.UiCondition condition) Synonym forexclude(UiCondition); reads more naturally in some chains.filter(com.workfusion.rpa.helpers.conditionsnative.UiCondition condition) Returns a new collection containing only the elements that match the given condition.filterBy(com.workfusion.rpa.helpers.conditionsnative.UiCondition condition) Synonym forfilter(UiCondition); reads more naturally in some chains.find(com.workfusion.rpa.helpers.conditionsnative.UiCondition condition) Returns the first element that matches the given condition.findBy(com.workfusion.rpa.helpers.conditionsnative.UiCondition condition) Synonym forfind(UiCondition); reads more naturally in some chains.first()get(int index) Returns the element at the given position in the collection.String[]getTexts()Returns the visible text of every element in the collection, in order.static String[]getTexts(Collection<? extends com.workfusion.rpa.internal.api.element.WebElement> elements) Fail-safe method for retrieving texts of given elements.iterator()last()listIterator(int index) Returns a list iterator over the elements of this collection, starting at the given position.shouldBe(com.workfusion.rpa.helpers.conditionsnative.collection.UiCollectionCondition... conditions) Asserts that the collection meets all the given conditions immediately (no waiting).shouldHave(com.workfusion.rpa.helpers.conditionsnative.collection.UiCollectionCondition... conditions) Asserts that the collection meets all the given conditions immediately (no waiting).shouldHaveSize(int expectedSize) Asserts that the collection currently contains exactly the given number of elements.intsize()toString()Returns a human-readable, multi-line representation of the collection, intended for logging and debugging.waitUntil(com.workfusion.rpa.helpers.conditionsnative.collection.UiCollectionCondition condition) Wait until given collection meets given conditions.waitUntil(com.workfusion.rpa.helpers.conditionsnative.collection.UiCollectionCondition condition, long timeoutMilliseconds) Wait until given collection meets given conditions.waitUntil(com.workfusion.rpa.helpers.conditionsnative.collection.UiCollectionCondition condition, long timeoutMilliseconds, long pollingIntervalMilliseconds) Wait until given collection meets given conditions.waitWhile(com.workfusion.rpa.helpers.conditionsnative.collection.UiCollectionCondition condition) Wait while given collection meets given conditions.waitWhile(com.workfusion.rpa.helpers.conditionsnative.collection.UiCollectionCondition condition, long timeoutMilliseconds) Wait while given collection meets given conditions.waitWhile(com.workfusion.rpa.helpers.conditionsnative.collection.UiCollectionCondition condition, long timeoutMilliseconds, long pollingIntervalMilliseconds) Wait while given collection meets given conditions.Methods inherited from class java.util.AbstractList
add, add, addAll, clear, equals, hashCode, indexOf, lastIndexOf, remove, set, subListMethods inherited from class java.util.AbstractCollection
addAll, contains, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArrayMethods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArrayMethods inherited from interface java.util.List
addAll, addFirst, addLast, contains, containsAll, getFirst, getLast, isEmpty, remove, removeAll, removeFirst, removeLast, replaceAll, retainAll, reversed, sort, spliterator, toArray, toArray
-
Method Details
-
get
Returns the element at the given position in the collection.- Specified by:
getin interfaceList<UiElement>- Specified by:
getin classAbstractList<UiElement>- Parameters:
index- zero-based position (0 is the first element)- Returns:
- the element at that position
- Throws:
IndexOutOfBoundsException- if the index is out of range
-
first
- Returns:
- the first element of the collection, or
nullif the collection is empty
-
last
- Returns:
- the last element of the collection, or
nullif the collection is empty
-
size
public int size()- Specified by:
sizein interfaceCollection<UiElement>- Specified by:
sizein interfaceList<UiElement>- Specified by:
sizein classAbstractCollection<UiElement>- Returns:
- the number of elements in the collection
-
shouldHaveSize
Asserts that the collection currently contains exactly the given number of elements. Shorthand forshouldHave(size(expectedSize)). Checked immediately (no waiting).- Parameters:
expectedSize- expected number of elements in the collection- Returns:
- this collection, for chaining
- Throws:
IllegalStateException- if the actual size differs fromexpectedSize
-
shouldBe
public UiElementCollection shouldBe(com.workfusion.rpa.helpers.conditionsnative.collection.UiCollectionCondition... conditions) Asserts that the collection meets all the given conditions immediately (no waiting). Reads naturally with "be"-style conditions, e.g.$$(".error").shouldBe(empty).- Parameters:
conditions- collection conditions that must all hold- Returns:
- this collection, for chaining
- Throws:
IllegalStateException- if any condition is not met
-
shouldHave
public UiElementCollection shouldHave(com.workfusion.rpa.helpers.conditionsnative.collection.UiCollectionCondition... conditions) Asserts that the collection meets all the given conditions immediately (no waiting). Reads naturally with "have"-style conditions, e.g.$$(".error").shouldHave(size(3)).- Parameters:
conditions- collection conditions that must all hold- Returns:
- this collection, for chaining
- Throws:
IllegalStateException- if any condition is not met
-
waitUntil
public UiElementCollection waitUntil(com.workfusion.rpa.helpers.conditionsnative.collection.UiCollectionCondition condition) Wait until given collection meets given conditions.
- Throws:
IllegalStateException- if collection still is not matched to condition after the timeout
-
waitUntil
public UiElementCollection waitUntil(com.workfusion.rpa.helpers.conditionsnative.collection.UiCollectionCondition condition, long timeoutMilliseconds) Wait until given collection meets given conditions.
- Parameters:
timeoutMilliseconds- timeout in milliseconds.- Throws:
IllegalStateException- if collection still is not matched to condition after the timeout
-
waitUntil
public UiElementCollection waitUntil(com.workfusion.rpa.helpers.conditionsnative.collection.UiCollectionCondition condition, long timeoutMilliseconds, long pollingIntervalMilliseconds) Wait until given collection meets given conditions.
- Parameters:
timeoutMilliseconds- timeout in milliseconds.pollingIntervalMilliseconds- interval in milliseconds, when checking condition- Throws:
IllegalStateException- if collection still is not matched to condition after the timeout
-
waitWhile
public UiElementCollection waitWhile(com.workfusion.rpa.helpers.conditionsnative.collection.UiCollectionCondition condition) Wait while given collection meets given conditions.
- Throws:
IllegalStateException- if collection still is not matched to condition after the timeout
-
waitWhile
public UiElementCollection waitWhile(com.workfusion.rpa.helpers.conditionsnative.collection.UiCollectionCondition condition, long timeoutMilliseconds) Wait while given collection meets given conditions.
- Parameters:
timeoutMilliseconds- timeout in milliseconds.- Throws:
IllegalStateException- if collection still is not matched to condition after the timeout
-
waitWhile
public UiElementCollection waitWhile(com.workfusion.rpa.helpers.conditionsnative.collection.UiCollectionCondition condition, long timeoutMilliseconds, long pollingIntervalMilliseconds) Wait while given collection meets given conditions.
- Parameters:
timeoutMilliseconds- timeout in milliseconds.pollingIntervalMilliseconds- interval in milliseconds, when checking condition- Throws:
IllegalStateException- if collection still is not matched to condition after the timeout
-
filter
public UiElementCollection filter(com.workfusion.rpa.helpers.conditionsnative.UiCondition condition) Returns a new collection containing only the elements that match the given condition. The original collection is left unchanged.- Parameters:
condition- the condition each retained element must satisfy- Returns:
- a new collection with the matching elements (possibly empty)
-
filterBy
public UiElementCollection filterBy(com.workfusion.rpa.helpers.conditionsnative.UiCondition condition) Synonym forfilter(UiCondition); reads more naturally in some chains.- Parameters:
condition- the condition each retained element must satisfy- Returns:
- a new collection with the matching elements (possibly empty)
- See Also:
-
exclude
public UiElementCollection exclude(com.workfusion.rpa.helpers.conditionsnative.UiCondition condition) Returns a new collection with the elements that match the given condition removed (the inverse offilter(UiCondition)). The original collection is left unchanged.- Parameters:
condition- the condition identifying elements to exclude- Returns:
- a new collection without the matching elements (possibly empty)
-
excludeWith
public UiElementCollection excludeWith(com.workfusion.rpa.helpers.conditionsnative.UiCondition condition) Synonym forexclude(UiCondition); reads more naturally in some chains.- Parameters:
condition- the condition identifying elements to exclude- Returns:
- a new collection without the matching elements (possibly empty)
- See Also:
-
find
Returns the first element that matches the given condition.- Parameters:
condition- the condition the returned element must satisfy- Returns:
- the first matching element, or
nullif none matches
-
findBy
Synonym forfind(UiCondition); reads more naturally in some chains.- Parameters:
condition- the condition the returned element must satisfy- Returns:
- the first matching element, or
nullif none matches - See Also:
-
getTexts
Returns the visible text of every element in the collection, in order. Fail-safe: if reading an element throws aWebDriverException(e.g. it went stale), the exception's string representation is stored in that slot instead of failing the call.- Returns:
- an array of element texts, one per element (same order as the collection)
-
getTexts
public static String[] getTexts(Collection<? extends com.workfusion.rpa.internal.api.element.WebElement> elements) Fail-safe method for retrieving texts of given elements.- Parameters:
elements- Any collection of WebElements- Returns:
- Array of texts (or exceptions in case of any WebDriverExceptions)
-
elementsToString
public static String elementsToString(List<? extends com.workfusion.rpa.internal.api.element.WebElement> elements) Builds a human-readable, multi-line string representation of an element collection. Intended for logging and debugging, not for test verifications.- Parameters:
elements- the elements to render;nullyields"[not loaded yet...]"and an empty list yields"[]"- Returns:
- a formatted string listing the elements
-
iterator
-
listIterator
- Specified by:
listIteratorin interfaceList<UiElement>- Overrides:
listIteratorin classAbstractList<UiElement>- Returns:
- a list iterator over the elements of this collection, starting at the first element
-
listIterator
Returns a list iterator over the elements of this collection, starting at the given position.- Specified by:
listIteratorin interfaceList<UiElement>- Overrides:
listIteratorin classAbstractList<UiElement>- Parameters:
index- position of the first element to be returned by the iterator- Returns:
- a list iterator starting at the given position
- Throws:
IndexOutOfBoundsException- if the index is out of range
-
toString
Returns a human-readable, multi-line representation of the collection, intended for logging and debugging. Fail-safe: any error while rendering is captured and returned as the string instead of being thrown.- Overrides:
toStringin classAbstractCollection<UiElement>- Returns:
- a formatted string listing the elements
-