Verify visibility and text of a welcome message on homepage
Preconditions (1)
✅ Expected Result: The element with id 'welcome-msg' exists, is visible, and displays the text 'Welcome to Our Site!'
describe('Homepage welcome message test', () => { it('should verify the welcome message exists, is visible, and has correct text', () => { cy.visit('http://localhost:3000/') cy.get('#welcome-msg') .should('exist') .and('be.visible') .and('have.text', 'Welcome to Our Site!') }) })
This test opens the homepage URL using cy.visit(). Then it finds the element with id welcome-msg using cy.get(). The test chains three assertions: exist to confirm the element is in the page's HTML, be.visible to confirm the user can see it, and have.text to check the exact text content. Chaining assertions improves readability and efficiency. Cypress automatically waits for elements to appear, so no manual waits are needed.
Now add data-driven testing with 3 different welcome messages for different user roles
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