select 自动选择 检查下拉列表
下面我们来看一下selenium webdriver是如何来处理select下拉框的,以Apple注册页面为例。
https://appleid.apple.com/cgi-bin/WebObjects/MyAppleId.woa/wa/createAppleId
- 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 {
- @Test
- public 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");
- /** 检查下拉列表的选项 */
- // 预期的选项内容StateOptions
- List<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());
- }
- }
- 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 {
- @Test
- public 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");
- /** 检查下拉列表的选项 */
- // 预期的选项内容StateOptions
- List<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”)) 方 法
运行结果
select 自动选择 检查下拉列表的更多相关文章
- js单击自动选择文本
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
- Android小项目之四 自动更新检查的逻辑
------- 源自梦想.永远是你IT事业的好友.只是勇敢地说出我学到! ---------- 按惯例,写在前面的:可能在学习Android的过程中,大家会和我一样,学习过大量的基础知识,很多的知识点 ...
- ECshop网点程序优化-后台添加类目自动选择上次父类目并计算Sort Order
如果在ECshop后台批量添加过大量类目的人都能体会到是多么的不方便(这点还是要说一下ECshop的产品经理,细节上还是要多注意),每次添加都需要在几百个类目里面找到要添加的父类目也是一个麻烦事,比如 ...
- JS-加载页面的时候自动选择刚才所选择option
<body class="no-skin" onload="option_auto(${pd.PACK_SORT})"> <select na ...
- 自动选择最佳特征进行分类-SVM (Halcon)
HALCON12里的example,classify_pills_auto_select_features.hdev. 执行流程: 1.选取相关特征(本例选取color和region组的所有特征)(本 ...
- 【摘录】JAVA内存管理-自动选择垃圾收集器算法
在J2SE 5.0,垃圾收集的默认值:垃圾收集器.堆大小以及JVM的类型(客户端还是服务器)都会根据应用运行的硬件平台和操作系统自动选择.相比之前设置命令行参数的方式,自动选择很好的匹配了不同类型的应 ...
- js进阶 9-10 html控件如何实现点击自动选择控件内容
js进阶 9-10 html控件如何实现点击自动选择控件内容 一.总结 一句话总结: 1.在click事件中,如果focus,那就select 2.blur 1.html中控件添加两种方式? 在表单 ...
- Uipath 选择页面下拉列表中的选项
http://www.rpatokyo.com/ 使用Select item Activity选择页面下拉列表中的选项 在open browser中拖入Select Item Activity,配置参 ...
- XML:使用DOM技术解析xML文件中的城市,实现select级联选择
中国的城市xml格式:cities.xml <?xml version="1.0" encoding="utf-8"?> <china> ...
随机推荐
- ContextLoader,ContextLoaderListener解读
一.ServletContext 有 addListener(..) 方法,也有创建的方法 createListener(Class<T> c) . 有addFilter(..) 方法,也 ...
- LandMVC HttpHandler web.config配置
<system.webServer> <validation validateIntegratedModeConfiguration="false" /> ...
- 关于ie中实现弹性盒模型-我的css
css3中的弹性盒模型大家都不陌生,但是能否在ie6中实现呢?第三方库中涉及到的页少之又少,也有一部分css框架中支持各种布局,下面给出我用的盒模型样式(为了以后copy方便而已): /******* ...
- POJ 2567 Code the Tree & POJ 2568 Decode the Tree Prufer序列
题目大意:2567是给出一棵树,让你求出它的Prufer序列.2568时给出一个Prufer序列,求出这个树. 思路:首先要知道Prufer序列.对于随意一个无根树,每次去掉一个编号最小的叶子节点,并 ...
- iOS开发之--搭建本地的SVN服务器
近期入职的新公司,后台没有分配svn账号,需要在本地搭建一个服务器,方便和代码,看了看网上的教程,一直有这样那样的问题, 其中最主要的问题还是路径拼接的问题,最后终于解决了,特在此分享下,如果大家有更 ...
- python中的函数的执行分类
author:headsen chen date: 2018-03-21 17:42:13 notice:This article created by headsen chen himself ...
- tomcat中文配置
tomcat传递中文乱码,修改server.xml文件 <Connector port=" protocol="HTTP/1.1" connectionTimeou ...
- List remove及ConcurrentModificationException异常
参考:http://blog.csdn.net/androidboy365/article/details/50540202/ 解决方案 // 1 使用Iterator提供的remove方法,用于删除 ...
- CodeForces 24A Ring road(dfs)
A. Ring road time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- 【转】SignalR来做实时Web聊天
本章和大家分享的内容是使用Signal R框架创建个简易的群聊功能,主要讲解如何在.Net的MVC中使用这个框架,由于这个项目有官方文档(当然全英文),后面也不打算写分享篇了,主要目的是让朋友们在需要 ...