如何验证selenium webdriver 2中下拉值的顺序?

时间:2014-03-20 08:53:34

标签: java selenium-webdriver

我是selenium web driver的新手。我只想验证下拉值的顺序。

例如,我有一个下拉列表:服务提供商。

它有7个值。 Aircel,Airtel,BSNL,Docomo,Idea,Reliance,Vodofone。此下拉值根据升序排序。

如何验证只有7个值且订单是否正确?

由于

2 个答案:

答案 0 :(得分:5)

以下代码应该有所帮助:

// Create expected list :: This will contain expected drop-down values
ArrayList expectedDropDownItems = new ArrayList();
expectedDropDownItems.add("Aircel");
expectedDropDownItems.add("Airtel");
expectedDropDownItems.add("BSNL");
expectedDropDownItems.add("Docomo");
expectedDropDownItems.add("Idea");
expectedDropDownItems.add("Reliance");
expectedDropDownItems.add("Vodofone");

// Create a webelement for the drop-down
WebElement serviceProviderDropDownElement = driver.findElement(<locator of the drop-down e.g. By.id, By.xpath etc.>);

// Instantiate Select class with the drop-down webelement
Select serviceProviderDropDown = new Select(serviceProviderDropDownElement);

// Retrieve all drop-down values from the app and store in actual list
List<WebElement> valuesUnderServiceProviderDropDown  = serviceProviderDropDown.getOptions();

ArrayList actualDropDownItems = new ArrayList();

for(WebElement value : valuesUnderServiceProviderDropDown){
    actualDropDownItems.add(value.getText());
}

// Verify Drop-down contains ONLY 7 values
if(valuesUnderServiceProviderDropDown.size()==7)
    System.out.println("Drop-down contain exactly 7 values");

else
    System.out.println("Drop-down doesn't contain exactly 7 values");

// Compare expected and actual list
for (int i = 0; i < actualDropDownItems.size(); i++) {
    if (!expectedDropDownItems.get(i).equals(actualDropDownItems.get(i)))
    System.out.println("Drop-down values are NOT in correct order");
}

答案 1 :(得分:0)

您可以使用以下代码获取下拉值计数。

Webelement ele = driver.findElement(By.id("Dropdown Locator Value"));
Select sel = new Select(ele);
List<WebElement> list  = ele.getOptions();
System.Out.println("Drop down Value Count :" + list.size());
// To display the drop down value
for(WebElement e : list){
System.Out.println("Drop down Value :" + e.getText());
}

您可以通过比较检索到的值和预期值来检查下拉列表的值。