Files
cypress/docs/source/api/commands/focused.md
2017-06-09 14:04:20 -04:00

1.9 KiB

title, comments
title comments
focused true

Get the DOM element that is currently focused.

Syntax

cy.focused()
cy.focused(options)

Usage

cy.focused() cannot be chained off any other cy commands, so should be chained off of cy for clarity.

{% fa fa-check-circle green %} Valid Usage

cy.focused()   

Arguments

{% fa fa-angle-right %} options (Object)

Pass in an options object to change the default behavior of cy.focused().

Option Default Notes
log true Whether to display command in Command Log

Yields

cy.focused() yields the DOM element that is currently focused.

Timeout

cy.focused() will continue to look for the focused element for the duration of the defaultCommandTimeout.

Examples

Focused

Get the element that is focused

cy.focused().then(function($el) {
  // do something with $el
})

Blur the element with focus

cy.focused().blur()

Make an assertion on the focused element

cy.focused().should('have.attr', 'name', 'username')

Command Log

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:

screen shot 2015-11-27 at 1 01 51 pm

When clicking on the focused command within the command log, the console outputs the following:

screen shot 2015-11-27 at 1 02 02 pm

See also

  • {% url .blur() blur %}
  • focus