0
0
Cypresstesting~10 mins

cy.parent() and cy.children() in Cypress - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test checks that a specific child element exists within a parent container on a webpage. It verifies that the child element is correctly nested inside the parent element using cy.parent() and cy.children() commands.

Test Code - Cypress
Cypress
describe('Test cy.parent() and cy.children()', () => {
  it('verifies child element is inside the parent container', () => {
    cy.visit('https://example.cypress.io/commands/traversal')
    // Find the child element with class 'traversal-mark'
    cy.get('.traversal-mark')
      .should('exist')
      // Check its parent has class 'traversal-button-states'
      .parent()
      .should('have.class', 'traversal-button-states')
      // Then check the parent has children and one of them is the original child
      .children()
      .should('contain.text', 'Mark')
  })
})
Execution Trace - 6 Steps
StepActionSystem StateAssertionResult
1Test startsTest runner initializes the Cypress test environment-PASS
2Browser opens and navigates to 'https://example.cypress.io/commands/traversal'The webpage with traversal commands examples is loaded-PASS
3Find element with class '.traversal-mark'The element with text 'Mark' inside the page is locatedCheck element existsPASS
4Get parent of '.traversal-mark' elementParent element with class 'traversal-button-states' is selectedVerify parent has class 'traversal-button-states'PASS
5Get children of the parent elementAll child elements inside the parent container are selectedVerify children contain text 'Mark'PASS
6Test ends successfullyAll assertions passed, test completes-PASS
Failure Scenario
Failing Condition: The element with class '.traversal-mark' does not exist or is not inside a parent with class 'traversal-button-states', or the children do not contain the expected text.
Execution Trace Quiz - 3 Questions
Test your understanding
What does the command cy.parent() do in this test?
ASelects all child elements of the current element
BClicks on the current element
CSelects the immediate parent element of the current element
DNavigates to a new page
Key Result
Always verify element relationships by checking parents and children to ensure the page structure is as expected. This helps catch layout or selector issues early.