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.