Run Cypress Tests Using Jenkins Pipeline

Run Cypress Tests Using Jenkins Pipeline

In this blog post, we will create a Jenkins pipeline to run Cypress scenario tests for a web application. Cypress is a popular JavaScript end-to-end testing framework that allows you to write and run tests for web applications. By automating the testing process, you can ensure the stability and reliability of your application.

Pipeline Setup

Let's start by setting up the pipeline in Jenkins. We will use the declarative pipeline syntax to define the stages and steps of our pipeline. Here is the pipeline script:

pipeline {

????agent {

????????docker {?

????????????image 'node:14'

????????????args '-u root'

????????}

????}

????parameters {

????????choice(name: 'TestType', choices: ['smoke', 'e2e'], description: 'Select Type Of Test')

????????choice(name: 'Version', choices: ['1.5.11', '1.7.6.5', '1.7.6.6', '1.7.7'], description: 'Select Application Version')

????????string(name: 'baseUrl', defaultValue: 'https://172.168.9.139/login', description: 'Validate Application URL')

????????string(name: 'wait', defaultValue: '5000', description: 'Enter Additional Wait')

????}

????stages {

????????stage('Clone the repo') {

????????????steps {

????????????????git branch: 'master', url: '[email protected]:buildbot/testing.git'

????????????}

????????}

s
tage('Install requirements') {?

???????????steps {?

???????????????sh """?

???????????????apt-get -y update &&?

???????????????apt-get install -y python3 make gcc g++ curl postgresql-contrib libnotify-dev xauth xvfb?

???????????????apt-get install -y libgtk2.0-0 libgtk-3-0 libgbm-dev libgconf-2-4 libnss3 libxss1 libasound2 libxtst6?

???????????????apt-get install -y wget?

???????????????wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd




64.deb?

???????????????apt-get install -y ./google-chrome-stable_current_amd64.deb?

???????????????"""?

???????????}?

???????}?

???????stage('Run the Cypress scenario tests') {?

???????????steps {?

???????????????sh """?

???????????????cd Cypress &&?

???????????????npm install &&?

???????????????export baseUrl=${params.baseUrl} &&?

???????????????export wait=${params.wait} &&?

???????????????export testtype=${params.TestType} &&?

???????????????export version=${params.Version} &&?

???????????????export NODE_TLS_REJECT_UNAUTHORIZED=0 &&?

???????????????./node_modules/.bin/cypress install --force &&?

???????????????./node_modules/.bin/cypress run --spec 'cypress/integration/connected-pharma/**/*.spec.js' --config baseUrl="$baseUrl" --env wait="$wait",testtype="$testtype",version="$version" --reporter mochawesome --reporter-options reportDir="cypress/results",overwrite=false,html=false,json=true --headless --browser chrome?

???????????????"""?

???????????}?

???????}?

???}?

???post {?

???????always {?

???????????sh 'npx mochawesome-merge "Cypress/cypress/results/*.json" > Cypress/mochawesome.json'?

???????????sh 'npx mochawesome-report-generator Cypress/mochawesome.json'?

???????????publishHTML([allowMissing: false, alwaysLinkToLastBuild: false, keepAll: false, reportDir: 'mochawesome-report', reportFiles: 'mochawesome.html', reportName: 'HTML Report', reportTitles: ''])?

???????????cleanWs()?

???????}?

???}
}        

Agent Configuration

The pipeline is configured to run in a Docker container with the `node:14` image. This image provides a Node.js environment, which is required to install and run Cypress.

Parameters

The pipeline includes several parameters that allow you to customize the test execution. Here are the available parameters:

- `TestType`: Select the type of test (smoke or e2e).

- `Version`: Select the application version.

- `baseUrl`: Enter the URL of the application to validate.

- `wait`: Enter an additional wait time.

These parameters allow you to configure different aspects of the test execution, such as the test type, application version, device configuration, and more.

Stages

The pipeline consists of three stages:

1. Clone the repo: This stage clones the repository from Bitbucket. You can specify the branch to checkout if needed.

2. Install requirements: In this stage, we install the necessary dependencies and tools required for running Cypress tests. This includes installing Python, Chrome browser, and other dependencies.

3. Run the Cypress scenario tests: This stage executes the Cypress tests. It first navigates to the `Cypress` directory, installs the project dependencies using `npm install`, and then runs the Cypress tests using the `cypress run` command. The test execution is customized based on the provided parameters.

Post-build Actions

In the post-build actions, we perform the following tasks:

- Merge the generated JSON test reports using `mochawesome-merge` and store the merged report as `Cypress/mochawesome.json`.

- Generate the HTML report using `mochawesome-report-generator`.

- Publish the HTML report using the Jenkins `publishHTML` plugin.

- Clean the workspace to remove any leftover files.

These post-build actions provide visibility into the test results by generating an HTML report and publishing it as part of the Jenkins build.

Conclusion

In this blog post, we created a Jenkins pipeline to run Cypress scenario tests for a web application. The pipeline allows you to configure various parameters, such as the test type, application version, and baseURL. The pipeline also generates a detailed HTML report of the test results, providing visibility into the test execution. By automating the testing process with Cypress and Jenkins, you can ensure the quality and reliability of your web application.


要查看或添加评论,请登录

Buildbot Technologies Private Limited的更多文章

社区洞察

其他会员也浏览了