java - 遍历表中的 webelements 列表并断言每个字符串相等
问题描述
我的目标是遍历表中的 webelements 列表(使用过滤器生成),分布在多个页面中,并在 Cucumber 步骤中使用提供的字符串断言这些 webelements 中的每个字符串相等。
这些 webelements 由放置在表格中的列中的字符串(1 个 webelement = 1 个字符串)组成。
所有这些字符串都相等。
他们的 data-testid 是一样的。
这些 web 元素分布在多个页面(动态)中。
循环将在到达最后一页时结束,其中包含一个按钮,该按钮的属性文本被禁用(当显示最后一页时)。
这是我开始写的内容,但我现在有点卡住了。如果你能告诉我如何继续下去,我真的很感激。目前,当我执行测试时,我得到了这个错误。
1. Tests in error:
stale element reference: element is not attached to the page document
2. Not sure how to integrate the assert.
实际代码:
By nextPageBtn = By.xpath("//*[@data-testid='asd']");
By disabledNextPageBtn = By.xpath("//*[@data-testid='asdf']");
By filterValue = By.xpath("//*[@data-testid='asdf1']");
public List<String> sameFilterResultIsDisplayedForAllRows() {
List<WebElement> filterResultsList = new ArrayList<WebElement>();
List<String> stringsFromFilterResultsList = new ArrayList<String>();
boolean disabledNext = false;
do {
click(driver, nextPageBtn);
filterResultsList.addAll(driver.findElements(filterValue));
try {
getValueFromSomeAttribute(driver, disabledNextPageBtn,
"randomElementAttribute");
disabledNext = true;
} catch (Exception e) {
}
} while (disabledNext == false);
for (WebElement filterResultsList) {
System.out.println(a.getText());
try {
stringbookings.add(a.getText());
} catch (Exception e) {
}
}
return stringsFromFilterResultsList;
}
The assert would be something like this:
@Then("the results filtered for all rows contain value {string}")
public void expectedFilteredValues(String expectedFilteredValueString) {
String expectedFilteredResult;
expectedFilteredResult = 'randomString';
List<String> actualFilteredValues = javaMethods.sameFilterResultIsDisplayedForAllRows();
Assert.assertEquals(expectedFilteredResult, actualFilteredValues);
解决方案
问题在于 addAll(),prolly 因为这个:
“如果在操作过程中修改了指定的集合,则该操作的行为是不确定的。(注意,如果指定的集合是这个列表,并且它是非空的,就会发生这种情况。)”
我必须使用 add(),并在 for 循环中创建一个辅助数组。
for (WebElement element : elements) { stringsFromFilterResultsList.add(bookingTypeElement.getText()); }
然后,断言是这样的:
列表列表 = javaMethods。sameFilterResultIsDisplayedForAllRows();
for (int i = 0; i < list.size(); i++) { Assert.assertEquals(expectedValue, list.get(i)); }
推荐阅读
- python-3.x - 将数据拆分为测试和训练,包括
- javascript - 在 Mappa .Js 中映射纬度和经度数组
- firebase - 获取请求具有无效的身份验证凭据错误 FCM
- python - 如何设置 Zappa 直接调用 lambda 函数?
- wordpress - 如何阻止 Wordpress 插入 noindex 元标记?
- python - sciklearn 线性回归(最终预测始终为 0)
- html - stroke-dashoffset 动画在 Safari 中不起作用
- android - FirebaseMessasingService 从我的服务器获取通知,但如果我使用 FirebaseConsole,则不会调用我的类
- linux - awk 打印行问题
- android - 在 RecyclerView 上添加新项目