Selenium + Webdriver自动选择、检查下拉列表。


导包过程中如果有冲突,找到冲突的包,取交集,选择主要用的包

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

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


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

/**从上面可以看出,对下拉框进行操作时首先要定位到这个下拉框,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”)) 方 法

运行结果


https://blog.csdn.net/Anniejunyan/article/details/39313591

猜你喜欢

转载自blog.csdn.net/weixin_41585557/article/details/80984524