除了在我的测试中使用 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();
}
最佳答案
php-webdriver来自 Facebook 已于 2013 年 6 月重写。您可以像这样轻松等待 URL。
// wait for at most 10 seconds until the URL is 'http://example.com/account'.
// check again 500ms after the previous attempt.
$driver->wait(10, 500)->until(function ($driver) {
return $driver->getCurrentURL() === 'http://example.com/account';
})
关于php-webdriver : wait for browser response after submitting form using click(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14466993/