What is a subject and how do commands pass values along a chain?
A subject is the value one Cypress command yields to the next command in the chain. For example, cy.get yields a jQuery collection, .its('length') a number, .invoke('text') a string. A subject lives only within the chain, and after an action such as .click() it can go stale if the app re-rendered the DOM, so a new chain starts from cy. A value can be kept with .then() or an alias.
Cypress · Asynchrony
After .click() the chain suddenly works on a "dead" element. What exactly is passed between commands?
You read someone else's test: cy.get('.row').first().click().find('.badge'). It sometimes passes and sometimes fails with an error about a detached element. To understand why, you need to know what a subject is: the value one command hands to the next.
How it works and why. Every Cypress command "yields" a result, and the next command in the chain receives it as its subject. cy.get('li') yields a jQuery collection, .its('length') a number, .invoke('text') a string, cy.visit() the window object, cy.request() the response object. By their relation to the subject, commands come in three kinds: parent commands start a chain and need no previous one (cy.get, cy.visit, cy.request), child commands work only with the subject of the previous one (.click, .find, .should), and dual commands can do both (cy.contains searches the whole document or inside the yielded element).
A subject along a chain
Actions like .click() and .type() usually yield the same element, and .should() usually yields the same subject it received (the exception: assertions like should('have.attr', 'href'), which yield the value). So a chain reads left to right: at each step ask yourself what is in the subject now.
cy.get('.cart-item') // subject: all cart items
.first() // subject: the first of them
.find('.price') // subject: the price inside
.invoke('text') // subject: a string
.should('match', /\d+/); // subject: the same string
Example: why a chain after an action is fragile. The documentation advises ending a chain after an action such as a click or a navigation and starting a new one from cy. The reason is practical. After a click the app may re-render the list, while the child command keeps working with the element the click yielded.
Continuing the chain after a click
The subject after the click is the old element. If the framework replaced it, the lookup inside fails with a detached-element error.
A new chain from cy
cy.get() looks up the current elements again, and the query retries until the check goes green.
cy.get('.row').first().click().find('.badge').should('be.visible');
cy.get('.row').first().click();
// a new chain: the element is queried again
cy.get('.row').first().find('.badge').should('be.visible');
Limits and alternatives. A subject lives only until the next command: you cannot "put it" in a variable and take it later. For that there is .then() (the value in a callback) and aliases through .as(), covered in a separate lesson. The .then() callback yields to the next command whatever it returns, and if it returns undefined, the result of the last cy command inside, or the previous subject. This is where a chain often changes type unexpectedly.
There is no subject chain. A locator is an ordinary object, you can keep it in a variable and narrow it (locator.locator()), and values come from await.
const row = page.locator('.row').first();
await row.click();
await expect(row.locator('.badge')).toBeVisible();Typical mistakes.
- Continuing a chain after
.click()or another action when the DOM may have changed. - Assuming that
.should()changes the subject: by default it stays the same. - Forgetting that
cy.get()yields a jQuery collection, not an array or a string: take text through.invoke('text')or$el.text(). - Trying to use the subject on the next line without
.then(): it exists only inside the chain.
Check yourself
What does cy.get('.item').its('length') yield, and what is the subject after .should('eq', 3)?
Answer
its('length') yields a number (the count of elements). After .should('eq', 3) the subject stays the same number: should yields what it received.
Why does the documentation advise starting a new chain after .click()?
Answer
Because child commands after the click work with the element the click yielded. If the app re-rendered the DOM, that element is already detached. A new cy.get() looks the element up again.