question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

What do we recommend people do for elements that have no implicit role (like input[type=password])

See original GitHub issue

Say you have a login form:

<form>
  <div>
    <label for="username-field">Username</label>
    <input id="username-field" name="username" type="text" />
  </div>
  <div>
    <label for="password-field">Password</label>
    <input id="password-field" name="password" type="password" />
  </div>
  <div>
    <button type="submit">Submit</button>
  </div>
</form>

You can retrieve the username field via: screen.getByRole('textbox', {name: /username/i}) However, the password field has no implicit role so you’d have to use getByLabelText(/password/i) for that one.

I’m guessing that we just recommend people use getByLabelText as a fallback in this case, but I’m curious what other people think about this (especially curious to hear @eps1lon’s thoughts here).

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:23
  • Comments:12 (7 by maintainers)

github_iconTop GitHub Comments

12reactions
eps1loncommented, May 12, 2020

I’m guessing that we just recommend people use getByLabelText as a fallback in this case

I think this is our safest bet. I wouldn’t want to diverge from the spec. Otherwise people might use <input type="text" role="password" />.

If people stumble over this often when doing getByRole('textbox', { name: 'Password' }) we could check if there are other roles with the same superclass (related to #448) and name (and add a special case for input[type="text"]).

11reactions
Tohakercommented, May 23, 2020

Quite conveniently (or not), I’ve just faced this issue and was confused as to why my password input just couldn’t be found. Using getByLabelText works, but it does a look a bit strange in my test;

const usernameInput = getByRole("textbox", { name: /username/i });
const domainInput = getByRole("textbox", { name: /domain/i });
const passwordInput = getByLabelText(/password/i);

I’d appreciate eps1lon’s suggestion, but if this would result in some hacky, unstable solution I’d rather stick with what I already have.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Test type of input by React Testing Library - text or password?
I want test the type of input ("text or password") but I don't know how to make it with React Testing Library.
Read more >
<input>: The Input (Form Input) element - HTML
The <input> HTML element is used to create interactive controls for web-based forms in order to accept data from the user; a wide...
Read more >
Form <input> elements must have labels | Axe Rules
The recommended method for most circumstances is to use the label element and an explicit association using the for and id attributes. The...
Read more >
Form field has non-empty accessible name | ACT Rule | WAI
Depending on this, some elements can have one of the applicable semantic roles and fail this rule with some technology but users of...
Read more >
Form <input> elements must have labels
Why it Matters. Programmatically associate labels with all form controls. The recommended method for most circumstances is to use the label element and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found