Get the DOM element that is currently focused.
cy.focused()
cy.focused(options)
Correct Usage
cy.focused() // Yields the element currently in focus
options (Object)
Pass in an options object to change the default behavior of cy.focused().
| Option | Default | Description |
|---|---|---|
log |
true |
Displays the command in the Command log |
timeout |
defaultCommandTimeout |
Time to wait for cy.focused() to resolve before timing out
|
cy.focused() yields the DOM element(s) it found. cy.focused().then(($el) => {
// do something with $el
})
cy.focused().blur()
cy.focused().should('have.attr', 'name', 'username')
cy.focused() requires being chained off a command that yields DOM element(s). cy.focused() will automatically retry until the element(s) exist in the DOM cy.focused() will automatically retry until all chained assertions have passed cy.focused() can time out waiting for the element(s) to exist in the DOM . cy.focused() can time out waiting for assertions you've added to pass. Make an assertion on the focused element
cy.focused().should('have.attr', 'name').and('eq', 'num')
The commands above will display in the Command Log as:
When clicking on the focused command within the command log, the console outputs the following:
© 2017 Cypress.io
Licensed under the MIT License.
https://docs.cypress.io/api/commands/focused