Command Chaining

It's important to understand the mechanism Cypress uses to chain commands together. It manages a Promise chain on your behalf, with each command yielding a 'subject' to the following command until the chain ends or there is an error. The developer should not need to use Promises directly, but understanding how they work is helpful.

For example, a chain of Cypress commands looks like this:

cy.get(".todo-list li").find("label").should("contain", "Buy Milk")

In this example, cy.get() will yield the <li> subject to .find() which will then search for the <label> element. Finally, we make an assertion that the <label> contains the text "Buy Milk."

However, it is important to note that not all Cypress commands yield a subject that can be chained. For instance, cy.clearCookies() yields null, which cannot be chained.

Cypress commands like cy.get() and cy.contains() yield DOM elements that can be chained, like in the example above.

When you want to act upon a subject directly from a Cypress command, you need to yield the subject to .then(). We cover .then() and cy.wrap() in the Understanding the Asynchronous nature of Cypress lesson.

You can learn more about command chaining from our docs here.


Unlock the next lesson

What does Cypress yield to the next command in the chain?