栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

通过Selenium WebDriver验证列表元素

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

通过Selenium WebDriver验证列表元素

不要使用for-each构造。仅在对单个

Iterable
/数组进行迭代时有用。您需要同时遍历
List<WebElement>
和数组。

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

OP稍微改变了他的问题后进行编辑:

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

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 elementsif (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 valuefor (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);    }}

这段代码实质上完成了我的第一个代码。唯一的区别是,现在您手动进行工作并打印结果。

以前,我使用了JUnit框架类中的

assertEquals()
静态方法
Assert
。该框架是编写Java测试的实际标准,
assertEquals()
方法家族是验证程序结果的标准方法。它们确保传递给方法的参数相等,如果不相等,则抛出
AssertionError

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



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/452329.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号