我的应用程序使用Mink和Selenium 2驱动程序。当我尝试加载一些加载缓慢(或根本不加载)的资源的页面时,应用程序会无限等待,直到所有内容加载完毕。
,因为我的应用程序中有数百次迭代-您可以想象脚本执行的时间。
问题:是否有可能设置页面加载的超时时间?并抛出一些异常,如果页面在此期间没有加载?
提前感谢!
根据这篇文章,你可以这样做:
$driver->setTimeouts(['page load' => 10000]);
要在selenium ide中设置页面加载超时,请遵循以下步骤:
1。打开selenium ide。
2。点击选项菜单
3。在general选项卡中更改所记录命令的默认超时值。
![1]
在selenium 2中使用这个函数
WebDriver driver = new FirefoxDriver();
driver.get("http://somedomain/url_that_delays_loading");
WebElement myDynamicElement = (new WebDriverWait(driver, 10))
.until(ExpectedConditions.presenceOfElementLocated(By.id("myDynamicElement")));
试试这个
private $timeout = 60000;
public function reload()
{
$this->browser
->refresh()
->waitForPageToLoad($this->timeout)
;
}
[1]: h
ttp://i.stack.imgur.com/0NKoC.png
请在您的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);
}
behavior文档建议在上下文中使用自定义spin()函数。
下面的spin()函数示例摘自行为文档:
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'] . "()'n" .
$backtrace[1]['file'] . ", line " . $backtrace[1]['line']
);
}
不幸的是,我没有一个工作的例子如何将其集成到您的上下文中