在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):laracasts/cypress开源软件地址(OpenSource Url):https://github.com/laracasts/cypress开源编程语言(OpenSource Language):PHP 65.0%开源软件介绍(OpenSource Introduction):Laravel + Cypress IntegrationThis package provides the necessary boilerplate to quickly begin testing your Laravel applications using Cypress. Video TourIf you'd prefer a more visual review of this package, please watch this video on Laracasts. Table of ContentsInstallationIf you haven't already installed Cypress; that's your first step. npm install cypress --save-dev Now you're ready to install this package through Composer. Pull it in as a development-only dependency. composer require laracasts/cypress --dev Finally, run the php artisan cypress:boilerplate That's it! You're ready to go. We've provided an
In the Cypress window that opens, Choose "E2E Testing," and then "Start E2E Testing in Chrome." This will bring up a list of all specs in your application. Of course, at this point, we only have the single example spec. Click Cypress ConfigurationWe've declared some initial settings in your project's Environment HandlingAfter running the Likely, you'll want to use a special database to ensure that your Cypress acceptance tests are isolated from your development database.
When running your Cypress tests, this package, by default, will automatically back up your primary
However, when your Cypress tests fail, it can often be useful to manually browse your application in the exact state that triggered the test failure. You can't do this if your environment is automatically reverted after each test run. To solve this, you have two choices: Option 1:Temporarily disable the Cypress task that resets the environment. Visit after(() => {
// cy.task("activateLocalEnvFile", {}, { log: false });
}); That should do it! Just remember to manually revert to your local .env file when you're done performing your Cypress tests. Option 2:When booting a server with
^ This command instructs Laravel to boot up a server and use the configuration that is declared in Now visit
And you're all set! I'd recommend creating an npm script to simplify this process. Open
Now from the command line, you can run If you choose this second option, visit APIThis package will add a variety of commands to your Cypress workflow to make for a more familiar Laravel testing environment. We allow for this by exposing a handful of Cypress-specific endpoints in your application. Don't worry: these endpoints will never be accessible in production. cy.login()Find an existing user matching the optional attributes provided and set it as the authenticated user for the test. If not found, it'll create a new user and log it in. test('authenticated users can see the dashboard', () => {
cy.login({ username: 'JohnDoe' });
cy.visit('/dashboard').contains('Welcome Back, JohnDoe!');
}); Should you need to also eager load relationships on the user model or specifiy a certain model factory state before it's returned from the server, instead pass an object to test('authenticated users can see the dashboard', () => {
cy.login({
attributes: { username: 'JohnDoe' },
state: ['guest'],
load: ['profile']
});
cy.visit('/dashboard').contains('Welcome Back, JohnDoe!');
}); If written in PHP, this object would effectively translate to: $user = User::factory()->guest()->create([ 'username' => 'JohnDoe' ])->load('profile');
auth()->login($user); cy.currentUser()Fetch the currently authenticated user from the server, if any. Equivalent to Laravel's test('assert the current user has email', () => {
cy.login({ email: '[email protected]' });
cy.currentUser().its('email').should('eq', '[email protected]');
// or...
cy.currentUser().then(user => {
expect(user.email).to.eql('[email protected]');
});
}); cy.logout()Log out the currently authenticated user. Equivalent to Laravel's test('once a user logs out they cannot see the dashboard', () => {
cy.login({ username: 'JohnDoe' });
cy.logout();
cy.visit('/dashboard').assertRedirect('/login');
}); cy.create()Use Laravel factories to create and persist a new Eloquent record. test('it shows blog posts', () => {
cy.create('App\\Post', { title: 'My First Post' });
cy.visit('/posts').contains('My First Post');
}); Note that the App\Post::factory()->create(['title' => 'My First Post']); You may optionally specify the number of records you require as the second argument. This will then return a collection of posts. test('it shows blog posts', () => {
cy.create('App\\Post', 3, { title: 'My First Post' });
}); Lastly, you can alternatively pass an object to test('it shows blog posts', () => {
cy.create({
model: 'App\\Post',
attributes: { title: 'My First Post' },
state: ['archived'],
load: ['author'],
count: 10
})
}); If written in PHP, this object would effectively translate to: $user = \App\Post::factory(10)->archived()->create([ 'title' => 'My First Post' ])->load('author');
auth()->login($user); cy.refreshRoutes()Before your Cypress test suite run, this package will automatically fetch a collection of all named routes for your Laravel app and store them in memory. You shouldn't need to manually call this method, however, it's available to you if your routing will change as side effect of a particular test. test('it refreshes the list of Laravel named routes in memory', () => {
cy.refreshRoutes();
}); cy.refreshDatabase()Trigger a beforeEach(() => {
cy.refreshDatabase();
});
test('it does something', () => {
// php artisan migrate:fresh has been
// called at this point.
}); cy.seed()Run all database seeders, or a single class, in the current Cypress environment. test('it seeds the db', () => {
cy.seed('PlansTableSeeder');
}); Assuming that php artisan db:seed --class=PlansTableSeeder --env=acceptance cy.artisan()Trigger any Artisan command under the current environment for the Cypress test. Remember to precede options with two dashes, as usual. test('it can create posts through the command line', () => {
cy.artisan('post:make', {
'--title': 'My First Post',
});
cy.visit('/posts').contains('My First Post');
}); This call is equivalent to: php artisan post:make --title="My First Post" cy.php()While not exactly in the spirit of acceptance testing, this command will allow you to trigger and evaluate arbitrary PHP. test('it can evaluate PHP', () => {
cy.php(`
App\\Plan::first();
`).then(plan => {
expect(plan.name).to.equal('Monthly');
});
}); Be thoughtful when you reach for this command, but it might prove useful in instances where it's vital that you verify the state of the application or database in response to a certain action. It could also be used
for setting up the "world" for your test. That said, a targeted database seeder - using RoutingEach time your test suite runs, this package will fetch all named routes for your Laravel application,
and store them in memory. You'll additionally find a This package overrides the base test('it loads the about page using a named route', () => {
cy.visit({
route: 'about'
});
}); If the named route requires a wildcard, you may include it using the test('it loads the team dashboard page using a named route', () => {
cy.visit({
route: 'team.dashboard',
parameters: { team: 1 }
});
}); Should you need to access the full list of routes for your application, use the // Get an array of all routes for your app.
Cypress.Laravel.routes; // ['home' => []] Further, if you need to translate a named route to its associated URL, instead use the Cypress.Laravel.route('about'); // /about-page
Cypress.Laravel.route('team.dashboard', { team: 1 }); // /teams/1/dashboard SecurityIf you discover any security related issues, please email [email protected] instead of using the issue tracker. CreditsLicenseThe MIT License (MIT). Please see License File for more information. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论