Class Locator
-
- All Implemented Interfaces:
public abstract class Locator<C extends Component, SELF extends Locator<C, SELF>>Base class for the
find*tester API. A locator is a fluent combination of a ComponentQuery filter chain and a tester: the subclass exposes both the filter methods inherited from this class and the action methods specific to the component type.Resolution is deferred to the first action call (component) and cached. Every filter method on this class clears the resolution cache before mutating the underlying query, so the next action re-resolves and callers never have to call invalidate between fluent steps. Filter steps keep the locator's atIndex pick sticky — it is part of the filter chain — so a single locator instance can be reused across an asynchronous boundary (e.g.
roundTrip()) without holding on to a stale component reference. invalidate is the explicit rewind hatch and additionally clears the pick.Filters that this class does not expose directly (for example withPropertyValue or withResultsSize) are reachable through the with escape hatch, which lets callers compose any filter the underlying ComponentQuery supports without subclassing.
Construction modes. The default constructor (Locator) seeds an empty query that searches the active UI. Tests that already hold a direct reference to the component they want to act on can instead use the seeded-query constructor (Locator), which pre-filters the query with an identity predicate. Both modes share the same filter/resolution machinery — additional filters compose on top of the identity predicate, and a filter that excludes the seeded component just makes exists return
falseand component throw. Custom locator subclasses can opt in by declaring a second constructor that forwards tosuper(Class, component).- Since:
1.1
-
-
Method Summary
Modifier and Type Method Description SELFwithId(String id)Requires the matched component to have the given id. SELFwithTestId(String testId)Requires the matched component to have the given data-testidattribute, as set by setTestId.SELFwithClassName(Array<String> className)Requires the matched component to have all the given CSS class names. SELFwithoutClassName(Array<String> className)Requires the matched component to have none of the given CSS class names. SELFwithAttribute(String attribute)Requires the matched component to have the given attribute set. SELFwithAttribute(String attribute, String value)Requires the matched component to have the given attribute with the expected value. SELFwithoutAttribute(String attribute)Requires the matched component not to have the given attribute. SELFwithoutAttribute(String attribute, String value)Requires the matched component not to have the given attribute value (or not to have the attribute at all). SELFwithCondition(Predicate<C> condition)Requires the matched component to satisfy the given predicate. SELFwith(UnaryOperator<ComponentQuery<C>> op)Escape hatch for filters not directly exposed on Locator. SELFinside(Component parent)Scopes the search to descendants of the given component. SELFinside(Locator<out Object, out Object> parent)Scopes the search to descendants of the component matched by the given locator. SELFatIndex(int index)Picks the n-th match (1-based) when the filter chain yields multiple matches. Ccomponent()Resolves the locator to a single matching component, caching the result. List<C>components()Returns all matching components, bypassing the cache. booleanexists()Returns trueif the filter chain matches at least one component.SELFinvalidate()Rewinds picker state: discards any cached resolution and clears the atIndex pick. -
-
Method Detail
-
withTestId
SELF withTestId(String testId)
Requires the matched component to have the given
data-testidattribute, as set by setTestId.
-
withClassName
SELF withClassName(Array<String> className)
Requires the matched component to have all the given CSS class names.
-
withoutClassName
SELF withoutClassName(Array<String> className)
Requires the matched component to have none of the given CSS class names.
-
withAttribute
SELF withAttribute(String attribute)
Requires the matched component to have the given attribute set.
-
withAttribute
SELF withAttribute(String attribute, String value)
Requires the matched component to have the given attribute with the expected value.
-
withoutAttribute
SELF withoutAttribute(String attribute)
Requires the matched component not to have the given attribute.
-
withoutAttribute
SELF withoutAttribute(String attribute, String value)
Requires the matched component not to have the given attribute value (or not to have the attribute at all).
-
withCondition
SELF withCondition(Predicate<C> condition)
Requires the matched component to satisfy the given predicate.
-
with
SELF with(UnaryOperator<ComponentQuery<C>> op)
Escape hatch for filters not directly exposed on Locator. Applies the given operator to the underlying ComponentQuery, letting users compose any filter the query supports without subclassing.
Honors the UnaryOperator contract: whatever the operator returns becomes the locator's new underlying query.findButton().with(q -> q.withPropertyValue(Button::getText, "Save")) .click();ComponentQuery's built-in filter methods all returnthis, so a fluent chain just re-installs the same instance; an operator that builds and returns a fresh query replaces the prior one wholesale.
-
inside
SELF inside(Component parent)
Scopes the search to descendants of the given component. Replaces any lazy parent previously installed by inside with a fixed reference.
-
inside
SELF inside(Locator<out Object, out Object> parent)
Scopes the search to descendants of the component matched by the given locator.
The parent is resolved lazily, at child-resolution time: each call to component, components, or exists first invokes
parent.component()and installs the result as this locator's search context. A later invalidate onparenttherefore propagates — the next child action re-resolves both. Calling inside afterwards replaces this lazy parent with a fixed reference; callinginside(Locator)again replaces the lazy parent.
-
atIndex
SELF atIndex(int index)
Picks the n-th match (1-based) when the filter chain yields multiple matches. Without this, the default expectation is exactly one match.
-
component
C component()
Resolves the locator to a single matching component, caching the result. Subclasses call this from action methods (e.g.
click).- Returns:
the matched component
-
components
List<C> components()
Returns all matching components, bypassing the cache. Useful for assertions on counts without committing to a single match.
-
exists
boolean exists()
Returns
trueif the filter chain matches at least one component.
-
invalidate
SELF invalidate()
Rewinds picker state: discards any cached resolution and clears the atIndex pick. Filter methods on this class call a private cache-only reset internally, so they keep the locator's
atIndex(n)sticky as part of the filter chain.invalidate()is the explicit "rewind" hatch: after a UI change that replaces or detaches the resolved component, calling it forces the next action to re-resolve, and also drops the pick so the next resolution defaults back to "single match expected" until the caller re-applies atIndex.
-
-
-
-