Invoking Cypress test with GITHub Actions

 

Setting up a CI/CD approach is vital for teams to get fast responses on the execution of their test. GITHub actions is a CI/CD platform which teams can use to execute the automation code when a new build is being generated.

With GITHub Actions, developers can execute the automation scripts when a new pull request is being created or code is pushed to the repo or even executed manually. Further with GitHub actions it provides  its own runner to perform the CI/CD flow. The actions are created in a .yaml file and are placed within the .github/workflows directory.

The below example shows how we can create a GITHub action that we can use to execute the automation scripts manually.

on:This directive tells to run the test either on either a [push] a [pull-request] or even manually


workflow_dispatch: This will allows us to trigger the action manually, and provide a button to invoke the test. The button would have a label "Cypress-Test".



runs-on: This directive spawns a new ubuntu instance each time the cypress test are to be invoked.

steps: This section contains (groups to gather) each of the steps the GitHub action is to invoke.

name: This allows us to put a label to each section that the GitHub action will execute, this helps to clearly identify which section of the actions is being executed at a given time.

actions/checkout@v2 : This action checks out you repository


actions/setup-node@v3 : This helps to setup the node version for the build under the ubuntu-latest.

if: always(): This directive tells the GitHub action to invoke the next steps even if the previous steps have failed.

working-directory: With this directive we can give the folder to look into for the available Cypress test.

spec: This directive we can point to the test script file to be invoked.

env: This directive contains all the keys that are needed to access the cypress test to be invoked.


Example script


name: Cypress Tests 

on:
workflow_dispatch:
inputs:
appName:
description: "App Name"
required: false
default: "Cypress-Test"
jobs:
cypress-run:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- uses: actions/setup-node@v3
with:
node-version: 16
- name: Run the Cypress test
if: always()
uses: cypress-io/github-action@v4
with:
working-directory: TEST-SCRIPTS
spec: cypress/e2e/TEST-SCRIPTS/TEST-Script.cy.js
build: npm run cy:run
browser: chrome
config-file: cypress.config.js
env:
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}



x
x

Comments