This is a command that does not exist as a default command.
Enables you to get the text contents of the subject yielded from the previous command.
.text()
allows you to be more specific than you can be with .contains()
or .should('contain')
.
Note: When using
.text()
you should be aware about how Cypress only retries the last command.
.text()
.text(options)
cy.get('nav').text(); // Yields the text inside the `nav` element
cy.text(); // Errors, cannot be chained off 'cy'
cy.location().text(); // Errors, 'location' does not yield DOM element
> options (Object)
Pass in an options object to change the default behavior of .text()
.
Option | Default | Description |
---|---|---|
timeout |
defaultCommandTimeout |
Time to wait for .text() to resolve before timing out |
log |
true |
Displays the command in the Command log |
whitespace |
simplify |
Replace complex whitespace with a single regular space. Accepted values: simplify , keep-newline & keep |
depth |
0 |
Include the text contents of child elements upto n levels |
.text()
yields the text inside the subject..text()
yields an array of the texts inside multiple subjects.
<div>Teriffic Tiger</div>
// yields "Teriffic Tiger"
cy.get('div').text();
<div>Catastrophic Cat</div>
<div>Dramatic Dog</div>
<div>Amazing Ant</div>
// yields [
// "Catastrophic Cat",
// "Dramatic Dog",
// "Amazing Ant"
// ]
cy.get('div').text();
By default all whitespace will be simplified.
<div> Extravagant
Eagle </div>
// yields "Extravagant Eagle"
cy.get('div').text();
The default value of whitespace
is simplify
so the following yields the same.
// yields "Extravagant Eagle"
cy.get('div').text({ whitespace: 'simplify' });
// yields "Extravagant\nEagle"
cy.get('div').text({ whitespace: 'keep-newline' });
// yields "Extravagant \n Eagle"
cy.get('div').text({ whitespace: 'keep' });
Note that the whitespace at the beginning and end of the string is still removed.
By default only the text of the subject itself will be yielded. Use this option to also get the text of underlying elements.
<div class="grandparent">
Grandma Gazelle
<div class="parent">
Mother Meerkat
<div class="child">
Son Scorpion
</div>
</div>
<div class="parent">
Father Fox
</div>
</div>
// yields "Grandma Gazelle"
cy.get('.grandparent').text();
The default value of depth
is 0
so the following yields the same.
// yields "Grandma Gazelle"
cy.get('.grandparent').text({ depth: 0 });
The text of the child elements are concatenated and yielded as a single string.
// yields "Grandma Gazelle Mother Meerkat Father Fox"
cy.get('.grandparent').text({ depth: 1 });
Selecting multiple elements will yield an array of concatenated strings.
// yields [
// "Mother Meerkat Son Scorpion",
// "Father Fox"
// ]
cy.get('.parent').text({ depth: 1 });
To infinity and beyond!
// yields "Grandma Gazelle Mother Meerkat Son Scorpion Father Fox"
cy.get('.grandparent').text({ depth: Infinity });
.text()
also gets text from form elements like input
and textarea
.
cy.get('input').text();
.text()
requires being chained off a command that yields DOM element(s).
.text()
will automatically retry itself until assertions you've chained all pass.
.text()
can time out waiting for a chained assertion to pass.
.text()
will output to the command log.