Given the HTML structure below, what will cy.get('.item').parent().should('have.class', 'container') verify?
<div class="container">
<ul>
<li class="item">One</li>
<li class="item">Two</li>
</ul>
</div>cy.get('.item').parent().should('have.class', 'container')
Remember that parent() selects only the immediate parent element, not all ancestors.
The parent() command in Cypress selects the direct parent of the element. Here, the direct parent of '.item' is the <ul> element, which does not have the class 'container'. Therefore, the assertion fails.
Given this HTML snippet:
<div class="list">
<span>A</span>
<span>B</span>
<span>C</span>
</div>Which Cypress command correctly asserts that the .list element has exactly 3 children?
Use children() to get direct children and should('have.length', n) to check count.
children() returns all direct child elements. The assertion should('have.length', 3) checks that there are exactly 3 children. Option A checks the parent, which is incorrect. Option A uses an invalid selector '.span' (should be 'span' without dot). Option A incorrectly uses contain.text to check count.
Consider this test code:
cy.get('.parent').children('.child').should('exist')But the test fails even though the HTML contains a child with class 'child' inside '.parent'. What is the most likely reason?
Recall that children() only selects direct children, not nested descendants.
The children() command selects only direct children. If the '.child' element is nested deeper (e.g., a grandchild), it won't be found. Option C and C are possible but less likely if the test setup is correct. Option C is false because children() does not select all descendants.
Choose the statement that best describes the difference between cy.parent() and cy.parents() commands.
Think about the difference between immediate and all ancestors in the DOM tree.
cy.parent() selects only the direct parent element of the current subject. cy.parents() selects all ancestor elements up to the root of the DOM tree. The other options confuse these relationships.
Given this HTML:
<div class="wrapper">
<div class="parent">
<span class="child">Hello</span>
</div>
<div class="sibling">World</div>
</div>Which Cypress command chain correctly verifies that the sibling of '.parent' has class 'sibling'?
Use parent() to go up, then siblings() to find siblings.
Starting from '.child', parent() selects '.parent'. Then siblings() selects elements at the same level as '.parent', which includes '.sibling'. The assertion checks that this sibling has class 'sibling'. Other options misuse chaining or select wrong elements.