Bird
Raised Fist0
Cypresstesting~20 mins

Value and attribute assertions in Cypress - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Value and Attribute Assertion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ assertion
intermediate
2:00remaining
Check input value with correct assertion

You want to verify that an input field with id #username contains the value admin. Which Cypress assertion correctly checks this?

Cypress
cy.get('#username').should(/* fill in assertion here */);
A"have.value", "admin"
B"contain.text", "admin"
C"have.attr", "value", "admin"
D"have.text", "admin"
Attempts:
2 left
💡 Hint

Input fields store their typed content in the value property, not as text content.

❓ assertion
intermediate
2:00remaining
Verify element has specific attribute with value

How do you assert that a button with class .submit-btn has the attribute disabled set to true?

Cypress
cy.get('.submit-btn').should(/* fill in assertion here */);
A"have.attr", "disabled", "true"
B"have.attr", "disabled"
C"be.disabled"
D"have.prop", "disabled", true
Attempts:
2 left
💡 Hint

Attributes are strings, but some properties are booleans. The question asks about the attribute value.

❓ Predict Output
advanced
2:00remaining
Output of attribute assertion on checkbox

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')
APass - the checkbox is checked, so assertion passes
BPass - the checkbox has attribute 'checked' with value 'checked'
CFail - attribute 'checked' has no value, so assertion fails
DError - 'have.attr' does not accept three arguments
Attempts:
2 left
💡 Hint

Boolean attributes like checked may appear without a value in HTML.

🔧 Debug
advanced
2:00remaining
Debug failing value assertion on input

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?

AThe input field is disabled, so value cannot be read
BThe selector '#email' is incorrect and selects no element
CThe assertion syntax is wrong; 'have.value' should be 'contain.value'
DThe input's value attribute is set, but the property is empty because user typed after page load
Attempts:
2 left
💡 Hint

Remember the difference between HTML attributes and DOM properties.

🧠 Conceptual
expert
3:00remaining
Best assertion to check if a link opens in a new tab

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?

Acy.get('a.external-link').should('contain.text', '_blank')
Bcy.get('a.external-link').should('have.attr', 'target', '_blank')
Ccy.get('a.external-link').should('have.text', 'target="_blank"')
Dcy.get('a.external-link').should('have.prop', 'target', '_blank')
Attempts:
2 left
💡 Hint

Attributes are strings set in HTML tags. Properties are DOM object values.

Practice

(1/5)
1. What does the Cypress command cy.get('input').should('have.value', 'hello') check?
easy
A. It confirms the input field is enabled for typing.
B. It checks that the input field has an attribute named 'value' regardless of content.
C. It checks that the input field contains the text 'hello' as its value.
D. It verifies the input field is visible on the page.

Solution

  1. Step 1: Understand the command context

    The command targets an input element and uses should('have.value', 'hello') which asserts the value property of the input.
  2. Step 2: Interpret the assertion meaning

    This assertion checks the actual content inside the input field, expecting it to be exactly 'hello'.
  3. Final Answer:

    It checks that the input field contains the text 'hello' as its value. -> Option C
  4. Quick Check:

    Value assertion = check input content [OK]
Hint: Value assertions check input content, not just attribute presence [OK]
Common Mistakes:
  • Confusing 'have.value' with checking attribute existence
  • Assuming it checks visibility or enabled state
  • Mixing up value with text content
2. Which of the following is the correct Cypress syntax to assert that a button has an attribute disabled?
easy
A. cy.get('button').should('have.attr', 'disabled')
B. cy.get('button').should('have.value', 'disabled')
C. cy.get('button').should('contain', 'disabled')
D. cy.get('button').should('be.disabled')

Solution

  1. Step 1: Identify attribute assertion syntax

    The correct way to check for an attribute presence is using should('have.attr', 'attributeName').
  2. Step 2: Match the attribute name

    Here, the attribute is 'disabled', so the command is should('have.attr', 'disabled').
  3. Final Answer:

    cy.get('button').should('have.attr', 'disabled') -> Option A
  4. Quick Check:

    Attribute assertion uses 'have.attr' [OK]
Hint: Use 'have.attr' to check attribute presence [OK]
Common Mistakes:
  • 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
3. Consider this Cypress code snippet:
cy.get('input#email')
  .should('have.attr', 'placeholder', 'Enter your email')
  .and('have.value', '')

What will this test verify?
medium
A. The input has placeholder text 'Enter your email' and is empty.
B. The input has value 'Enter your email' and placeholder is empty.
C. The input is visible and enabled for typing.
D. The input has attribute 'value' set to 'Enter your email'.

Solution

  1. Step 1: Analyze the attribute assertion

    should('have.attr', 'placeholder', 'Enter your email') checks the placeholder attribute equals 'Enter your email'.
  2. Step 2: Analyze the value assertion

    and('have.value', '') asserts the input's current value is empty (no text typed).
  3. Final Answer:

    The input has placeholder text 'Enter your email' and is empty. -> Option A
  4. Quick Check:

    Attribute + value assertions combined = The input has placeholder text 'Enter your email' and is empty. [OK]
Hint: Attribute checks attribute text; value checks input content [OK]
Common Mistakes:
  • Mixing placeholder attribute with input value
  • Assuming value is placeholder text
  • Ignoring the empty string value assertion
4. You wrote this Cypress test:
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?
medium
A. The test should use 'have.attr' instead of 'have.value'.
B. The selector is incorrect and does not find the input.
C. The input is disabled, so value cannot be read.
D. The input's value attribute is empty; text is shown by placeholder.

Solution

  1. Step 1: Understand difference between value and placeholder

    If the input visually shows 'admin' but have.value fails, likely 'admin' is placeholder text, not actual value.
  2. 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.
  3. Final Answer:

    The input's value attribute is empty; text is shown by placeholder. -> Option D
  4. Quick Check:

    Value assertion fails if text is placeholder [OK]
Hint: Check if text is placeholder, not actual input value [OK]
Common Mistakes:
  • Confusing placeholder text with input value
  • Assuming selector is wrong without checking
  • Using 'have.attr' incorrectly for value check
5. You want to verify a checkbox is checked and has a custom attribute data-test with value agree. Which Cypress assertion chain is correct?
hard
A. cy.get('input[type="checkbox"]').should('have.attr', 'checked').and('have.value', 'agree')
B. cy.get('input[type="checkbox"]').should('be.checked').and('have.attr', 'data-test', 'agree')
C. cy.get('input[type="checkbox"]').should('have.value', 'checked').and('have.attr', 'data-test')
D. cy.get('input[type="checkbox"]').should('be.checked').and('have.value', 'agree')

Solution

  1. Step 1: Check checkbox state assertion

    To verify a checkbox is checked, use should('be.checked').
  2. Step 2: Check attribute with specific value

    To verify an attribute and its value, use should('have.attr', 'data-test', 'agree').
  3. Step 3: Combine assertions correctly

    Chain both assertions with and() to check both conditions on the same element.
  4. Final Answer:

    cy.get('input[type="checkbox"]').should('be.checked').and('have.attr', 'data-test', 'agree') -> Option B
  5. Quick Check:

    Checkbox checked + attribute value = cy.get('input[type="checkbox"]').should('be.checked').and('have.attr', 'data-test', 'agree') [OK]
Hint: Use 'be.checked' for checkbox state, 'have.attr' for attribute value [OK]
Common Mistakes:
  • Using 'have.value' to check checkbox state
  • Checking 'checked' attribute instead of state
  • Not specifying attribute value in assertion
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard