【发布时间】:2021-07-13 23:12:08
【问题描述】:
我的目标是遍历表格中的 webelements 列表(使用过滤器生成),分布在多个页面中,并在 Cucumber 步骤中使用提供的字符串断言这些 webelements 中的每个字符串相等。
-
这些网络元素由放置在表格中的列中的字符串(1 个网络元素 = 1 个字符串)组成。
-
所有这些字符串都相等。
-
他们的data-testid是一样的。
-
这些网络元素分布在多个页面(动态)。
-
循环将在到达最后一页时结束,其中包含一个按钮 哪个属性文本被禁用(显示最后一页时)。
这是我开始写的,但我现在有点卡住了。如果 你可以告诉我如何继续下去,我真的很感激。目前,我在执行测试时收到此错误。
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);
【问题讨论】:
-
stale 元素表明当您使用其中一个 filterResultsList webelement 引用时 DOM 仍在更新。要么捕获那些陈旧的元素引用并在捕获时重新获取列表(功能化并重新调用),或者在单击后添加足够长的睡眠......当在该 webelement 引用上调用该方法时,陈旧元素将抛出。 (在这种情况下,我猜那是“a.getText()”......但那里的代码有点混乱。