Skip to main content

Continuous Integration in Cypress

 

 What you’ll learn

    • How to run Cypress tests in Continuous Integration
    • How to configure Cypress in various CI Providers
    • How to record tests to the Cypress Dashboard
    • How to run tests in parallel on CI
  • Basics

    Running Cypress in Continuous Integration is almost the same as running it locally in your terminal. You generally only need to do two things:

    1. Install Cypress

      npm install cypress --save-dev
      
    2. Run Cypress

      cypress run
      

    Depending on which CI provider you use, you may need a config file. You’ll want to refer to your CI provider’s documentation to know where to add the commands to install and run Cypress. For more configuration examples check out our examples.

    Boot your server

    Challenges

    Typically you will need to boot a local server prior to running Cypress. When you boot your web server, it runs as a long running process that will never exit. Because of this, you’ll need it to run in the background - else your CI provider will never move onto the next command.
    Backgrounding your server process means that your CI provider will continue to execute the next command after executing the signal to start your server.

    Many people approach this situation by running a command like the following:

    npm start & cypress run // Do not do this
    

    The problem is - what happens if your server takes time to boot? There is no guarantee that when the next command runs (cypress run) that your web server is up and available. So your Cypress test may start and try to visit your local server before it is ready to be visited.

    Solutions

    Luckily, there are some solutions for this. Instead of introducing arbitrary waits (like sleep 20) you can use a better option.
    wait-on module
    Using the wait-on module, you can block the cypress run command from executing until your server has booted.

    npm start & wait-on http://localhost:8080
    
    cypress run
    

    Most CI providers will automatically kill background processes so you don’t have to worry about cleaning up your server process once Cypress finishes.

    However, if you’re running this script locally you’ll have to do a bit more work to collect the backgrounded PID and then kill it after cypress run.

    start-server-and-test module

    If the server takes a very long time to start, we recommend trying the start-server-and-test module.

    npm install --save-dev start-server-and-test
    

    In your package.json scripts, pass the command to boot your server, the url your server is hosted on and your Cypress test command.

    {
      ...
      "scripts": {
        "start": "my-server -p 3030",
        "cy:run": "cypress run",
        "test": "start-server-and-test start http://localhost:3030 cy:run"
      }
    }
    

    In the example above, the cy:run command will only be executed when the URL http://localhost:3030 responds with an HTTP status code of 200. The server will also shut down when the tests complete.

    Gotchas

    When working with webpack-dev-server that does not respond to HEAD requests, use an explicit GET method to ping the server like this:

    {
      "scripts": {
        "test": "start-server-and-test start http-get://localhost:3030 cy:run"
      }
    }
    

    When working with local https in webpack, set an environment variable to allow local certificate:

    {
      "scripts": {
        "start": "my-server -p 3030 --https",
        "cy:run": "cypress run",
        "cy:ci": "START_SERVER_AND_TEST_INSECURE=1 start-server-and-test start https-get://localhost:3030 cy:run"
      }
    }
    

    Record tests

    Cypress can record your tests and make the results available in the Cypress Dashboard, which is a service that gives you access to recorded tests - typically when running Cypress tests from your CI provider. The Dashboard provides you insight into what happened when your tests ran.

    Recording tests allow you to:

    • See the number of failed, pending and passing tests.
    • Get the entire stack trace of failed tests.
    • View screenshots taken when tests fail and when using cy.screenshot().
    • Watch a video of your entire test run or a clip at the point of test failure.
    • See which machines ran each test when parallelized.

    To record tests:

    1. Set up your project to record
    2. Pass the --record flag to cypress run within CI.
    cypress run --record --key=abc123
    
    Read the full guide on the Dashboard Service.

    Run tests in parallel

    Cypress can run tests in parallel across multiple machines.

    You’ll want to refer to your CI provider’s documentation on how to set up multiple machines to run in your CI environment.

    Once multiple machines are available within your CI environment, you can pass the --parallel flag to have your tests run in parallel.

    cypress run --record --key=abc123 --parallel
    
    Read the full guide on parallelization.

    Examples

    Cypress should run on all CI providers. We have provided some example projects and configuration for some CI providers to help you get started.

    CI ProviderExample ProjectExample Config
    AWS Amplify Consolecypress-example-kitchensinkamplify.yml
    AppVeyorcypress-example-kitchensinkappveyor.yml
    Azure / VSTS CI / TeamFoundationcypress-example-kitchensinkazure-ci.yml
    BitBucketcypress-example-kitchensinkbitbucket-pipelines.yml
    Buildkitecypress-example-kitchensink.buildkite/pipeline.yml
    CircleCIcypress-example-kitchensink.circleci/config.yml
    CodeShip Basic (has cy.exec() issue)
    CodeShip Procypress-example-docker-codeship
    Concourse
    Dockercypress-docker-images
    GitLabcypress-example-kitchensink.gitlab-ci.yml
    Jenkinscypress-example-kitchensinkJenkinsfile
    Semaphore
    Shippablecypress-example-kitchensinkshippable.yml
    TravisCIcypress-example-kitchensink.travis.yml

    Travis

    Example .travis.yml config file

    language: node_js
    node_js:
      - 10
    addons:
      apt:
        packages:
          # Ubuntu 16+ does not install this dependency by default, so we need to install it ourselves
          - libgconf-2-4
    cache:
      # Caches $HOME/.npm when npm ci is default script command
      # Caches node_modules in all other cases
      npm: true
      directories:
        # we also need to cache folder with Cypress binary
        - ~/.cache
    install:
      - npm ci
    script:
      - $(npm bin)/cypress run --record
    

    Caching folders with npm modules saves a lot of time after the first build.

Comments

Popular posts from this blog

The terminal process "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" terminated with exit code: 259.

Integrated terminal in VS code crashes PowerShell: Solution of this Problem:- 1) Open your Project's/file location. 2)Click in this bar after opening the exact location of your project.                                                                                                                                         3)Now, Write 'cmd' in this bar.                                                                                                                     4)Your Terminal will be open as command, Now you can give any command here related to your Project. 5) Your project will be Executed/Start Normally in Terminal                                                                            6) You can ask any question.

Use of for Loop & return in Cypress Automation

  Use of  For Loop & return method in Cypress automation : describe(" Use Of For Loop and return method ",function(){   it ( 'Test Case 15' ,  function () { cy . visit ( 'www.exapmle.com/',{timeout:1000} );        cy . get ( '.tab-tools > .icon > .svg-inline--fa' ). click ({ force : true }); // Now this method will fetch the text which is already displayed & will save it.      cy . get ( '.last-updated-date > span' ). then (( $txt )  =>  {  const   txt  =  $txt . text ()      cy . log ( txt )                  // For loop will run 50 times & after each 3 seconds // it will check Visibility of element. // If it gets newly appeared element/Text It will be terminated before 50 runs var   i ;          for  ( i  =  0 ;  i  <  50 ;  i ++) {              cy . wait ( 3000 )                           cy . get ( '.last-updated-date > span' ). each (( $text )  =>  {   c

Test files sequence in Cypress Or Test file Order in Cypress

  In any case, I used the  testFiles  config in  cypress.json  to set the spec file run order:   You can use your file names. Only the files will be shown in cypress runner which you will define here "New_Api" is a Sub-folder in integration folder. All other are spec files. {      "baseUrl" : "https://exapmle.com" , "testFiles" :  [          "New_Api/*" ,          "Add_Product.spec.js" ,          "Add_Client.spec.js" ,          "Seller.spec.js" ,          "Deal_Status.spec.js" ,          "Buyer_Offer.spec.js" ,          "Buyer_2.spec.js" ,          "Deal_Status.spec.js"            ], }