We use value and attribute assertions to check if a web element has the right value or attribute. This helps us make sure the page works as expected.
Value and attribute assertions in Cypress
cy.get('selector').should('have.value', 'expectedValue') cy.get('selector').should('have.attr', 'attributeName', 'expectedAttributeValue')
have.value checks the value property of form elements like inputs.
have.attr checks any attribute on the element, like href, src, disabled.
cy.get('#username').should('have.value', 'john_doe')
cy.get('button').should('have.attr', 'disabled')
cy.get('img.logo').should('have.attr', 'src', '/images/logo.png')
This test suite visits a sample page. It types an email and checks the input value. It checks if a button is disabled. It also verifies a link's href attribute.
describe('Value and Attribute Assertions Example', () => { beforeEach(() => { cy.visit('https://example.cypress.io/commands/actions') }) it('checks input value after typing', () => { cy.get('.action-email') .type('test@example.com') .should('have.value', 'test@example.com') }) it('checks button disabled attribute', () => { cy.get('.action-disabled') .should('have.attr', 'disabled') }) it('checks link href attribute', () => { cy.get('.home-link') .should('have.attr', 'href', '/') }) })
Use have.value only for form elements like input, textarea, select.
Use have.attr to check any attribute, including boolean attributes like disabled.
Assertions wait automatically in Cypress, so no need for extra waits.
Value assertions check the content of form fields.
Attribute assertions check any attribute on an element.
These assertions help confirm the page behaves as expected.
Practice
cy.get('input').should('have.value', 'hello') check?Solution
Step 1: Understand the command context
The command targets an input element and usesshould('have.value', 'hello')which asserts the value property of the input.Step 2: Interpret the assertion meaning
This assertion checks the actual content inside the input field, expecting it to be exactly 'hello'.Final Answer:
It checks that the input field contains the text 'hello' as its value. -> Option CQuick Check:
Value assertion = check input content [OK]
- Confusing 'have.value' with checking attribute existence
- Assuming it checks visibility or enabled state
- Mixing up value with text content
disabled?Solution
Step 1: Identify attribute assertion syntax
The correct way to check for an attribute presence is usingshould('have.attr', 'attributeName').Step 2: Match the attribute name
Here, the attribute is 'disabled', so the command isshould('have.attr', 'disabled').Final Answer:
cy.get('button').should('have.attr', 'disabled') -> Option AQuick Check:
Attribute assertion uses 'have.attr' [OK]
- Using 'have.value' instead of 'have.attr' for attributes
- Using 'contain' which checks text content, not attributes
- Confusing 'be.disabled' which checks element state, not attribute
cy.get('input#email')
.should('have.attr', 'placeholder', 'Enter your email')
.and('have.value', '')What will this test verify?
Solution
Step 1: Analyze the attribute assertion
should('have.attr', 'placeholder', 'Enter your email')checks the placeholder attribute equals 'Enter your email'.Step 2: Analyze the value assertion
and('have.value', '')asserts the input's current value is empty (no text typed).Final Answer:
The input has placeholder text 'Enter your email' and is empty. -> Option AQuick Check:
Attribute + value assertions combined = The input has placeholder text 'Enter your email' and is empty. [OK]
- Mixing placeholder attribute with input value
- Assuming value is placeholder text
- Ignoring the empty string value assertion
cy.get('input[name="username"]').should('have.value', 'admin')But it fails even though the input shows 'admin' on the page. What is the likely problem?
Solution
Step 1: Understand difference between value and placeholder
If the input visually shows 'admin' buthave.valuefails, likely 'admin' is placeholder text, not actual value.Step 2: Confirm attribute vs value difference
Placeholder text appears when value is empty. The test checks value, so it fails if value is empty.Final Answer:
The input's value attribute is empty; text is shown by placeholder. -> Option DQuick Check:
Value assertion fails if text is placeholder [OK]
- Confusing placeholder text with input value
- Assuming selector is wrong without checking
- Using 'have.attr' incorrectly for value check
data-test with value agree. Which Cypress assertion chain is correct?Solution
Step 1: Check checkbox state assertion
To verify a checkbox is checked, useshould('be.checked').Step 2: Check attribute with specific value
To verify an attribute and its value, useshould('have.attr', 'data-test', 'agree').Step 3: Combine assertions correctly
Chain both assertions withand()to check both conditions on the same element.Final Answer:
cy.get('input[type="checkbox"]').should('be.checked').and('have.attr', 'data-test', 'agree') -> Option BQuick Check:
Checkbox checked + attribute value = cy.get('input[type="checkbox"]').should('be.checked').and('have.attr', 'data-test', 'agree') [OK]
- Using 'have.value' to check checkbox state
- Checking 'checked' attribute instead of state
- Not specifying attribute value in assertion
Ready to prove it?
Pass the test and this pattern moves unknown
