Skip to content

Latest commit

 

History

History
223 lines (154 loc) · 5.52 KB

text.md

File metadata and controls

223 lines (154 loc) · 5.52 KB

text

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.

Syntax

.text()
.text(options)

Usage

✔️ Correct Usage

cy.get('nav').text(); // Yields the text inside the `nav` element

❌ Incorrect Usage

cy.text(); // Errors, cannot be chained off 'cy'
cy.location().text(); // Errors, 'location' does not yield DOM element

Arguments

> 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

Yields

  • .text() yields the text inside the subject.
  • .text() yields an array of the texts inside multiple subjects.

Examples

No Args

Get the text of a div

<div>Teriffic Tiger</div>
// yields "Teriffic Tiger"
cy.get('div').text();

Get the text of multiple divs

<div>Catastrophic Cat</div>
<div>Dramatic Dog</div>
<div>Amazing Ant</div>
// yields [
//   "Catastrophic Cat",
//   "Dramatic Dog",
//   "Amazing Ant"
// ]
cy.get('div').text();

Whitespace handling

By default all whitespace will be simplified.

<div> Extravagant &nbsp;
  Eagle            </div>

Simplify whitespace by default

// 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' });

Simplify whitespace but keep newline characters

// yields "Extravagant\nEagle"
cy.get('div').text({ whitespace: 'keep-newline' });

Do not simplify whitespace

// 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.

Depth of elements

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>

Only the subject by default

// 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 });

Include the direct children

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 });

Multiple elements with depth

Selecting multiple elements will yield an array of concatenated strings.

// yields [
//   "Mother Meerkat Son Scorpion",
//   "Father Fox"
// ]
cy.get('.parent').text({ depth: 1 });

Remove all depth limitations

To infinity and beyond!

// yields "Grandma Gazelle Mother Meerkat Son Scorpion Father Fox"
cy.get('.grandparent').text({ depth: Infinity });

Notes

Form elements

.text() also gets text from form elements like input and textarea.

cy.get('input').text();

Rules

Requirements

  • .text() requires being chained off a command that yields DOM element(s).

Assertions

  • .text() will automatically retry itself until assertions you've chained all pass.

Timeouts

  • .text() can time out waiting for a chained assertion to pass.

Command Log

.text() will output to the command log.

See also