Test Overview
This test checks if a specific button exists on the page, is visible to the user, and has the correct text label. It verifies these common assertions using Cypress commands.
This test checks if a specific button exists on the page, is visible to the user, and has the correct text label. It verifies these common assertions using Cypress commands.
describe('Common assertions test', () => { it('checks button existence, visibility, and text', () => { cy.visit('https://example.cypress.io') cy.get('#query-btn') .should('exist') .and('be.visible') .and('have.text', 'Button') }) })
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | Test runner initialized, no browser opened yet | — | PASS |
| 2 | Browser opens | Browser window opens ready for navigation | — | PASS |
| 3 | Navigates to 'https://example.cypress.io' | Page loads with example Cypress site content | — | PASS |
| 4 | Finds element with selector '#query-btn' using cy.get | Button element with id 'query-btn' is present in DOM | Checks element exists in DOM | PASS |
| 5 | Checks element 'exist' assertion | Element is confirmed present | Element exists in the DOM | PASS |
| 6 | Checks element 'be.visible' assertion | Element is visible on the page | Element is visible to the user | PASS |
| 7 | Checks element 'have.text' assertion with text 'Button' | Element text content is 'Button' | Element text matches expected 'Button' | PASS |
| 8 | Test ends | All assertions passed, test completes successfully | — | PASS |
exist assertionexist assertion checks if the element is present in the DOM, even if it is hidden.be.visible checks if the element is visible, have.text checks exact text content, and contain.text checks partial text. Only exist fits the question.submitBtn is visible?should('be.visible') checks if the element is visible on the page.cy.get('#submitBtn') selects the element by id. Combining with should('be.visible') is correct syntax to check visibility.cy.get('.message').should('have.text', 'Welcome!')<div class='message'>Welcome!</div> is present and visible.have.text assertion<div class='message'>Welcome!</div> matches the text 'Welcome!' exactly, so the assertion passes.cy.get('.alert').should('be.visible').should('have.text', 'Error occurred')<div class='alert' style='display:none'>Error occurred</div> is hidden.style='display:none', so it is hidden and not visible.should('be.visible') assertion will fail because the element is hidden, causing the test to fail before checking text..save-btn exists, is visible, and has the exact text Save. Which code snippet correctly performs all these checks?should('exist'), should('be.visible'), and should('have.text', 'Save') correctly to check all conditions.Pass the test and this pattern moves unknown