导包过程中如果有冲突,找到冲突的包,取交集,选择主要用的包
下面我们来看一下selenium webdriver是如何来处理select下拉框的,以Apple注册页面为例。
https://appleid.apple.com/cgi-bin/WebObjects/MyAppleId.woa/wa/createAppleId
-
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 {
-
-
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”)) 方 法
运行结果
https://blog.csdn.net/Anniejunyan/article/details/39313591