簡體   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