Creating Tests
The test logic in TestBench tests consist typically of two things: find an element or component with which to interact; and interact with an element or component.
For this, TestBench offers the Element and ElementQuery API.
An Element class is a representation of an element on the web page or in the web application. The represented element can be a built-in HTML element such as <div> or <span>, or it can be a custom element such as <vaadin-button> or <vaadin-grid>.
An Element class offers methods to interact with the element in the same way that a user of the application could interact with the element. A <vaadin-button> element can, for instance, be clicked and you can check what the text on the button says. A <vaadin-grid> element has more complex methods for scrolling, checking the visible contents, headers, etc.
Low-level methods applicable to all elements, such as sendKeys() and getSize(), are also available when needed.
|
Tip
|
Element classes are provided for all Vaadin components. If an element class isn’t available or some functionality is missing, you can create your own version from the beginning or by extending an existing one (see Extending a Page Object). An element class is in practice the same as a page object (see Creating a Page Object for more details).
|
An ElementQuery is what’s used to find a given Element (component) on the page so that you can interact with it. The high-level ElementQuery API allows querying Vaadin components in the browser according to their component class type, hierarchy, caption, and other properties. An ElementQuery is constructed using a builder-like pattern and returns a single element or a list of matching elements.
Further queries can be performed on the returned elements to find the desired element. Related to this, consider the following query:
Source code
Java
List<ButtonElement> buttons = $(ButtonElement.class).all();The query returns a list of HTML elements containing all of the Button components in the UI. The buttons found by the query could be controlled, for example, by clicking them like so:
Source code
Java
for (ButtonElement button : buttons)
button.click();Element Query Methods
The $() method creates an ElementQuery that looks for the given element class. This method is available both for TestBenchTestCase (i.e., searches the whole current page) and in TestBenchElement (i.e., searches inside the given element).
Source code
Java
// Find the button with id="ok"
ButtonElement button = $(ButtonElement.class).id("ok");Source code
Java
// Find the span inside the layout with id="content"
VerticalLayoutElement layout = $(VerticalLayoutElement.class).id("content");
SpanElement span = layout.$(SpanElement.class).single();|
Note
|
If there is no suitable element class available, you can also use the $("tag-name") method to find an element of a given type.
|
You can use the ElementQuery instance returned by $() to refine the search query. The following methods narrow the search and return the query, so you can chain them:
-
withId()keeps the elements with the given id. -
withAttribute()keeps the elements that have the given attribute, or that have it with the given value.withAttributeContaining()matches a part of the value.withAttributeContainingWord()matches a word in a space-separated value, such as one of several class names. -
withClassName()andwithTheme()keep the elements with the given class names or theme. -
withText(),withLabel()andwithPlaceholder()keep the elements whose text, label or placeholder equals the given value. -
withCaption()compares the given value with the label of the element. When the label is empty, it compares with the placeholder. For an element that has neither, such as a button, it compares with the text. -
withTextContaining(),withLabelContaining(),withPlaceholderContaining()andwithCaptionContaining()match a part of the value. -
withPropertyValue()keeps the elements whose property getter returns the given value, for examplewithPropertyValue(TextFieldElement::getHelperText, "Required"). -
withCondition()keeps the elements that satisfy the given predicate, for examplewithCondition(TestBenchElement::isEnabled). -
onPage()searches the whole page instead of the current element.
The attribute, class name and theme filters have counterparts that exclude the matching elements, such as withoutAttribute() and withoutClassName().
The following methods run the query and return the result:
-
id("some-id")returns the element with the given id. -
single()returns the only matching element. It throws aNoSuchElementExceptionwhen no element or more than one element matches. -
waitForSingle()returns the only matching element. Until exactly one element matches, it keeps searching, by default for up to ten seconds. -
get(N)returns the element at index N among the matching elements, counting from zero. -
last()returns the last matching element. -
exists()returnstrueif the query matches at least one element. -
all()returns a list of all matching elements.
Use single() when the query should match exactly one element. If a change in the UI makes the query match several elements, the test fails instead of picking one of them. Use get() only when picking one of several matching elements is intentional.
Writing Tests
Using Element Queries and Elements you can now compose test methods like so:
Source code
Java
@Test
public void fillForm() {
$(TextFieldElement.class).id("firstName").setValue("John");
$(TextFieldElement.class).id("lastName").setValue("Doe");
$(ButtonElement.class).id("ok").click();
Assert.assertEquals("Thank you for submitting the form", $(DivElement.class).id("result").getText());
}Java
Java
Java
Be aware that if you write tests in this manner, you can have difficulty maintaining the tests. A good way to structure tests is to have only the high-level logic in the test itself — your manager should be able to read and understand the test method — and extract the ElementQuery parts to a separate Page Object class.
See Creating Maintainable Tests using Page Objects for more information.
25C2F30D-0C20-4994-B50D-B7C4B5E09FBB