删除行后刷新webelements列表

时间:2017-07-26 10:16:06

标签: list selenium webdriver selenium-chromedriver

我正在使用带有chromedriver和testNG的Selenium Webdriver编写动态网表的自动化测试。

目标是断言某个表项存在,如果存在则将其删除,然后断言是否删除。然而,第二个断言不能正常工作。

在第一个断言期间,我调用创建Webelements列表的方法并获取行数。我用这个数字知道何时停止迭代表。 第二个断言使用相同的表来做同样的事情,但是现在DOM已经改变了,在我的表中只剩下18个规则,之前有19个。一旦迭代尝试获得第19行,我得到以下内容:

  

org.openqa.selenium.NoSuchElementException:没有这样的元素:无法   locate元素:{“method”:“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();

1 个答案:

答案 0 :(得分:0)

在deleteWishlistsEntry中,首先得到行数,点击“锚点”之后行项目将被删除,之后你必须在点击锚标记后从该循环中断/退出...但是在代码中你循环直到第19个元素就是为什么你得到“无法定位元素:{”方法“:”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;

        } 
    }
}