2013-04-12 42 views
3

我的应用程序使用与Selenium 2驱动程序貂皮。当我尝试加载页面时加载缓慢的一些资源(或根本不加载),应用程序会无限等待,直到加载所有内容为止。我如何设置Selenium 2驱动程序的貂皮页面加载超时?

因为我在我的应用程序中有几百次迭代 - 你可以想象脚本执行了多长时间。

问题:有没有可能设置页面加载超时?并在该期间没有加载页面时抛出一些异常?

在此先感谢!

回答

-1

要设置超时时间为硒IDE中装入的页面请按照下列步骤操作:

1.打开硒IDE。

2.点击选项菜单。

3.在一般标签中更改记录命令的defult超时值。 !

[硒点击选项菜单后IDE图像] [1]

硒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

+0

谢谢,纳文!但我不是在谈论Selenium IDE。我使用Selenium WebDriver。 – Axarsu

+0

我是新来的硒,所以我现在不是那个时候的硒2。对不起.... :) –

+0

我希望这一次你得到你的答案。 –

-2

Behat documentation建议在您的上下文中使用自定义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'] 
    ); 
} 

不幸的是我没有一个工作示例如何将其整合到您的上下文。

+0

谢谢你,t3node :)但问题在这里:$ this-> session = new \ Behat \ Mink \ Session($ driver); $ this-> session-> start(); $ this-> session-> visit('some URL') - 当访问被执行时 - 在加载页面之前,您无法执行任何操作。并且您建议的决定可以在页面加载后应用。一些硬页面(在弱服务器上)可以加载很长时间。我想控制这一点。 – Axarsu

-1

,请使用以下三种功能于你的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); 
} 
1

根据this article你可以做这样的:

$driver->setTimeouts(['page load' => 10000]); 

此超时是毫秒。

相关问题