2016-11-01 75 views
0

考慮下面的代碼:如何等待頁面重新加載硒?

$this->visit('/'); 
$text = $this->byCssSelector('.some-element')->text(); 
$this->byCssSelector('.some-link')->click(); 
# how do I wait for a new page to be loaded here? 
$this->assertEquals($text, $this->byCssSelector('.some-element')->text()); 
+0

https://www.google.com/search?q=selenium+php+wait - > https://github.com/facebook/php-webdriver/wiki/HowTo-Wait – ceejayoz

回答

0

從我可以在網絡上看到,執行JavaScript是要避免如果可能的話,所以我想出了非JavaScript的解決方案:

$this->visit('/'); 
$text = $this->byCssSelector('.some-element')->text(); 
$this->waitForPageReload(function() { 
    $this->byCssSelector('.some-link')->click(); 
}, 5000); 
$this->assertEquals($text, $this->byCssSelector('.some-element')->text()); 

function waitForPageReload($navigate_f, $timeout) { 
    $id = $this->byCssSelector('html')->getId(); 
    call_user_func($navigate_f); 
    $this->waitUntil(function() use ($id) { 
     $html = $this->byCssSelector('html'); 
     if ($html->getId() != $id) 
      return TRUE; 
    }, $timeout); 
} 

靈感來自這個great article

0

您可以設置(通過JS調用)window.is_old_page = true,並檢查,直到這將是undefined