【问题标题】:What would be a faster method for handling pagination?处理分页的更快方法是什么?
【发布时间】:2014-10-22 22:54:23
【问题描述】:

在我正在测试的 Web 应用程序中,我有一个显示在页面上的元素列表。根据列表中元素的数量,列表变为分页。

分页是这样的:

  • 如果只有 1 页,则不会发生分页。
  • 如果有 2 个页面,页面底部会显示一个下一页 (>) 按钮。
  • 如果有3页或更多页,除了下一页按钮(>)外,还有一个最后一页按钮(>>)。

我目前正在使用以下方法来确保始终到达列表的最后一页。然后我需要它来断言我创建的元素已显示,因为该元素始终在最后一页上。 它工作得很好,但我的问题是,当我的测试达到这一点时,运行该语句大约需要 20 秒,并且由于我在所有测试中都使用它,它大大减慢了我的测试过程。 对于我描述的情况,有没有更快的方法?

public void jumpToLastPage(){ 
    List<WebElement> lastPage = driver.findElements(locator for last page button));
    if (lastPage.size() == 0){
        List<WebElement> nextPage = driver.findElements(locator for next page button));
        if(nextPage.size() >= 1){
            nextPage.get(0).click();
        }
    }
    else lastPage.get(0).click();
}

【问题讨论】:

  • 如果您使用的是框架,它应该有一个分页插件,您可以启用或在线查找如何集成它。
  • 为什么要在所有测试中走到最后一页?
  • 如果网络性能很慢,您总是会与浏览器扩展发生一些网络开销冲突。
  • 你的算法看起来不错。考虑重组你如何称呼它!例如:是否可以在 @BeforeClass 方法中导航到最后一页(假设为 junit4),然后从该点运行所有测试?
  • 我需要转到最后一页才能断言在上一个测试中最后创建的元素已显示。最后创建的元素总是列表中的最后一个。因此,如果列表中有更多页面,我需要一种方法来确保在进行断言时始终位于最后一页。

标签: java selenium selenium-webdriver pagination


【解决方案1】:

经过多次尝试,我发现问题实际上来自我在 @BeforeMethod (TestNG) 中的设置。

我的超时设置为 10 秒: driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

当它试图找到元素但失败时,它仍然会徘徊 10 秒以希望找到它,这就是为什么我花了 20 秒来完成声明。我在一个页面上,没有显示任何元素。

我刚刚添加了 2 行代码来解决这个问题:

public void jumpToLastPage(){
    driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
    List<WebElement> lastPage = driver.findElements(By.xpath(locator));
    if (lastPage.size() == 0){
        List<WebElement> nextPage = driver.findElements(By.xpath(locator));
        if(nextPage.size() >= 1){
            nextPage.get(0).click();
        }
    }
    else lastPage.get(0).click();
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}

这样,它会在执行语句时将隐式等待时间设置为 3 秒,然后在完成后返回默认的 10 秒,从而大大减少执行语句所需的时间。

仍然感谢大家的回答。

【讨论】:

    【解决方案2】:

    我认为这样会更快:

    public void jumpToLastPage(){
        List<WebElement> lastPageButton = driver.findElements(By locator);
        List<WebElement> nextPageButton = driver.findElements(By locator);
    
        // if last page button is on the page, click it
        if(lastPageButton.size() != 0) {
            lastPageButton.get(0).click();
        }
        // else if next page button, click it
        else if(nextPageButton.size() != 0) {
            nextPageButton.get(0).click();
        }
        // otherwise you should be on the only page
    }
    

    【讨论】:

    • 这个方法和我现在用的时间完全一样。不过还是谢谢你:)。不过仍在寻找答案。
    猜你喜欢
    • 2013-07-01
    • 1970-01-01
    • 2012-07-19
    • 2016-06-12
    • 1970-01-01
    • 2020-03-16
    • 1970-01-01
    • 1970-01-01
    • 2015-02-03
    相关资源
    最近更新 更多