Verify interaction with a button using correct element selection
Preconditions (1)
✅ Expected Result: The message 'Button clicked!' is visible after clicking the button
describe('Why element selection drives interaction', () => { beforeEach(() => { cy.visit('/sample-page') }) it('Clicks the button and verifies the message', () => { // Use data-cy attribute for reliable selection cy.get('[data-cy=click-me-button]').should('be.visible').click() // Verify the message appears cy.contains('Button clicked!').should('be.visible') }) })
The test starts by visiting the sample page before each test to ensure a fresh state.
We select the button using a data-cy attribute, which is a stable and semantic selector that won't break if the UI changes slightly.
We assert the button is visible before clicking to avoid errors if the element is missing or hidden.
After clicking, we verify the expected message appears, confirming the interaction worked as intended.
Now add data-driven testing with 3 different buttons each showing a unique message
cy.get('.menu-item').click()cy.get('#login').click()Pass the test and this pattern moves unknown