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 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 an also the type of element returned.

  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static class 
     
  • 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

    Modifier and Type
    Method
    Description
    all()
    Search the open Vaadin application for a list of matching components relative to given context.
    attribute(String name, String value)
    Requires the given attribute to match the given value.
    Requires the given attribute to contain the given value.
    context(org.openqa.selenium.SearchContext searchContext)
    Sets the context to search inside.
    boolean
    Checks if this ElementQuery describes existing elements.
    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.
    Requires the given attribute to be set.
    id(String id)
    Executes a search for element with the given id.
    Executes the search and returns the last result.
    Defines that the query should start the search from the root of the page, in practice from the <body> tag.
    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.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • ElementQuery

      public ElementQuery(Class<T> elementClass)
      Instantiate a new ElementQuery to look for the given type of element.
      Parameters:
      elementClass - the type of element to look for and return
    • ElementQuery

      public ElementQuery(Class<T> elementClass, String tagName)
      Instantiate a new ElementQuery to look for the given type of element.
      Parameters:
      elementClass - the type of element to return
      tagName - the tag name of the element to find
  • Method Details

    • 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 name
      value - 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'); matches class='someclass myclass'.

      For matching the full attribute value, see attribute(String, String).

      Parameters:
      name - the attribute name
      token - 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 - a SearchContext; either a TestBenchElement or WebDriver (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 SearchContext instance
    • 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 or timeOutInSeconds seconds has elapsed.

      Parameters:
      timeOutInSeconds - timeout in seconds before this method throws a NoSuchElementException exception
      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
    • all

      public List<T> all()
      Search the open Vaadin application for a list of matching components relative to given context.
      Returns:
      Components as a list of corresponding elements