Files
cypress/docs/source/api/commands/focus.md
T
2017-05-19 12:01:36 -04:00

2.4 KiB

title, comments, description
title comments description
focus true

Focus on a DOM element. If there is currently a different DOM element currently with focus, Cypress will issue a blur event to that element first.

The following events are fired during focus: focusin, focus

Returns the new DOM element(s) found by the command.
Timeout cy.focus will retry for the duration of the defaultCommandTimeout

cy.focus()

Focus on the DOM element found in the previous command.

Options

Pass in an options object to change the default behavior of cy.focus.

cy.focus( options )

Option Default Notes
log true whether to display command in command log

Usage

Focus on the current subject.

cy.get("[name='comment']").focus()

Focus, type, and blur the current subject.

// returns the <textarea> for further chaining
cy.get("[name='comment']").focus().type("Nice Product!").blur()

Command Log

Focus the textarea.

cy.get("[name='comment']").focus()

The commands above will display in the command log as:

screen shot 2015-11-27 at 1 32 37 pm

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

screen shot 2015-11-27 at 1 33 00 pm

Errors

cy.focus() can only be called on a valid focusable element.

Ensure the element you are trying to call cy.focus() on is a focusable element. Most commonly, you'll want to ensure that the element is not disabled although there are other factors.

cy.focus() timed out because your browser did not receive any focus events. This is a known bug in Chrome when it is not the currently focused window.

If you see this error, you may want to ensure that the main browser window is currently focused. This means not being focused in debugger or any other window when the command is executed.

Related