0
0
Cypresstesting~15 mins

Multiple assertions chaining in Cypress - Build an Automation Script

Choose your learning style9 modes available
Verify multiple properties of a user profile card
Preconditions (2)
Step 1: Locate the user profile card element
Step 2: Check that the profile card is visible
Step 3: Check that the profile card contains the username 'testuser'
Step 4: Check that the profile card contains the email 'testuser@example.com'
Step 5: Check that the profile card has a CSS class 'profile-card'
✅ Expected Result: The profile card is visible, displays the correct username and email, and has the correct CSS class
Automation Requirements - Cypress
Assertions Needed:
Visibility of profile card
Text content includes username
Text content includes email
CSS class presence
Best Practices:
Use cy.get() with a stable selector
Chain multiple assertions on the same element
Avoid flaky selectors like XPath
Use descriptive assertion messages
Automated Solution
Cypress
describe('User Profile Card Tests', () => {
  beforeEach(() => {
    cy.login('testuser', 'password123'); // Assume custom command for login
    cy.visit('/profile');
  });

  it('should verify multiple properties of the profile card', () => {
    cy.get('.profile-card')
      .should('be.visible')
      .and('contain.text', 'testuser')
      .and('contain.text', 'testuser@example.com')
      .and('have.class', 'profile-card');
  });
});

The test starts by logging in the user and visiting the profile page in beforeEach to ensure a clean state for each test.

We locate the profile card using a stable CSS class selector .profile-card.

We chain multiple assertions on the same element using .should() and .and() to check visibility, text content for username and email, and the presence of the CSS class.

This chaining keeps the code clean and efficient, verifying all required properties in one go.

Common Mistakes - 3 Pitfalls
Using multiple separate cy.get() calls for each assertion
Using flaky selectors like XPath or overly complex selectors
Not checking element visibility before other assertions
Bonus Challenge

Now add data-driven testing with 3 different user profiles to verify their profile cards

Show Hint