cypress: cy.focused() inconsistent behavior

  • Operating System: Mac OS X Sierra
  • Cypress Version: 1.0.2
  • Browser Version: All

Is this a Feature or Bug?

Bug

Current behavior:

cy.focused() does the right thing when you keep the Cypress UI window focused. It does the wrong thing if you blur the Cypress UI window or if you run headlessly.

Desired behavior:

cy.focused() should produce consistent results, regardless of being run headed/headless or focused/blurred.

How to reproduce:

https://github.com/verheyenkoen/cypress-focused-issue

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 9
  • Comments: 17 (12 by maintainers)

Most upvoted comments

Hey, while this works in headless mode, focused() still fails when using the ui and not having the cypress window focused. It would be great if it worked even when I’m on another tab or in my IDE, etc.

We don’t use chai-jquery anymore

@brian-mann meaning these docs are out of date, and you implement the chai-jquery natively (via the string args in .should())?

I also second the OP – currently cy.focused() yields window object (in some(?) cases) ).

IMO cy.focused() should correctly yield focused element.

Meanwhile, it can be done like this:

// get focused element
cy.get(":focus")

// assert if yielded element has focus
cy.get( selector ).should( $elem => {
    expect( $elem.is(":focus") ).to.be.true;
})

Damn, what was I thinking?? Nevermind all that. Sorry for bothering…

I ran into this issue when testing moving between text boxes in a form. Basically was testing the behavior of focusing on the first text box, typed something, jumped to next one and typing something else.