Get one or more DOM elements by selector or alias.
The querying behavior of this command is similar to how
$(...)works in jQuery.
cy.get(selector)
cy.get(alias)
cy.get(selector, options)
cy.get(alias, options)
Correct Usage
cy.get('.list > li') // Yield the <li>'s in .list
selector (String selector)
A selector used to filter matching DOM elements.
alias (String)
An alias as defined using the .as() command and referenced with the @ character and the name of the alias.
You can use cy.get() for aliases of primitives, regular objects, or even DOM elements.
When using aliases with DOM elements, Cypress will query the DOM again if the previously aliased DOM element has gone stale.
Core Concept
You can read more about aliasing objects and elements in our Core Concept Guide.
options (Object)
Pass in an options object to change the default behavior of cy.get().
| Option | Default | Description |
|---|---|---|
log |
true |
Displays the command in the Command log |
timeout |
defaultCommandTimeout |
Time to wait for cy.get() to resolve before timing out
|
withinSubject |
null | Element to search for children in. If null, search begins from root-level DOM element |
includeShadowDom |
includeShadowDom config option value |
Whether to traverse shadow DOM boundaries and include elements within the shadow DOM in the yielded results. |
cy.get() yields the DOM element(s) it found. cy.get('input').should('be.disabled')
li descendent within a ul
cy.get('ul li:first').should('have.class', 'active')
cy.get('.dropdown-menu').click()
cy.get('[data-test-id="test-example"]').should('have.length', 5)
cy.get('a[href*="questions"]').click()
cy.get('[id^=local-]')
cy.get('[id$=-remote]')
cy.get('[id^=local-][id$=-remote]')
cy.get('#id\\.\\.\\.1234') // escape the character with \\
.within()
cy.get() in the .within() commandSince cy.get() is chained off of cy, it always looks for the selector within the entire document. The only exception is when used inside a .within() command.
cy.get('form').within(() => {
cy.get('input').type('Pamela') // Only yield inputs within form
cy.get('textarea').type('is a developer') // Only yield textareas within form
})
The cy.get command always starts its search from the cy.root element. In most cases, it is the document element, unless used inside the .within() command. The .find command starts its search from the current subject.
<div class="test-title">cy.get vs .find</div>
<section id="comparison">
<div class="feature">Both are querying commands</div>
</section>
cy.get('#comparison')
.get('div')
// finds the div.test-title outside the #comparison
// and the div.feature inside
.should('have.class', 'test-title')
.and('have.class', 'feature')
cy.get('#comparison')
.find('div')
// the search is limited to the tree at #comparison element
// so it finds div.feature only
.should('have.length', 1)
.and('have.class', 'feature')
For a detailed explanation of aliasing, read more about aliasing here.
cy.get('ul#todos').as('todos')
//...hack hack hack...
//later retrieve the todos
cy.get('@todos')
beforeEach(() => {
cy.get('button[type=submit]').as('submitBtn')
})
it('disables on click', () => {
cy.get('@submitBtn').should('be.disabled')
})
beforeEach(() => {
cy.fixture('users.json').as('users')
})
it('disables on click', () => {
// access the array of users
cy.get('@users').then((users) => {
// get the first user
const user = users[0]
cy.get('header').contains(user.name)
})
})
cy.get() requires being chained off a command that yields DOM element(s). cy.get() will automatically retry until the element(s) exist in the DOM cy.get() will automatically retry until all chained assertions have passed cy.get() can time out waiting for the element(s) to exist in the DOM . cy.get() can time out waiting for assertions you've added to pass. Get an input and assert on the value
cy.get('input[name="firstName"]').should('have.value', 'Homer')
The commands above will display in the Command Log as:
When clicking on the get command within the command log, the console outputs the following:
| Version | Changes |
|---|---|
| 5.2.0 | Added includeShadowDom option. |
© 2017 Cypress.io
Licensed under the MIT License.
https://docs.cypress.io/api/commands/get