Class ElementQuery<T extends TestBenchElement>
- java.lang.Object
-
- com.vaadin.testbench.ElementQuery<T>
-
public class ElementQuery<T extends TestBenchElement> extends Object
Query class used for finding a given element inside a given search context.The search context is either a
WebDriverinstance which searches starting from the root of the current document, or aWebElementinstance, 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 an also the type of element returned.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classElementQuery.AttributeMatch
-
Constructor Summary
Constructors Constructor Description ElementQuery(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
All Methods Instance Methods Concrete Methods Modifier and Type Method Description List<T>all()Search the open Vaadin application for a list of matching components relative to given context.ElementQuery<T>attribute(String name, String value)Requires the given attribute to match the given value.ElementQuery<T>attributeContains(String name, String token)Requires the given attribute to contain the given value.ElementQuery<T>context(org.openqa.selenium.SearchContext searchContext)Sets the context to search inside.booleanexists()Checks if this ElementQuery describes existing elements.Tfirst()Executes the search and returns the first result.Tget(int index)Executes the search and returns the requested element.protected org.openqa.selenium.SearchContextgetContext()Returns the context (element or driver) to search inside.ElementQuery<T>hasAttribute(String name)Requires the given attribute to be set.Tid(String id)Executes a search for element with the given id.Tlast()Executes the search and returns the last result.ElementQuery<T>onPage()Defines that the query should start the search from the root of the page, in practice from the<body>tag.TwaitForFirst()Executes the search and returns the first result once at least once result is available.TwaitForFirst(long timeOutInSeconds)Executes the search and returns the first result once at least once result is available.
-
-
-
Method Detail
-
id
public T id(String id)
Executes a search for element with the given id.- Parameters:
id- the id to look up- Returns:
- the element with the given id
- Throws:
org.openqa.selenium.NoSuchElementException- if no element is found
-
hasAttribute
public ElementQuery<T> hasAttribute(String name)
Requires the given attribute to be set.- Parameters:
name- the attribute name- Returns:
- this element query instance for chaining
-
attribute
public ElementQuery<T> attribute(String name, String value)
Requires the given attribute to match the given value.For matching a token in the attribute, see
attributeContains(String, String).- Parameters:
name- the attribute namevalue- the attribute value- Returns:
- this element query instance for chaining
-
attributeContains
public ElementQuery<T> attributeContains(String name, String token)
Requires the given attribute to contain the given value.Compares with space separated tokens so that e.g.
attributeContains('class','myclass');matchesclass='someclass myclass'.For matching the full attribute value, see
attribute(String, String).- Parameters:
name- the attribute nametoken- the token to look for- Returns:
- this element query instance for chaining
-
context
public ElementQuery<T> context(org.openqa.selenium.SearchContext searchContext)
Sets the context to search inside.- Parameters:
searchContext- aSearchContext; either aTestBenchElementorWebDriver(to search from the root) instance- Returns:
- this element query instance for chaining
-
onPage
public ElementQuery<T> 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
SearchContextinstance
-
first
public T 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
public T 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
-
waitForFirst
public T waitForFirst(long timeOutInSeconds)
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 ortimeOutInSecondsseconds has elapsed.- Parameters:
timeOutInSeconds- timeout in seconds before this method throws aNoSuchElementExceptionexception- Returns:
- The element of the type specified in the constructor
-
last
public T 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
public T get(int index)
Executes the search and returns the requested element.- Parameters:
index- the index of the element to return- Returns:
- 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
-
-