Files
cypress/docs/source/api/commands/focused.md
T
2017-05-25 17:57:56 -04:00

1.9 KiB

title, comments, description
title comments description
focused true

Get the DOM element that is currently focused.

Syntax

.focused()
.focused(options)

Usage

.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 (Objecdt)

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

.filter() yields the new DOM elements found by the command.

Timeout

.filter() will continue to look for the focuse 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