標籤:nbsp equal selected option 序列 UI oar false rate
操作下拉式清單
<html><body> <select name=‘fruit‘ size=6> <option id=‘peach‘ value=‘taozi‘>桃子</option> <option id=‘maybush‘ value=‘shanzha‘>山楂</option> <option id=‘litchi‘ value=‘lizhi‘>荔枝</option> <option id=‘orange‘ value=‘juzi‘>橘子</option> </select></body></html>
@Test public void operateDropList() { Select dropList =new Select(driver.findElement(By.name("fruit"))); //判斷下拉式清單是否可以複選,因為是單選列表所以用False,多選用true; Assert.assertFalse(dropList.isMultiple()); //getFirstSelectedOption()方法擷取下拉式清單第一個屬性,getText()方法擷取選項文本文字 Assert.assertEquals("桃子", dropList.getFirstSelectedOption().getText()); //通過序號來鎖定下拉選項,從0開始為第一個下拉元素; dropList.selectByIndex(3); //通過value值來進行選中操作 dropList.selectByValue("shanzha"); //dropList.selectByIndex(3).getText()方法報錯,占時不知道什麼原因,都引用dropList.getFirstSelectedOption().getText()進行斷言 //Assert.assertEquals("山楂", dropList.selectByIndex(3).getText()); //通過選項文字進行選中,dropList.selectByVisibleText("荔枝").getText()報錯了; // Assert.assertEquals("荔枝", dropList.selectByVisibleText("荔枝").getText()); List<String>expect_options=Arrays.asList((new String[]{"桃子","山楂","荔枝","橘子"})); List<String>actual_options=new ArrayList<String>(); //dropList.getOptions()擷取下拉式清單所有選項文本 for(WebElement option : dropList.getOptions()){ actual_options.add(option.getText()); } Assert.assertEquals(expect_options.toArray(), actual_options.toArray()); //多選下拉式清單方法也是同上,注意斷言多選dropList.isMultiple()使用assertTrue()
//dropList.deselectByIndex(0);取消第一個下拉元素的選項
webdriver --API--(java版) the second part