1. 程式人生 > >select 自動選擇 檢查下拉列表

select 自動選擇 檢查下拉列表

cut comm arrays nes main stat 多選 tools 不支持

下面我們來看一下selenium webdriver是如何來處理select下拉框的,以Apple註冊頁面為例。

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

技術分享

[java] view plaincopy
  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. }
[java] view plain copy
  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”)) 方 法

運行結果

技術分享

select 自動選擇 檢查下拉列表