title |
---|
get |
Get one or more DOM elements by selector or {% url 'alias' variables-and-aliases %}.
{% note info %}
The querying behavior of this command matches exactly how {% url $(...)
http://api.jquery.com/jQuery/ %} works in jQuery.
{% endnote %}
cy.get(selector)
cy.get(alias)
cy.get(selector, options)
cy.get(alias, options)
{% fa fa-check-circle green %} Correct Usage
cy.get('.list > li') // Yield the <li>'s in .list
{% fa fa-angle-right %} selector (String selector)
A selector used to filter matching DOM elements.
{% fa fa-angle-right %} alias (String)
An alias as defined using the {% url .as()
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.
{% note info 'Core Concept' %} {% url 'You can read more about aliasing objects and elements in our Core Concept Guide' variables-and-aliases#Aliases %}. {% endnote %}
{% fa fa-angle-right %} options (Object)
Pass in an options object to change the default behavior of cy.get()
.
Option | Default | Description |
---|---|---|
log |
true |
{% usage_options log %} |
timeout |
{% url defaultCommandTimeout configuration#Timeouts %} |
{% usage_options timeout cy.get %} |
withinSubject |
null | {% usage_options withinSubject %} |
{% yields sets_dom_subject cy.get %}
cy.get('input').should('be.disabled')
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()
Since 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 {% url ".within()
" 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
})
For a detailed explanation of aliasing, {% url 'read more about aliasing here' variables-and-aliases#Aliases %}.
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)
})
})
{% requirements dom cy.get %}
{% assertions existence cy.get %}
{% timeouts existence cy.get %}
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:
{% imgTag /img/api/get/get-element-and-make-an-assertion.png "Command Log get" %}
When clicking on the get
command within the command log, the console outputs the following:
{% imgTag /img/api/get/console-log-get-command-and-elements-found.png "Console Log get" %}
- {% url
.as()
as %} - {% url
cy.contains()
contains %} - {% url
.find()
find %} - {% url
.within()
within %} - {% url "Retry-ability" retry-ability %}