thetechyhub / cypress
Laravel Cypress 模板
Requires
- php: ^7.2.5
- illuminate/support: ^6.0|^7.0
Requires (Dev)
- orchestra/testbench: ^4.0
- phpunit/phpunit: ^8.0
This package is auto-updated.
Last update: 2024-09-13 00:47:12 UTC
README
此包是laracasts/cypress的克隆,以支持7.2.5或更高版本的php版本
此包提供了必要的模板,以便快速开始使用Cypress测试您的Laravel应用程序。
安装
首先,将包作为Composer的开发依赖项安装。
composer require thetechyhub/cypress --dev
如果您还没有通过npm引入Cypress,那么下一步就是安装它
npm install cypress --save-dev && npx cypress open
接下来,生成必要的Laravel Cypress模板
php artisan cypress:boilerplate
最后一步是无论您是否使用此包,您都会执行的操作。更新您的cypress.json文件,包含您应用程序的baseUrl。
{
"baseUrl": "http://my-app.test"
}
在您的Cypress测试中发出请求时,此baseUrl将被添加到您提供的任何相对URL之前。
cy.visit('/foo'); // http://my-app.test/foo
环境处理
在运行php artisan cypress:boilerplate命令后,您现在在项目根目录中将有.env.cypress文件。为了帮助您开始,此文件是.env的副本。请随时根据需要更新它,以准备您的Cypress测试。
很可能,您希望使用特殊的数据库来确保您的Cypress验收测试与本地数据库隔离。
DB_CONNECTION=mysql
DB_DATABASE=cypress
在运行Cypress测试时,此包将自动备份您的原始.env文件,并将其与env.cypress交换。当然,一旦完成,环境文件将重置为原始状态。
所有Cypress测试都根据
.env.cypress中指定的环境执行。
用法
此包将为您的Cypress工作流程添加各种命令,以便在更熟悉的Laravel测试环境中进行测试。
我们通过在您的应用程序中公开一些Cypress特定的端点来实现这一点。不用担心:这些端点永远不会在生产环境中可用。
cy.login()
创建一个新的用户记录,匹配提供的可选属性,并将其设置为测试的认证用户。
test('authenticated users can see the dashboard', () => { cy.login({ name: 'John Doe' }); cy.visit('/dashboard').contains('Welcome Back, John Doe!'); });
cy.logout()
注销当前认证用户。等同于auth()->logout()。
test('once a user logs out they cannot see the dashboard', () => { cy.login({ name: 'John Doe' }); cy.visit('/dashboard').contains('Welcome Back, John Doe!'); cy.logout(); cy.visit('/dashboard').assertRedirect('/login'); });
cy.create()
使用Laravel工厂创建并持久化新的Eloquent记录。
test('it shows blog posts', () => { cy.create('App\\Post', { title: 'My First Post' }); cy.visit('/posts').contains('My First Post'); });
注意,上面的cy.create()调用等同于
factory('App\Post')->create(['title' => 'My First Post']);
您可以指定所需的记录数作为第二个参数。如果提供了,则可以将属性作为第三个参数提供。
test('it shows blog posts', () => { cy.create('App\\Post', 3); // });
cy.refreshDatabase()
触发测试数据库的migrate:refresh。通常,您会在beforeEach调用中应用此操作,以确保在文件中的每个新测试之前,您的数据库都进行了新鲜迁移和清理。
beforeEach(() => { cy.refreshDatabase(); }); test('it does something', () => { // php artisan migrate:fresh has been // called at this point. });
cy.seed()
在当前Cypress环境中运行所有数据库种子器或单个类。
test('it seeds the db', () => { cy.seed('PlansTableSeeder'); });
假设您的.env.cypress文件中的APP_ENV设置为"acceptance",上面的调用将等同于
php artisan db:seed --class=PlansTableSeeder --env=acceptance
cy.artisan()
触发Cypress测试当前环境下的任何Artisan命令。请记住,像往常一样,使用两个短横线来指定选项。
test('it can create posts through the command line', () => { cy.artisan('post:make', { '--title': 'My First Post', }); cy.visit('/posts').contains('My First Post'); });
此调用等同于
php artisan post:make --title="My First Post"
cy.php()
虽然这不是验收测试的精神,但此命令将允许您触发并评估任意PHP。
test('it can evaluate PHP', () => { cy.php(` App\\Plan::first(); `).then(plan => { expect(plan.name).to.equal('Monthly'); }); });
在执行此命令时要三思而后行,但在某些情况下,如果需要验证应用程序或数据库的状态以响应某些操作,它可能非常有用。它也可以用来为您的测试设置“世界”。尽管如此,使用cy.seed()的针对性数据库种子器通常是更好的方法。
安全
如果您发现任何与安全相关的问题,请通过电子邮件发送至jeffrey@laracasts.com,而不是使用问题跟踪器。
致谢
许可证
MIT许可证(MIT)。更多信息请参阅许可证文件。
