Checking an element's status
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 not displayed on the page; this will result in an error being thrown and the test resulting in failures. For building 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
class provides the following methods to check the state of an element:
Method |
Purpose |
---|---|
|
This method checks if an element is enabled. Returns |
|
This method checks if element is selected (radio button, checkbox, and so on). It returns |
|
This method checks if 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 a test...