Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for "focus" assertion (to work effectively as Chai jQuery assertion does) #97

Closed
jennifer-shehane opened this issue Feb 5, 2016 · 11 comments · Fixed by #4274
Labels
E2E Issue related to end-to-end testing type: feature New feature that does not currently exist

Comments

@jennifer-shehane
Copy link
Member

Chai jQuery added a focus assertion recently.

Cypress needs support for focus assertions so I can assert whether a DOM element has or does not have focus.

Example

cy
  .get('input')
    .type('Foo').should('have.focus')
    .blur().should('not.have.focus')
@jennifer-shehane jennifer-shehane added the type: feature New feature that does not currently exist label Feb 5, 2016
@anniekvandijk
Copy link

Hi, I need a test for checking focus too and I think this could be a nice workaround?

Instead of checking if the element you select is focussed, you get the focussed element and check if the properties/attributes or whatever are conform expectations. In my case, I needed a focus on the field with a name

cy.focused().should('have.attr', 'name', fieldName);

@kuceb
Copy link
Contributor

kuceb commented Jan 24, 2019

Hi, after this PR #3219 , you will be able to do the following assertions:

cy.get('div').should('have.focus')
cy.get('div').should('not.have.focus')
cy.get('div').should('be.focused')
cy.get('div').should('not.be.focused')

expect(div).to.have.focus
expect(div).to.not.have.focus
expect(div).to.be.focused
expect(div).to.not.be.focused

@kuceb kuceb self-assigned this Jan 24, 2019
@jennifer-shehane jennifer-shehane added the stage: needs review The PR code is done & tested, needs review label Jan 25, 2019
@brian-mann
Copy link
Member

Fixed by #3219

@jennifer-shehane jennifer-shehane added stage: pending release and removed stage: needs review The PR code is done & tested, needs review labels May 1, 2019
@jennifer-shehane
Copy link
Member Author

The code for this is done in #3219, but this has yet to be released. We'll update this issue and reference the changelog when it's released.

@cypress-bot
Copy link
Contributor

cypress-bot bot commented May 17, 2019

Released in 3.3.0.

@denkristoffer
Copy link

I'm getting "CypressError: The chainer: 'focus' was not found. Could not build assertion." with Cypress 3.3.0 when I do cy.get('search_input').should('have.focus'). Am I using it wrong or should I create a new issue?

@wojciech-sygier
Copy link

wojciech-sygier commented May 21, 2019

I'm getting "CypressError: The chainer: 'focus' was not found. Could not build assertion." with Cypress 3.3.0 when I do cy.get('search_input').should('have.focus'). Am I using it wrong or should I create a new issue?

I second that. Whether I use have.focus or be.focused I always get the error. It looks as if it wasn't even present in this version.

@kuceb
Copy link
Contributor

kuceb commented May 21, 2019

I'm getting "CypressError: The chainer: 'focus' was not found. Could not build assertion." with Cypress 3.3.0 when I do cy.get('search_input').should('have.focus'). Am I using it wrong or should I create a new issue?

Currently looking into this

Edit: looks like it never made it into the develop branch https://github.com/cypress-io/cypress/compare/v3.3.0

We'll have this fixed soon

@cypress-bot
Copy link
Contributor

cypress-bot bot commented May 21, 2019

The code for this is done in cypress-io/cypress#4274, but has yet to be released.
We'll update this issue and reference the changelog when it's released.

@cypress-bot
Copy link
Contributor

cypress-bot bot commented May 23, 2019

Released in 3.3.1.

@alirezamirian
Copy link

alirezamirian commented May 5, 2023

Would be nice to have another assertion to check if the element is either focus, or contains focused element.
Currently contains.focus or have.focus both check if the element has focus. the former can become broader.

@nagash77 nagash77 added the E2E Issue related to end-to-end testing label May 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
E2E Issue related to end-to-end testing type: feature New feature that does not currently exist
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants