cy.click() in Cypress - Build an Automation Script
describe('Form Submit Button Click Test', () => { it('should navigate to confirmation page after clicking Submit', () => { cy.visit('/form'); cy.get('[data-cy=submit-btn]') .should('be.visible') .click(); cy.url().should('include', '/confirmation'); }); });
This test visits the form page first using cy.visit(). Then it locates the Submit button using the data-cy attribute selector, which is a best practice for stable selectors. Before clicking, it asserts the button is visible to ensure the element is ready for interaction. The click() command simulates the user clicking the button. Finally, it asserts the URL includes '/confirmation' to confirm navigation happened as expected. Cypress automatically waits for elements and page loads, so no explicit waits are needed.
Now add data-driven testing to click Submit button on three different form pages with URLs '/form1', '/form2', '/form3' and verify navigation each time
Practice
cy.click() command do in Cypress testing?Solution
Step 1: Understand the purpose of
Thecy.click()cy.click()command is used to simulate a user clicking on an element in the browser during a test.Step 2: Differentiate from other commands
Typing text usescy.type(), selecting elements usescy.get(), and waiting usescy.wait(). So,cy.click()specifically simulates clicks.Final Answer:
Simulates a user clicking on a selected element -> Option CQuick Check:
cy.click()= Simulate click [OK]
- Confusing
cy.click()with typing or selecting elements - Thinking
cy.click()waits or pauses the test - Using
cy.click()without selecting an element first
submitBtn using Cypress?Solution
Step 1: Select the element before clicking
In Cypress, you must first select the element usingcy.get()with the CSS selector, then call.click()on it.Step 2: Check each option's syntax
cy.click('#submitBtn') tries to callcy.click()with a selector, which is invalid. cy.find('#submitBtn').click() usescy.find(), which is not a Cypress command. cy.click().get('#submitBtn') callscy.click()before selecting, which is incorrect.Final Answer:
cy.get('#submitBtn').click() -> Option DQuick Check:
Select withcy.get(), then.click()[OK]
- Calling
cy.click()directly with a selector - Using non-existent commands like
cy.find() - Calling
click()before selecting the element
cy.get('.btn-primary').click()Assuming there are multiple elements with class
btn-primary on the page.Solution
Step 1: Understand
cy.get()behavior with multiple matchescy.get()selects all matching elements, but.click()clicks only the first matched element by default.Step 2: Confirm no error is thrown
Cypress does not throw an error when multiple elements match; it acts on the first element unless specified otherwise.Final Answer:
Clicks the first element with classbtn-primary-> Option AQuick Check:
cy.get()+.click()clicks first match [OK]
- Assuming all matched elements are clicked
- Expecting an error when multiple elements match
- Thinking click chooses a random element
cy.get('.submit-btn').click()But the test fails with error:
Timed out retrying: Expected to find element: .submit-btn, but never found it.What is the most likely cause?
Solution
Step 1: Analyze the error message
The error says Cypress could not find any element matching.submit-btn. This means the selector is incorrect or the element is missing.Step 2: Check other options
You did callclick()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 usecy.visit()and wait commands, so A is less likely.Final Answer:
The selector.submit-btndoes not match any element on the page -> Option AQuick Check:
Selector not found = test fails [OK]
- Assuming
click()is missing when it is present - Thinking
click()can't be used on buttons - Ignoring selector correctness or page load state
Solution
Step 1: Understand the requirement
You want to click the checkbox only if it is not checked yet, to avoid toggling it off accidentally.Step 2: Analyze each option
cy.get('#agree').then($el => { if (!$el.is(':checked')) { cy.wrap($el).click() } }) usesthento 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() usescheck(), which is a better way to check a checkbox but not a click. cy.get('#agree').click() clicks unconditionally.Step 3: Choose the best option
cy.get('#agree').then($el => { if (!$el.is(':checked')) { cy.wrap($el).click() } }) correctly checks the state and clicks only if unchecked, matching the requirement.Final Answer:
cy.get('#agree').then($el => { if (!$el.is(':checked')) { cy.wrap($el).click() } }) -> Option BQuick Check:
Check state before click to avoid toggling off [OK]
- Clicking checkbox without checking state
- Using
click({ force: true })unnecessarily - Confusing
click()withcheck()
Ready to prove it?
Pass the test and this pattern moves unknown
