What do we recommend people do for elements that have no implicit role (like input[type=password])
See original GitHub issueSay 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:
- Created 3 years ago
- Reactions:23
- Comments:12 (7 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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 forinput[type="text"]
).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;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.