Test Overview
This test opens a webpage, finds a button, clicks it, and then checks multiple things about a message that appears. It verifies the message is visible, has the correct text, and has the right CSS color.
This test opens a webpage, finds a button, clicks it, and then checks multiple things about a message that appears. It verifies the message is visible, has the correct text, and has the right CSS color.
describe('Multiple assertions chaining test', () => { it('checks message visibility, text, and color after button click', () => { cy.visit('https://example.cypress.io') cy.get('#show-message-btn').click() cy.get('#message') .should('be.visible') .and('have.text', 'Hello, Cypress!') .and('have.css', 'color', 'rgb(0, 128, 0)') }) })
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | Test runner is ready to execute the test | — | PASS |
| 2 | Browser opens and navigates to 'https://example.cypress.io' | The example Cypress page is loaded in the browser | — | PASS |
| 3 | Find element with id 'show-message-btn' using cy.get | Button with id 'show-message-btn' is found on the page | — | PASS |
| 4 | Click the button '#show-message-btn' | Button is clicked, triggering message display | — | PASS |
| 5 | Find element with id 'message' using cy.get | Message element is present in the DOM | — | PASS |
| 6 | Assert the message is visible using .should('be.visible') | Message is visible on the page | Message visibility is true | PASS |
| 7 | Assert the message text is 'Hello, Cypress!' using .and('have.text', ...) | Message text content is 'Hello, Cypress!' | Message text matches expected | PASS |
| 8 | Assert the message CSS color is 'rgb(0, 128, 0)' using .and('have.css', ...) | Message color style is green (rgb(0, 128, 0)) | Message color matches expected | PASS |
| 9 | Test ends successfully | All assertions passed, test completed | — | PASS |
.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