我正在使用带有chromedriver和testNG的Selenium Webdriver为动态网络表编写自动测试。
目标是断言某个表条目存在,如果存在,则将其删除,然后断言是否已删除。但是,第二个断言无法正常工作。
在第一个断言期间,我调用创建 Webelements 列表并获取行数的方法。我使用此数字来了解何时停止遍历表。 第二个断言使用相同的表来做同样的事情,但现在 DOM 发生了变化,en 我的表中只剩下 18 条规则,而之前有 19 条。一旦迭代尝试获取第 19 行,我就会得到以下内容:
org.openqa.selenium.NoSuchElementException: no suchElement: Impossible to 定位元素: {"方法":"xpath","selector":".//tr[19]/td[1]"}
我尝试创建MyWishlistPage的新实例,但是这个新实例还看到了"旧"数量的表规则
我还尝试了线程睡眠和行删除后的驱动程序刷新,但这也无济于事(代码段仍然存在,已注释(
我最终更改了我的测试类以转到另一个页面,然后返回到MYWishlistPage。这有效,但这是一个我不满意的草率解决方法
谁能告诉我从表中删除条目后如何获得正确的行数?
这是我遇到问题的页面类的一部分:
public class MyWishlistsPage
{
private WebDriver driver;
public MyWishlistsPage(WebDriver driver)
{
this.driver = driver;
//This call sets the WebElements
PageFactory.initElements(driver, this);
}
public Boolean isWishlistAvailable(String nameToAssert)
{
//This list gets the number of rows from the table
List<WebElement> rows = driver.findElements(By.xpath(".//tr"));
//This loop finds the first row which' title matches sRowValue
for (int i = 1; i < rows.size(); i++)
{
String sValue = driver.findElement(By.xpath(".//tr[" + i + "]/td[1]")).getText();
if (sValue.equalsIgnoreCase(nameToAssert))
{
return true;
}
}
return false;
}
public void deleteWishlistsEntry (String sRowValue)
{
//This list gets the number of rows from the table
List<WebElement> rows = driver.findElements(By.xpath(".//tr"));
//This loop finds the first row which' title matches sRowValue
for (int i = 1; i < rows.size(); i++)
{
String sValue = driver.findElement(By.xpath(".//tr[" + i + "]/td[1]")).getText();
if (sValue.equalsIgnoreCase(sRowValue))
{
// If the sValue matches with the description, the element in the seventh column of the row will be clicked
driver.findElement(By.xpath(".//tr[" + i + "]/td[7]/a/i")).click();
driver.switchTo().alert().accept();
/* try
{
Thread.sleep(10000);
} catch(InterruptedException ex)
{
Thread.currentThread().interrupt();
}
driver.navigate().refresh(); */
}
}
}
}
这是我从以下位置调用页面的测试类的一部分:
Assertions.assertThat(mywishlistspage.isWishlistAvailable(listToAssert)).as("The list you were trying to delete did not exist, and an attempt to create it failed ").isTrue();
//Deletes the chosen list
mywishlistspage.deleteWishlistsEntry(listToAssert);
//Verifies that list has been deleted
//homepage.clickMyAccountPage();
//myaccountpage.goToMyWishlistsPage();
Assertions.assertThat(mywishlistspage.isWishlistAvailable(listToAssert)).as("The list you tried to delete is still there").isFalse();
在删除愿望清单条目中,首先它获取行数,单击"锚点"后,行项目将被删除,之后您必须在单击锚标记后中断/退出该循环...但是在代码中,您循环直到第 19 个元素,这就是为什么 u 得到"无法找到元素:{"方法":"xpath","selector":".//tr[19]/td[1]"}">
public void deleteWishlistsEntry (String sRowValue)
{
//This list gets the number of rows from the table
List<WebElement> rows = driver.findElements(By.xpath(".//tr"));
//This loop finds the first row which' title matches sRowValue
for (int i = 1; i < rows.size(); i++)
{
String sValue = driver.findElement(By.xpath(".//tr[" + i + "]/td[1]")).getText();
if (sValue.equalsIgnoreCase(sRowValue))
{
// If the sValue matches with the description, the element in the seventh column of the row will be clicked
driver.findElement(By.xpath(".//tr[" + i + "]/td[7]/a/i")).click();
driver.switchTo().alert().accept();
break;
}
}
}