简体   繁体   English

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

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

In a setup with Java, Cucumber and Selenium: 在使用Java,Cucumber和Selenium的设置中:

I've got the following code which attempts to navigate to a page, and then look for the presence of one or two elements to verify that I am on the page itself. 我有以下代码尝试导航到一个页面,然后查找一个或两个元素的存在,以验证我在页面本身。

The problem: About 1 out of 5 times, the test hangs on the page before the target page, apparently looking for (and not finding) the elements before the page navigation is completed. 问题:大约5次中有1次,测试在目标页面之前的页面上挂起,显然在页面导航完成之前查找(并且没有找到)元素。

The error message: 错误消息:

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)

The Cucumber step: 黄瓜步骤:

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

The step definition: 步骤定义:

@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: DecisionListPage.java:

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

The URL is correct. 网址是正确的。 I can enter it manually when the test hangs, and then the test continues (to the element verification). 我可以在测试挂起时手动输入,然后测试继续(到元素验证)。 So the problem definetely seem to be that the verification is attempted before the navigation is performed. 因此问题似乎是在执行导航之前尝试验证。

When troubleshooting, I've tried adding extra waits and replacing driver.navigate.To() with driver.get(), with no luck. 在进行故障排除时,我尝试添加额外的等待并用driver.get()替换driver.navigate.To(),没有运气。 It still fails frequently. 它仍然经常失败。

However, if I repeat the navigate().to() step, then it seems to work. 但是,如果我重复导航()。到()步骤,那么它似乎工作。 That is, just do it twice, like this: 也就是说,只做两次,像这样:

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

I've run the tests manually 30-40 times, and it hasn't failed with the above solution. 我已经手动运行测试30-40次,并且上述解决方案没有失败。 But that's just a silly way to do it. 但这只是一种愚蠢的方式。

So what I'm wondering about is: The best wait to ensure that the driver.navigate.To() is actually performed before the execution continues? 所以我想知道的是:最好等待确保在执行继续之前实际执行driver.navigate.To()? I thought driver.get() was the way to acchieve that, but that fails just as surely. 我认为driver.get()是实现这一目标的方法,但这肯定会失败。

NB: This code is not written by me, but I'm using it. 注意:这段代码不是我写的,但是我正在使用它。 I'm not sure that I'd do the element verification quite like this myself, but the problem seems to be with the navigation not completing/waiting, and not the checks themselves. 我不确定我自己会做这样的元素验证,但问题似乎是导航没有完成/等待,而不是检查本身。

I apologize if the question is unclear, or if I should've checked this or that obvious thing before asking. 如果问题不清楚,或者在询问之前我应该​​检查这个或那个显而易见的事情,我道歉。 If so, please tell me and I'll update the question. 如果是这样,请告诉我,我会更新问题。

UPDATE There is also a link button for navigating to the page. 更新还有一个用于导航到页面的链接按钮。 When I use THAT, it seems to work all the time. 当我使用THAT时,它似乎一直都在工作。 But this is kind of worse; 但这有点糟糕; Why on earth doesn't it work with navigate().to() (unless I do it twice), when it works with the link? 为什么它在使用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