Phpunit - Selenium2 - 等待页面完全加载

时间:2013-06-19 16:14:44

标签: phpunit selenium-webdriver wait assert pageload

我正在使用phpunit和selenium 2(Selenium2TestCase)的扩展,用于检查我网站的界面。 所以我的测试页面包含这个:

testDisplay()
{
...
file_put_contents('/home/toto/www/screenshots/before.html', $this->source());
sleep(5);
file_put_contents(/home/toto/www/screenshots/after.html', $this->source());
$this->assertContains('toto42', $this->source());
...
}

然后当我执行:

$> phpunit testSelenium.php

我的页面有2个html文件,在睡眠前(5)和之后:

before.html:

<META charset="utf-8"/>
<META content="text/html; charset=utf-8" http-equiv="Content-Type"/>
<TITLE>Test</TITLE>

<LINK href="/css/style.css" rel="stylesheet"/>
<LINK href="/css/bootstrap.css" rel="stylesheet"/>
<SCRIPT src="/js/jquery-1.7.1.min.js" type="text/javascript"/>
</HEAD></HTML>

after.html:

<META charset="utf-8"/>
<META content="text/html; charset=utf-8" http-equiv="Content-Type"/>
<TITLE>Test</TITLE>

<LINK href="/css/style.css" rel="stylesheet"/>
<LINK href="/css/bootstrap.css" rel="stylesheet"/>
<SCRIPT src="/js/jquery-1.7.1.min.js" type="text/javascript"/>
</HEAD>
<BODY>
...
toto42
...
</BODY>

我不明白为什么我要睡一觉(5)来完全加载我的页面,有人有想法吗?

另一个问题是Selenium2TestCase的clickAtAndWait(PHPUnit_Extensions_SeleniumTestCase)的等价物是什么?

提前致谢

1 个答案:

答案 0 :(得分:2)

我找到了答案。 我在我的页面中寻找一个带有超时的HTML元素,如果没有出现,我等待函数implicitWait();

testDisplay()
{
...
//I use 25000 for the timeout but anything else works too it depends on your loading page 
$this->timeouts()->implicitWait(25000);
$this->assertContains('toto42', $this->source());
...
}