0
0
Cypresstesting~15 mins

cy.url() assertions in Cypress - Build an Automation Script

Choose your learning style9 modes available
Verify URL after login
Preconditions (2)
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 navigation to complete
✅ Expected Result: The URL should include '/dashboard' after successful login
Automation Requirements - Cypress
Assertions Needed:
Assert that the URL includes '/dashboard' after login
Best Practices:
Use cy.get() with id selectors for stable element targeting
Use cy.url().should() for URL assertions
Use explicit waits by chaining commands instead of arbitrary waits
Keep tests isolated and independent
Automated Solution
Cypress
describe('Login Page URL Test', () => {
  it('should navigate to dashboard after login', () => {
    cy.visit('/login');
    cy.get('#email').type('user@example.com');
    cy.get('#password').type('Password123!');
    cy.get('#loginBtn').click();
    cy.url().should('include', '/dashboard');
  });
});

This test starts by visiting the login page using cy.visit('/login').

It then types the email and password into their respective input fields using cy.get() with id selectors for reliability.

After clicking the login button, the test waits for the page to navigate by chaining the commands.

Finally, it asserts that the URL includes /dashboard using cy.url().should('include', '/dashboard'), which confirms successful navigation.

This approach avoids arbitrary waits and uses Cypress's built-in retry and wait mechanisms for stability.

Common Mistakes - 3 Pitfalls
Using cy.url() without assertion
Using arbitrary waits like cy.wait(5000) before asserting URL
Using complex or brittle selectors instead of stable id selectors
Bonus Challenge

Now add data-driven testing with 3 different sets of valid user credentials and verify the URL after login for each.

Show Hint