问题
How to force Selenium2 to follow all redirects before doing some asserts?
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"
I figured that I could simply wait:
/**
* @Then /^I should be redirected to "([^"]*)"$/
*/
public function assertRedirect($url)
{
$this->getSession()->wait(10000);
$this->assertPageAddress($url);
}
The problem is that however long I wait, I always end up on "/auth" page, not "/main".
UPDATE: It turns out the problem is mythical, selenium isn't doing anything special and browser is following redirects by default as it usually does. It my case the page that was supposed to produce redirect was actually sending 200 response.
回答1:
I have run into a situation similar to yours. I set up a wait method that polls for an element every second for x number of seconds waiting for the element to become visiable. I then pass an Xpath to an element only available on the last page, or /main in your case. Here is the method I use in 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));
}
You may or may not need the last fluentWait for visibility as when the element is returned you will be on the correct /main page.
Hope this helps. Good Luck!
来源:https://stackoverflow.com/questions/12725974/mink-with-selenium2-follow-all-redirects