【发布时间】:2013-01-06 04:21:12
【问题描述】:
除了在我的测试中使用sleep() 之外,我想知道是否有人知道在继续我的断言之前明确等待表单提交 (POST) 完成的更好策略。这是我的测试的一个非常精简的版本,使用 phpunit 一起使用 php-webdriver 来自 Facebook)。
function test_form_submission()
{
// setup
$web_driver = new WebDriver();
$session = $web_driver->session();
$session->open('http://example.com/login');
// enter data
$session->element('css selector', 'input[name=email]')->value(array('value' => str_split('test@example.com')));
$session->element('css selector', 'input[name=password]')->value(array('value' => str_split('testpassword')));
// click button to submit form
$session->element('css selector', 'button[name=submit]')->click();
// How do I wait here until the click() above submits the form
// so I can check that we correctly arrives at the destination below
// without resorting to sleep()?
// Confirm that login was successful because we landed on account page
$this->assertSame('http://example.com/account', $session->url());
// teardown
$session->close();
}
【问题讨论】:
-
那么,当您现在执行此代码时,倒数第二行(断言)中的 url 给出的是 example.com/login 而不是 example.com/account?据推测,Selenium 服务器应该等到页面加载完毕后再查找元素。异步请求是个例外,但您的请求看起来非常同步。请注意,我正在使用 PHPUnit_Selenium 来驱动 Selenium
-
没错。事实上,我当时也可以截屏,看到我仍在 example.com/login 上,但如果我睡 1 秒,断言通过,截屏显示页面现在是示例。 com/帐户。我读到 PHPUnit_Selenium 还不能运行 Selenium 2(又名 webdriver),所以这就是我采用上述方法的原因。
标签: php selenium phpunit selenium-webdriver