Cypress CLI lets you run your tests from the command line. This helps you test your app automatically without opening the browser UI.
Cypress CLI execution
npx cypress run [options]
npx cypress run runs all tests in headless mode by default.
You can add options like --browser chrome or --spec 'cypress/e2e/test.cy.js' to customize the run.
npx cypress run
npx cypress run --browser chrome
login.cy.js.npx cypress run --spec 'cypress/e2e/login.cy.js'npx cypress run --headed --browser firefox
This command runs the sample_test.cy.js test file using Chrome browser in headless mode. You will see test results in the terminal.
npx cypress run --spec 'cypress/e2e/sample_test.cy.js' --browser chromeUse npx to run Cypress without installing it globally.
Headless mode means tests run without opening the browser window.
You can save test videos and screenshots automatically with Cypress CLI.
Cypress CLI runs tests from the command line without the UI.
You can choose browser, test files, and headless or headed mode with options.
Test results show in the terminal, useful for automation and continuous integration.
Practice
cypress run from the command line do?Solution
Step 1: Understand the command
This command runs Cypress tests from the command line without opening the UI.cypress runStep 2: Identify the mode of execution
By default,cypress runruns tests in headless mode, meaning no browser window appears.Final Answer:
Runs all tests in headless mode without opening the Cypress UI -> Option AQuick Check:
cypress run= headless test run [OK]
- Confusing 'cypress run' with 'cypress open'
- Thinking it opens the UI
- Assuming it starts a server
Solution
Step 1: Identify the correct command to run tests in CLI
cypress runis used to run tests from CLI, notcypress openorcypress start.Step 2: Check the correct option for specifying browser
The option to specify browser is--browser, socypress run --browser chromeis correct syntax.Final Answer:
cypress run --browser chrome -> Option AQuick Check:
Use--browserwithcypress run[OK]
- Using 'cypress open' instead of 'cypress run' for CLI execution
- Using wrong option '-b' instead of '--browser'
- Using non-existent command 'cypress start'
cypress run --spec "cypress/e2e/login.cy.js"?Solution
Step 1: Understand the
The--specoption--specoption tells Cypress to run only the specified test file(s).Step 2: Analyze the command behavior
The command runs tests only incypress/e2e/login.cy.jsand outputs results in the terminal becausecypress runis headless.Final Answer:
Runs only the tests inside login.cy.js and shows results in terminal -> Option BQuick Check:
--specruns specified test file only [OK]
- Thinking it runs all tests except specified file
- Expecting Cypress UI to open with 'run' command
- Assuming --spec is invalid option
cypress run --browser chrome --headed but tests still run headless. What is the likely cause?Solution
Step 1: Understand the
The--headedflag--headedflag runs tests with the browser UI visible, not headless.Step 2: Identify why tests run headless despite
The likely cause is that the--headed--headedflag was misspelled (e.g.,--headless) or missing from the actual command executed.cypress rundefaults to headless mode without the correct flag.Final Answer:
The--headedflag is misspelled or missing -> Option DQuick Check:
Missing or misspelled--headeddefaults to headless [OK]
--headed flag spelling and presence when headed mode fails [OK]- Confusing
--headedwith--headless - Assuming Cypress doesn't support headed Chrome
- Ignoring browser installation status
results.json. Which command achieves this?Solution
Step 1: Choose correct command and browser
Usecypress runto run tests headlessly and specify--browser firefox.Step 2: Specify headless mode and JSON reporter output
Use--headlessfor no UI,--reporter jsonto format output, and--reporter-options output=results.jsonto save results.Final Answer:
cypress run --browser firefox --headless --reporter json --reporter-options output=results.json -> Option CQuick Check:
Use reporter options to save JSON results [OK]
- Using 'cypress open' instead of 'cypress run'
- Missing --reporter or reporter-options flags
- Confusing --headed with --headless
Ready to prove it?
Pass the test and this pattern moves unknown
