Cypress Run Headless

Profile picture for user arilio666

Once you are done with installing cypress and have written a test you may wanna run tests headless meaning run on the DOM itself instead of opening the fancy UI of cypress and executing the test. This is a quick way to run tests and know the test results via the DOM.

Also command line is faster and most preferable option to using with Jenkins, Travis, or any other CI/CD tool.

For the headless run, we use the cypress run command besides the traditional cypress open where it opens the cypress UI window and lists our test to run.

Table of Content

  1. Syntax
  2. Run Specific Spec File
  3. Cypress Chrome Headless
  4. Cypress Firefox Headless
  5. Video Tutorial

1. Syntax

cypress run --- To run all the present tests.
cypress run --spec filepath/filename.spec.js

By default cypress run execute all command headless. To run our specific spec file we use --spec option by passing the path and file name as arguments.

  • --headless: Hide the browser instead of running headed (default during cypress run)
  • --headed: Displays the browser instead of running headlessly

2. Run Specific Spec File

Command

npx cypress run --spec "cypress\integration\Tests\ProgramsBuzz.spec.js"

Spec File 

describe('Automating The Signin Of ProgramsBuzz Site',()=> {
    it('visit the site ',()=>{
        cy.visit('https://www.programsbuzz.com/user/login')
        cy.log('Before Reload')
        cy.reload()
        cy.log('After Reload')
    })

   it('Test 2',()=>{
    })

   it('Test 3 ',()=>{
    })

   it('Test 4 ',()=>{
    })

   it('Test 5',()=>{
    })
})

This is the code we are gonna execute headless using npx cypress run and it is not on the npn script.

Terminal Output

  • As you can see the script has run and completed successfully in headless mode.
  • If you wanna run all the tests just simple give npx cypress run with the tests folder name relative path

3. Cypress Chrome Headless

npx cypress run --headless -b chrome --spec "cypress\integration\Tests\ProgramsBuzz.spec.js"

4. Run Firefox in headed mode 

npx cypress run --headed --browser firefox --spec "cypress\integration\Tests\ProgramsBuzz.spec.js"

You can use --browser also instead of -b. It will execute in Firefox headed mode.

Command line option will also create videos folder for you where you can watch recorded video of your test.

run cypress in headless mode

You can also check your screenshot in screenshots folder.