ITPUB论坛-中国最专业的IT技术社区

 找回密码
 注册
查看: 146|回复: 6

Tutorial #14 – Looping and Conditional commands

[复制链接]
认证徽章
论坛徽章:
982
紫蜘蛛
日期:2015-10-15 14:48:45紫蜘蛛
日期:2015-10-15 13:48:52紫蜘蛛
日期:2015-09-22 15:53:22紫蜘蛛
日期:2015-10-15 14:47:47紫蜘蛛
日期:2015-10-15 14:45:48九尾狐狸
日期:2015-10-15 14:47:47九尾狐狸
日期:2015-10-15 14:45:48九尾狐狸
日期:2015-10-15 14:48:45九尾狐狸
日期:2015-10-15 13:50:37九尾狐狸
日期:2015-09-22 15:53:22
发表于 2017-6-18 00:15 | 显示全部楼层 |阅读模式
Check Visibility of Web Elements Using Various Types 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

What You Will Learn:




认证徽章
论坛徽章:
982
紫蜘蛛
日期:2015-10-15 14:48:45紫蜘蛛
日期:2015-10-15 13:48:52紫蜘蛛
日期:2015-09-22 15:53:22紫蜘蛛
日期:2015-10-15 14:47:47紫蜘蛛
日期:2015-10-15 14:45:48九尾狐狸
日期:2015-10-15 14:47:47九尾狐狸
日期:2015-10-15 14:45:48九尾狐狸
日期:2015-10-15 14:48:45九尾狐狸
日期:2015-10-15 13:50:37九尾狐狸
日期:2015-09-22 15:53:22
发表于 2017-6-18 00:18 | 显示全部楼层
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;

  5. public class VisibilityConditions {

  6.        /**
  7. * @param args
  8. */

  9.        public static void main(String[] args) {

  10.               // objects and variables instantiation
  11. WebDriver driver = new FirefoxDriver();
  12. String appUrl = "https://google.com";

  13.               // launch the firefox browser and open the application url
  14. driver.get(appUrl);

  15.               // maximize the browser window
  16. driver.manage().window().maximize();

  17.               // declare and initialize the variable to store the expected title of the webpage.
  18. String expectedTitle = "Google";

  19.               // fetch the title of the web page and save it into a string variable
  20. String actualTitle = driver.getTitle();

  21.               // compare the expected title of the page with the actual title of the page and print the result
  22. if (expectedTitle.equals(actualTitle))
  23. {
  24. System.out.println("Verification Successful - The correct title is displayed on the web page.");
  25. }
  26. else
  27. {
  28. System.out.println("Verification Failed - An incorrect title is displayed on the web page.");
  29. }

  30.               // verify if the “Google Search” button is displayed and print the result
  31. boolean submitbuttonPresence=driver.findElement(By.id("gbqfba")).isDisplayed();
  32. System.out.println(submitbuttonPresence);

  33.               // enter the keyword in the “Google Search” text box by which we would want to make the request
  34. WebElement searchTextBox = driver.findElement(By.id("gbqfq"));
  35. searchTextBox.clear();
  36. searchTextBox.sendKeys("Selenium");

  37.               // verify that the “Search button” is displayed and enabled
  38. boolean searchIconPresence = driver.findElement(By.id("gbqfb")).isDisplayed();
  39. boolean searchIconEnabled = driver.findElement(By.id("gbqfb")).isEnabled();

  40.               if (searchIconPresence==true && searchIconEnabled==true)
  41. {
  42. // click on the search button
  43. WebElement searchIcon = driver.findElement(By.id("gbqfb"));
  44. searchIcon.click();
  45. }

  46.               // close the web browser
  47. driver.close();
  48. System.out.println("Test script executed successfully.");

  49.               // terminate the program
  50. System.exit(0);
  51. }
  52. }
复制代码

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();

使用道具 举报

回复
认证徽章
论坛徽章:
982
紫蜘蛛
日期:2015-10-15 14:48:45紫蜘蛛
日期:2015-10-15 13:48:52紫蜘蛛
日期:2015-09-22 15:53:22紫蜘蛛
日期:2015-10-15 14:47:47紫蜘蛛
日期:2015-10-15 14:45:48九尾狐狸
日期:2015-10-15 14:47:47九尾狐狸
日期:2015-10-15 14:45:48九尾狐狸
日期:2015-10-15 14:48:45九尾狐狸
日期:2015-10-15 13:50:37九尾狐狸
日期:2015-09-22 15:53:22
发表于 2017-6-18 00:19 | 显示全部楼层
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.

使用道具 举报

回复
认证徽章
论坛徽章:
982
紫蜘蛛
日期:2015-10-15 14:48:45紫蜘蛛
日期:2015-10-15 13:48:52紫蜘蛛
日期:2015-09-22 15:53:22紫蜘蛛
日期:2015-10-15 14:47:47紫蜘蛛
日期:2015-10-15 14:45:48九尾狐狸
日期:2015-10-15 14:47:47九尾狐狸
日期:2015-10-15 14:45:48九尾狐狸
日期:2015-10-15 14:48:45九尾狐狸
日期:2015-10-15 13:50:37九尾狐狸
日期:2015-09-22 15:53:22
发表于 2017-6-18 00:19 | 显示全部楼层
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.
Webdriver-commands-11.jpg
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();

使用道具 举报

回复
认证徽章
论坛徽章:
982
紫蜘蛛
日期:2015-10-15 14:48:45紫蜘蛛
日期:2015-10-15 13:48:52紫蜘蛛
日期:2015-09-22 15:53:22紫蜘蛛
日期:2015-10-15 14:47:47紫蜘蛛
日期:2015-10-15 14:45:48九尾狐狸
日期:2015-10-15 14:47:47九尾狐狸
日期:2015-10-15 14:45:48九尾狐狸
日期:2015-10-15 14:48:45九尾狐狸
日期:2015-10-15 13:50:37九尾狐狸
日期:2015-09-22 15:53:22
发表于 2017-6-18 00:20 | 显示全部楼层
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.

使用道具 举报

回复
认证徽章
论坛徽章:
982
紫蜘蛛
日期:2015-10-15 14:48:45紫蜘蛛
日期:2015-10-15 13:48:52紫蜘蛛
日期:2015-09-22 15:53:22紫蜘蛛
日期:2015-10-15 14:47:47紫蜘蛛
日期:2015-10-15 14:45:48九尾狐狸
日期:2015-10-15 14:47:47九尾狐狸
日期:2015-10-15 14:45:48九尾狐狸
日期:2015-10-15 14:48:45九尾狐狸
日期:2015-10-15 13:50:37九尾狐狸
日期:2015-09-22 15:53:22
发表于 2017-6-18 00:20 | 显示全部楼层
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.

使用道具 举报

回复
认证徽章
论坛徽章:
982
紫蜘蛛
日期:2015-10-15 14:48:45紫蜘蛛
日期:2015-10-15 13:48:52紫蜘蛛
日期:2015-09-22 15:53:22紫蜘蛛
日期:2015-10-15 14:47:47紫蜘蛛
日期:2015-10-15 14:45:48九尾狐狸
日期:2015-10-15 14:47:47九尾狐狸
日期:2015-10-15 14:45:48九尾狐狸
日期:2015-10-15 14:48:45九尾狐狸
日期:2015-10-15 13:50:37九尾狐狸
日期:2015-09-22 15:53:22
发表于 2017-6-18 00:20 | 显示全部楼层

使用道具 举报

回复

您需要登录后才可以回帖 登录 | 注册

本版积分规则

TOP技术积分榜 社区积分榜 徽章 电子杂志 团队 统计 虎吧 老博客 知识索引树 读书频道 积分竞拍 文本模式 帮助
  ITPUB首页 | ITPUB论坛 | 数据库技术 | 企业信息化 | 开发技术 | 微软技术 | 软件工程与项目管理 | IBM技术园地 | 行业纵向讨论 | IT招聘 | IT文档 | IT博客
  ChinaUnix | ChinaUnix博客 | ChinaUnix论坛 | SAP ERP系统
CopyRight 1999-2011 itpub.net All Right Reserved. 北京皓辰网域网络信息技术有限公司版权所有 联系我们 网站律师 隐私政策 知识产权声明
京ICP证:060528号 北京市公安局海淀分局网监中心备案编号:1101082001 广播电视节目制作经营许可证:编号(京)字第1149号
  
快速回复 返回顶部 返回列表