isFocused
Return true or false if the selected DOM-element currently has focus. If the selector matches multiple elements, it will return true if one of the elements has focus.
Usage
$(selector).isFocused()
Examples
- Asynchronous Mode
- Synchronous Mode
index.html
<input name="login" autofocus="" />
hasFocus.js
it('should detect the focus of an element', async () => {
await browser.url('/');
const loginInput = await $('[name="login"]');
console.log(await loginInput.isFocused()); // outputs: false
await loginInput.click();
console.log(await loginInput.isFocused()); // outputs: true
})
index.html
<input name="login" autofocus="" />
hasFocus.js
it('should detect the focus of an element', () => {
browser.url('/');
const loginInput = $('[name="login"]');
console.log(loginInput.isFocused()); // outputs: false
loginInput.click();
console.log(loginInput.isFocused()); // outputs: true
})
caution
Synchronous Mode will depcrecated with Node.js v16. With an update to the underlying Chromium version it became technically impossible to provide the same synchronous behavior. We recommend to start transition to asynchronous command execution. For more information, see our RFC.