当前位置: 首页 > article >正文

Selenium + Webdriver 学习(六) 自动选择、检查下拉列表

下面我们来看一下selenium webdriver是如何来处理select下拉框的,以Apple注册页面为例。

https://appleid.apple.com/cgi-bin/WebObjects/MyAppleId.woa/wa/createAppleId


[java] view plain copy print ?
  1. package com.annie.test;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.Arrays;  
  5. import java.util.List;  
  6.   
  7. import org.openqa.selenium.By;  
  8. import org.openqa.selenium.WebDriver;  
  9. import org.openqa.selenium.WebElement;  
  10. import org.openqa.selenium.firefox.FirefoxDriver;  
  11. import org.openqa.selenium.support.ui.Select;  
  12. import static org.junit.Assert.*;  
  13. import org.junit.Test;  
  14.   
  15. public class SelectTest {  
  16.     @Test  
  17.     public void testDropdown() {  
  18.         // System.setProperty("webdriver.firefox.bin","D:\\Program Files\\Mozilla Firefox\\firefox.exe");   
  19.         WebDriver dr = new FirefoxDriver();  
  20.         dr  
  21.                 .get("https://appleid.apple.com/cgi-bin/WebObjects/MyAppleId.woa/wa/createAppleId");  
  22.   
  23.         // 通过下拉列表中选项的索引选中第二项,   
  24.         // 即What is the first name of your best friend in high school?   
  25.         Select sq1 = new Select(dr.findElement(By.id("security-question_1")));  
  26.         sq1.selectByIndex(2);  
  27.   
  28.         // 通过下拉列表中的选项的value属性选中"January"value=1 这一项   
  29.         Select selectMon = new Select(dr.findElement(By.id("month")));  
  30.         selectMon.selectByValue("1");  
  31.         assertFalse(selectMon.isMultiple());// 验证下拉列表的不支持多选   
  32.         // assertEquals(4,selectMon().size()); //验证下拉数量   
  33.         Select selectDay = new Select(dr.findElement(By.id("day")));  
  34.         selectDay.selectByVisibleText("23");  
  35.         /** 检查下拉列表的选项 */  
  36.         // 预期的选项内容StateOptions   
  37.         List<String> StateOptions = Arrays.asList(new String[] {  
  38.                 "Please select""Alabama""Alaska""Arizona""Arkansas",  
  39.                 "Armed Forces Americas""Armed Forces Europe",  
  40.                 "Armed Forces Pacific""California""Colorado",  
  41.                 "Connecticut""Delaware""Dist of Columbia""Florida",  
  42.                 "Georgia""Guam""Hawaii""Idaho""Illinois""Indiana",  
  43.                 "Iowa""Kansas""Kentucky""Louisiana""Maine""Maryland",  
  44.                 "Massachusetts""Michigan""Minnesota""Mississippi",  
  45.                 "Missouri""Montana""Nebraska""Nevada""New Hampshire",  
  46.                 "New Jersey""New Mexico""New York""North Carolina",  
  47.                 "North Dakota""Ohio""Oklahoma""Oregon""Pennsylvania",  
  48.                 "Puerto Rico""Rhode Island""South Carolina",  
  49.                 "South Dakota""Tennessee""Texas""Utah""Vermont",  
  50.                 "Virgin Islands""Virginia""Washington""West Virginia",  
  51.                 "Wisconsin""Wyoming" });  
  52.   
  53.         /** 遍历一下下拉列表所有选项,用click进行选中选项 **/  
  54.         Select selectState = new Select(dr.findElement(By.id("state-province")));  
  55.         List<String> act_StateOptions = new ArrayList<String>();  
  56.         // 判断选择内容   
  57.         assertEquals("Please select", selectState.getFirstSelectedOption()  
  58.                 .getText());  
  59.         for (WebElement e : selectState.getOptions()) {  
  60.             e.click();  
  61.         //  s = s + "\"" + e.getText() + "\"" + ",";   
  62.             // 将实际的下拉列表内容注入到act_StateOptions中,进行比较。   
  63.             act_StateOptions.add(e.getText());  
  64.         }  
  65.         assertArrayEquals(StateOptions.toArray(), act_StateOptions.toArray());  
  66.   
  67.     }  
  68. }  
package com.annie.test;import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
import static org.junit.Assert.*;
import org.junit.Test;public class SelectTest {@Testpublic void testDropdown() {// System.setProperty("webdriver.firefox.bin","D:\\Program Files\\Mozilla Firefox\\firefox.exe");WebDriver dr = new FirefoxDriver();dr.get("https://appleid.apple.com/cgi-bin/WebObjects/MyAppleId.woa/wa/createAppleId");// 通过下拉列表中选项的索引选中第二项,// 即What is the first name of your best friend in high school?Select sq1 = new Select(dr.findElement(By.id("security-question_1")));sq1.selectByIndex(2);// 通过下拉列表中的选项的value属性选中"January"value=1 这一项Select selectMon = new Select(dr.findElement(By.id("month")));selectMon.selectByValue("1");assertFalse(selectMon.isMultiple());// 验证下拉列表的不支持多选// assertEquals(4,selectMon().size()); //验证下拉数量Select selectDay = new Select(dr.findElement(By.id("day")));selectDay.selectByVisibleText("23");/** 检查下拉列表的选项 */// 预期的选项内容StateOptionsList<String> StateOptions = Arrays.asList(new String[] {"Please select", "Alabama", "Alaska", "Arizona", "Arkansas","Armed Forces Americas", "Armed Forces Europe","Armed Forces Pacific", "California", "Colorado","Connecticut", "Delaware", "Dist of Columbia", "Florida","Georgia", "Guam", "Hawaii", "Idaho", "Illinois", "Indiana","Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland","Massachusetts", "Michigan", "Minnesota", "Mississippi","Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire","New Jersey", "New Mexico", "New York", "North Carolina","North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania","Puerto Rico", "Rhode Island", "South Carolina","South Dakota", "Tennessee", "Texas", "Utah", "Vermont","Virgin Islands", "Virginia", "Washington", "West Virginia","Wisconsin", "Wyoming" });/** 遍历一下下拉列表所有选项,用click进行选中选项 **/Select selectState = new Select(dr.findElement(By.id("state-province")));List<String> act_StateOptions = new ArrayList<String>();// 判断选择内容assertEquals("Please select", selectState.getFirstSelectedOption().getText());for (WebElement e : selectState.getOptions()) {e.click();//	s = s + "\"" + e.getText() + "\"" + ",";// 将实际的下拉列表内容注入到act_StateOptions中,进行比较。act_StateOptions.add(e.getText());}assertArrayEquals(StateOptions.toArray(), act_StateOptions.toArray());}
}

/**从上面可以看出,对下拉框进行操作时首先要定位到这个下拉框,new 一个Selcet对象,然后对它进行操作。 在执行上面的例子时需要导入
* org.openqa
* .selenium.support.ui.Select类。首先创建一个Select癿对象,isMultiple()用来判断是丌是多选下拉框
* 。Select类提供了3种方法来选择下拉选项
* 。selectByVisibleText(),selectByValue(),selectByIndex()。
* 在使用返些方法癿时候要注意下拉列表是否是动态变化的 。
*/

如果只是单选的下拉列表,通过 如果只是单选的下拉列表,通过 getFirstSelectedOption()就可以得到所选择的项, 再调 用 getText() 就可以得到本文。 如果是多选的下拉列表,使用 getAllSelectedOptions() 得到所有已选择的项,此方法 会返回元素的集合。 使用  assertArrayEquals()方法来对比期望和实际所选的项是否正确。 调用 getAllSelectedOptions().size()方法来判断已选的下拉列表项数量。 如果想检查 某一个选项是否被择了,可以使用 assertTrue(act_sel_options.contains(“Red”)) 方 法

运行结果



http://www.lryc.cn/news/2412589.html

相关文章:

  • smplayer 中文字幕乱码,进度条及拖放MKV
  • 四年背的单词 笔记目录
  • KVM 虚拟化详解
  • nrf52832 sdk15.2.0 dfu升级攻略
  • SanDisk U盘加密软件 在其他u盘使用
  • springboot笔记整理(超详细,手把手教程!)
  • 真正的RISC-V开发板——VEGA织女星开发板开箱评测
  • ROS学习笔记-安装、环境搭建、初步体验与基本包命令
  • 2020-12-21细雨算法2.0解读
  • 一款免费无限制的AI视频生成工具火了!国内无障碍访问!目前真正免费无限制,可以用来制作抖音短视频,视频效果体验不逊色于pika和runway,以及其他的免费AI在线人工智能大模型, 附教程
  • 计算机专业毕业设计题目大全——各种类型系统设计大全
  • 磁盘分区格式FAT32与NTFS
  • 网络入门基础(网络布线)
  • 2017 我的第一篇个人博客
  • 移动网络为什么“慢”? 腾讯工程师分享弱联网优化之道
  • 卷上天!上海交大博士应聘中学保健员 复旦附中回应
  • 大学操作系统课程笔记
  • 渗透利器Weevely之奇淫技巧篇
  • 大学英语四级考试大纲
  • 高仿富途牛牛-组件化(三)-界面美化
  • QQ快速登录实现原理分析之localhost.ptlogin2.qq.com 怎么会映射到 127.0.0.1问题
  • 学编程需要哪些入门标准?(非常详细)零基础入门到精通,收藏这一篇就够了
  • 互联网 Java 工程师面试题1
  • web端使用HTML5开发《捕鱼达人》小游戏教程
  • C/C++编写的人机猜拳游戏教程
  • thzvv.com forum php,为什么Naver账号不能用了?
  • 2023年3月Python三级
  • 简单使用Kali WiFi破解实例
  • minecraft java版皮肤查看_厨师长教你:提取 Minecraft 基岩版付费皮肤并用于 Java 版...
  • linux服务篇-HTTP服务