与Selenium2貂皮:遵循所有重定向

时间:2012-10-04 11:09:26

标签: selenium selenium-webdriver behat mink

如何在执行某些断言之前强制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”。

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

1 个答案:

答案 0 :(得分:0)

我遇到了与你类似的情况。我设置了一个wait方法,每隔一秒轮询一个元素,持续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));
 }

您可能需要或可能不需要最后一个流畅的可见性,因为当元素返回时,您将位于正确的/主页面上。

希望这会有所帮助。祝你好运!