zoukankan      html  css  js  c++  java
  • select 自动选择 检查下拉列表

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

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

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

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

    运行结果

  • 相关阅读:
    你还在把Java当成Android官方开发语言吗?Kotlin了解一下!
    如何站在大数据的角度看100000个故事
    AI从入门到放弃:CNN的导火索,用MLP做图像分类识别?
    Hadoop Yarn REST API未授权漏洞利用挖矿分析
    c# 修饰词public, protected, private,internal,protected的区别
    缩放到被选择的部分: ICommand Cmd = new ControlsZoomToSelectedCommandClass();
    警告 7 隐藏了继承的成员。如果是有意隐藏,请使用关键字 new
    arcgis10.5.1 对齐要素
    arcgis10.1安装出现1606错误怎么办?找不到盘符
    arcmap搜索脚本错误
  • 原文地址:https://www.cnblogs.com/111testing/p/7189442.html
Copyright © 2011-2022 走看看