Cypress integration with Github Actions
To run cypress tests on Github Actions, you need to create one workflow by using YAML configuration file in your github repository. Here are the steps to create a workflow that runs your cypress tests.
Prerequisite — Before starting this, you need to have cypress test suites running successfully. Please refer this article for reference— https://puneetvashisth.medium.com/cucumber-integration-with-cypress-9465db0fde83
Step 1) You need to create a workflow file in your repository under the .github/workflows/ directory. Create folder in the root of the project .github and create one more folder name “workflow” inside that. Create a file say manual.yml (any name you can select) inside workflow folder.
OR you can directly go to github repository and select Actions tab. Click on Workflow inside Automation section. This will automatically create the folders and workflow file.
Click on Configure and commit the file like-
This will create your workflow file. Now take the latest pull and you will see the workflow file in the .github/workflows folder.
Step 2) Now open the manual.yml file in the editor and replace the code with below code-
name: Run Cypress Tests
on:
push:
branches:
- main
pull_request:
branches:
- main
jobs:
cypress-run:
runs-on: ubuntu-latest
steps:
- name: Checkout the code
uses: actions/checkout@v3
- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: '18' # or the version of Node.js your project requires
- name: Install dependencies
run: npm ci # or 'npm install' if you are not using lock files
- name: Start the application (Optional if your app needs to be served)
run: npm start &
env:
CI: true
- name: Run Cypress Tests
uses: cypress-io/github-action@v5 # Use the latest version of the Cypress GitHub Action
with:
record: false # Set this to 'true' if you are recording on the Cypress Dashboard
env:
CI: true
Explanation —
on: it defines the event that triggers the workflow. Here it will trigger whenever a code is pushed or created PR to the main branch.
runs-on: It specifies the environment for the job which is ubuntu-latest here.
Checkout the code- This step checks out your repository so that GitHub Actions has access to your code.
Set up Node.js- It uses the correct version of Node.js which is mentioned in your package.json.
Install dependencies- run: npm ci this whill install dependencies mentioned in package-lock.json. You can use npm install also if not using lock file.
Start the application(optional)- If your application needs to be running to test, then this step is required.
Run Cypress tests- This step runs the cypress tests on official github actions.
After committing this workflow file and pushing the code, your repository will automatically run the cypress tests on the Github Actions like -
By clicking on the above link you can see your tests running like -
You can check each steps and see the individual actions. You can expand “Run cypress tests” row and see your test cases running successfully.
Hope you enjoyed this article and please share any feedback which requires improvement in the article. I would be very happy to hear the improvements. Happy Learning!! :)