0
0
Cypresstesting~15 mins

Automatic retry mechanism in Cypress - Build an Automation Script

Choose your learning style9 modes available
Verify login with automatic retry on failure
Preconditions (2)
Step 1: Enter 'testuser' in the username input field with id 'username'
Step 2: Enter 'Test@1234' 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
Step 5: Verify the URL contains '/dashboard'
Step 6: Verify the welcome message with id 'welcomeMsg' contains text 'Welcome, testuser'
✅ Expected Result: User is successfully logged in and dashboard page is displayed with welcome message
Automation Requirements - Cypress
Assertions Needed:
URL contains '/dashboard'
Welcome message contains 'Welcome, testuser'
Best Practices:
Use Cypress automatic retry feature for assertions
Use data-test attributes or stable selectors for locating elements
Avoid hard waits; rely on Cypress built-in retries and commands
Use beforeEach hook to set up preconditions if needed
Automated Solution
Cypress
describe('Login Test with Automatic Retry', () => {
  beforeEach(() => {
    cy.visit('/login');
  });

  it('should login successfully and show dashboard with welcome message', () => {
    cy.get('#username').type('testuser');
    cy.get('#password').type('Test@1234');
    cy.get('#loginBtn').click();

    // Cypress automatically retries assertions until they pass or timeout
    cy.url().should('include', '/dashboard');
    cy.get('#welcomeMsg').should('contain.text', 'Welcome, testuser');
  });
});

This test uses Cypress to automate the login process. The beforeEach hook ensures the login page is loaded before each test.

The test types the username and password into their respective fields and clicks the login button.

After clicking, Cypress automatically retries the cy.url().should('include', '/dashboard') assertion until the URL contains '/dashboard' or the default timeout is reached.

Similarly, the welcome message assertion retries until the expected text appears.

This automatic retry mechanism helps handle delays in page loading or rendering without needing explicit waits.

Common Mistakes - 3 Pitfalls
Using fixed waits like cy.wait(5000) before assertions
Using unstable selectors like CSS classes that change frequently
Not verifying the URL or page content after login
Bonus Challenge

Now add data-driven testing with 3 different sets of valid credentials to verify login success for each user.

Show Hint