http://www.softwaretestinghelp.com/webdriver-commands-selenium-tutorial-14/
How to check visibility of web elements using various types of looping and conditional commands in WebDriver:
Previously in the series, we discussed about WebDriver’s Select classwhich is primarily used to handle web elements like dropdowns and selecting various options under the dropdowns.
Moving ahead in the Selenium series, we would be discussing about the various types of looping and conditional commands in WebDriver like isSelected(), isEnabled() and isDispalyed(). These methods are used to determine the visibility scope for the web elements.
So let us start with a brief introduction – WebDriver has a W3C specification that details out the information about the different visibility preferences based out on the types of the web elements upon which the actions are to be performed.
WebDriver facilitates the user with the following methods to check the visibility of the web elements. These web elements can be buttons, dropboxes, checkboxes, radio buttons, labels etc.
- isDisplayed()
- isSelected()
- isEnabled()
For an improved understanding, let us discuss the aforementioned methods with code examples.
As a specimen, we would be using the “google.com” as an application under test and the “Learning_Selenium” project created in the previous tutorials for script generation.
Scenario to be automated
- Launch the web browser and open the application under test – http://google.com
- Verify the web page title
- Verify if the “Google Search” button is displayed
- Enter the keyword in the “Google Search” text box by which we would want to make the request
- Verify that the “Search button” is displayed and enabled
- Based on visibility of the Search button, click on the search button
WebDriver Code
Step 1: Create a new java class named as “VisibilityConditions” under the “Learning_Selenium” project.
Step 2: Copy and paste the below code in the “VisibilityConditions.java” class.
Below is the test script that is equivalent to the above mentioned scenario:
1 |
import org.openqa.selenium.By;
|
2 |
import org.openqa.selenium.WebDriver;
|
3 |
import org.openqa.selenium.WebElement;
|
4 |
import org.openqa.selenium.firefox.FirefoxDriver;
|
6 |
public class VisibilityConditions {
|
12 |
public static void main(String[] args) {
|
15 |
WebDriver driver = new FirefoxDriver();
|
16 |
String appUrl = "https://google.com" ;
|
22 |
driver.manage().window().maximize();
|
25 |
String expectedTitle = "Google" ;
|
28 |
String actualTitle = driver.getTitle();
|
31 |
if (expectedTitle.equals(actualTitle))
|
33 |
System.out.println( "Verification Successful - The correct title is displayed on the web page." );
|
37 |
System.out.println( "Verification Failed - An incorrect title is displayed on the web page." );
|
41 |
boolean submitbuttonPresence=driver.findElement(By.id( "gbqfba" )).isDisplayed();
|
42 |
System.out.println(submitbuttonPresence);
|
45 |
WebElement searchTextBox = driver.findElement(By.id( "gbqfq" ));
|
46 |
searchTextBox.clear();
|
47 |
searchTextBox.sendKeys( "Selenium" );
|
50 |
boolean searchIconPresence = driver.findElement(By.id( "gbqfb" )).isDisplayed();
|
51 |
boolean searchIconEnabled = driver.findElement(By.id( "gbqfb" )).isEnabled();
|
53 |
if (searchIconPresence== true && searchIconEnabled== true )
|
56 |
WebElement searchIcon = driver.findElement(By.id( "gbqfb" ));
|
62 |
System.out.println( "Test script executed successfully." );
|
Code Walkthrough
Following are the ways in which we ascertain the presence of web elements on the web page.
booleansubmitbuttonPresence=driver.findElement(By.id(“gbqfba”)).isDisplayed();
isDispalyed()
isDisplayed() is the method used to verify presence of a web element within the webpage. The method is designed to result a Boolean value with each success and failure. The method returns a “true” value if the specified web element is present on the web page and a “false” value if the web element is not present on the web page.
Thus the above code snippet verifies for the presence of submit button on the google web page and returns a true value if the submit button is present and visible else returns a false value if the submit button is not present on the web page.
boolean searchIconEnabled = driver.findElement(By.id(“gbqfb”)).isEnabled();
The method deals with the visibility of all kinds of web elements not just limiting to any one type.
isEnabled()
isEnabled() is the method used to verify if the web element is enabled or disabled within the webpage. Like isDisplayed() method, it is designed to result a Boolean value with each success and failure. The method returns a “true” value if the specified web element is enabled on the web page and a “false” value if the web element is not enabled (state of being disabled) on the web page.
Thus the above code snippet verifies if the submit button is enabled or not and returns a Boolean value depending on the result.
The isEnabled() method is significant in scenarios where we want to ascertain that only if “Condition A” is fulfilled, then the element(principally button) is enabled. Refer the following illustration for the same.
In the above figure, Register button button is enabled only when the agreement checkbox is selected.
Akin to above methods, we have a method referenced as “isSelected()” which tests if the specified web element is selected or not.
boolean searchIconSelected = driver.findElement(By.id(“male”)).isSelected();
isSelected()
isSelected() is the method used to verify if the web element is selected or not. isSelected() method is pre-dominantly used with radio buttons, dropdowns and checkboxes. Analogous to above methods, it is designed to result a Boolean value with each success and failure.
Thus the above code snippet verifies if the male radio button is selected or not and returns a Boolean value depending on the result. Refer the following image for the same.
Conclusion
In this tutorial, we tried to make you acquainted with the WebDriver’s looping and conditional operations. These conditional methods often deal with almost all types of visibility options for web elements.
Article Summary:
- WebDriver has a W3C specification that details out the information about the different visibility preferences based out on the types of the web elements.
- isDisplayed() is the method used to verify presence of a web element within the webpage. The method returns a “true” value if the specified web element is present on the web page and a “false” value if the web element is not present on the web page.
- isDisplayed() is capable to check for the presence of all kinds of web elements available.
- isEnabled() is the method used to verify if the web element is enabled or disabled within the webpage.
- isEnabled() is primarily used with buttons.
- isSelected() is the method used to verify if the web element is selected or not. isSelected() method is pre-dominantly used with radio buttons, dropdowns and checkboxes.
Next Tutorial #15: While working on web applications, often we are re-directed to different web pages by refreshing the entire web page and re-loading the new web elements. At times there can be Ajax calls as well. Thus, a time lag can be seen while reloading the web pages and reflecting the web elements. Thus, our next tutorial in-line is all about dealing with such time lags by using implicit and explicit waits.
Note for the Readers: Till then, the reader can automate and test the visibility scope for the web elements using WebDriver’s methods
相关推荐
Selenium WebDriver 3 Practical Guide will walk you through the various APIs of Selenium WebDriver, which are used in automation tests, followed by a discussion of the various WebDriver implementations...
对于Web驱动程序(如ChromeDriver或GeckoDriver),需要下载对应浏览器版本的驱动,并将其添加到系统路径中,这样Selenium才能找到并使用它。例如,对于Firefox,可以从Mozilla官网下载GeckoDriver,然后将其路径...
综合以上信息,我们可以了解到《Test Automation Using Selenium WebDriver with Java》是一本专注于教授如何使用Java语言结合Selenium WebDriver进行Web应用自动化测试的实用指南。它涵盖了自动化测试的基本概念、...
Solve your Selenium WebDriver problems with this quick guide to automated testing of web applications with Selenium WebDriver in C#. Selenium WebDriver Recipes in C#, Second Edition contains hundreds ...
Selenium WebDriver 是一款广泛使用的自动化测试工具,专为Web应用程序设计。它允许程序员模拟真实用户在浏览器中的操作,如点击、输入、导航等,从而进行功能性和兼容性测试。了解其工作原理对于优化自动化测试脚本...
Selenium:SeleniumWebDriver基础操作教程.docx
【Selenium基础与实战】 ...通过这个项目,学习者不仅可以掌握Selenium WebDriver的基本使用,还能了解到自动化测试的最佳实践,从而提升测试效率和质量,为Web应用程序的开发和维护提供强大的支持。
Selenium WebDriver是一款强大的自动化测试工具,它允许程序员模拟真实用户在浏览器上的操作,进行Web应用程序的功能测试和验收测试。在Java环境下,Selenium WebDriver通常需要引入相应的jar包才能正常工作。...
Selenium2 WebDriver 是一个流行的自动化测试工具,用于模拟用户交互来测试Web应用程序。本文档将详细介绍 Selenium2 WebDriver 的安装、配置、基本操作和使用技巧。 安装 Selenium WebDriver 1. 安装 Firefox:...
《零成本实现Web自动化测试-基于Selenium和Bromine》是温素剑撰写的一本技术书籍,专注于介绍如何在不产生额外费用的情况下,利用开源工具进行高效的Web自动化测试。书中的内容涵盖了一系列与Web自动化测试相关的...
自动化测试:Selenium webdriver学习笔记 C#版 在本篇笔记中,我们将讨论 Selenium webdriver 的自动化测试中的对象定位方法。对象定位是自动化测试中非常重要的一步骤,它决定了我们的测试脚本是否能够正确地找到...
Over 90 recipes to help you build and run automated tests for your web applications with Selenium WebDriver About This Book Learn to leverage the power of Selenium WebDriver with simple examples that...
总的来说,Selenium WebDriver是一个强大的自动化测试工具,尤其对于Web应用的测试。了解其安装、配置和服务启动的方法是进行自动化测试的基础,而通过编写测试脚本,可以有效地提升测试效率,确保产品的质量。
本书是一本从入门到精通模式的Selenium WebDriver实战经验分享书籍。全书共分为四个部分:第1部分基础篇主要讲解自动化测试相关的基础理论、WebDriver 环境安装、单元测试工具的使用方法以及 WebDrvier的入门使用...
Ruby Selenium Web驱动程序是用于自动化Web浏览器操作的强大工具,它允许开发者使用Ruby语言编写脚本来控制浏览器的行为。这个工具主要用于Web应用的测试,但也可以用于其他需要浏览器交互的场景。在Ruby中,...
在IT行业的测试领域,Selenium是一个非常流行的自动化测试工具,它允许用户编写脚本来自动执行浏览器操作,从而实现对Web应用的功能性测试。在Selenium的众多组件中,WebDriver是核心的一部分,它提供了与不同浏览器...