2012-10-04 20 views
1

如何強制Selenium2之前做一些斷言跟隨所有重定向?水貂與硒2:按照所有重定向

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」頁面,而不是「/主」上。

UPDATE:原來的問題是神話,硒是沒有做什麼特別的東西和瀏覽器默認情況下,以下的重定向,因爲它通常不會。我的情況是,應該產生重定向的頁面實際上是發送200響應。

+0

您是否在Symfony 2項目上運行過這些測試? –

回答

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當元素返回你會正確/主頁上。

希望這會有所幫助。祝你好運!

+0

FluentWait類背後的代碼是什麼? – Dziamid

+0

http://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/support/ui/FluentWait.html org.openqa.selenium.support.ui.FluentWait – Falkenfighter

+0

此前使用fluentWait我使用了以下鏈接中的Explicit Wait。這可能會有更多的幫助,因爲它引用了除Java以外的其他語言。 http://seleniumhq.org/docs/04_webdriver_advanced.html – Falkenfighter