laravel/dusk
This article was peer reviewed by Younes Rafie. Thanks to all of SitePoint’s peer reviewers for making SitePoint content the best it can be!
该文章由Younes Rafie进行了同行评审。 感谢所有SitePoint的同行评审人员使SitePoint内容达到最佳状态!
End to end testing for JavaScript applications, particularly single-page-apps, has always been a challenge. To that end, Laravel released its 5.4 version recently with a new testing library: Dusk.
对JavaScript应用程序(尤其是单页应用程序)进行端到端测试一直是一个挑战。 为此,Laravel最近发布了其5.4版本,并带有一个新的测试库: Dusk 。
With the release of Dusk, Laravel hopes to give its users a common API for browser testing. It ships with the default ChromeDriver, and if we need support for other browsers, we can use Selenium. It will still have this common testing API to cater to our needs.
随着Dusk的发布,Laravel希望为用户提供用于浏览器测试的通用API。 它带有默认的ChromeDriver ,如果需要其他浏览器的支持,可以使用Selenium。 它将仍然具有此通用测试API来满足我们的需求。
The tutorial will assume you’re starting a new Laravel 5.4 app.
本教程将假定您正在启动新的Laravel 5.4应用程序。
安装 (Installation)
composer require laravel/dusk
This will install the most recent stable version of the package via Composer.
这将通过Composer安装软件包的最新稳定版本。
Next, we need to register DuskServiceProvider
within our application. We can do it in a couple of ways:
接下来,我们需要在我们的应用程序中注册DuskServiceProvider
。 我们可以通过以下两种方式来实现:
方法1 (Approach 1)
We can include it in the providers
array of our config/app.php
file.
我们可以将其包含在config/app.php
文件的providers
数组中。
...
App\Providers\RouteServiceProvider::class,
Laravel\Dusk\DuskServiceProvider::class,
...
The problem with this approach is that DuskServiceProvider
will be registered in our application for all the environments. We don’t need Dusk
to be available and registered in our production environment. We can avoid this with the second approach.
这种方法的问题在于, DuskServiceProvider
将针对所有环境在我们的应用程序中注册。 我们不需要Dusk
在我们的生产环境中可用和注册。 我们可以使用第二种方法避免这种情况。
方法2 (Approach 2)
Register DuskServiceProvider
in the AppServiceProvider
class for specific environments:
在AppServiceProvider
类中为特定环境注册DuskServiceProvider
:
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Laravel\Dusk\DuskServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
if ($this->app->environment('local', 'testing', 'staging')) {
$this->app->register(DuskServiceProvider::class);
}
}
}
Next, to complete the installatio