原代码:
<select id="edit-submitted-im-interesting-in" class="form-select required" name="submitted[im_interesting_in]"> <option value="1">Cloud Testing</option> <option value="2">Embedded System Testing</option> <option value="3">Globalization Testing</option> <option value="4">Offshore Test Center</option> <option value="5">System Integration Testing</option> <option value="6">Test Automation</option> <option value="7">Function Testing</option> <option value="8">Mobile Testing</option> <option value="9">Cloud Services</option> <option value="10">SKYPALACE</option> <option value="11">Mobile Test Automation</option> <option value="12">Test Automation Framework</option> <option value="13">IaaS Cloud Enablement</option> <option value="14">Desktop Virtualization</option> <option value="15">Others</option></select>
Selenium Java版代码:
int i=0; String c = "Cloud Testing,Embedded System Testing,Globalization Testing,Offshore Test Center,System Integration Testing,Test Automation,Function Testing,Mobile Testing,Cloud Services,SKYPALACE,Mobile Test Automation,Test Automation Framework,IaaS Cloud Enablement,Desktop Virtualization,Others"; String[] ptext = c.split(","); //将期望的Picklist 下拉选项值赋值给一个数组。 Select sel =new Select(driver.findElement(By.xpath("//*[@id='edit-submitted-im-interesting-in']"))); //用Xpath定位到该picklist. for(i =0;i<15;i++) { sel.selectByVisibleText(ptext[i]); //用selectByVisibleText()方法将目标的picklist找到,ptext[i]是数组的值,是一个变量,ptext[i]不要加“”。 String a =driver.findElement(By.xpath("//*[@id='edit-submitted-im-interesting-in']")).getAttribute("value"); //获取你找到的picklist的显示值,此处返回的是一个叫value属性的值,不是picklist文本 assertEquals(ptext[i],driver.findElement(By.xpath("//option[@value='"+a+"']")).getText()); } //将找到的picklist的显示文本与目标文本比较,a是一个变量,注意其格式,a不要直接加“”。 System.out.println("03.can be selected successfully."); //以上代码是验证picklist每一项能被正确选择。 String[] ptext2=new String[15]; for(i =0;i<15;i++) { sel.selectByIndex(i); //依次选择picklist String a =driver.findElement(By.xpath("//*[@id='edit-submitted-im-interesting-in']")).getAttribute("value"); ptext2[i]=driver.findElement(By.xpath("//option[@value='"+a+"']")).getText(); 将picklist的文本值赋给一个数组 if(!ptext[i].equals(ptext2[i])) { break; } //比较两个数组 // ptext2.equals(ptext);也可以直接比较两个数组 //以上验证picklist是和期望结果一样有序排列的。 }