Skip to content

getByRole doesn't work for passwords #1128

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

Closed
dwjohnston opened this issue May 13, 2022 · 3 comments
Closed

getByRole doesn't work for passwords #1128

dwjohnston opened this issue May 13, 2022 · 3 comments

Comments

@dwjohnston
Copy link

dwjohnston commented May 13, 2022

Note, I think I answered my own answer. But can someone clarify - when searching for password fields - should I just search by labelText?

This is possibly a regression of #737 ?

  • Found "@testing-library/react@13.2.0"
  • Found "jest@27.5.1"
  • Found "jsdom@16.7.0"

Relevant code or config:

const MyPassword = () => {
  return <label>Hello<input type="password" /></label>
}

const MyPassword2 = () => {
  return <><label htmlFor="password">Hello</label><input type="password" id="password" /></>
}
const MyPassword3 = () => {
  return <><label id="label">Hello</label><input type="password" aria-labelledby='label' /></>
}


describe('</>', () => {
  it("Passwords are selectedable label 1", () => {
    render(<MyPassword />)

    expect(screen.getByRole("textbox", {
      name: /hello/i
    })).toBeInTheDocument();
  });

  it("Passwords are selectedable label 2", () => {
    render(<MyPassword2 />)

    expect(screen.getByRole("textbox", {
      name: /hello/i
    })).toBeInTheDocument();
  });

  it("Passwords are selectedable label 3", () => {
    render(<MyPassword3 />)

    expect(screen.getByRole("textbox", {
      name: /hello/i
    })).toBeInTheDocument();
  });
});

What you did:

Try to write tests to assert that password fields exist.

What happened:

These tests fail.

TestingLibraryElementError: Unable to find an accessible element with the role "textbox" and name `/hello/i`

    There are no accessible roles. But there might be some inaccessible roles. If you wish to access them, then set the `hidden` option to `true`. Learn more about this here: https://testing-library.com/docs/dom-testing-library/api-queries#byrole

    Ignored nodes: comments, <script />, <style />
    <body>
      <div>
        <label
          id="label"
        >
          Hello
        </label>
        <input
          aria-labelledby="label"
          type="password"
        />
      </div>
    </body>

      35 |     render(<MyPassword3 />)
      36 |
    > 37 |     expect(screen.getByRole("textbox", {
         |                   ^
      38 |       name: /hello/i
      39 |     })).toBeInTheDocument();
      40 |   });


Reproduction:

Full repro here.

https://github.com/dwjohnston/rtl-password

Run yarn && yarn test

Problem description:

<input type='password'> should have role 'textbox'

Whoops, no it shouldn't - as per the W3 aria spec - it has no role.

Suggested solution:

@eps1lon
Copy link
Member

eps1lon commented May 13, 2022

Duplicate of #567
TL;DR: type="password" has no role and is therefore not suitable for getByRole

@eps1lon eps1lon closed this as completed May 13, 2022
@BudgieInWA
Copy link

This should be added to the examples in the docs. Many of the examples use a "username" input, but there is no mention of "password" near any of them.

type=password having no role is a surprise for many users (as indicated by the many issues in this repo) so I think it would be very valuable to provide guidance from veterans/authors as to which query tool fits the philosophy best for this awkward situation that new users are likely to encounter.

@MatanBobi
Copy link
Member

@BudgieInWA we're open for PR's in the docs repo if you feel that this can be helpful :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants