4

在测试期间,我需要执行以下操作:

  • 单击导致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);

问题

  • 消息检查在点击后立即进行,而不是在 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
  • 视窗 7 x64
  • JRE 7
4

2 回答 2

10

好吧,有一种解决方案,我不是很喜欢它,但它是有的,而不是无。

这个想法是使用更智能的“睡眠” ,有一种方法waitUntil()需要以毫秒为单位。什么是 - 在循环中运行这个传递的函数,直到超时或您的函数返回。所以你可以运行一些东西并等到上下文改变:anonymous functiontimeoutTrue

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

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

于 2013-07-23T17:52:02.120 回答
0

嗯,我不确定它是否对你有用,但你可以尝试使用它:

$this->timeouts()->implicitWait(20000);
于 2013-08-22T09:37:41.380 回答