select 自動選擇 檢查下拉列表
阿新 • • 發佈:2017-07-16
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- package com.annie.test;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
- import org.openqa.selenium.By;
- import org.openqa.selenium.WebDriver;
- import org.openqa.selenium.WebElement;
- import org.openqa.selenium.firefox.FirefoxDriver;
- import org.openqa.selenium.support.ui.Select;
- import static org.junit.Assert.*;
- import org.junit.Test;
- public class SelectTest {
- @Test
- public void testDropdown() {
- // System.setProperty("webdriver.firefox.bin","D:\\Program Files\\Mozilla Firefox\\firefox.exe");
- WebDriver dr = new FirefoxDriver();
- dr
- .get("https://appleid.apple.com/cgi-bin/WebObjects/MyAppleId.woa/wa/createAppleId");
- // 通過下拉列表中選項的索引選中第二項,
- // 即What is the first name of your best friend in high school?
- Select sq1 = new Select(dr.findElement(By.id("security-question_1")));
- sq1.selectByIndex(2);
- // 通過下拉列表中的選項的value屬性選中"January"value=1 這一項
- Select selectMon = new Select(dr.findElement(By.id("month")));
- selectMon.selectByValue("1");
- assertFalse(selectMon.isMultiple());// 驗證下拉列表的不支持多選
- // assertEquals(4,selectMon().size()); //驗證下拉數量
- Select selectDay = new Select(dr.findElement(By.id("day")));
- selectDay.selectByVisibleText("23");
- /** 檢查下拉列表的選項 */
- // 預期的選項內容StateOptions
- List<String> StateOptions = Arrays.asList(new String[] {
- "Please select", "Alabama", "Alaska", "Arizona", "Arkansas",
- "Armed Forces Americas", "Armed Forces Europe",
- "Armed Forces Pacific", "California", "Colorado",
- "Connecticut", "Delaware", "Dist of Columbia", "Florida",
- "Georgia", "Guam", "Hawaii", "Idaho", "Illinois", "Indiana",
- "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland",
- "Massachusetts", "Michigan", "Minnesota", "Mississippi",
- "Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire",
- "New Jersey", "New Mexico", "New York", "North Carolina",
- "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania",
- "Puerto Rico", "Rhode Island", "South Carolina",
- "South Dakota", "Tennessee", "Texas", "Utah", "Vermont",
- "Virgin Islands", "Virginia", "Washington", "West Virginia",
- "Wisconsin", "Wyoming" });
- /** 遍歷一下下拉列表所有選項,用click進行選中選項 **/
- Select selectState = new Select(dr.findElement(By.id("state-province")));
- List<String> act_StateOptions = new ArrayList<String>();
- // 判斷選擇內容
- assertEquals("Please select", selectState.getFirstSelectedOption()
- .getText());
- for (WebElement e : selectState.getOptions()) {
- e.click();
- // s = s + "\"" + e.getText() + "\"" + ",";
- // 將實際的下拉列表內容註入到act_StateOptions中,進行比較。
- act_StateOptions.add(e.getText());
- }
- assertArrayEquals(StateOptions.toArray(), act_StateOptions.toArray());
- }
- }
- package com.annie.test;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
- import org.openqa.selenium.By;
- import org.openqa.selenium.WebDriver;
- import org.openqa.selenium.WebElement;
- import org.openqa.selenium.firefox.FirefoxDriver;
- import org.openqa.selenium.support.ui.Select;
- import static org.junit.Assert.*;
- import org.junit.Test;
- public class SelectTest {
- @Test
- public void testDropdown() {
- // System.setProperty("webdriver.firefox.bin","D:\\Program Files\\Mozilla Firefox\\firefox.exe");
- WebDriver dr = new FirefoxDriver();
- dr
- .get("https://appleid.apple.com/cgi-bin/WebObjects/MyAppleId.woa/wa/createAppleId");
- // 通過下拉列表中選項的索引選中第二項,
- // 即What is the first name of your best friend in high school?
- Select sq1 = new Select(dr.findElement(By.id("security-question_1")));
- sq1.selectByIndex(2);
- // 通過下拉列表中的選項的value屬性選中"January"value=1 這一項
- Select selectMon = new Select(dr.findElement(By.id("month")));
- selectMon.selectByValue("1");
- assertFalse(selectMon.isMultiple());// 驗證下拉列表的不支持多選
- // assertEquals(4,selectMon().size()); //驗證下拉數量
- Select selectDay = new Select(dr.findElement(By.id("day")));
- selectDay.selectByVisibleText("23");
- /** 檢查下拉列表的選項 */
- // 預期的選項內容StateOptions
- List<String> StateOptions = Arrays.asList(new String[] {
- "Please select", "Alabama", "Alaska", "Arizona", "Arkansas",
- "Armed Forces Americas", "Armed Forces Europe",
- "Armed Forces Pacific", "California", "Colorado",
- "Connecticut", "Delaware", "Dist of Columbia", "Florida",
- "Georgia", "Guam", "Hawaii", "Idaho", "Illinois", "Indiana",
- "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland",
- "Massachusetts", "Michigan", "Minnesota", "Mississippi",
- "Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire",
- "New Jersey", "New Mexico", "New York", "North Carolina",
- "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania",
- "Puerto Rico", "Rhode Island", "South Carolina",
- "South Dakota", "Tennessee", "Texas", "Utah", "Vermont",
- "Virgin Islands", "Virginia", "Washington", "West Virginia",
- "Wisconsin", "Wyoming" });
- /** 遍歷一下下拉列表所有選項,用click進行選中選項 **/
- Select selectState = new Select(dr.findElement(By.id("state-province")));
- List<String> act_StateOptions = new ArrayList<String>();
- // 判斷選擇內容
- assertEquals("Please select", selectState.getFirstSelectedOption()
- .getText());
- for (WebElement e : selectState.getOptions()) {
- e.click();
- // s = s + "\"" + e.getText() + "\"" + ",";
- // 將實際的下拉列表內容註入到act_StateOptions中,進行比較。
- act_StateOptions.add(e.getText());
- }
- assertArrayEquals(StateOptions.toArray(), act_StateOptions.toArray());
- }
- }
/**從上面可以看出,對下拉框進行操作時首先要定位到這個下拉框,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 自動選擇 檢查下拉列表