com.vaadin.testbench.
Class ElementQuery<T extends TestBenchElement>
Query class used for finding a given element inside a given search context.
The search context is either a WebDriver
instance which searches
starting from the root of the current document, or a WebElement
instance, which searches both in the light DOM and inside the shadow root of
the given element.
When the search context is a WebElement
, the shadow root is searched
first. E.g. when searching by ID and the same ID is used by a light DOM child
of the element and also inside its shadow root, the element from the shadow
root is returned.
The element class specified in the constructor defines the tag name which is searched for and also the type of element returned.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
Class for holding name, comparison, and value for matching attributes.
-
Constructor Summary
ConstructorsConstructorDescriptionElementQuery
(Class<T> elementClass) Instantiate a new ElementQuery to look for the given type of element.
ElementQuery
(Class<T> elementClass, String tagName) Instantiate a new ElementQuery to look for the given type of element.
-
Method Summary
Modifier and TypeMethodDescriptionall()
Search the open Vaadin application for a list of matching components relative to given context.
Deprecated.
attributeContains
(String name, String word) Deprecated.
context
(org.openqa.selenium.SearchContext searchContext) Sets the context to search inside.
boolean
exists()
Checks if this ElementQuery describes existing elements.
first()
Executes the search and returns the first result.
get
(int index) Executes the search and returns the requested element.
protected org.openqa.selenium.SearchContext
Returns the context (element or driver) to search inside.
hasAttribute
(String name) Deprecated.
Executes the search and returns an element having the given unique id.
last()
Executes the search and returns the last result.
onPage()
Defines that the query should start the search from the root of the page, in practice from the
<body>
tag.single()
Executes the search and returns the sole result.
Executes the search and returns the first result once at least once result is available.
waitForFirst
(long timeOutInSeconds) Executes the search and returns the first result once at least once result is available.
withAttribute
(String attribute) Selects on elements having the given attribute.
withAttribute
(String attribute, String value) Selects on elements with the given attribute having the given value.
withAttribute
(String attribute, String value, ElementQuery.AttributeMatch.Comparison comparison) Selects on elements with the given attribute using the given comparison and value.
withAttributeContaining
(String attribute, String text) Selects on elements with the given attribute containing the given text.
withAttributeContainingWord
(String attribute, String word) Selects on elements with the given attribute containing the given word.
withCaption
(String caption) Requires the element's caption (i.e., label, placeholder, or text label) to exactly match the given caption value.
withCaption
(String text, BiPredicate<String, String> comparison) Requires the element's caption (i.e., label, placeholder, or text label) to satisfy the given comparison with the supplied text.
withCaptionContaining
(String text) Requires the element's caption (i.e., label, placeholder, or text label) to partially match the given text value.
withClassName
(String... classNames) Selects on elements having the given class names.
withCondition
(Predicate<T> condition) Requires the element to satisfy the given condition.
Selects on elements having the given id.
Requires the element's label to exactly match the given label value.
withLabel
(String text, BiPredicate<String, String> comparison) Requires the element's label to satisfy the given comparison with the supplied text.
withLabelContaining
(String text) Requires the element's label to partially match the given text value.
withoutAttribute
(String attribute) Selects on elements not having the given attribute.
withoutAttribute
(String attribute, String value) Selects on elements not having the given attribute with the given value.
withoutAttributeContaining
(String attribute, String text) Selects on elements not having the given attribute containing the given text.
withoutAttributeContainingWord
(String attribute, String word) Selects on elements not having the given attribute containing the given word.
withoutClassName
(String... classNames) Selects on elements not having the given class names.
withoutTheme
(String theme) Selects on elements not having the given theme.
withPlaceholder
(String placeholder) Requires the element's placeholder to exactly match the given placeholder value.
withPlaceholder
(String text, BiPredicate<String, String> comparison) Requires the element's placeholder to satisfy the given comparison with the supplied text.
Requires the element's placeholder to partially match the given text value.
<V> ElementQuery<T>
withPropertyValue
(Function<T, V> getter, V propertyValue) Requires the element's given property getter return value to equal the supplied value.
<V> ElementQuery<T>
withPropertyValue
(Function<T, V> getter, V propertyValue, BiPredicate<V, V> comparison) Requires the element's given property getter return value to satisfy the given comparison with the supplied value.
Requires the element's text to exactly match the given text value.
withText
(String text, BiPredicate<String, String> comparison) Requires the element's text to satisfy the given comparison with the supplied text.
withTextContaining
(String text) Requires the element's text to partially match the given text value.
Selects on elements having the given theme.
-
Constructor Details
-
ElementQuery
Instantiate a new ElementQuery to look for the given type of element.
Parameters:
elementClass
- the type of element to look for and return -
ElementQuery
Instantiate a new ElementQuery to look for the given type of element.
Parameters:
elementClass
- the type of element to returntagName
- the tag name of the element to find
-
-
Method Details
-
hasAttribute
Deprecated.Selects on elements having the given attribute.
Note, this attribute need not have a value.
Parameters:
name
- the attribute nameReturns:
this element query instance for chaining
-
attribute
Deprecated.Selects on elements with the given attribute having the given value.
For matching a substring of the attribute value, see
withAttributeContaining(String, String)
. For matching a word within the attribute, seewithAttributeContainingWord(String, String)
.Parameters:
name
- the attribute namevalue
- the attribute valueReturns:
this element query instance for chaining
See Also:
-
attributeContains
Deprecated.Selects on elements with the given attribute containing the given word.
Compares with space separated words so that e.g.
attributeContains("class", "myclass");
matchesclass='someclass myclass'
.For matching the full attribute value, see
withAttribute(String, String)
. For matching a substring of the attribute value, seewithAttributeContaining(String, String)
.Parameters:
name
- the attribute nameword
- the word to look forReturns:
this element query instance for chaining
See Also:
-
withAttribute
Selects on elements having the given attribute.
Note, the attribute need not have a value--it just needs to exist.
Parameters:
attribute
- the attribute nameReturns:
this element query instance for chaining
-
withAttribute
public ElementQuery<T> withAttribute(String attribute, String value, ElementQuery.AttributeMatch.Comparison comparison) Selects on elements with the given attribute using the given comparison and value.
The given value must match the attribute value according to the comparison.
Parameters:
attribute
- the attribute namevalue
- the attribute valuecomparison
- the comparison to useReturns:
this element query instance for chaining
-
withAttribute
Selects on elements with the given attribute having the given value.
The given value must match the attribute value exactly.
For matching a substring of the attribute, see
withAttributeContaining(String, String)
. For matching a word within the attribute, seewithAttributeContainingWord(String, String)
.Parameters:
attribute
- the attribute namevalue
- the attribute valueReturns:
this element query instance for chaining
See Also:
-
withAttributeContaining
Selects on elements with the given attribute containing the given text.
The given text must match a substring of the attribute value.
For matching the full attribute value, see
withAttribute(String, String)
. For matching a word within the attribute, seewithAttributeContainingWord(String, String)
.Parameters:
attribute
- the attribute nametext
- the substring to look forReturns:
this element query instance for chaining
See Also:
-
withAttributeContainingWord
Selects on elements with the given attribute containing the given word.
Compares with space separated words so that e.g.
withAttributeContainingWord("class", "myclass");
matchesclass='someclass myclass'
.For matching the full attribute value, see
withAttribute(String, String)
. For matching a substring of the attribute value, seewithAttributeContaining(String, String)
.Parameters:
attribute
- the attribute nameword
- the word to look forReturns:
this element query instance for chaining
See Also:
-
withoutAttribute
Selects on elements not having the given attribute.
Note, attributes both with and without values are skipped.
Parameters:
attribute
- the attribute nameReturns:
this element query instance for chaining
-
withoutAttribute
Selects on elements not having the given attribute with the given value.
The given value must match the attribute value exactly in order to be skipped.
For matching the full attribute value, see
withoutAttributeContaining(String, String)
. For skipping elements having a word within the attribute, seewithoutAttributeContainingWord(String, String)
.Parameters:
attribute
- the attribute namevalue
- the attribute valueReturns:
this element query instance for chaining
See Also:
-
withoutAttributeContaining
Selects on elements not having the given attribute containing the given text.
The given value must match any substring of the attribute value in order to be skipped.
For matching the full attribute value, see
withoutAttribute(String, String)
. For skipping elements having a word within the attribute, seewithoutAttributeContainingWord(String, String)
.Parameters:
attribute
- the attribute nametext
- the substring to look forReturns:
this element query instance for chaining
See Also:
-
withoutAttributeContainingWord
Selects on elements not having the given attribute containing the given word.
Compares with space separated words so that e.g.
withoutAttributeContainingWord("class", "myclass");
skipsclass='someclass myclass'
.For matching the full attribute value, see
withoutAttribute(String, String)
. For skipping elements containing a substring of the attribute, seewithoutAttributeContaining(String, String)
.Parameters:
attribute
- the attribute nameword
- the word to look forReturns:
this element query instance for chaining
See Also:
-
withId
Selects on elements having the given id.
This selector does not require the id to be unique. To obtain the unique id, chain with
or usesingle()
instead of this selector. If you legitimately have duplicate ids and just want the first one, chain withid(String)
.first()
Parameters:
id
- the id to look upReturns:
the element with the given id
See Also:
-
withClassName
Selects on elements having the given class names.
Parameters:
classNames
- the class namesReturns:
this element query instance for chaining
-
withoutClassName
Selects on elements not having the given class names.
Parameters:
classNames
- the class namesReturns:
this element query instance for chaining
-
withTheme
Selects on elements having the given theme.
Parameters:
theme
- the themeReturns:
this element query instance for chaining
-
withoutTheme
Selects on elements not having the given theme.
Parameters:
theme
- the themeReturns:
this element query instance for chaining
-
withCondition
Requires the element to satisfy the given condition.
For example, to select only enabled elements, you could use
withCondition(TestBenchElement::isEnabled)
or to select only those having a non-zero height, you could use
withCondition(element -> element.getSize().getHeight() != 0)
Note that conditions are evaluated in order after the element is selected by its attributes.
Parameters:
condition
- the condition for the element to satisfy; not nullReturns:
this element query instance for chaining
-
withPropertyValue
public <V> ElementQuery<T> withPropertyValue(Function<T, V> getter, V propertyValue, BiPredicate<V, V> comparison) Requires the element's given property getter return value to satisfy the given comparison with the supplied value.
For example, to select
TextFieldElement
s having helper text containing the word "person", you could usewithPropertyValue(TextFieldElement::getHelperText, "person", String::contains)
Type Parameters:
V
- the type of the property valuesParameters:
getter
- the function to get the value of the property of the element; not nullpropertyValue
- value to be compared with the one obtained from the getter function of the elementcomparison
- the comparison to use when comparing the getter's property value against the supplied property value (i.e.,comparison.test(elementPropertyValue, propertyValue)
; not nullReturns:
this element query instance for chaining
See Also:
-
withPropertyValue
Requires the element's given property getter return value to equal the supplied value.
Type Parameters:
V
- the type of the property valuesParameters:
getter
- the function to get the value of the property of the element, not nullpropertyValue
- value to be compared with the one obtained from the getter function of the elementReturns:
this element query instance for chaining
See Also:
-
withLabel
Requires the element's label to satisfy the given comparison with the supplied text.
For matching a label exactly, see
withLabel(String)
, and for matching a label partially, seewithLabelContaining(String)
.This method can be used for other label matching needs, such as performing a case-insensitive match:
withLabel("name", String::equalsIgnoreCase)
a label-ending match:withLabel(" Name", String::endsWith)
or a regular expression match:withLabel("(First|Last) Name", String::matches)
Parameters:
text
- the text to compare with the label; not nullcomparison
- the comparison to use when comparing element's label against the supplied text (i.e.,comparison.test(elementLabel, text)
; not nullReturns:
this element query instance for chaining
See Also:
-
withLabel
Requires the element's label to exactly match the given label value.
For partially matching text within the label, see
withLabelContaining(String)
.Parameters:
label
- the label to matchReturns:
this element query instance for chaining
See Also:
-
withLabelContaining
Requires the element's label to partially match the given text value.
For exactly matching the label, see
withLabel(String)
.Parameters:
text
- the text to matchReturns:
this element query instance for chaining
See Also:
-
withPlaceholder
Requires the element's placeholder to satisfy the given comparison with the supplied text.
For matching a placeholder exactly, see
withPlaceholder(String)
, and for matching a placeholder partially, seewithPlaceholderContaining(String)
.This method can be used for other placeholder matching needs, such as performing a case-insensitive match:
withPlaceholder("name", String::equalsIgnoreCase)
a placeholder-ending match:withPlaceholder(" Name", String::endsWith)
or a regular expression match:withPlaceholder("(First|Last) Name", String::matches)
Parameters:
text
- the text to compare with the placeholder; not nullcomparison
- the comparison to use when comparing the placeholder against the supplied text (i.e.,comparison.test(elementPlaceholder, text)
; not nullReturns:
this element query instance for chaining
See Also:
-
withPlaceholder
Requires the element's placeholder to exactly match the given placeholder value.
For partially matching text within the placeholder, see
withPlaceholderContaining(String)
.Parameters:
placeholder
- the placeholder to matchReturns:
this element query instance for chaining
See Also:
-
withPlaceholderContaining
Requires the element's placeholder to partially match the given text value.
For exactly matching the placeholder, see
withPlaceholder(String)
.Parameters:
text
- the text to matchReturns:
this element query instance for chaining
See Also:
-
withCaption
Requires the element's caption (i.e., label, placeholder, or text label) to satisfy the given comparison with the supplied text.
This is a convenience selector method to select an element by its label, placeholder, or text label, as supported by the element. These values are generically considered "captions" as they are used to identify the element to the user.
The comparison against the values of the element follows this priority:
- Label - If the element supports a label and its label is not empty, the element's label value is used in the comparison. If the comparison with the given text is not satisfied by the element's label value, the comparison does not fall through to compare against the placeholder.
- Placeholder - Even if an element supports a label, its label value may be empty. In that situation, the element may be using a placeholder in lieu of a label. So in that situation, the comparison falls through to compare against the placeholder if the element supports a placeholder and its placeholder is not empty, the element's placeholder value is used in the comparison.
- Text - If the element supports neither labels nor placeholders but does support a caption via its text (such as a button does), the comparison is made against the element's text.
For matching a caption exactly, see
withCaption(String)
, and for matching a caption partially, seewithCaptionContaining(String)
.This method can be used for other caption matching needs, such as performing a case-insensitive match:
withCaption("name", String::equalsIgnoreCase)
a caption-ending match:withCaption(" Name", String::endsWith)
or a regular expression match:withCaption("(First|Last) Name", String::matches)
Parameters:
text
- the text to compare with the caption; not nullcomparison
- the comparison to use when comparing the caption against the supplied text (i.e.,comparison.test(elementCaption, text)
; not nullReturns:
this element query instance for chaining
See Also:
-
withCaption
Requires the element's caption (i.e., label, placeholder, or text label) to exactly match the given caption value.
For partially matching text within the caption, see
withCaptionContaining(String)
.Parameters:
caption
- the caption to matchReturns:
this element query instance for chaining
See Also:
-
withCaptionContaining
Requires the element's caption (i.e., label, placeholder, or text label) to partially match the given text value.
For exactly matching the caption, see
withCaption(String)
.Parameters:
text
- the text to matchReturns:
this element query instance for chaining
See Also:
-
withText
Requires the element's text to satisfy the given comparison with the supplied text.
For matching the element's text exactly, see
withText(String)
, and for matching the element's text partially, seewithTextContaining(String)
.This method can be used for other text matching needs, such as performing a case-insensitive match:
withText("name", String::equalsIgnoreCase)
a text-ending match:withText(" Name", String::endsWith)
or a regular expression match:withText("(First|Last) Name", String::matches)
Parameters:
text
- the text to compare with the element's text; not nullcomparison
- the comparison to use when comparing the element's text against the supplied text (i.e.,comparison.test(elementText, text)
; not nullReturns:
this element query instance for chaining
See Also:
-
withText
Requires the element's text to exactly match the given text value.
For partially matching text within the element's text, see
withTextContaining(String)
.Parameters:
text
- the text to matchReturns:
this element query instance for chaining
See Also:
-
withTextContaining
Requires the element's text to partially match the given text value.
For exactly matching the text, see
withText(String)
.Parameters:
text
- the text to matchReturns:
this element query instance for chaining
See Also:
-
context
Sets the context to search inside.
Parameters:
searchContext
- aSearchContext
; either aTestBenchElement
orWebDriver
(to search from the root) instanceReturns:
this element query instance for chaining
-
onPage
Defines that the query should start the search from the root of the page, in practice from the
<body>
tag.Returns:
this element query instance for chaining
-
getContext
protected org.openqa.selenium.SearchContext getContext()Returns the context (element or driver) to search inside.
Returns:
a
SearchContext
instance -
id
Executes the search and returns an element having the given unique id.
This selector expects the id to be unique. If there are duplicate ids, this selector will throw an exception. If you legitimately have duplicate ids, use
instead. (Note, this alternate usage is the former behavior of this selector.)withId(String)
.first()
Parameters:
id
- the id to look upReturns:
the element with the given id
Throws:
org.openqa.selenium.NoSuchElementException
- if no unique id element is foundSee Also:
-
single
Executes the search and returns the sole result.
Returns:
The element of the type specified in the constructor
Throws:
org.openqa.selenium.NoSuchElementException
- if no unique element is found -
first
Executes the search and returns the first result.
Returns:
The element of the type specified in the constructor
Throws:
org.openqa.selenium.NoSuchElementException
- if no element is found -
waitForFirst
Executes the search and returns the first result once at least once result is available.
This method is identical to
first()
if at least one element is present. If no element is found, this method will keep searching until an element is found or if 10 seconds has elapsed.Returns:
The element of the type specified in the constructor
Throws:
org.openqa.selenium.NoSuchElementException
- if no element is foundSee Also:
-
waitForFirst
Executes the search and returns the first result once at least once result is available.
This method is identical to
first()
if at least one element is present. If no element is found, this method will keep searching until an element is found ortimeOutInSeconds
seconds has elapsed.Parameters:
timeOutInSeconds
- timeout in seconds before this method throws aNoSuchElementException
exceptionReturns:
The element of the type specified in the constructor
Throws:
org.openqa.selenium.NoSuchElementException
- if no element is foundSee Also:
-
last
Executes the search and returns the last result.
Returns:
The element of the type specified in the constructor
Throws:
org.openqa.selenium.NoSuchElementException
- if no element is found -
get
Executes the search and returns the requested element.
Parameters:
index
- the index of the element to returnReturns:
The element of the type specified in the constructor
Throws:
org.openqa.selenium.NoSuchElementException
- if no element is found -
exists
public boolean exists()Checks if this ElementQuery describes existing elements. Same as .all().isEmpty().
Returns:
true if elements exists. false if not
-
all
Search the open Vaadin application for a list of matching components relative to given context.
Returns:
Components as a list of corresponding elements
-
withAttribute(String, String)