0
0
Cypresstesting~15 mins

describe blocks for grouping in Cypress - Build an Automation Script

Choose your learning style9 modes available
Group login page tests using describe blocks
Preconditions (2)
Step 1: Open the login page at 'https://example.com/login'
Step 2: Verify the page title contains 'Login'
Step 3: Enter 'user@example.com' in the email input field
Step 4: Enter 'Password123' in the password input field
Step 5: Click the login button
Step 6: Verify the URL changes to the dashboard page URL 'https://example.com/dashboard'
Step 7: Verify the dashboard page contains a welcome message
✅ Expected Result: All login related tests are grouped inside a describe block named 'Login Page Tests' and all assertions pass successfully
Automation Requirements - Cypress
Assertions Needed:
Page title contains 'Login'
URL changes to 'https://example.com/dashboard' after login
Dashboard page contains welcome message
Best Practices:
Use describe block to group related tests
Use it blocks for individual test cases
Use beforeEach hook to visit login page before each test
Use clear and descriptive test names
Use Cypress commands consistently
Automated Solution
Cypress
describe('Login Page Tests', () => {
  beforeEach(() => {
    cy.visit('https://example.com/login')
  })

  it('should have the correct page title', () => {
    cy.title().should('include', 'Login')
  })

  it('should login successfully and redirect to dashboard', () => {
    cy.get('input#email').type('user@example.com')
    cy.get('input#password').type('Password123')
    cy.get('button#login').click()
    cy.url().should('eq', 'https://example.com/dashboard')
    cy.contains('Welcome').should('be.visible')
  })
})

The describe block groups all tests related to the login page under the name 'Login Page Tests'. This helps organize tests clearly.

The beforeEach hook runs before each test to open the login page, so each test starts fresh.

The first it block checks the page title includes 'Login'.

The second it block performs the login steps: entering email and password, clicking login, then verifying the URL and welcome message on the dashboard.

Using cy.get with IDs ensures stable selectors. Assertions use should to verify expected outcomes.

Common Mistakes - 4 Pitfalls
Not using describe blocks to group related tests
Visiting the login page inside each it block instead of beforeEach
{'mistake': "Using vague test names like 'test1' or 'check login'", 'why_bad': 'Does not clearly describe what the test does, making debugging harder', 'correct_approach': "Use descriptive names like 'should login successfully and redirect to dashboard'"}
Using unstable selectors like XPath or complex CSS selectors
Bonus Challenge

Now add data-driven testing with 3 different sets of login credentials inside the describe block

Show Hint