2012-02-15 81 views
3

,並在達到超時值,測試不停止或拋一個例外。相反,它只是繼續前進,就像頁面已成功加載一樣。它執行點擊並在第一個返回false的斷言處停止。Selenium測試超時不停止測試的PHPUnit 3.6.10硒RC 2.19.0

我發現這個PHPUnit中3.6.10的提交日誌:https://github.com/sebastianbergmann/phpunit/commit/cb772b06e9bd97f478fff7a212e3a4f7fe96bc29

在源:https://github.com/sebastianbergmann/phpunit/blob/5d0ff52bdff9afd479e38fdc880adce453ce88e5/PHPUnit/Framework/TestResult.php#L665

例外看起來應該還是被抓,但不幸的是例外而不是好像不引發測試停止,所以我在這裏失去了做什麼。

下面是一個例子測試惹我談論的行爲:

<?php 
class Example extends PHPUnit_Extensions_SeleniumTestCase 
{ 
    protected function setUp() { 
     $this->setBrowser('*firefox'); 
     $this->setBrowserUrl('http://myserver/timeout.php'); 
     $this->setTimeout(10); 
     $this->setWaitForPageToLoad(true); 
    } 

    public function testMyTestCase() { 
     // This should trigger an exception after timeout: 
     $this->openAndWait('/'); 

     // Anything below this line SHOULD never be executed because of timeout: 
     $this->click('id=clicketyclickthiswontbeclicked'); 
     $this->click('id=moreclicksthatwillnotbeclicked'); 

     // This will fail the test, because timeout didn't stop it 
     $this->assertTextPresent('this text'); 
    } 
} 
?> 

下面的PHP文件應該觸發超時。

timeout.php:

<?php 

// Should trigger timeout 
sleep(30); 

// (...) Rest of the page 

?> 

我做得不對或可這是一個錯誤?

回答

1

我要在這裏回答自己的情況下,任何人有這個問題。作爲解決方法,我重寫waitForPageToLoad函數。它調用父函數並跟蹤它花費等待頁面加載,以確定該頁面加載實際超時時間,如果它確實拋出異常。

下面是它的代碼:

protected function waitForPageToLoad($timeout=null) { 
    if (is_null($timeout)) { 
     $timeout = 30000; 
    } 
    $start = time(); 
    parent::waitForPageToLoad($timeout); 
    $end = time(); 
    if (($end - $start) >= ($timeout/1000)) { 
     throw new Exception('Timed out after '.$timeout.'ms.'); 
    } 
} 

這似乎令人難以置信的是不必要的,但在那一刻,我的作品,但我還是想回答,不需要這樣的黑客。