0
0
Cypresstesting~20 mins

cy.get() with CSS selectors in Cypress - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
CSS Selector Mastery in Cypress
Get all challenges correct to earn this badge!
Test your skills under time pressure!
locator
intermediate
2:00remaining
Selecting elements with multiple classes using cy.get()

Which cy.get() selector correctly targets an element with both btn and primary classes?

Acy.get('#btn.primary')
Bcy.get('.btn.primary')
Ccy.get('.btn, .primary')
Dcy.get('btn.primary')
Attempts:
2 left
💡 Hint

Remember that chaining classes in CSS selectors means the element must have all those classes.

assertion
intermediate
2:00remaining
Asserting element visibility after selection

Given cy.get('.menu-item'), which assertion correctly checks that the element is visible?

Cypress
cy.get('.menu-item')
A.should('be.visible')
B.should('exist')
C.should('be.hidden')
D.should('not.exist')
Attempts:
2 left
💡 Hint

Visibility means the element is shown on the page, not just present in the DOM.

Predict Output
advanced
2:00remaining
Output of chained cy.get() with descendant selector

What does the following code select?

cy.get('nav').get('ul.menu > li.active')
Cypress
cy.get('nav').get('ul.menu > li.active')
ASelects <code>nav</code> elements only
BSelects <code>li.active</code> inside <code>ul.menu</code> only within <code>nav</code>
CSelects all <code>li.active</code> inside <code>ul.menu</code> anywhere in the document
DThrows an error because chaining <code>get</code> is invalid
Attempts:
2 left
💡 Hint

Remember that cy.get() always searches the entire document, not scoped to previous commands.

🔧 Debug
advanced
2:00remaining
Debugging a failing cy.get() selector

Why does cy.get('button#submit.primary') fail to find the element?

Cypress
cy.get('button#submit.primary')
AThe element has class 'primary' but no ID 'submit'
BThe selector syntax is invalid because ID and class cannot be combined
CThe element is a <code>div</code>, not a <code>button</code>
DThe element has ID 'submit' but does not have class 'primary'
Attempts:
2 left
💡 Hint

Check the actual HTML element's classes and ID carefully.

framework
expert
3:00remaining
Best practice for selecting dynamic elements with cy.get()

In a web app where button classes change dynamically, which selector strategy is best to reliably select the submit button using cy.get()?

AUse a data attribute like <code>cy.get('[data-cy=submit-button]')</code>
BUse a complex CSS selector combining multiple classes
CUse <code>cy.get('button:contains("Submit")')</code> to find by text
DUse <code>cy.get('button.primary')</code> and hope the class stays stable
Attempts:
2 left
💡 Hint

Think about selectors that do not break when styles or classes change.