My application uses Mink with Selenium 2 driver. when i try to load the page with some resources that are loading slow (or not loading at all) the application waits infinitely until everything is loaded.
for i have several hundreds of iterations in my application - you can imagine how long the script is executed.
question: is there any possibility to set a timeout for page to load? and throw some exception if the page is not loaded during that period?
thanks in advance!
To set timeout for loading for page in selenium ide follow these steps:
1. Open selenium ide.
2. Click on option menu.
3. In general tab change defult timeout value for recorded command.
![Selenium ide image after click on option menu][1]
In selenium 2 use this function
WebDriver driver = new FirefoxDriver();
driver.get("http://somedomain/url_that_delays_loading");
WebElement myDynamicElement = (new WebDriverWait(driver, 10))
.until(ExpectedConditions.presenceOfElementLocated(By.id("myDynamicElement")));
Try this
private $timeout = 60000;
public function reload()
{
$this->browser
->refresh()
->waitForPageToLoad($this->timeout)
;
}
ttp://i.stack.imgur.com/0NKoC.png
The Behat documentation suggests to use a custom spin() function inside your context.
The following spin() function example is taken from the behat documentation:
public function spin ($lambda, $wait = 60)
{
for ($i = 0; $i < $wait; $i++)
{
try {
if ($lambda($this)) {
return true;
}
} catch (Exception $e) {
// do nothing
}
sleep(1);
}
$backtrace = debug_backtrace();
throw new Exception(
"Timeout thrown by " . $backtrace[1]['class'] . "::" . $backtrace[1]['function'] . "()
" .
$backtrace[1]['file'] . ", line " . $backtrace[1]['line']
);
}
Unfortunately I don't have a working example how to integrate this into your context.
please use below three functions in your Featurecontext.php
public function spin($lambda, $retries,$sleep) {
do {
$result = $lambda($this);
} while (!$result && --$retries && sleep($sleep) !== false);
}
public function find($type, $locator, $retries = 20, $sleep = 1) {
return $this->spin(function($context) use ($type,$locator) {
$page = $context->getSession()->getPage();
if ($el = $page->find($type, $locator)) {
if ($el->isVisible()) {
return $el->isVisible();
}
}
return null;
}, $retries, $sleep);
}
/**
* Wait for a element till timeout completes
*
* @Then /^(?:|I )wait for "(?P<element>[^"]*)" element$/
*/
public function iWaitForSecondsForFieldToBeVisible($seconds,$element) {
//$this->iWaitSecondsForElement( $this->timeoutDuration, $element);
$this->find('xpath',$element);
}
According to this article you can do it like that:
$driver->setTimeouts(['page load' => 10000]);
This timeout is in milliseconds.