您的位置:首页 > 其它

selenium操作选择列表

2016-03-10 10:57 232 查看
1.操作单选列表

Select dropList = new Select(driver.findElement(By.name("fruit")));
//siMultiple 表示此下拉列表是否允许多选,被测试网页是一个单选的下拉列表,所以函数返回结果false
Assert.assertFalse(dropList.isMultiple());
//getFirstSelectedOption().getText()表示获取当前被选中的下拉列表选项文本
Assert.assertEquals("桃子", dropList.getFirstSelectedOption().getText());

//selectByIndex方法表示选择下拉列表的第四个选项,即猕猴桃
dropList.selectByIndex(3);
Assert.assertEquals("猕猴桃", dropList.getFirstSelectedOption().getText());
//selectByValue 方法表示使用下拉列表选项的value属性值进行选中操作
dropList.selectByValue("shanzha");
Assert.assertEquals("山楂", dropList.getFirstSelectedOption().getText());

//selectByVisibleText方法表示通过选项的文字来进行选中
dropList.selectByVisibleText("荔枝");
Assert.assertEquals("荔枝", dropList.getFirstSelectedOption().getText());

/*声明一个list对象存储下拉列表中所有期望出现的选项文字,并通过泛型<Sreing>限定List对象中
* 的储存对象类型是String,Arrays.asList表示将一个数组转换成为一个list对象*/

List<String> expext_options = Arrays.asList((new String[]{"桃子","西瓜","橘子","猕猴桃","山楂","荔枝"}));
//声明一个新的list对象,用于存取从页面上获取的所有选项文字
List<String> actual_option = new ArrayList<String>();
//这里的for为foreach用法,drop.getoptions(),为一个list所以不能直接跟.getText()
//查看一个数据对象的数据类型用.getClass()函数,dropList.getOptions().getClass()
for (WebElement option : dropList.getOptions()) {
actual_option.add(option.getText());
}
Assert.assertEquals(expext_options.toString(), actual_option.toString());


2.操作多选列表

Assert.assertTrue(dropList.isMultiple());
//使用选项索引选择“猕猴桃”选项
dropList.selectByIndex(3);
//使用value
dropList.selectByValue("shanzha");
dropList.selectByVisibleText("荔枝");
//取消所有选项的选中状态
dropList.deselectAll();
//再次选中三个选项
dropList.selectByIndex(3);
dropList.selectByValue("shanzha");
dropList.selectByVisibleText("荔枝");


3.操作单选框

WebElement radioOption = driver.findElement(By.xpath("//input[@value='berry']"));
//如果按钮处于为被选中的状态,则调用click方法选中此按钮
if (!radioOption.isSelected())
radioOption.click();
//断言是否选中
Assert.assertTrue(radioOption.isSelected());
//查找属性值为fruit的多有单选按钮对象,并存储到一个list容器中
List<WebElement> fruits = driver.findElements(By.name("fruit"));
/*使用for玄幻讲list容器中的每一个单选按钮进行遍历,查找value属性值为“watermelon”的单选按钮,如果未选中,则click选中*/
for (WebElement fruit : fruits) {
//getAttribute这个方法是提取放置在某个共享区间的对象的,他对应了setAttribute方法,比如在session中,
//使用setAttribute将一个数据放入session区间,那么在一个会话区间内,便可以在其他页面中用getAttribute将数据提取并使用
if (fruit.getAttribute("value").equals("watermelon")) {
if (!fruit.isSelected()) {
fruit.click();
//断言是否被选中
Assert.assertTrue(fruit.isSelected());
break;
}
}
}


4.操作复选框

WebElement orangeChexBox =  driver.findElement(By.xpath("//input[@value='orange']"));
//如果复选框未选中,则调用click
if (!orangeChexBox.isSelected()) {
orangeChexBox.click();
}
Assert.assertTrue(orangeChexBox.isSelected());

if (orangeChexBox.isSelected()) {
orangeChexBox.click();
}
Assert.assertFalse(orangeChexBox.isSelected());
//查找所有属性值为fruit的所有复选框,并存放list容器
List<WebElement> checkBoxs = driver.findElements(By.name("fruit"));
//遍历list容器中所有的复选框元素,点击所有复选框,让全部复选框处于选中状态
for (WebElement checkBox : checkBoxs) {
checkBox.click();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: