Test Overview
This test opens a webpage, finds a button by its data-cy attribute, clicks it, and verifies that a confirmation message appears.
This test opens a webpage, finds a button by its data-cy attribute, clicks it, and verifies that a confirmation message appears.
describe('Button click test', () => { it('Clicks the submit button and checks confirmation', () => { cy.visit('https://example.com/form') cy.get('[data-cy="submit-button"]').click() cy.get('[data-cy="confirmation-message"]').should('be.visible').and('contain', 'Submitted successfully') }) })
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | Test runner initialized, no browser opened yet | — | PASS |
| 2 | Browser opens and navigates to 'https://example.com/form' | Browser displays the form page with a submit button | — | PASS |
| 3 | Find element with selector '[data-cy=submit-button]' | Submit button is located on the page | Element exists and is visible | PASS |
| 4 | Click the submit button using cy.click() | Button is clicked, form submission triggered | — | PASS |
| 5 | Find element with selector '[data-cy=confirmation-message]' | Confirmation message element is present after click | Element is visible and contains text 'Submitted successfully' | PASS |
| 6 | Test ends | Test completed successfully with confirmation message shown | — | PASS |
cy.click() command do in Cypress testing?cy.click()cy.click() command is used to simulate a user clicking on an element in the browser during a test.cy.type(), selecting elements uses cy.get(), and waiting uses cy.wait(). So, cy.click() specifically simulates clicks.cy.click() = Simulate click [OK]cy.click() with typing or selecting elementscy.click() waits or pauses the testcy.click() without selecting an element firstsubmitBtn using Cypress?cy.get() with the CSS selector, then call .click() on it.cy.click() with a selector, which is invalid. cy.find('#submitBtn').click() uses cy.find(), which is not a Cypress command. cy.click().get('#submitBtn') calls cy.click() before selecting, which is incorrect.cy.get(), then .click() [OK]cy.click() directly with a selectorcy.find()click() before selecting the elementcy.get('.btn-primary').click()btn-primary on the page.cy.get() behavior with multiple matchescy.get() selects all matching elements, but .click() clicks only the first matched element by default.btn-primary -> Option Acy.get() + .click() clicks first match [OK]cy.get('.submit-btn').click()Timed out retrying: Expected to find element: .submit-btn, but never found it..submit-btn. This means the selector is incorrect or the element is missing.click() correctly, so B is wrong. click() works on buttons, so C is wrong. If the page was not loaded, the error would be different or you would use cy.visit() and wait commands, so A is less likely..submit-btn does not match any element on the page -> Option Aclick() is missing when it is presentclick() can't be used on buttonsthen to check if the checkbox is unchecked, then clicks it. cy.get('#agree').click({ force: true }) forces a click regardless of state. cy.get('#agree').check() uses check(), which is a better way to check a checkbox but not a click. cy.get('#agree').click() clicks unconditionally.click({ force: true }) unnecessarilyclick() with check()Pass the test and this pattern moves unknown