How does Selenium isDisplayed() Method Work?
In Selenium, the isDisplayed() method is pivotal for web testing, determining if web elements are visible or hidden. This method checks an element's CSS properties for visibility and returns a boolean: true if visible, false if hidden or absent in the DOM. Understanding isDisplayed() is crucial for actions like clicking or sending input, and it aligns with Selenium's W3C specifications for element visibility. Selenium's versatility in automating web applications, a favourite in open-source automation tools, shines through methods like isDisplayed(). It requires locating elements via IDs, names, class names, tag names, CSS selectors, or XPath. This article delves into isDisplayed() usage, a testament to Selenium's robustness in identifying and acting on web elements, reflecting its widespread adoption and efficiency in automated browser testing scenarios.
What is isDisplayed() Method in Selenium?
The isDisplayed() method in Selenium is used to determine the visibility status of a web element on a webpage. It allows automation scripts to check whether an element is currently visible to the user or hidden from view. This method takes into account multiple factors to determine visibility, including the element's size, presence in the Document Object Model (DOM), and the visibility property of both the element itself and its ancestors.
When the isDisplayed() method is invoked on a web element, it performs the following checks:
- Presence in the DOM: It verifies whether the element is present in the Document Object Model (DOM) of the webpage. If the element is not present in the DOM, it is considered not displayed.
- Size and dimensions: It checks the width and height of the element. If the element has a non-zero width and height, it is considered displayed. However, even if an element has a non-zero size, it may still be visually hidden if it has a CSS property set to hide it or position it off-screen.
- Visibility property: It examines the CSS visibility property of the element and its ancestors in the DOM hierarchy. Suppose any of the elements in the hierarchy have a visibility property set to "hidden" or "collapse", the element is considered hidden and not displayed. If the visibility property is set to "visible" or not explicitly defined, the element is considered displayed.
The isDisplayed() method returns a boolean value indicating the visibility status of the element. It is commonly used in test automation scenarios to verify the presence and visibility of elements before performing actions or assertions. By using this method, automation scripts can ensure that they interact only with visible elements, providing accurate and reliable testing results.
Syntax
The syntax for using the isDisplayed() method in Selenium WebDriver is as follows:
- driver refers to the WebDriver instance used for browser automation.
- findElement(By.locator("elementLocator")) is used to locate the web element on the webpage using a suitable locator strategy (e.g., By.id, By.name, By.xpath, etc.).
- isDisplayed() is invoked on the located element to check its visibility status.
- The boolean value isDisplayed will be assigned true if the element is displayed and visible or false if it is hidden or not visible.
How to Use isDisplayed() Method in Selenium?
To use the isDisplayed() method in Selenium, you can follow these steps:
- Create an instance of the WebDriver and open a webpage:
- Locate the web element using a suitable locator strategy (e.g., By.id, By.xpath, By.cssSelector, etc.):
- Use the isDisplayed() method on the WebElement to check its visibility:
- Perform actions or assertions based on the visibility status:
Some cases where we use isdisplayed() in Selenium:
- User Interface Validation: You can use isDisplayed() to verify the visibility of specific elements on a webpage. For example, you might want to ensure that a "Submit" button is displayed after filling out a form. You can write a test script that finds the button element and checks if it is displayed using the isDisplayed() method.
- Dynamic Content Verification: In some cases, web pages may have dynamically loaded content. You can use isDisplayed() to wait for the presence of certain elements before proceeding with further actions. For instance, if a page displays a loading spinner while fetching data via AJAX, you can wait until the spinner is no longer displayed using isDisplayed() before interacting with the loaded content.
- Error Message Validation: Error messages are often displayed when form validation fails. By using isDisplayed(), you can verify whether the error message is visible after submitting an invalid form. This ensures that the error message is properly shown to the user.
- Dropdown Menu Testing: Dropdown menus are a common UI component. You can use isDisplayed() to verify the visibility of dropdown options. For example, if a dropdown menu appears when a specific action is performed, you can write a test case to check whether the dropdown options are displayed when the action is triggered.
- Image Verification: Images play a crucial role in many applications. You can use isDisplayed() to check if images are correctly loaded and displayed on a page. This can be useful when performing visual validation tests or verifying that the correct image is being shown.
- Tab Switching: Many applications use tabs or tabbed navigation. You can utilize isDisplayed() to validate whether a specific tab is active or visible. This can be helpful in ensuring that the expected tab is selected and its associated content is being displayed correctly.
What is isEnabled() Method in Selenium?
The isEnabled() method in Selenium is a built-in method provided by the Selenium WebDriver library. It is used to determine whether an HTML element on a web page is enabled or disabled. The method returns a Boolean value indicating the status of the element.
When an element is enabled, it means that it can receive user interactions such as clicking, typing, or selecting. On the other hand, if an element is disabled, it is typically greyed out or non-interactive, and users cannot interact with it.
The isEnabled() method is primarily used to perform assertions or conditional operations based on the state of an element. For example, before clicking a button, you may want to check if it is enabled to ensure that the action can be performed.
Syntax
The syntax for the isEnabled() method in Selenium WebDriver is as follows:
- webElement is an instance of the WebElement class representing the HTML element you want to check for enabled or disabled status. You can obtain the WebElement instance by using one of the various locating mechanisms provided by Selenium, such as findElement() or findElements(), or by performing actions on elements, such as interacting with dropdowns or checkboxes.
- isEnabled() is the method that checks whether the element is enabled or disabled. It returns a boolean value indicating the status of the element. If the element is enabled, the method returns true; if it is disabled, it returns false.
- boolean isEnabled is a variable that captures the result of the isEnabled() method. It is of type boolean since the method returns a boolean value.
Let's look into a sample code using isEnabled() method:
Code Explanation
In this example, we find an input element on the web page using the findElement() and the By.id() locators. Then, we call the isEnabled() method on the WebElement instance inputElement and store the result in the isEnabled variable. Finally, we use an if statement to check the value of isEnabled and print the appropriate message.
Some cases where we use isEnabled() in Selenium:
- Form validation: Before submitting a form, you can use isEnabled() to check if all the required input fields and buttons are enabled. This helps ensure that the form is in a valid state for submission.
- Button availability: When interacting with buttons on a webpage, you can use isEnabled() to determine if a button is enabled or disabled based on certain conditions. For example, a "Submit" button may be disabled until all required fields are filled out.
- User input validation: When entering data into input fields, you can use isEnabled() to verify if additional input is allowed or if the field is read-only. This helps ensure that users provide valid and necessary information.
- Dynamic elements: In cases where elements on a webpage are dynamically enabled or disabled based on certain events or conditions, you can use isEnabled() to check if an element is currently enabled for interaction.
- Conditional actions: Based on the enabled or disabled state of an element, you can conditionally perform certain actions. For example, if a button is disabled, you may want to display an error message or take alternative actions.
What is isSelected() Method in Selenium?
The isSelected() method in Selenium is a built-in method that is used to determine if a web element, such as a checkbox or a radio button, is currently selected or not. It is part of the Selenium WebDriver API, which is a popular tool for automating web browsers.
When working with web forms that contain checkboxes or radio buttons, it is often necessary to check whether a particular option is selected or not. This is where the isSelected() method becomes useful.
The isSelected() method is typically applied to a WebElement object, which represents an element on a web page. By invoking this method on a checkbox or radio button element, you can determine if it is currently selected or not.
The isSelected() method returns a boolean value, either true or false, indicating the selection status of the element. If the element is selected, the method will return true; otherwise, it will return false.
Syntax
The syntax for using the isSelected() method in Selenium:
Sample code demonstrating the use of the isSelected() method is as follows:
Code Explanation
In this example, we first set the path to the ChromeDriver executable using System.setProperty() method. Then, we create a new instance of the ChromeDriver. Next, we navigate to a web page using the get() method. After that, we locate the checkbox element on the page using findElement() method with a suitable locator (in this case, we're using an ID-based locator). Finally, we call the isSelected() method on the checkbox element to determine if it is selected or not. The result is then printed to the console.
Some cases where we use isSelected() in Selenium:
- Checkbox selection verification: When automating a form that contains checkboxes, you can use isSelected() to verify if a checkbox is selected or not. This allows you to validate that the expected checkboxes are checked or unchecked based on the test conditions.
- Radio button selection validation: In forms with radio buttons, you can use isSelected() to ensure that the appropriate radio button option is selected. It allows you to confirm that only one option is selected at a time, as intended.
- Multi-select list validation: When dealing with multi-select lists or dropdowns that permit multiple selections, isSelected() can help you verify the selected options. By iterating through each option and using isSelected(), you can ensure that the expected options are chosen.
- Pre-selection confirmation: Some webpages may have pre-selected checkboxes or radio buttons based on default values or user preferences. By using isSelected(), you can confirm that the expected options are already selected, saving unnecessary interaction and ensuring the correct starting state for your tests.
- Conditional operations: In certain scenarios, the selection of a checkbox or radio button may trigger different behaviour or enable/disable certain elements on the webpage. By using isSelected(), you can perform conditional operations based on the selection status of these elements.
FAQs
Q. What is the difference between isEnabled() and isDisplayed()?
A. The table below summarizes the main differences between the isEnabled and isDisplayed properties:
Property | isEnabled | isDisplayed |
---|---|---|
Definition | Indicates if an element is enabled | Indicates if an element is displayed |
Applicable | All types of elements | All types of elements |
Visibility | Can be enabled but not visible | Must be both enabled and visible |
User input | Can receive user input | May or may not receive user input |
CSS | Not related to CSS | Not related to CSS |
Implications | Inactive or grayed out appearance | Hidden or not rendered on the page |
Usage | To check the availability of an element for interaction or input | To check the visibility of an element on the page |
Methods | isEnabled() | isDisplayed() |
Return value | true if enabled, false otherwise | true if displayed, false otherwise |
Q. What is the difference between isDisplayed() and isVisible()?
A.
Property | isDisplayed | isVisible |
---|---|---|
Definition | Indicates if an element is visible to the user | Indicates if an element is visible, considering various factors |
Applicable | Web automation/testing frameworks | Web development, user interface design |
Visibility | Considers if the element is visible and not explicitly hidden through CSS or other means | Considers visual presence, accessibility, and other factors related to visibility |
CSS | Not directly related to CSS | Can consider CSS styles in determining visibility |
Partial Visibility | May consider an element as displayed even if partially or fully obscured by other elements | Considers if an element is within the viewport or scrollable area |
Usage | To determine if an element is visible to the user in automation/testing | To assess the overall visibility of an element in web development and design |
Methods | isDisplayed() | Not a standard method, may vary depending on the framework or context |
Return value | true if displayed, false otherwise | Can vary depending on the specific definition and implementation |
Q. What is the use of isDisplayed in Selenium?
A. The isDisplayed() in Selenium is used to check the visibility of a web element on a webpage. It is a frequently used method in web automation to verify if an element is visible to the user or not. The isDisplayed method returns a boolean value, true if the element is visible and false if it is not.
Q. What is the difference between isExisting and isDisplayed in Webdriverio?
A. In WebdriverIO, isExisting and isDisplayed are two methods used to interact with elements in the browser. Here are the differences between these two methods:
isExisting:
- Definition: Checks if an element exists in the DOM (Document Object Model), regardless of its visibility.
- Usage: Use isExisting to determine if an element is present in the DOM or not.
- Return value: Returns a boolean value (true if the element exists, false if it does not).
isDisplayed:
- Definition: Checks if an element is visible on the page.
- Usage: Use isDisplayed to verify if an element is visible to the user.
- Return value: Returns a boolean value (true if the element is visible, false if it is not).
Q. What is the difference between isDisplayed and isPresent?
A. The isDisplayed and isPresent methods are commonly used in test automation frameworks to interact with web elements and assess their presence and visibility. Here are the differences between these two methods:
isDisplayed:
- Definition: Determines if an element is visible on the webpage.
- Usage: Used to check the visibility of an element to the user.
- Return value: Returns a boolean value (true if the element is visible, false if it is not).
- Considerations: It takes into account various factors such as CSS properties, size, and position.
isPresent:
- Definition: Checks if an element exists in the DOM, regardless of its visibility.
- Usage: Used to determine if an element is present in the DOM structure of the webpage.
- Return value: Returns a boolean value (true if the element is present, false if it is not).
- Considerations: It does not consider the visibility or appearance of the element.
Conclusion
- The isDisplayed() method in Selenium is used to determine the visibility status of a web element on a webpage.
- It checks factors such as presence in the DOM, size and dimensions, and CSS visibility property to determine visibility. The method returns a boolean value (true if the element is visible, false if it is hidden).
- It is commonly used in test automation scenarios to verify the presence and visibility of elements before performing actions or assertions.
- The isEnabled() method in Selenium is used to determine if an HTML element is enabled or disabled. It returns a boolean value (true if the element is enabled, false if it is disabled).
- The isSelected() method is used to determine if a checkbox or radio button element is currently selected or not. It returns a boolean value (true if the element is selected, false if it is not selected).
- These methods are essential for making informed decisions and interacting with elements accurately during automation testing.