Skip to content

Commit

Permalink
feat: setup Cypress for e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
boazpoolman committed Jan 20, 2025
1 parent a9f7f07 commit 7a34432
Show file tree
Hide file tree
Showing 5 changed files with 687 additions and 29 deletions.
16 changes: 16 additions & 0 deletions cypress.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const { defineConfig } = require('cypress');

module.exports = defineConfig({
e2e: {
baseUrl: 'http://localhost:1337',
specPattern: '**/*.cy.{js,ts,jsx,tsx}',
video: true,
defaultCommandTimeout: 10000,
requestTimeout: 10000,
setupNodeEvents(on, config) {
// implement node event listeners here.
// eslint-disable-next-line global-require
require('cypress-terminal-report/src/installLogsPrinter')(on);
},
},
});
70 changes: 70 additions & 0 deletions cypress/support/commands.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// <reference types="cypress" />
// ***********************************************
// This example commands.ts shows you how to
// create various custom commands and overwrite
// existing commands.
//
// For more comprehensive examples of custom
// commands please read more here:
// https://on.cypress.io/custom-commands
// ***********************************************
//
//
// -- This is a parent command --
// Cypress.Commands.add('login', (email, password) => { ... })
//
//
// -- This is a child command --
// Cypress.Commands.add('drag', { prevSubject: 'element'}, (subject, options) => { ... })
//
//
// -- This is a dual command --
// Cypress.Commands.add('dismiss', { prevSubject: 'optional'}, (subject, options) => { ... })
//
//
// -- This will overwrite an existing command --
// Cypress.Commands.overwrite('visit', (originalFn, url, options) => { ... })
//

Cypress.Commands.add('login', (path) => {
cy.visit('/');

cy.intercept({
method: 'GET',
url: '/admin/users/me',
}).as('sessionCheck');

cy.intercept({
method: 'GET',
url: '/admin/init',
}).as('adminInit');

// Wait for the initial request to complete.
cy.wait('@adminInit').its('response.statusCode').should('equal', 200);

// Wait for the form to render.
cy.wait(1000);

cy.get('body').then(($body) => {
// Login
if ($body.text().includes('Log in to your Strapi account')) {
cy.get('input[name="email"]').type('[email protected]');
cy.get('input[name="password"]').type('Abc12345678');
cy.get('button[type="submit"]').click();
cy.wait('@sessionCheck').its('response.statusCode').should('equal', 200);
}
// Register
if ($body.text().includes('Credentials are only used to authenticate in Strapi')) {
cy.get('input[name="firstname"]').type('John');
cy.get('input[name="email"]').type('[email protected]');
cy.get('input[name="password"]').type('Abc12345678');
cy.get('input[name="confirmPassword"]').type('Abc12345678');
cy.get('button[type="submit"]').click();
cy.wait('@sessionCheck').its('response.statusCode').should('equal', 200);
}
});
});

Cypress.Commands.add('navigateToAdminPage', (path) => {
cy.get('a[href="/admin/plugins/boilerplate"]').click();
});
22 changes: 22 additions & 0 deletions cypress/support/e2e.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// ***********************************************************
// This example support/e2e.ts is processed and
// loaded automatically before your test files.
//
// This is a great place to put global configuration and
// behavior that modifies Cypress.
//
// You can change the location of this file or turn off
// automatically serving support files with the
// 'supportFile' configuration option.
//
// You can read more here:
// https://on.cypress.io/configuration
// ***********************************************************

// Import commands.js using ES2015 syntax:
import './commands';

require('cypress-terminal-report/src/installLogsCollector')();

// Alternatively you can use CommonJS syntax:
// require('./commands')
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"test:ts:front": "tsc -p admin/tsconfig.json",
"test:ts:back": "tsc -p server/tsconfig.json",
"test:jest": "ENV_PATH=./playground/.env jest --verbose --runInBand --forceExit",
"test:cypress": "cypress open",
"playground:install": "yarn playground:yalc-add-link && cd playground && yarn install",
"playground:yalc-add": "cd playground && yalc add strapi-plugin-boilerplate",
"playground:yalc-add-link": "cd playground && yalc add --link strapi-plugin-boilerplate",
Expand All @@ -48,6 +49,8 @@
"@strapi/typescript-utils": "^5.0.0",
"@types/react": "^19.0.0",
"@types/react-dom": "^19.0.0",
"cypress": "^13.9.0",
"cypress-terminal-report": "^6.0.2",
"jest": "^29.7.0",
"prettier": "^3.4.2",
"react": "^19.0.0",
Expand Down
Loading

0 comments on commit 7a34432

Please sign in to comment.