Skip to main content

Cypress Run Headless

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.

Syntax: Cypress Headless

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

Example 1: 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

Example 2: Cypress Headless Chrome

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

Example 3: 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.

Tags
Submitted by arilio666 on December 10, 2021

Ashwin is working with iVagus as a Technical Lead. He has experience in various technologies like Cypress, JavaScript, TypeScript, Rest Assured, Selenium, Cucumber, and Java.

About

At ProgramsBuzz, you can learn, share and grow with millions of techie around the world from different domain like Data Science, Software Development, QA and Digital Marketing. You can ask doubt and get the answer for your queries from our experts.