Checking an element's state
Many a time a test fails to click on an element or enter text in a field, as the element is disabled or exists in the DOM but is hidden on the page. This will result in an error being thrown and the test resulting in failure. To build reliable tests that can run unattended, a robust exception and error handling is needed in the test flow.
We can handle these problems by checking the state of elements. The WebElement
interface provides the following methods to check the state of an element:
Method |
Purpose |
---|---|
|
This method checks if an element is enabled. It returns |
|
This method checks if an element is selected (radio button, checkbox, and so on). It returns |
|
This method checks if an element is displayed. |
In this recipe, we will use some of these methods to check the status and handle possible errors.
How to do it...
We will create...