PHPUnit + Selenium 2:对ajax加载的操作

Pav*_*vel 4 php ajax selenium phpunit automated-tests

在测试期间,我需要做以下事情:

  • 点击导致ajax请求的按钮,然后重定向
  • 检查用户是否已重定向到正确的页面

我的代码:

$this->byId('reg_email')->value('test@example.com');
$this->byId('reg_password')->value('seecret');
// No form here, so i can't just call submit()
// This click invokes ajax request
$this->byId('reg_submit')->click();

// Check page content (this page should appear after redirect)
$msg = $this->byCssSelector('h1')->text();
$this->assertEquals('Welcome!', $msg);
Run Code Online (Sandbox Code Playgroud)

问题

  • 单击后立即进行消息检查,而不是在ajax请求和页面重定向之前

解决方案,我不喜欢:

  • sleep(3);在内容检查之前添加.

我不喜欢它,因为:

  • 这很傻
  • 在快速响应的情况下,我将浪费时间,如果长时间请求,我将在ajax请求完成之前获得内容检查.

我想知道,有没有办法跟踪ajax请求+刷新并及时检查内容?

我的设置:

  • PHP 5.4,5.5也可用
  • PHPUnit 3.8
  • 用于PHPUnit 1.3.1的Selenium RC集成
  • Selenium-server-standalone 2.33.0
  • Windows 7 x64
  • JRE 7

Pav*_*vel 10

好吧,有一种解决方案,我不是很喜欢它,但它是一种东西,而不是什么.

我们的想法是使用更智能"休眠",有一种方法,waitUntil()它接受一个anonymous functiontimeout以毫秒为单位.做什么 - 在循环中运行这个传递的函数,直到超时命中或函数返回True.所以你可以运行一些东西并等到上下文改变:

$this->waitUntil(function () {
    if ($this->byCssSelector('h1')) {
        return true;
    }
    return null;
}, 5000);
Run Code Online (Sandbox Code Playgroud)

如果有人提供更好的解决方案,我仍然会很高兴.