博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
laravel/dusk_Laravel Dusk简介
阅读量:2518 次
发布时间:2019-05-11

本文共 9541 字,大约阅读时间需要 31 分钟。

laravel/dusk

Laravel Dusk was one of the new features introduced in Laravel 5.4. Dusk is a tool for application testing.

Laravel Dusk是Laravel 5.4中引入的新功能之一。 Dusk是用于应用程序测试的工具。

One of the challenges of testing with PHPUnit was the inablity to test JavaScript based application functionality.

用PHPUnit测试的挑战之一是无法测试基于JavaScript的应用程序功能。

With Dusk the test is run in the browser so client-side features like JavaScript are testable.

使用Dusk可以在浏览器中运行测试,因此可以测试JavaScript等客户端功能

Dusk is based on the open source tools ChromeDriver and Facebook Php-webdriver which makes it simple to use without the need to experience the intense procedure of setting up Selenium. In this article we will would cover installing Dusk in a Laravel application and running a few basic tests including authentication testing.

Dusk基于开放源代码工具ChromeDriver和Facebook Php-webdriver,使它易于使用,而无需经历繁琐的设置Selenium的过程。 在本文中,我们将介绍如何在Laravel应用程序中安装Dusk并运行一些基本测试,包括身份验证测试。

( )

Prior versions of Laravel used Symfony BrowserKit component to simulate a web browser. This had it's limitations as this is not a real browser. With Dusk the automation comes natively through it's use of ChromeDriver and Facebook Php-webdriver.

Laravel的早期版本使用Symfony BrowserKit组件来模拟Web浏览器。 这有其局限性,因为它不是真正的浏览器。 借助Dusk,自动化本身就是通过使用ChromeDriver和Facebook Php-webdriver来实现的。

One of Dusk features also inlcudes the ability to wait for a condition to be true at the frontend, before executing tests. For example it could wait for a JavaScript component or CSS selector to load before taking any action.

黄昏功能之一还包括能够在执行测试之前在前端等待条件为真的能力。 例如,它可以在执行任何操作之前等待JavaScript组件或CSS选择器加载。

( )

To begin create a new Laravel project:

要开始创建一个新的Laravel项目:

laravel new dusk

Then go into the directory and pull in the laravel/dusk package

然后进入目录并拉入laravel/dusk

cd duskcomposer require laravel/dusk

When installing a Laravel package the service provider class is usually registered in the config\app.php file. However Dusk exposes some security holes that you don't want to have in the production environment, for example the ability to log in as other users.

在安装Laravel软件包时,服务提供者类通常在config\app.php文件中注册。 但是, Dusk暴露了一些您在生产环境中不需要的安全漏洞,例如,以其他用户身份登录的能力。

To get around this, you should register the service provider class in a conditional that makes sure we are in a local environment in the

要解决此问题,您应该在确保我们位于本地环境中的条件下注册服务提供者类。

app\Providers\AppServiceProvider.php file.

app\Providers\AppServiceProvider.php文件。

use Laravel\Dusk\DuskServiceProvider;// Importing DuskServiceProvider class...public function register(){
if ($this->app->environment('local', 'testing')) {
$this->app->register(DuskServiceProvider::class); }}

Next run the artisan command to install Dusk:

接下来运行artisan命令以安装Dusk:

php artisan dusk:install

This command creates a Browser directroy inside the tests folder of your project and adds a basic example test. Also amongst other sub-folders you would notice a folder named screenshots, when a test fails Dusk takes a picture of the screen and saves it in this folder. This can be useful when debugging.

此命令在项目的tests文件夹内创建Browser Directroy,并添加基本示例测试。 在其他子文件夹中,您也会注意到一个名为screenshots的文件夹,当测试失败时,Dusk拍摄屏幕图片并将其保存在此文件夹中。 这在调试时很有用。

Next you need to update the APP_URL key of your .env file, with the URL you use to access your application. For example if you are using PHP's built-in development server to serve your application the value of App_URL could be http://localhost:8000. Make sure your developement server is turned on before running a test otherwise you would have a failed test. For PHP's built-in server run php artisan serve.

接下来,您需要使用用于访问应用程序的URL更新.env文件的APP_URL密钥。 例如,如果您使用PHP的内置开发服务器为您的应用程序提供服务,则App_URL的值可以为http://localhost:8000 。 在运行测试之前,请确保您的开发服务器已打开,否则测试将失败。 对于PHP的内置服务器,请运行php artisan serve

You also have the option of letting Dusk use it's own environment file, to do this create a .env.dusk.{environment} file. For example, in a local environment this file would be .env.dusk.local This is useful when you do not want to test on your current database but you want to run a test off a test database.

您还可以选择让Dusk使用自己的环境文件,以创建一个.env.dusk.{environment}文件。 例如,在local环境中,此文件将是.env.dusk.local当您不想在当前数据库上进行测试,而是要在test数据库上运行测试时,此文件很有用。

( )

To run your tests use the command:

要运行测试,请使用以下命令:

php artisan dusk

This will run the ExampleTest in tests\Browser\ExampleTest.php. You should see something like this:

This shows the test was passed and the result:
OK (1 test, 1 assertion) was displayed. If you open the
tests\Browser\ExampleTest.php file you would see the following method:

这将运行ExampleTesttests\Browser\ExampleTest.php 。 您应该会看到以下内容: OK (1 test, 1 assertion)被显示。 如果打开tests\Browser\ExampleTest.php文件,则会看到以下方法:

...public function testBasicExample()    {
//Visit the homepage and look for the text 'Laravel' $this->browse(function (Browser $browser) {
$browser->visit('/') ->assertSee('Laravel'); }); }

Here the browse method accepts a callback which is an instance of the Browser class. Inside this callback you can call different methods and assertion unique to Dusk. The visit() method as its name implies is a test that visits the URL corresponding to the arguement provided. The assertSee() method checks if the text corresponding to the arguement given is on the page.

在这里, browse方法接受一个回调也就是实例Browser类。 在此回调中,您可以调用Dusk独有的不同方法和断言。 顾名思义, visit()方法是一种测试,它访问与提供的论据相对应的URL。 assertSee()方法检查页面上是否存在与给定论据相对应的文本。

So in the above example the hompage is visited and a check is made for the text 'Laravel'. There are several assertions available, see the for more information.

因此,在上面的示例中,访问了网页,并检查了文本“ Laravel”。 有几种可用的断言,有关更多信息,请参见 。

What happens if we replace the 'Laravel' text in the arguement of the assertSee() method with a text that is not on the visited page? Obviously you would get a failed test. You will see something like this:

如果我们用不在访问页面上的文本替换assertSee()方法中的“ Laravel”文本,该怎么办? 显然,您将获得失败的测试。 您将看到如下内容:

Here the test has failed because Dusk could not find the text 'Bla' in the homepage to which I have changed the arguement in the assertSee() method. Also take a look at your tests\Browser\screenshots directory you should see a screenshot of the just failed test. You should see this:

这里的测试失败了,因为Dusk在主页中找不到文本“ Bla”,而我已通过assertSee()方法更改了论点。 还要查看您的tests\Browser\screenshots目录,您应该会看到刚刚失败的测试的屏幕截图。 您应该看到以下内容:

( )

With Dusk you can also interact with forms and test authentication. To demostrate this let's setup Laravel's basic login and registration forms. First create a database and update your .env file with the database information. Then run:

使用Dusk,您还可以与表单进行交互并测试身份验证。 为了说明这一点,让我们设置Laravel的基本登录和注册表格。 首先创建一个数据库,并使用数据库信息更新您的.env文件。 然后运行:

php artisan make:auth

This command creates scaffolding and migration files for authentication. Note that If you are running MariaDB or MYSQL version lower than 5.7.7 you need to edit the app\Providers\AppServiceProvider.php file setting the default string length in the boot method:

此命令创建用于验证的脚手架和迁移文件。 请注意,如果运行的MariaDB或MYSQL版本低于5.7.7,则需要编辑app\Providers\AppServiceProvider.php文件,并在引导方法中设置默认字符串长度:

use Illuminate\Support\Facades\Schema;public function boot(){
Schema::defaultStringLength(191);}

Next run:

下一轮:

php artisan migrate

This command generates our database tables from the migration files. If you go to the homepage of your project you should see the following with the Login and Register links at the far right corner:

此命令从迁移文件生成我们的数据库表。 如果转到项目的主页,则应该在右下角看到带有“登录”和“注册”链接的以下内容:

Next create a new test named RegisterTest test by running

接下来,通过运行以下命令创建一个名为RegisterTest test的新测试

php artisan dusk:make RegisterTest

Now in your tests\Browserfolder you will see the file RegisterTest.phpwhich look like this:

现在,在您的tests\Browser文件夹中,您将看到文件RegisterTest.php ,如下所示:

browse(function ($browser) {
$browser->visit('/') ->assertSee('Laravel'); }); }}

Edit the testExample() method to look like this

编辑testExample()方法看起来像这样

public function testExample()    {
$this->browse(function ($browser) {
$browser->visit('/') //Go to the homepage ->clickLink('Register') //Click the Register link ->assertSee('Register') //Make sure the phrase in the arguement is on the page //Fill the form with these values ->value('#name', 'Joe') ->value('#email', 'joe@example.com') ->value('#password', '123456') ->value('#password-confirm', '123456') ->click('button[type="submit"]') //Click the submit button on the page ->assertPathIs('/home') //Make sure you are in the home page //Make sure you see the phrase in the arguement ->assertSee("You are logged in!"); }); }

You may also choose to delete the ExampleTest.php file as it is no longer required. Now run

您也可以选择删除ExampleTest.php文件,因为它不再需要。 现在运行

php artisan dusk

You should see this:

您应该看到以下内容:

( )

Laravel Dusk makes it a lot easier and efficient to write tests. To recap we have covered the basic setup of Dusk, introduced how to write tests and wrote an authentication test. These by no means exhausts all it's features, there is still a lot more to write about. In a later article we would delve into more of its features. If you have any questions or comments do not hesitate to post them below.

Laravel Dusk使编写测试变得更加容易和有效。 回顾一下,我们介绍了Dusk的基本设置,介绍了如何编写测试和编写身份验证测试。 这些绝不耗尽其所有功能,还有很多要写的。 在下一篇文章中,我们将深入研究其功能。 如果您有任何问题或意见,请随时在下面发布。

翻译自:

laravel/dusk

转载地址:http://xguwd.baihongyu.com/

你可能感兴趣的文章
算法(转)
查看>>
IT职场人生系列之十五:语言与技术II
查看>>
如何在FreePBX ISO 中文版本安装讯时网关,潮流16FXS 网关和潮流话机
查看>>
基于Wolfpack开发业务监控系统
查看>>
通过Jexus 部署 dotnetcore版本MusicStore 示例程序
查看>>
程序员最常见的谎话和我自己的理解
查看>>
mine 数据
查看>>
poj2728 Desert King
查看>>
三个和尚的故事 与 项目机构管理
查看>>
Excel 日期转换
查看>>
js中的NaN、Infinity、null和undefined
查看>>
Runtime
查看>>
struts2 if标签示例
查看>>
Animate CSS
查看>>
.NET安全审核检查表
查看>>
application.properties数据库敏感信息加密这么简单?
查看>>
Language Codes: ISO 639, Microsoft and Macintosh
查看>>
centos6 x64安装elasticsearch5.5.2启动报错
查看>>
公司拷贝回家的工程用sts导入clean package报错java.lang.NoClassDefFoundError
查看>>
Aborting a running program
查看>>