Verify multiple properties of a user profile card
Preconditions (2)
✅ Expected Result: The profile card is visible, displays the correct username and email, and has the correct CSS class
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.
Now add data-driven testing with 3 different user profiles to verify their profile cards
.should() and .and() in Cypress?.should() and .and() lets you check multiple things on one element without repeating the element selection..should(), then additional checks use .and()..should('be.visible') then .and('contain.text', 'Hello'). Options B and D wrongly start with .and(). cy.get('button').should('contain.text', 'Hello').should('be.visible') uses two .should() but chaining prefers .and() after first .should().cy.get('#login').should('be.visible').and('have.class', 'active').and('contain.text', 'Login')cy.get('.btn').and('be.enabled').should('have.text', 'Submit').should(). Using .and() first is invalid syntax..should() and continue with .and() for multiple assertions on the same element..should('be.visible') then chains .and() for other checks. cy.get('#email').should('have.value', '').should('be.visible').should('be.enabled').should('have.attr', 'placeholder', 'Enter email') uses multiple .should() starting with value check, which is allowed but less concise and best practice is to start with visibility. cy.get('#email').and('be.visible').and('be.enabled').and('have.attr', 'placeholder', 'Enter email').and('have.value', '') incorrectly starts with .and(). cy.get('#email').should('be.enabled').should('be.visible').should('have.attr', 'placeholder', 'Enter email').should('have.value', '') uses multiple .should() which is allowed but less concise.Pass the test and this pattern moves unknown