php-webdriver:使用click()提交表单后等待浏览器响应

Jus*_*att 10 php selenium phpunit selenium-webdriver

除了sleep()在我的测试中使用之外,我想知道是否有人知道在继续我的断言之前明确等待表单提交(POST)完成的更好策略.这是我的测试看起来非常精简的版本,使用phpunit和php的 web -webdriver).

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();
}
Run Code Online (Sandbox Code Playgroud)

小智 19

来自Facebook的php-webdriver已于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';
})
Run Code Online (Sandbox Code Playgroud)


hel*_*ert 0

相反,click()应该可以使用clickAndWait(), 来等待下一页加载。