You want to verify that an input field with id #username contains the value admin. Which Cypress assertion correctly checks this?
cy.get('#username').should(/* fill in assertion here */);
Input fields store their typed content in the value property, not as text content.
The have.value assertion checks the value property of input elements. Using contain.text or have.text checks visible text, which input fields do not have. have.attr checks attributes, but the value attribute may not reflect the current input value after user typing.
How do you assert that a button with class .submit-btn has the attribute disabled set to true?
cy.get('.submit-btn').should(/* fill in assertion here */);
Attributes are strings, but some properties are booleans. The question asks about the attribute value.
The have.attr assertion with three arguments checks the attribute and its exact value. have.attr with two arguments only checks presence, not value. be.disabled checks the disabled property, not the attribute value. have.prop checks the property, which is boolean, not the attribute string.
Given this HTML:
<input type="checkbox" id="agree" checked>
What will be the result of this Cypress assertion?
cy.get('#agree').should('have.attr', 'checked', 'checked')Boolean attributes like checked may appear without a value in HTML.
The checked attribute in HTML can be present without a value. Cypress expects the exact attribute value when using three arguments. Since the attribute has no value, the assertion fails. To check if the checkbox is checked, use be.checked instead.
This test fails unexpectedly:
cy.get('#email').should('have.value', 'user@example.com');The input field shows the correct email on the page. What is the most likely reason for failure?
Remember the difference between HTML attributes and DOM properties.
The value attribute is the initial value in HTML. After user input, the DOM property value changes. Cypress's have.value checks the property, so if the attribute is set but the property is empty, the assertion fails. The selector is correct, and syntax is valid. Disabled inputs still have values.
You want to test that a link with selector a.external-link opens in a new tab by having the attribute target="_blank". Which assertion is best to verify this?
Attributes are strings set in HTML tags. Properties are DOM object values.
The target attribute controls where the link opens. Using have.attr with the exact value _blank checks this correctly. have.prop may not reflect the attribute as expected. Checking text content does not verify attributes.