【问题标题】:Selenium Java Test Wait for Redirect to FinishSelenium Java 测试等待重定向完成
【发布时间】:2018-05-08 16:16:40
【问题描述】:

假设我想测试我的 LoginPage 功能。因此,如果我输入正确的用户名和密码,那么我将被重定向到 welcomepage.jsp。

我使用了容器管理的安全性,因此您会在 URL 中看到一些 j_security_check。

Feature: MyApp Login Feature
  Validate MyApp Login Features

  Scenario Outline: User logs to MyApp
    Given I navigate to MyApp login page
    And I enter <username> and <password>
    And I click on login button
    Then user entered correct username and password then they should be redirected to the proper <url>

    Examples: 
      | username     | password              | url                  |
      | correctuser  | correctpassword       | welcome.jsp          |

我的问题是如何处理 302 重定向。 如果我在浏览器中查看我的网络选项卡,我会看到以下序列

  1. POST j_security_check
  2. 获取welcome.jsp

    @Then("^Then user entered correct username and password then they should be redirected to the proper ([^\"]*)$")
    public void user_should_be_redirected_to_the_proper(String expectedURL) throws Throwable {
        System.out.println("expectedURL :: " + expectedURL );
        if (driver.getCurrentUrl().contains(expectedURL)) {
            System.out.println("MyApp Test Pass");
        } else {
            System.out.println("MyApp Test Failed");
            Assert.fail("MyApp Test failed!");
        }
    }
    

我在这里看到了一些类似的问题 JavaScript Executor in Selenium WebDriver

但我认为这不是 AJAX 调用,因为我们正在等待 DOM 就绪

【问题讨论】:

    标签: java selenium selenium-webdriver cucumber selenium-chromedriver


    【解决方案1】:

    在断言之前尝试使用ExpectedConditionsurlMatchesurlContainsurlToBe 之一以等待重定向完成。

    【讨论】:

    • 嗨,伙计..我的问题是你如何延迟这样的测试,直到当前的 url 已经转发到 welcome.jsp? url 更改并被重定向到新的welcome.jsp 页面。这不是一个 ajax 请求,我们只是在等待返回值。谢谢你。
    【解决方案2】:

    @Grasshopper 的答案是正确的,您需要将WebDriverWaitExpectedConditions 结合使用。但是,由于您将使用 expectedURL 而不是 urlMatchesurlContainsurlToBe 断言 getCurrentUrl(),因此您需要使用 @987654323 @ 方法传递最终 urlPage Title 的参数。 ExpectedConditions titleContains() 当标题根据您的应用程序标题匹配时返回 true。这反过来将确保您设置了最终的 url。您可以使用以下代码块,我假设最终的 urlwelcome.jsp 结尾 将有一个 Page Title 作为 Welcome - Mark Estrada - Home Page

    import org.openqa.selenium.support.ui.WebDriverWait;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    // other lines of code
    @Then("^Then user entered correct username and password then they should be redirected to the proper ([^\"]*)$")
    public void user_should_be_redirected_to_the_proper(String expectedURL) throws Throwable 
    {
        new WebDriverWait(driver, 20).until(ExpectedConditions.titleContains("Welcome - Mark Estrada - Home Page"));
        System.out.println("expectedURL :: " + expectedURL );
        if (driver.getCurrentUrl().contains(expectedURL)) {
        System.out.println("MyApp Test Pass");
        } else {
        System.out.println("MyApp Test Failed");
        Assert.fail("MyApp Test failed!");
        }
    }
    

    【讨论】:

      【解决方案3】:

      您可以等到 URL 包含或匹配某个字符串:

      import org.openqa.selenium.support.ui.ExpectedConditions;
      
      WebDriverWait wait5s = new WebDriverWait(driver,5);
      wait5s.until(ExpectedConditions.urlContains("welcome.jsp"));
      

      要等待DOM被加载最好等待一个元素是可点击的:

      wait5s.until(ExpectedConditions.elementToBeClickable(locator);
      

      或可选:

      wait5s.until(ExpectedConditions.attributeToBe(locator, attribute, value);
      

      在凭据错误的情况下,只需用if/else 包围即可。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-12-08
        • 2023-01-28
        • 1970-01-01
        • 2017-06-15
        • 2021-01-04
        • 2014-05-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多