mirror of
https://github.com/cypress-io/cypress.git
synced 2026-05-19 06:38:45 -05:00
2.9 KiB
2.9 KiB
title, comments, description
| title | comments | description |
|---|---|---|
| children | true |
Get the children of each DOM element in the set of matched DOM elements.
| Returns | the new DOM element(s) found by the command. |
| Timeout | cy.children will retry for the duration of the defaultCommandTimeout |
cy.children()
Get the children of each DOM element in the set of matched DOM elements.
cy.children( selector )
The .children() method optionally accepts a selector expression. If the selector is supplied, the DOM elements will be filtered by testing whether they match it.
Options
Pass in an options object to change the default behavior of cy.children.
cy.children( options ) cy.children( selector, options )
| Option | Default | Notes |
|---|---|---|
log |
true |
whether to display command in command log |
timeout |
defaultCommandTimeout |
Total time to retry getting the element |
Usage
Get the children of the "secondary-nav"
<ul class="primary-nav">
<li class="about">About</li>
<li class="services">Services
<ul class="secondary-nav">
<li class="services-1">Web Design</li>
<li class="services-2">Print Design
<ul class="tertiary-nav">
<li class="item-1">Signage</li>
<li class="item-2">T-Shirt</li>
<li class="item-3">Business Cards</li>
</ul>
</li>
<li class="services-3">Logo Design</li>
</ul>
</li>
<li class="Contact">Contact</li>
</ul>
// returns [
// <li class="services-1"></li>,
// <li class="services-2"></li>,
// <li class="services-3"></li>
// ]
cy.get('ul.secondary-nav').children()
Selector Usage
Get the children with class "active"
<div>
<ul>
<li class="active">Unit Testing</li>
<li>Integration Testing</li>
</ul>
</div>
// returns [<li class="active">Unit Testing</li>]
cy.get('ul').children('.active')
Command Log
Assert that there should be 8 children elements in a nav
cy.get('.left-nav>.nav').children().should('have.length', 8)
The commands above will display in the command log as:
When clicking on the children command within the command log, the console outputs the following: