1

如何在做一些断言之前强制 Selenium2 遵循所有重定向?

  Scenario: Guest member can pay with card
    When I go to "/test"
    #test page redirects to "/auth" which then redirects to "/main"
    Then I should be redirected to "/main"

我想我可以简单地等待:

  /**
   * @Then /^I should be redirected to "([^"]*)"$/
   */
  public function assertRedirect($url)
  {
    $this->getSession()->wait(10000);

    $this->assertPageAddress($url);
  }

问题是无论我等待多久,我总是在“/auth”页面上结束,而不是“/main”。

更新:事实证明问题是神话般的,硒没有做任何特别的事情,并且浏览器默认情况下会像往常一样遵循重定向。在我的情况下,应该产生重定向的页面实际上是发送 200 响应。

4

1 回答 1

0

我遇到了和你类似的情况。我设置了一个等待方法,每秒轮询一个元素,持续 x 秒,等待元素变为可见。然后,我将 Xpath 传递给仅在最后一页上可用的元素,或 /main 在您的情况下。这是我在java中使用的方法。

 public void waitForElement(WebDriver driver, final String xpath)
 {
     //Set up fluentWait to wait for 35 seconds polling every 1
     Wait<WebDriver> fluentWait = new FluentWait<WebDriver>(driver)
         .withTimeout(35, TimeUnit.SECONDS)
         .pollingEvery(1, TimeUnit.SECONDS)
         .ignoring(NoSuchElementException.class);

     WebElement element;

     //Look for element, if not found start fluentWait
     try
     {
         element = driver.findElement(By.xpath(xpath));
     }
     catch (WebDriverException e)
     {
         logger.info("[getElementByXpath] Element not initially found. Starting fluentWait ["+xpath+"]");

         try
         {
             element = fluentWait.until(new Function<WebDriver, WebElement>() {
                 public WebElement apply(WebDriver d) {

                     return d.findElement(By.xpath(xpath));
                 }
             });
         }
         catch (WebDriverException f)
         {
             logger.info("[getElementByXpath] FluentWait findElement threw exception:\n\n" + f +"\n\n");

             throw new WebDriverException("Unable to find element ["+xpath+"]");
         }
     }

     //Once we've found the element wait for element to become visible
     fluentWait.until(ExpectedConditions.visibilityOf(element));
 }

您可能需要也可能不需要最后一个 fluentWait 以获取可见性,因为当元素返回时,您将位于正确的 /main 页面上。

希望这可以帮助。祝你好运!

于 2012-10-04T15:23:37.147 回答