Java,黄瓜和硒:Navigation()。to()问题

时间:2019-05-20 10:46:13

标签: 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)

黄瓜步骤:

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/");
}

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

在进行故障排除时,我尝试添加额外的等待时间,并用driver.get()替换driver.navigate.To(),但是没有运气。它仍然经常失败。

但是,如果我重复执行navigation()。to()步骤,那么它似乎可以工作。也就是说,只需执行两次,就像这样:

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

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

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

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

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

更新 还有一个链接按钮,用于导航到页面。当我使用THAT时,它似乎一直都在工作。但是,这种情况更糟。当它与链接一起工作时,为什么它实际上不与Navigation()。to()一起工作(除非我两次)?

1 个答案:

答案 0 :(得分:0)

    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.