Java、Cucumber 和 Selenium : Problems with navigate(). to()

标签 java selenium-webdriver cucumber

在使用 Java、Cucumber 和 Selenium 的设置中:

我有以下代码,它尝试导航到页面,然后查找是否存在一两个元素以验证我是否位于页面本身。

问题:大约五分之一的情况下,测试会卡在目标页面之前的页面上,显然是在页面导航完成之前寻找(但没有找到)元素。

错误信息:

org.openqa.selenium.TimeoutException: Expected condition failed: waiting 
for e2e.navigation.NavigationSteps$$Lambda$260/975372289@202898d7 (tried 
for 40 second(s) with 500 milliseconds interval)

cucumber 步骤:

And I navigate to the decision list for the "case"

步骤定义:

@When("^I navigate to the decision list for the \"([^\"]*)\"$")
public void INavigateToDecisionListFor(String case) throws Throwable {
    long caseId = ScenarioState.getCaseId(case);

    desicionlistPage.navigateTo(caseId);

    // It looks like this code is executed before the page navigation 
    // (above) is executed, hence making the test hang here looking for 
    // the elements on the
    // target page:

    Browser.Wait().ignoring(WebDriverException.class).until(webDriver -> 
    {
        WebElement enabledButton = null;
        WebElement disabledButton = null;
        try {
            enabledButton = webDriver.findElement(By.id("opprett-
            innstilling-btn"));
        } catch (NoSuchElementException ignore) {  }
        try {
            disabledButton = webDriver.findElement(By.id("opprett-
            innstilling-btn-disabled"));
        } catch (NoSuchElementException ignore) {  }

        return enabledButton != null || disabledButton != null;
    });
}

DecisionListPage.java:

public void navigateTto(long vased) {
    driver.navigate().to(Config.getSimulatedUrl() + "/#/case/" + cased + 
    "/decision/");
}

网址正确。我可以在测试挂起时手动输入它,然后测试继续(到元素验证)。因此,问题显然是在执行导航之前尝试进行验证。

在排除故障时,我尝试添加额外的等待并用 driver.get() 替换 driver.navigate.To(),但没有成功。还是经常失败。

但是,如果我重复navigate().to()步骤,那么它似乎有效。也就是说,只需执行两次,如下所示:

driver.navigate().to(Config.getSimulatedUrl() + "/#/case/" + cased + 
    "/decision/");
driver.navigate().to(Config.getSimulatedUrl() + "/#/case/" + cased + 
    "/decision/");

我已经手动运行了 30-40 次测试,并且上述解决方案没有失败。但这只是一种愚蠢的方法。

所以我想知道的是:在继续执行之前最好等待以确保 driver.navigate.To() 实际执行?我认为 driver.get() 是实现这一目标的方法,但这同样肯定会失败。

注意:这段代码不是我写的,但我正在使用它。我不确定我自己是否会像这样进行元素验证,但问题似乎在于导航未完成/等待,而不是检查本身。

如果问题不清楚,或者我是否应该在提问之前检查一下这个或那个明显的事情,我深表歉意。如果是这样,请告诉我,我会更新问题。

更新 还有一个用于导航到该页面的链接按钮。当我使用它时,它似乎一直有效。但这有点更糟糕;当它与链接一起使用时,为什么它不能与 navigation().to() 一起使用(除非我做了两次)?

最佳答案

    After reviewing your code, I hope you are using Scenario Outline concept of cucumber to run the same test case with multiple cases like below:

    Scenario Outline:
    When I navigate to the decision list for the "<code>"
    Examples:
    |code|
    |100|
    |200|
    |300|
    |400|
    |500|

    In your code, you are performing navigation and validation whether the expected elements are displaying on the loaded page or not in single step. Instead you can try in this in two steps to overcome navigational issues to the right coded url:

    -- feature file

    Scenario Outline:
    When I navigate to the decision list for the "<code>"
    Then I perform validation on loaded page
    Examples:
    |code|
    |100|
    |200|
    |300|
    |400|
    |500|

    --spec file

     @When("^I navigate to the decision list for the \"([^\"]*)\"$")
     public void INavigateToDecisionListFor(String case) {
           long caseId = ScenarioState.getCaseId(case);
           DecisionListPage.navigerTil(caseId );
     }
     // Above step will navigates you to the specific case page

    @Then("^I perform validation on loaded page"$)
    public boolean performValidation() throws Throwable {

        WebElement enabledButton = null;
        WebElement disabledButton = null;
        try {
            enabledButton = webDriver.findElement(By.id("opprett-
            innstilling-btn"));
        } catch (Exception ignore) {  }
        try {
            disabledButton = webDriver.findElement(By.id("opprett-
            innstilling-btn-disabled"));
        } catch (Exception ignore) {  }

        return enabledButton != null || disabledButton != null;
    }
    // above step will perform validations for you

    --DecisionListPage.java:

    public static void navigerTil(long caseId ) {
        driver.navigate().to(Config.getSimulatedUrl() + "/#/case/" + caseId + 
        "/decision/");
    }

Do these changes in your code and run it once. I hope, this process will resolve this issue.

关于Java、Cucumber 和 Selenium : Problems with navigate(). to(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56219316/

相关文章:

java - 使用ini4j编辑Windows注册表

java - Android 设备 session 管理的最佳实践

ruby-on-rails - Cucumber - 启用默认驱动程序(selenium)时数据库清理器不工作

ruby-on-rails - DHH 单元测试 : Is RSpec indeed needlessly complicated?

java - OpenNLP 与斯坦福 CoreNLP

java - Spring @ControllerAdvice 不起作用

javascript - 无法将对象转换为 Selenium 中的字符串

python - Selenium - onclick 按钮后切换到动态 iframe

Python/Selenium 网络驱动程序。在页面上查找一个元素并打印/返回它的 xpath

java - 如何使用 Grid 并行执行 cucumber 测试用例?