
Continuous Integration and Continuous Deployment (CI/CD) pipelines streamline software development by automating code integration, testing, and deployment. Incorporating end-to-end (E2E) testing ensures that code changes align with real user workflows before deployment. GitHub Actions and Cypress offer a seamless solution for automating testing, reducing manual effort, and accelerating feedback cycles.
This article explains automating CI/CD pipelines for Cypress testing with GitHub Actions. You will create a sample Next.js application and configure Cypress to automate a CI/CD pipeline with GitHub actions.
Before you begin, you need to:
cypress-dev-test.Next.js is a React framework that streamlines the development of production-ready web applications with features such as server-side rendering, static site generation, and API routes. Follow the steps below to set up a sample Next.js project to use in your CI/CD pipeline for Cypress testing.
Update the APT package index.
Install Node.js and NPM.
Verify that the installed Node.js version is 18.x or later.
Your output should be similar to the one below.
Visit How to Install Node.js and NPM on Ubuntu 24.04 if the installed Node.js version is not 18.x or later that's required for Cypress testing.
Switch to your user's home directory.
Initialize a Next.js project using npx. Replace my-project with your desired project name.
Press Enter when prompted to install the Next.js package.
Press Enter to keep the default option selected and use the recommended packages in your project.
Output:
Switch to the my-project directory.
List all files and verify the project directory structure.
Output:
Allow connections to the Next.js application port 3000 through the firewall.
Run the following command to install UFW if it's unavailable and allow SSH connections.
Reload UFW to apply the firewall configuration changes.
Start the development server.
Your output should be similar to the one below.
Access the Next.js application port 3000 using your server's public IP address in a web browser such as Chrome and verify that the default Next.js page displays.
Press Ctrl + C in your terminal session to stop the development server.
Follow the steps below to simulate user interactions, allowing end-to-end (E2E) testing to detect usability issues using a contact form with first name, last name, email, and message fields. The contact form logs the output to the console to validate the user input.
Print your working directory and verify it's the Next.js project.
Your output should be similar to the one below.
Back up the default app/page.tsx file.
Create the app/page.tsx file using a text editor such as nano.
Add the following contact form components to the file.
Save and close the file.
The above contact form configuration creates multiple input fields to validate the user input and simulate user interactions.
Start the development server as a background process to keep the application active on port 3000.
Access the application port 3000 using your server's IP address in a web browser window and verify that the contact form displays.
Follow the steps below to install and configure Cypress on your workstation.
Install all required dependencies for Cypress.
Install Cypress as a development dependency.
Run the following command to generate a new Cypress configuration and directory structure.
Verify that the Cypress launchpad opens on your desktop workstation and follow the steps below to modify the default settings.
Click Continue within the Cypress release information prompt to continue.
Select E2E Testing within the Cypress launchpad.
Verify the Cypress configuration files in your project and click Continue.
Select your desired web browser for E2E testing and click Start E2E Testing.
Click Create new spec to generate a new test file.
Replace the default spec.cy.ts name with your desired filename, such as contact.cy.ts.
Verify that a Great! The spec was successfully added prompt displays, and click Okay, run the spec to complete the setup.
Switch to your terminal session.
List all files in your my_project directory and verify the generated Cypress configurations.
Output:
Open the cypress.config.ts file.
Add the following baseUrl configuration after the e2e block to specify the default host for all tests on your workstation.
Save and close the file.
Your modified cypress.config.ts should look like the one below.
The above configuration specifies the host URL to use in Cypress tests. Specifying a custom URL limits the hardcoding of new URLs in individual test specs.
Well-structured tests follow three phases, setup, action, and assertion, to define the initial state, execute user actions, and verify the expected outcomes, respectively. Follow the steps below to run an example test that loads the contact page, fills out sample information, and submits the form. Then, tests the expected data in the console log.
Back up the original contact.cy.ts file within the cypress/e2e directory.
Create the contact.cy.ts file.
Add the following test code to the file.
Save and close the file.
Run the following command to perform a new cypress test.
Verify the test results in your output similar to the one below:
If you receive a Cypress failed to verify that your server is running error, run the npm run dev command to start the development server and start the Cypress test again.
Cypress automatically stores screenshots that offer a visual record of all failed tests for debugging purposes. You can reupload the test results as artifacts in GitHub Actions for centralized troubleshooting. Follow the steps below to enable Cypress to store screenshots for all failed tests.
Open the cypress.config.ts file.
Add the following configurations below the baseUrl directive.
Save and close the file.
The above configuration enables Cypress to capture and store screenshots in the specified cypress/screenshots directory for all failed tests.
Your modified cypress.config.ts should look like the one below.
Back up the cypress/e2e/contact.cy.ts file.
Open the contact.cy.ts file to simulate a failed test.
Find the cy.get('input[name="email"]').type('john_doe@example.com'); field and change the email address to john@example.com.
Save and close the file.
The above configuration simulates a failed test with a wrong email address provided to the contact form application. Cypress captures a screenshot of the failed test when it completes.
Run a new Cypress test and verify that the failed test is detected, a screenshot is stored in your project directory.
Verify that the test fails with the following output.
Open a new file explorer window.
Navigate to cypress/screenshots/contact.cy.ts/ in your my_project directory and open the 'Contact Page Tests -- Submits the form successfully (failed).png' image to verify the failed Cypress test contents.
GitHub workflows consist of jobs and steps that automate tasks in CI/CD pipelines. A job defines a set of instructions that execute in an isolated environment, while a step represents an individual task within a job, such as running a command. Follow the steps below to create a new workflow using GitHub actions to automate and validate your application's lifecycle.
Create a new .github/workflows directory.
Create a new cypress.yml file in the .github/workflows directory.
Add the following configuration to the cypress.yml file.
The above configuration automates Cypress end-to-end testing by triggering workflows on pushes to the main branch. It uses the ubuntu-latest development environment and performs the following steps:
actions/checkout@v4 to retrieve your repository information.actions/setup-node@v4.npm install and runs npm run build to build the Next.js application.cypress-io/github-action@v6 and runs npm start to start the application.actions/upload-artifact@v4 to store all failed test screenshots.Initialize Git in your project directory.
Stage all files.
Commit the changes with a custom message such as Cypress E2E test workflow.
Add your target GitHub repository to push the changes. Replace https://github.com/example-user/cypress-dev-test with your actual repository URL.
Push your code changes to the main branch.
GitHub offers built-in logging tools for monitoring Cypress test workflows. Logs display each Cypress test step, allowing you to review all test results and identify possible issues in your application.
Navigate to the Actions tab in your GitHub repository.
Select the workflow run.
Verify the Cypress test results within the test summary section.
View all test screenshots within the Artifacts section.
Click test within the Jobs section on the left navigation bar to view all executed steps for the job.
Verify the job's log details, including:
Slack notifications allow you to receive real-time alerts when Cypress tests fail in GitHub Actions. Follow the steps below to integrate Slack notifications to enable real-time prompt issue detection for faster debugging and resolution in your project.
Install the Incoming Webhooks app in your Slack workspace
Configure the webhook for your desired channel (for example, #ci-cd-alerts).
Select your target channel or create a new channel to post all incoming alerts.
Copy the generated Webhook URL.
Access your GitHub repository.
Navigate to Settings > Secrets and Variables > Actions.
Click New Repository Secret.
Enter SLACK_WEBHOOK_URL in the Name field to identify it.
Paste the webhook URL you copied from Slack in the Secret field.
Switch to your terminal session.
Open the cypress.yml workflow file.
Add the following configuration at the end of the file.
Save and close the file.
The above configuration uses the SLACK_WEBHOOK_URL secret you set earlier to send Slack notifications to your target channel in case of any failures in your workflow.
Verify that all failed workflows trigger a Slack notification with a link to the GitHub Actions log.
You have created a CI/CD pipeline using GitHub Actions and automated Cypress end-to-end testing using a sample Next.js project. You automated test execution on push actions to the GitHub main branch, enabled screenshot capturing to debug failed tests, and integrated Slack notifications to receive real-time alerts for all failed tests in your workflow.
Automating your pipeline ensures consistent application quality and accelerated feedback cycles during development. To further streamline deployments and extend your workflow using a Kubernetes cluster for automated application deployment, visit the Implement a CI/CD Pipeline with GitHub Actions and Vultr Kubernetes Engine guide.
0 Comments
Be the first to comment and share your perspective with the community.