cypressreact-testing-librarycypress-testing-library

Is it possible to use findByRole for input of type search?


I am trying to select a search input using react-testing-library in the more semantic way possible. I tagged the input with type=search, and I was expecting to be able to do something like this:

  cy.findByRole('search').click().clear().type(content);

However the type fails because there is no element with the role search. Is this a limitation? Or is it supposed to be put in a different place? For example in a wrapping form? Currently the search input is just that, an isolated input that triggers search queries.


Solution

  • If you don't have an explicit role attribute on the element,

    <input type="search">
    

    the role to use is searchbox

    cy.findByRole('searchbox').click().clear().type(content);  // passes