Headless mode in Cypress - Build an Automation Script
describe('Login Test in Headless Mode', () => { it('should log in successfully and show welcome message', () => { cy.visit('https://example.com/login'); cy.get('#username').type('testuser'); cy.get('#password').type('Test@1234'); cy.get('#loginBtn').click(); cy.url().should('include', '/dashboard'); cy.get('#welcomeMessage').should('contain.text', 'Welcome, testuser!'); }); }); // To run this test in headless mode, use the command: // npx cypress run --headless --spec 'cypress/e2e/login_spec.cy.js'
This Cypress test script automates the login process as described in the manual test case.
First, it visits the login page URL. Then it types the username and password into the input fields identified by their id attributes, which is a best practice for stable selectors.
After clicking the login button, it waits automatically for the page to load and verifies the URL contains /dashboard to confirm navigation.
Finally, it checks that the welcome message contains the expected text, confirming successful login.
The test is designed to run in headless mode by using the Cypress CLI command with the --headless flag, which runs the browser without opening a visible window, useful for automated pipelines.
Now add data-driven testing with 3 different sets of valid username and password combinations.
Practice
headless mode?Solution
Step 1: Understand headless mode purpose
Headless mode runs tests without opening the browser window, making execution faster.Step 2: Compare options with headless mode features
Options B and C describe interactive or visible tests, which are not headless. Tests only run on mobile devices is unrelated.Final Answer:
Tests run faster without opening the browser window -> Option AQuick Check:
Headless mode = faster tests without UI [OK]
- Thinking headless mode shows browser UI
- Confusing headless with manual testing
- Assuming headless only runs on specific devices
Solution
Step 1: Recall Cypress commands for running tests
The correct command to run tests in headless mode isnpx cypress run --headless.Step 2: Eliminate incorrect commands
openlaunches the interactive GUI,startandtestare not valid Cypress commands.Final Answer:
npx cypress run --headless -> Option AQuick Check:
Run + --headless = correct command [OK]
- Using 'open' instead of 'run' for headless
- Typing invalid commands like 'start' or 'test'
- Omitting the --headless flag
npx cypress run --headless in your terminal?Solution
Step 1: Understand the effect of --headless flag
The --headless flag runs tests without opening the browser window, so tests run in the background.Step 2: Match options with expected behavior
Cypress opens the browser window and runs tests interactively and D describe interactive modes, which do not happen with --headless. Tests fail because --headless is not a valid option is incorrect because --headless is valid.Final Answer:
Tests run in the background without showing the browser UI -> Option BQuick Check:
--headless = background test run [OK]
- Expecting browser window to open
- Thinking --headless is invalid
- Confusing headless with interactive mode
npx cypress run --headless but tests did not start. Which is the most likely cause?Solution
Step 1: Check the command used
Usingcypress open --headlessis invalid becauseopendoes not support --headless.Step 2: Evaluate other options
Installing Cypress globally is not required withnpx. Missing test files cause errors but not silent failure. Headless mode does not require GUI.Final Answer:
You usedcypress open --headlessinstead ofrun-> Option CQuick Check:
Use 'run' command with --headless [OK]
- Using 'open' command with --headless
- Assuming global install is mandatory
- Thinking headless needs GUI
Solution
Step 1: Understand CI pipeline needs
CI pipelines require automated, non-interactive test runs without UI.Step 2: Choose the command that fits automation
npx cypress run --headlessruns tests automatically without opening browser UI, ideal for CI.Step 3: Eliminate other options
Runnpx cypress openand manually start tests requires manual start, C limits tests to local GUI, D shows browser UI which slows CI.Final Answer:
Runnpx cypress run --headlessin the pipeline script -> Option DQuick Check:
CI automation = run with --headless [OK]
- Running tests manually in CI
- Expecting GUI in CI environment
- Omitting --headless in automated runs
Ready to prove it?
Pass the test and this pattern moves unknown
