Consider the following Cypress test code. What will be the test execution result?
cy.get('button#submit').click();
Remember that cy.get() finds elements and click() simulates a click.
The cy.get('button#submit') finds the button with id 'submit'. The click() command clicks it. If the button exists, the test passes.
You want to check that clicking a button changes its text to 'Clicked!'. Which assertion is correct?
cy.get('button#action').click();
Check the text content after clicking the button.
Option B correctly asserts the button's text changed to 'Clicked!'. Option B checks for partial text 'Click' which is not exact. Option B wrongly checks 'value' property. Option B only clicks but does not assert.
You need to click a button that has a dynamic class name but a stable data attribute data-cy='submit-btn'. Which locator is best?
Use stable attributes for selectors to avoid flaky tests.
Option A uses the stable data attribute selector which is best practice. Option A uses a class that is dynamic and may change. Option A uses a partial class selector which is fragile. Option A uses an id that may not exist.
Given the code below, why does the click command fail?
cy.get('button#save').click(); // fails with 'element is not visible' error
Check if the element is visible and not covered by other elements.
Cypress requires elements to be visible and not covered to click. If the button is hidden or overlapped, the click fails with this error.
Consider this code snippet:
cy.get('button#multi').click().click().click();What happens when this runs?
Remember how Cypress queues commands and executes them one by one.
Cypress queues commands and runs them sequentially. Each click() waits for the previous to complete, so the button is clicked three times in order.