3.6 KiB
title, comments, description
| title | comments | description |
|---|---|---|
| get | true |
Get one or more DOM elements by selector or alias.
cy.get supports all CSS based selectors. It is analogous to jQuery's $(...) in that any selector you pass to jQuery you can also pass to cy.get.
| Returns | the new DOM element(s) found by the command. |
| Timeout | cy.get will retry for the duration of the defaultCommandTimeout |
cy.get( selector )
Finds one or more DOM elements based on the selector.
cy.get( alias )
{% note info New to Cypress? %} Read about using aliases first. {% endnote %}
You can pass in the @ character and the name of an alias as a parameter to find an aliased element.
Internally Cypress keeps a cache of all aliased elements. If the element currently exists in the DOM, it is immediately returned. If the element no longer exists, Cypress will re-query the element based on the previous selector path to find it again.
Options
Pass in an options object to change the default behavior of cy.get.
cy.get( selector, options ) cy.get( alias, options )
| Option | Default | Notes |
|---|---|---|
log |
true |
whether to display command in command log |
timeout |
defaultCommandTimeout |
Total time to retry getting the element |
Selector Usage
Find the element with an id of main
cy.get("#main")
Find the first li descendent within a ul
cy.get("ul li:first")
Find the element with class dropdown-menu and click it.
cy
.get(".dropdown-menu").click()
// Break out of the previous command chain and
// query for #search from the root document.
.get("#search").type("mogwai")
Reset the current scope in a cy.within
// Find form and scope all new queries to within form.
cy.get("form").within(function(){
cy
// Find the input within form and type Pamela
.get("input").type("Pamela")
// Find the element textarea within form and type in it
.get("textarea").type("is a developer")
})
Alias Usage
For a detailed explanation of aliasing, read more about aliasing here.
Retrieve aliased todos elements
cy.get("ul#todos").as("todos")
//...hack hack hack...
//later retrieve the todos
cy.get("@todos")
Alias the submitBtn in a beforeEach
beforeEach(function(){
cy.get("button[type=submit]").as("submitBtn")
})
it("disables on click", function(){
cy.get("@submitBtn").should("be.disabled")
})
Command Log
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: