PHPUnit + Selenium 2: Action on ajax load(PHPUnit + Selenium 2:对 ajax 加载的操作)
问题描述
在测试期间我需要做以下事情:
During test I need to do following:
- 单击按钮会导致 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 请求+刷新并及时检查内容?
I wonder, is there any way to track ajax request+refresh and check for content just in time?
我的设置:
- 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
推荐答案
好吧,有一种解决方案,我不是很喜欢它,但它是有的而不是无.
Ok, there is a kind of solution, I do not really like it, but it is something instead of nothing.
这个想法是使用更智能的睡眠",有一个方法 waitUntil() 采用 anonymous function 和 timeout in毫秒.什么是 - 在循环中运行这个传递的函数,直到超时或你的函数返回 True.所以你可以运行一些东西并等到上下文改变:
The idea is to use more smart "sleep", there is a method waitUntil() which takes an anonymous function and timeout in milliseconds. What is does - runs this passed function in loop until timeout hits or your function return True. So you can run something and wait until context is changed:
$this->waitUntil(function () {
if ($this->byCssSelector('h1')) {
return true;
}
return null;
}, 5000);
如果有人提供更好的解决方案,我仍然会很高兴.
I still will be glad if somebody give better solution.
这篇关于PHPUnit + Selenium 2:对 ajax 加载的操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:PHPUnit + Selenium 2:对 ajax 加载的操作
基础教程推荐
- 如何替换eregi() 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
