【问题标题】:PHPUnit + Selenium 2: Action on ajax loadPHPUnit + Selenium 2:对 ajax 加载的操作
【发布时间】:2013-07-22 04:02:11
【问题描述】:

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

  • 单击按钮会导致 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
  • Windows 7 x64
  • JRE 7

【问题讨论】:

    标签: php ajax selenium phpunit automated-tests


    【解决方案1】:

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

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

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

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

    【讨论】:

    • 这会导致内存泄漏。
    • 这实际上效果很好,它几乎与implicitWait一样。
    【解决方案2】:

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

    $this->timeouts()->implicitWait(20000);
    

    【讨论】:

      猜你喜欢
      • 2017-05-07
      • 2015-08-05
      • 2011-05-11
      • 2012-11-16
      • 1970-01-01
      • 2019-01-17
      • 1970-01-01
      • 2014-01-07
      • 2023-03-09
      相关资源
      最近更新 更多