通过Selenium WebDriver验证列表元素 [英] Verify list elements by Selenium WebDriver

查看:20
本文介绍了通过Selenium WebDriver验证列表元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

WebElement select = myD.findElement(By.xpath("//*[@id='custfoodtable']/tbody/tr[2]/td/div/select"));
List<WebElement> allOptions = select.findElements(By.tagName("option"));
for (WebElement option : allOptions) {
    System.out.println(String.format("Value is: %s", option.getAttribute("value")));
    option.click();
    Object vaLue = "Gram";
    if (option.getAttribute("value").equals(vaLue)) {
        System.out.println("Pass");
    } else {
        System.out.println("fail");
    }
}

我可以验证列表中的一个元素,但是下拉菜单中有20个元素需要验证,我不想使用上述逻辑20次.有没有更简单的方法?

I can verify one element in a list, but there are like 20 elements in a dropdown I need to verify and I do not want to use above logic 20 times. Is there any easier way to do it?

推荐答案

不要使用for-each构造.仅在迭代单个 Iterable /数组时有用.您需要同时遍历 List< WebElement> 和数组.

Don't use the for-each construct. It's only useful when iterating over a single Iterable / array. You need to iterate over the List<WebElement> and the array simultaneously.

// assert that the number of found <option> elements matches the expectations
assertEquals(exp.length, allOptions.size());
// assert that the value of every <option> element equals the expected value
for (int i = 0; i < exp.length; i++) {
    assertEquals(exp[i], allOptions.get(i).getAttribute("value"));
}


修改OP的问题后进行


EDIT after OP's changed his question a bit:

假设您有一组期望值,则可以执行以下操作:

Assuming you have an array of expected values, you can do this:

String[] expected = {"GRAM", "OUNCE", "POUND", "MILLIMETER", "TSP", "TBSP", "FLUID_OUNCE"};
List<WebElement> allOptions = select.findElements(By.tagName("option"));

// make sure you found the right number of elements
if (expected.length != allOptions.size()) {
    System.out.println("fail, wrong number of elements found");
}
// make sure that the value of every <option> element equals the expected value
for (int i = 0; i < expected.length; i++) {
    String optionValue = allOptions.get(i).getAttribute("value");
    if (optionValue.equals(expected[i])) {
        System.out.println("passed on: " + optionValue);
    } else {
        System.out.println("failed on: " + optionValue);
    }
}

此代码实质上完成了我的第一个代码.唯一的不同是,现在您手动进行工作并将结果打印出来.

This code essentially does what my first code did. The only real difference is that now you're doing the work manually and are printing the results out.

之前,我使用了 Assert 类中的rel ="nofollow"> assertEquals() 静态方法.该框架是编写Java测试的实际标准,而 assertEquals()方法族是验证程序结果的标准方法.他们确保传递给方法的参数相等,如果不相等,则抛出 AssertionError .

Before, I used the assertEquals() static method from the Assert class of the JUnit framework. This framework is a de-facto standard in writing Java tests and the assertEquals() method family is the standard way to verify the results of your program. They make sure the arguments passed into the method are equal and if they are not, they throw an AssertionError.

无论如何,您也可以手动进行操作,没问题.

Anyway, you can do it the manual way, too, no problem.

这篇关于通过Selenium WebDriver验证列表元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆