Laravel Dusk 是 Laravel 官方提供的浏览器测试解决方案:
https://laravel.com/docs/5.4/dusk
一个演示视频:
https://www.youtube.com/watch?v=V75hPsS6cvk
体验下来的优势包括:
- Laravel 官方的工具,质量非常可靠
- 因为是后端实现,所以 Seed、数据伪造、用户身份套用 等都非常方便
- 如果仅仅用 Chrome 作为默认测试浏览器,那么就不需要安装 JDK 和 Selenium,很方便
- 测试遇到错误会截图保存,非常方便
风险:
- 移动端测试的需求无法满足
- 在正式环境无法良好运行测试
自定义环境变量:
To force Dusk to use its own environment file when running tests, create a .env.dusk.{environment} file in the root of your project. For example, if you will be initiating the dusk command from your local environment, you should create a .env.dusk.local file.
When running tests, Dusk will back-up your .env file and rename your Dusk environment to .env. Once the tests have completed, your .env file will be restored.
绕过登录
$this->browse(function ($first, $second) {
$first->loginAs(User::find(1))
->visit('/home');
});
支持 Drag & Drop
The drag method may be used to drag an element matching the given selector to another element:
$browser->drag('.from-selector', '.to-selector');
Or, you may drag an element in a single direction:
$browser->dragLeft('.selector', 10);
$browser->dragRight('.selector', 10);
$browser->dragUp('.selector', 10);
$browser->dragDown('.selector', 10);
Pages 机制
如果你的测试用例会出现比较复杂的页面跳转情况,那么就可以用 Pages 的机制实现代码分离,也便于让测试用例看起来更清晰,具体可以参照官方文档
持续集成
官方推荐 Travis CI 或者 CircleCI
备忘
运行测试:
./artisan dusk