咨询 / dusk-standalone
独立dusk测试,非常适合老旧项目或远程测试。
Requires
- php: >=7.2
- konsulting/project-root: ^1.1
- laravel/dusk: ^7.0
- orchestra/dusk-updater: ^1.2
- phpunit/phpunit: ^7.5|^8.0|^9.0
README
这是一个简单的包,用于在Laravel Dusk和PHPUnit中测试Laravel应用程序之外的浏览器测试。
该包旨在允许本地和远程测试没有类似测试框架的老旧应用程序。有其他选项可以这样做,但我们喜欢Dusk的API。
由于我们没有集成到Laravel,可能不是所有东西都能按预期工作。这个包还处于早期阶段,所以欢迎改进。
我们还帮助另一个包,用于使用Laravel Dusk测试Laravel包,Orchestra Testbench-Dusk。
版本兼容性
该包的版本与Dusk的主要版本相一致。
安装
我们建议使用composer。
composer require konsulting/dusk-standalone
用法
我们倾向于将浏览器测试放在tests/Browser
中。创建一个新的基测试用例,它扩展了Konsulting\DuskStandalone\TestCase
。
这将允许你在Dusk测试中添加任何你想要的定制。
<?php namespace Application\Tests; use Konsulting\DuskStandalone\TestCase; abstract class DuskTestCase extends TestCase { // Set the base url for the browser requests protected function baseUrl() { return 'https://www.klever.co.uk'; } // Set the path for browser tests, this is where screenshots/console logs // are stored. The default is [app root]/tests/Browser based on the // vendor folder location as per a normal Composer install. protected function browserTestsPath() { return parent::browserTestsPath(); } // Set the default user - however... this is only useful if you will be // using the Dusk type login. It's worth reviewing the Laravel Dusk // trait InteractsWithAuthentication to combine with your app. protected function user() { return parent::user(); } }
现在创建你的测试
<?php namespace Application\Tests\Browser; use Laravel\Dusk\Browser; use Konsulting\DuskStandalone\Tests\DuskTestCase; class ExampleTest extends DuskTestCase { /** @test * */ public function it_can_browse_a_site() { $this->browse(function (Browser $browser) { $browser->visit('/')->assertSee('Klever'); }); } }
作为你的测试套件的一部分运行你的测试,或者将它们分开以单独运行(因为浏览器测试可能很慢)。
有关Dusk文档,请参阅这里
身份验证
如果你能够添加Dusk登录路由到你的应用程序,并且它们是安全的,你将能够使用标准的dusk login
、loginAs
、logout
和其他方法。
如果这不可能,你可以利用Browser类的'Macroable'特性在运行时添加自定义方法。请小心不要尝试在Browser类上覆盖现有方法。
例如
<?php // In your testing bootstrap file. \Laravel\Dusk\Browser::macro('customLoginAs', function ($user, $pass) { $this->browse('login_url') ->type('username', $user) ->type('pasword', $pass) ->press('Login'); });
Chrome版本
如果你收到一个包含有关Chrome版本的信息的错误(例如,Chrome版本必须在70到73之间)
请运行vendor/bin/dusk-updater update
有关更多信息,请参阅Dusk Updater项目
贡献
欢迎贡献,并将得到充分认可。我们将接受通过Pull Request的贡献。
请
- 使用PSR-2编码规范
- 添加测试,如果你不确定如何,请提问。
- 记录行为的变化,包括readme.md。
测试
我们使用PHPUnit
使用PHPUnit运行测试:vendor/bin/phpunit