Skip to content

The findByRole does not match the expectation #178

@nemonemi

Description

@nemonemi
  • cypress-testing-library version: 6.6.0
  • node version: 14.16.0
  • yarn version: 1.22.10

Relevant code or config

// doesn't work
    cy.getIframeBody()
      .findByRole('button', { name: /Hover me!/ })
      .should('exist');

// works
    cy.getIframeBody()
      .findByText(/Hover me!/)
      .should('exist');

// Iframe custom command
// https://www.cypress.io/blog/2020/02/12/working-with-iframes-in-cypress/
Cypress.Commands.add('getIframeBody', () => {
  // get the iframe > document > body
  // and retry until the body element is not empty
  return (
    cy
      .get('#storybook-preview-iframe')
      .its('0.contentDocument.body')
      .should('not.be.empty')
      // wraps "body" DOM element to allow
      // chaining more Cypress commands, like ".find(...)"
      // https://on.cypress.io/wrap
      .then(cy.wrap)
  );
});

What you did:
I'm asserting that the button exists.

What happened:
It works when I query for it by text, but not by role.

image

Reproduction repository:

Problem description:
The problem is that it finds the button and registers that its title is its name. I was expecting that the content of the button would be the identifier.

Also, if I would select the button with findByText and trigger mouseover, it works:

    cy.getIframeBody()
      .findByText(/Hover me!/)
      .trigger('mouseover');

    cy.getIframeBody()
      .findByRole('button', { name: /Hover me!/ })
      .trigger('mouseover');

but not if I try to verify if the button exist with .should('exist') or .should('be.visible'), which I find strange a behavior.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions