Class Locator<C extends com.vaadin.flow.component.Component,SELF extends Locator<C,SELF>>
- Type Parameters:
C- the component typeSELF- the concrete locator subtype, used for fluent chaining
- Direct Known Subclasses:
AccordionLocator,AnchorLocator,AvatarGroupLocator,BigDecimalFieldLocator,BreadcrumbsLocator,ButtonLocator,CardLocator,ChartLocator,CheckboxGroupLocator,CheckboxLocator,ComboBoxLocator,ConfirmDialogLocator,ContextMenuLocator,DashboardLocator,DatePickerLocator,DateTimePickerLocator,DecimalRangeSliderLocator,DecimalSliderLocator,DescriptionListLocator,DetailsLocator,DialogLocator,DivLocator,EmailFieldLocator,EmphasisLocator,GridContextMenuLocator,GridLocator,GridProLocator,H1Locator,H2Locator,H3Locator,H4Locator,H5Locator,H6Locator,HrLocator,ImageLocator,InputLocator,IntegerFieldLocator,IntegerRangeSliderLocator,IntegerSliderLocator,ListBoxLocator,ListItemLocator,LoginFormLocator,LoginOverlayLocator,MarkdownLocator,MasterDetailLayoutLocator,MenuBarLocator,MessageInputLocator,MessageListLocator,MultiSelectComboBoxLocator,MultiSelectListBoxLocator,NativeButtonLocator,NativeDetailsLocator,NativeLabelLocator,NotificationLocator,NumberFieldLocator,OrderedListLocator,ParagraphLocator,PasswordFieldLocator,PopoverLocator,PreLocator,RadioButtonGroupLocator,RadioButtonLocator,RangeInputLocator,RouterLinkLocator,SelectLocator,SideNavLocator,SpanLocator,SplitLayoutLocator,SwitchLocator,TabSheetLocator,TabsLocator,TextAreaLocator,TextFieldLocator,TextLocator,TimePickerLocator,TreeGridLocator,UnorderedListLocator,UploadButtonLocator,UploadDropZoneLocator,UploadFileListLocator,UploadLocator,VirtualListLocator
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(int) 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
ComponentQuery.withPropertyValue(java.util.function.Function<T, V>, V) or
ComponentQuery.withResultsSize(int)) are reachable through the
with(UnaryOperator) escape hatch, which lets callers compose any
filter the underlying ComponentQuery supports without subclassing.
Construction modes. The default constructor
(Locator(Class)) 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(Class, Component)), 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
false and component() throw. Custom locator subclasses can
opt in by declaring a second constructor that forwards to
super(Class, component).
- Since:
- 1.1
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionatIndex(int index) Picks the n-th match (1-based) when the filter chain yields multiple matches.Resolves the locator to a single matching component, caching the result.Returns all matching components, bypassing the cache.booleanexists()Returnstrueif the filter chain matches at least one component.Scopes the search to descendants of the component matched by the given locator.inside(com.vaadin.flow.component.Component parent) Scopes the search to descendants of the given component.Rewinds picker state: discards any cached resolution and clears theatIndex(int)pick.protected SELFself()Returns this locator, typed as the concrete subtype, so the filter steps can chain fluently.with(UnaryOperator<ComponentQuery<C>> op) Escape hatch for filters not directly exposed on Locator.withAttribute(String attribute) Requires the matched component to have the given attribute set.withAttribute(String attribute, String value) Requires the matched component to have the given attribute with the expected value.withClassName(String... className) Requires the matched component to have all the given CSS class names.withCondition(Predicate<C> condition) Requires the matched component to satisfy the given predicate.withCondition(Predicate<C> condition, String description) Requires the matched component to satisfy the given predicate, described by the given text in the message of a failed lookup.Requires the matched component to have the given id.withinSlot(String slot) Requires the matched component to sit in the given named slot of the component that hosts it, nested content included.withoutAttribute(String attribute) Requires the matched component not to have the given attribute.withoutAttribute(String attribute, String value) Requires the matched component not to have the given attribute value (or not to have the attribute at all).withoutClassName(String... className) Requires the matched component to have none of the given CSS class names.withTestId(String testId) Requires the matched component to have the givendata-testidattribute, as set byComponent.setTestId(String).
-
Constructor Details
-
Locator
Creates a locator that searches for components of the given type from the currentUIroot.- Parameters:
componentType- the component type to match
-
Locator
Creates a locator seeded with a direct reference to the component to match. The query is pre-filtered with an identity predicate so the only resolution is the given instance; additional filter steps compose on top of it.- Parameters:
componentType- the component type to matchcomponent- the component instance to seed the query with; must not benull
-
-
Method Details
-
withId
Requires the matched component to have the given id.- Parameters:
id- the expected component id- Returns:
- this locator, for chaining
-
withTestId
Requires the matched component to have the givendata-testidattribute, as set byComponent.setTestId(String).- Parameters:
testId- the expecteddata-testidvalue- Returns:
- this locator, for chaining
-
withClassName
Requires the matched component to have all the given CSS class names.- Parameters:
className- the CSS class names the component must have- Returns:
- this locator, for chaining
-
withoutClassName
Requires the matched component to have none of the given CSS class names.- Parameters:
className- the CSS class names the component must not have- Returns:
- this locator, for chaining
-
withAttribute
Requires the matched component to have the given attribute set.- Parameters:
attribute- the attribute that must be present- Returns:
- this locator, for chaining
-
withAttribute
Requires the matched component to have the given attribute with the expected value.- Parameters:
attribute- the attribute that must be presentvalue- the value the attribute must have- Returns:
- this locator, for chaining
-
withoutAttribute
Requires the matched component not to have the given attribute.- Parameters:
attribute- the attribute that must be absent- Returns:
- this locator, for chaining
-
withoutAttribute
Requires the matched component not to have the given attribute value (or not to have the attribute at all).- Parameters:
attribute- the attribute to checkvalue- the value the attribute must not have- Returns:
- this locator, for chaining
-
withinSlot
Requires the matched component to sit in the given named slot of the component that hosts it, nested content included.Everything below a slot matches, and where slots nest the outermost one wins. Pair this with
inside(Component)orinside(Locator)to bound the search to one host, which is also what keeps a slot that host is itself placed in out of the result. Slot names are component specific — seeComponentQuery.withinSlot(String)for annotated component trees.- Parameters:
slot- the name of the slot the component must sit in- Returns:
- this locator, for chaining
- Since:
- 25.3
- See Also:
-
withCondition
Requires the matched component to satisfy the given predicate.- Parameters:
condition- the predicate the component must satisfy- Returns:
- this locator, for chaining
-
withCondition
Requires the matched component to satisfy the given predicate, described by the given text in the message of a failed lookup.- Parameters:
condition- the predicate the component must satisfydescription- describes the condition in the message of a failed lookup- Returns:
- this locator, for chaining
- See Also:
-
with
Escape hatch for filters not directly exposed on Locator. Applies the given operator to the underlyingComponentQuery, letting users compose any filter the query supports without subclassing.findButton().with(q -> q.withPropertyValue(Button::getText, "Save")) .click();Honors theUnaryOperatorcontract: whatever the operator returns becomes the locator's new underlying query.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.- Parameters:
op- the operator applied to the underlying query- Returns:
- this locator, for chaining
- Throws:
IllegalStateException- if the operator returnsnullinstead of aComponentQuery— the operator is expected either to mutate and return the same instance, or to build and return a fresh one.
-
inside
Scopes the search to descendants of the given component. Replaces any lazy parent previously installed byinside(Locator)with a fixed reference.- Parameters:
parent- the component whose descendants are searched- Returns:
- this locator, for chaining
-
inside
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(), orexists()first invokesparent.component()and installs the result as this locator's search context. A laterinvalidate()onparenttherefore propagates — the next child action re-resolves both. Callinginside(Component)afterwards replaces this lazy parent with a fixed reference; callinginside(Locator)again replaces the lazy parent.- Parameters:
parent- the locator matching the component whose descendants are searched- Returns:
- this locator, for chaining
- Throws:
NullPointerException- ifparentisnullIllegalArgumentException- ifparentis this locator itself — a self-reference would recurse indefinitely under lazy resolution
-
atIndex
Picks the n-th match (1-based) when the filter chain yields multiple matches. Without this, the default expectation is exactly one match.- Parameters:
index- the 1-based index of the match to pick- Returns:
- this locator, for chaining
- Throws:
IllegalArgumentException- ifindexis zero or negative — mirrorsComponentQuery.atIndex(int)'s own contract, so the violation is reported at the locator's filter step rather than masked into a "single match" resolution at action time.
-
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
- Throws:
NoSuchElementException- if no component matches or more than one matches (and noatIndex(int)was provided)
-
components
Returns all matching components, bypassing the cache. Useful for assertions on counts without committing to a single match.- Returns:
- every component the filter chain matches
-
exists
public boolean exists()Returnstrueif the filter chain matches at least one component.- Returns:
trueif at least one component matches
-
invalidate
Rewinds picker state: discards any cached resolution and clears theatIndex(int)pick. Filter methods on this class call a private cache-only reset internally, so they keep the locator'satIndex(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-appliesatIndex(int).- Returns:
- this locator, for chaining
-
self
Returns this locator, typed as the concrete subtype, so the filter steps can chain fluently.- Returns:
- this locator
-