Parent commands help you find the parent element of a selected element on a web page. This is useful to check or interact with the container or group that holds the element.
Parent commands in Cypress
cy.get('child-selector').parent([selector])
The parent() command gets the direct parent of the element found by get().
You can optionally pass a selector to filter the parent element.
cy.get('button').parent()
item, then gets their parent only if it has class container.cy.get('.item').parent('.container')
input1, then gets its direct parent element.cy.get('#input1').parent()
This test visits a sample page, finds a button with class traversal-button, then checks if its parent has the class btn-group.
describe('Parent command example', () => { it('checks parent element class', () => { cy.visit('https://example.cypress.io/commands/traversal') cy.get('.traversal-button').parent().should('have.class', 'btn-group') }) })
The parent() command only gets the direct parent, not ancestors higher up.
If no parent matches the optional selector, the command will fail.
Use parents() if you want to get all ancestors instead of just the direct parent.
Parent commands help find the direct parent of an element.
You can check or interact with the parent element easily.
Use optional selectors to filter the parent element.
Practice
parent() command do?Solution
Step 1: Understand the purpose of
Theparent()parent()command in Cypress is used to get the immediate parent of the selected element in the DOM tree.Step 2: Compare with other options
Options A, B, and D describe different commands or actions unrelated toparent(). Only It finds the direct parent element of the current element. correctly describes the direct parent selection.Final Answer:
It finds the direct parent element of the current element. -> Option AQuick Check:
parent() = direct parent element [OK]
- Confusing parent() with children()
- Thinking parent() clicks the element
- Assuming parent() waits for elements
child-item in Cypress?Solution
Step 1: Identify the basic parent command syntax
The basic syntax to get the direct parent iscy.get(selector).parent();. This returns the immediate parent element.Step 2: Analyze options
cy.get('.child-item').parents(); usesparents()which gets all ancestors, not just the direct parent. cy.get('.child-item').parent('.parent-item'); filters the direct parent to only those matching '.parent-item', but the question does not specify the parent's class. cy.get('.child-item').children(); gets children, not parent.Final Answer:
cy.get('.child-item').parent(); -> Option DQuick Check:
Correct syntax = cy.get(...).parent() [OK]
- Using parents() instead of parent()
- Trying to pass selector directly inside parent() incorrectly
- Confusing children() with parent()
cy.get('.list-item').parent('.list-container').should('have.class', 'active');Solution
Step 1: Understand the parent() with selector
Theparent('.list-container')command filters the direct parent to only those with classlist-container.Step 2: Understand the assertion
Theshould('have.class', 'active')asserts that the selected parent element also has the classactive.Final Answer:
The direct parent of '.list-item' that has class 'list-container' and also has class 'active'. -> Option BQuick Check:
parent(selector) filters direct parent [OK]
- Thinking parents() selects only direct parent
- Confusing child elements with parent elements
- Ignoring the filter selector in parent()
cy.get('.button').parent('.container').click();What is the likely cause of the failure?
Solution
Step 1: Check the parent selector usage
Theparent('.container')filters the direct parent to only those with class 'container'. If no direct parent matches, the command yields no element.Step 2: Understand why click() fails
Since no element is found,click()fails because it has nothing to act on.Final Answer:
The parent element with class 'container' does not exist directly above '.button'. -> Option AQuick Check:
parent(selector) fails if no direct parent matches [OK]
- Assuming parent() always finds an element
- Thinking click() can't chain after parent()
- Using parents() unnecessarily
submit-btn is inside a form with class user-form. Which Cypress code correctly checks the form is the direct parent of the button?Solution
Step 1: Understand direct parent check
To check if the direct parent matches a selector, useparent(selector). cy.get('.submit-btn').parent('form.user-form').should('exist'); uses this correctly.Step 2: Compare other options
cy.get('.submit-btn').parents('form.user-form').should('exist'); checks all ancestors, not just direct parent. cy.get('.user-form').children('.submit-btn').should('exist'); checks children of the form, reversed logic. cy.get('.submit-btn').parent().should('have.class', 'user-form'); checks parent but does not filter by selector, so it may pass incorrectly.Final Answer:
cy.get('.submit-btn').parent('form.user-form').should('exist'); -> Option CQuick Check:
parent(selector) verifies direct parent matches selector [OK]
- Using parents() when only direct parent matters
- Checking children instead of parent
- Not filtering parent by selector
Ready to prove it?
Pass the test and this pattern moves unknown
