Use Custom Scripts in package.json for Cypress Commands

Profile picture for user arilio666

Every cypress project folder has a package.json file here we can enter our customized command in the scripts field. The goal of this JSON file is to make the complicated command easy with an alias command fixated to it.

For example, take a look at my package.json scripts section

  "scripts": {
    "cy:open": "cypress open",
    "cy:run": "cypress run",
   "cy:spec": "cypress run --spec"
  }
  • Here I have included the cypress open and run command which is the official command to execute aliased it to cy:open and cy:run.
  • This is easy for me and comfortable. For you, it can be anything you guys can customize too.

NOTE: To invoke and run this customized command from package.json call it with:

npm run cy:open
npm run cy:run

If they are not on npm script it is vital to mention npx if you are to run commands locally.

Ok then enough talking let's run a specific spec file using the run command in this article:

 If you wanna run a specific spec file via npm script you can add the following line into your package.json file:

 "cy:spec": "cypress run --spec"

and you can execute by typing the following command in terminal 

npm run cy:spec  "cypress\integration\Tests\ProgramsBuzz.spec.js"

Example

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')
    })
})

This is the example code we are gonna use to test our package.json command 'cypress run --spec' we gave.

So here we have used the custom headless mode command assigned in our package.json cy:spec.

In this way there are countless commands which we can set it in package.json file as our own custom call command to be invoked in our cypress.