How To Run Cypress Tests In Jenkins Pipeline [Jenkins and Cypress Tutorial]

Enrique

Posted On: September 15, 2022

view count54836 Views

Read time13 Min Read

Cypress is one of the fast-growing test automation frameworks. As you learn Cypress, you will probably come across the need to integrate your Cypress tests with your CI environment. Jenkins is an open-source Continuous Integration (CI) server that automates your web applications’ build and deploys process. By running your Cypress test suite in Jenkins, you also automate testing as part of the build process.

Adding LambdaTest to the formula, you can run your apps across different OS and browser combinations. In this Jenkins and Cypress tutorial, we will dive deep into integrating the Jenkins pipeline with LambdaTest’s Cypress Cloud.

So, let’s get started.

Advantages of Jenkins

Jenkins is a widely popular Continuous Integration (CI) tool. It is free-to-use, open-source software written in Java. Jenkins allows boosting your release agility by providing Continuous Integration services. You can trigger these services by a web application server or simply using a command line to hasten the deployment pipeline & release migration activities. In addition, Jenkins’s popularity offers thousands of plugins to fast-track your productivity.

Detailed down are some of the significant advantages of using Jenkins:

  • Jenkins is open-source and free to use. It is a preferred CI tool by early-stage startups and large corporations since it has been under development for a long time.
  • It is a rich plugin ecosystem. At the time of this blog post, there were close to 1500+ plugins available for use.
  • Jenkins can be integrated with popular cloud platforms such as Amazon EC2, Google Cloud, VMWare vSphere, Digital Ocean, and more.
  • Anyone can write the tests to detect the errors in their code as soon as possible. So the developers don’t waste time on large-scale error-ridden integrations.
  • Issues are detected and resolved almost immediately, which keeps the software in a state where it can be safely released at any time.
  • Most of the integration work is automated. Hence fewer integration issues. It saves both time and money over the lifespan of a project.

If you are new to Jenkins and wish to learn more about it, you can go through this Jenkins tutorial that covers what is Jenkins and how & why to use it. If you are preparing for an interview you can learn more through Jenkins Interview Questions.

In the next section of this Jenkins and Cypress tutorial, we will look into why Cypress Jenkins is the right option for your team.

Why Cypress and Jenkins?

Cypress is an open-source, full-featured, and easy-to-use testing framework for end to end testing. Currently, Cypress is preferred by front-end developers and automation testers who know JavaScript. We can install Cypress using NPM. With Cypress visual regression testing, you can verify the visual component of a web application. Also, we don’t need to install any additional libraries, dependencies, drivers, servers, etc., as we have to do in the case of Selenium.

Why Cypress and Jenkins

As mentioned above, Jenkins is the predicted option of many startups and large companies trying to accomplish Continuous Integration. It offers a significant plugin ecosystem plus easy configuration with Jenkins; if you are looking for an open-source CI tool that is easy to configure with Cypress, then Jenkins is the right option for your team.

Cypress Jenkins

Cypress CTA.

In the next section of this tutorial Jenkins and Cypress tutorial , we will learn how to configure Jenkins to run Cypress tests.

Configuring Jenkins to run Cypress tests

We are going to need NodeJS to execute Cypress. In this section of this Jenkins and Cypress tutorial, we will learn how to install NodeJS in our Jenkins environment. However, before this, you must install Cypress. You can go through this blog on getting started with Cypress to install Cypress on the local machine.

  1. In Jenkins, go to Manage Jenkins > Manage Plugins > Available Plugins and search for NodeJS Plugin and install it.
  2. NodeJS Plugin

  3. After you successfully install the NodeJs Plugin, we need to add NodeJs to it. Go to Manage Jenkins > Global Tool Configuration and click Add NodeJS. Then,
    • Provide a name for your NodeJS installation Eg: Node14.
    • Hit the Install automatically.
    • Choose a cypress-compatible NodeJS version.
    • Add Cypress for Global npm packages to install.
    • We can keep the other configurations as it is.

    install the NodeJs Plugin,

Now we need to create your Cypress Jenkins project. First, we must keep things simple; let’s use a freestyle project; as a freestyle, we can break down our build job into several smaller build steps, making it more comfortable to manage builds in separate stages. E.g., in one step, a build might run a suite of functional tests and then tag the build in a second step if all available tests are successful.

A Jenkins freestyle project can be as powerful and complicated as any build job built with a Jenkins pipeline or a Groovy DSL. The drawback to freestyle projects is that the user must know how to script all these actions, and developers must learn how to manage these scripts.

  1. In Jenkins, click New Item, provide a project name, and choose Freestyle project.
  2. New Item, provide a project name

  3. As we have our Git project in Source Code Management, add your Git URL and credentials for this Jenkins and Cypress tutorial. If you do not use Git, I encourage you to try it. Git is lightning fast; we’re talking about only a few seconds per command; it quickly adds to your work day. Remember that a large community often is an advantage because an ecosystem revolves around the system.
  4. Now we need to switch to the build environment section. Here we will find and select the option, Provide Node & npm bin/ folder to PATH.
  5. switch to the build environment section

    It is where we set our NodeJS environment to run Cypress.

    set our NodeJS environment to run Cypress

  6. If you remember, we added a NodeJS installation before. We will see the name we provided in the NodeJS Installation dropdown. Choose it, and then we can keep the other configurations.

We are all set; don’t forget to add our Cypress command to execute the script.

Configuring the Jenkins Pipeline

A pipeline is a series of events or tasks interconnected in a particular order. In plain English, a Jenkins pipeline is a set of modules or plugins which enable the implementation and integration of Continuous Delivery pipelines within Jenkins.

The Jenkins pipeline has an expandable automation system for building basic or complex “template” distribution pipelines via the Domain-specific language (DSL) used in the Pipeline. For example, stages in the Declarative Pipeline may have a stages section containing a list of nested stages to be run in sequential order.

The concepts in Jenkins pipelines are as follows:

  • Pipeline – It is code executed for the build, test, and delivery of the applications and is a set of instructions in the state of code for continuous delivery.
  • Node – It is a machine or system on which Jenkins runs. A node block is a pipeline syntax scripted.
  • Stage – Here, we can picture the process of Jenkins pipelines. It consists of a series of steps: build, test, and deploy. In earlier versions of Declarative Pipeline, the only way to run chunks of Pipeline code in parallel was to use the parallel step inside the steps block for a stage).
  • Step – It is a single step in the stage; for example, either build or test or deploy from a single step.

The Jenkins pipeline’s main advantages are automating many tasks and making the CI/CD pipeline tasks reliable, efficient, repeatable, and high in quality.

We can find the Jenkins file for the Pipeline as follows for this Jenkins and Cypress tutorial:

  1. Create a new project for Pipeline by going to Jenkins –> New Item. Jenkins -> Select the Pipeline.
  2. Create a new project for Pipeline

  3. Scroll down to Pipeline Options. And update according to your Git repo.
  4. Scroll down to Pipeline Options

  5. Our Jenkins file will contain our configuration, and then we can see our results.
  6. Jenkins file will contain our configuration

So far, we have seen how to configure the Jenkins pipeline. In the next section of this Jenkins and Cypress tutorial, we will deep-dive into integrating your Cypress Jenkins pipeline with the Cypress cloud grid.

How to integrate your Cypress Jenkins Pipeline with Cypress cloud grid?

Jenkins Pipeline, also called “Pipeline,” presents a suite of plugins to assist in integrating our continuous delivery pipeline into Jenkins. Jenkins Pipeline does so with the help of Pipeline DSL (Domain Specific Language) syntax that facilitates easy modeling of even the most complex delivery pipeline.

Configuring your Jenkins Pipeline with LambdaTest

Follow these steps to configure LambdaTest with Jenkins:

  1. Jenkins 2.X or the latest version.
  2. A Jenkins User with root access.
  3. LambdaTest Authentication Credentials.
  4. Keep an eye on your LambdaTest authentication credentials, i.e., your LambdaTest username and access key. You need to set them up as your environment variables. You can retrieve them from your LambdaTest Automation Dashboard by clicking on the key icon around the help button.

Setting up Jenkins

In this section of this Jenkins and Cypress tutorial, we will look into the steps to set up Jenkins.

GitHub

Github Button

  1. Create a new project for Pipeline by going to Jenkins –> New Item. Jenkins -> Select the Pipeline.
  2. new project for Pipeline

  3. Scroll down to Pipeline Options. And update according to your Git repo.
  4. Pipeline Options

  5. Our Jenkinsfile will contain our configuration:
  6. our configuration

  7. The “cypress:lambda” task will be executed by the “e2e Tests” step in the Jenkins pipeline. So now we can press the “Build Now” button in Jenkins and get the following result:
  8. cypress:lambda

  9. When checking the console output of the build, we can verify that the Cypress tests ran.
  10. Cypress tests

As we can notice from the image above, our build was successfully executed, and the tests were uploaded to our LambdaTest Dashboard.

LambdaTest Dashboard

In the following section of this Jenkins and Cypress tutorial, we will learn how to perform parallel testing on Cypress cloud.

How to perform Cypress parallel testing on a cloud grid?

We can use a Cypress cloud grid like LambdaTest, which provides automated cross browser testing on 40+ browsers and operating systems, and Cypress parallel testing to expedite the test execution and help perform our Jenkin pipelines. In addition, it will help improve our overall test coverage by resulting in better product quality, as we can cover different combinations using the same test scripts.

You can also Subscribe to the LambdaTest YouTube Channel and stay updated with the latest tutorials around automated browser testing, Cypress testing, CI/CD, and more.

To get started with Cypress e2e testing, follow the below-mentioned steps:

  1. Install LambdaTest Cypress CLI on your machine. Trigger the following command to install the same:
  2. After installation is completed, set up the configuration using the below command:
  3. Once the command is completed, lambdatest-config.json is created in the project folder. Next, enter the LambdaTest credentials from the LambdaTest Profile Section.
  4. Here is how you can configure the required browser & OS combinations in lambdatest-config.json:
  5. The run_settings section in the JSON file contains the desired Cypress test suite capabilities, including Cypress_version, build_name, number of parallel sessions, etc.
  6. Tunnel_settings in the JSON file lets you connect your local system with LambdaTest servers via an SSH-based integration tunnel. Once this tunnel is established, you can test locally hosted pages on all the browsers currently supported by Cypress on LambdaTest.

Using LambdaTest, we can run our tests in parallel and reduce execution time. Instead of running tests one by one in sequence, parallel testing allows for running multiple tests simultaneously. In addition, this approach executes tests on various browsers, devices, and operating systems parallelly.

Without Cypress Cloud Grid (Compare e2e Tests timing):

Without Cypress Cloud Grid

Using Cypress Cloud Grid and running multiple OS and Browsers (Compare e2e Tests timing):

Using Cypress Cloud Grid

To view test performance metrics, navigate to the LambdaTest Analytics Dashboard. The Test Overview will provide a snapshot of tests consistent with stable behavior. You can see all test results, their status, and the overall number of tests passed or failed in both failures and failures’ breakdowns areas.

If you’re a developer or a tester, Cypress 101 certification will prepare you to write tests faster and with greater confidence. You’ll learn the fundamentals of Cypress end to end testing and walk away with a clear understanding of how to write better tests with this Jenkins and Cypress Tutorial.

Jenkins and Cypress Tutorial: Final Thoughts

Lastly, in this Jenkins and Cypress Tutorial we have covered Jenkins’s importance and how it can be integrated with our Cypress projects to run our tests efficiently and achieve the goal of continuous integration. Using Jenkins to run our tests is time effective, and the results can be visualized with complete logs. It helps fulfill the entire software development life cycle, from development, deployment, testing, monitoring, and releasing.

Jenkins supports various plugins for achieving the project requirements. It also keeps alerting the user via emails regarding the build’s stability. Thanks for reading through this Jenkins and Cypress tutorial.

Happy Bug Hunting!

Frequently Asked Questions (FAQs)

Does Cypress work with Jenkins?

Yes. The best way to set up Cypress with Jenkins is by installing and configuring the official cypress-jenkins plugin. It allows you to run Cypress tests from Jenkins whenever a new build is created or on every pull request.

Is Cypress good for E2E testing?

Cypress is a popular end-to-end testing framework that allows for the creation of full test suites in JavaScript. It supports test runners like Karma, Protractor, and Jest, and it can be used to test any web application that can run in a browser, including Node applications.

Author Profile Author Profile Author Profile

Author’s Profile

Enrique

Enrique DeCoss, Senior Quality Assurance Manager at FICO is an industry leader in quality strategy with 16+ of experience implementing automation tools. Enrique has a strong background in Testing Tools, API testing strategies, performance testing, Linux OS and testing techniques. Enrique loves to share his in-depth knowledge in competencies including, Selenium, JavaScript, Python, ML testing tools, cloud computing, agile methodologies, and people Management.

Blogs: 11



linkedintwitter

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free