0
0
Cypresstesting~20 mins

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

Choose your learning style9 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.