0
0
Cypresstesting~15 mins

it blocks for test cases in Cypress - Build an Automation Script

Choose your learning style9 modes available
Verify login functionality with valid credentials
Preconditions (1)
Step 1: Enter 'user@example.com' in the email input field with id 'email'
Step 2: Enter 'Password123!' in the password input field with id 'password'
Step 3: Click the login button with id 'loginBtn'
Step 4: Wait for the dashboard page to load
✅ Expected Result: User is redirected to the dashboard page with URL containing '/dashboard' and a welcome message with id 'welcomeMsg' is visible
Automation Requirements - Cypress
Assertions Needed:
Verify URL contains '/dashboard' after login
Verify welcome message with id 'welcomeMsg' is visible
Best Practices:
Use 'it' blocks to define individual test cases
Use 'cy.get' with valid selectors for elements
Use assertions like 'should' for verification
Avoid hardcoded waits; use Cypress automatic waits
Keep tests isolated and independent
Automated Solution
Cypress
describe('Login Tests', () => {
  it('should login successfully with valid credentials', () => {
    cy.visit('https://example.com/login');
    cy.get('#email').type('user@example.com');
    cy.get('#password').type('Password123!');
    cy.get('#loginBtn').click();
    cy.url().should('include', '/dashboard');
    cy.get('#welcomeMsg').should('be.visible');
  });
});

The describe block groups related tests under 'Login Tests'.

The it block defines one test case: logging in with valid credentials.

cy.visit opens the login page.

cy.get selects elements by their IDs and type enters text.

click clicks the login button.

cy.url().should('include', '/dashboard') asserts the URL changed to dashboard.

cy.get('#welcomeMsg').should('be.visible') asserts the welcome message is visible.

This structure uses Cypress best practices: clear selectors, no hard waits, and isolated test case in an it block.

Common Mistakes - 3 Pitfalls
Using hardcoded waits like cy.wait(5000) after clicking login
{'mistake': "Not using 'it' blocks and putting all steps inside 'describe' only", 'why_bad': "Makes tests hard to read and maintain; 'describe' is for grouping, 'it' for test cases.", 'correct_approach': "Use 'it' blocks for each test case to keep tests clear and isolated."}
Using invalid or overly complex selectors like absolute XPath
Bonus Challenge

Now add data-driven testing with 3 different sets of valid login credentials

Show Hint