繁体   English   中英

Java,Cucumber和Selenium:navigate()。to()的问题

[英]Java, Cucumber and Selenium: Problems with navigate().to()

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

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

问题:大约5次中有1次,测试在目标页面之前的页面上挂起,显然在页面导航完成之前查找(并且没有找到)元素。

错误消息:

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)

黄瓜步骤:

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(),没有运气。 它仍然经常失败。

但是,如果我重复导航()。到()步骤,那么它似乎工作。 也就是说,只做两次,像这样:

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

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

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

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

如果问题不清楚,或者在询问之前我应该​​检查这个或那个显而易见的事情,我道歉。 如果是这样,请告诉我,我会更新问题。

更新还有一个用于导航到页面的链接按钮。 当我使用THAT时,它似乎一直都在工作。 但这有点糟糕; 为什么它在使用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.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM