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.

Query by autocomplete attribute

See original GitHub issue

Describe the feature you’d like:

Consider this HTML that’s gone through localization into (my amateur) Spanish:

<form method=post lang=es>
  <p><label>Nombre de usuario: <input autocomplete=username name=u></label>

  <p><label>Contraseña: <input autocomplete=current-password name=p></label>

  <p><button>Acceder</button>
</form>

Because the autocomplete attribute is standardized to map directly to user expectations for the purpose of autofill, browser/extension login managers, and password generators, it seems about as robust as the role attribute.

In the above example, you could query the right input across languages via the name or maybe a prefilled value, but those are more likely to change in the case of new tests, new backends, additional auth endpoints, new providers, etc.

However, you can be certain that [autocomplete=username] will always map to the User ID field (or you’ve got bigger problems than a rotting test). In fact, it‘s specified to mean the same thing regardless of the underlying element (banks love using <select> for some reason) or input type; consider login forms that ask for a handle, email address, phone number, ID number, or any combination thereof, but only use them as an opaque user identifier. It even works for <input type=hidden>, as suggested by Chrome for multi-step login flows.

Suggested implementation:

ByAutocomplete seems like the simplest thing that could possibly work.

Describe alternatives you’ve considered:

React’s JSX mappings have unfortunately camel-cased the autocomplete DOM property as autoComplete, but that might just be a specific quirk for only react-testing-library.

Teachability, Documentation, Adoption, Migration Strategy:

I think this would go under “Queries Accessible to Everyone”, as it’s roughly as exposed as role. But I’m certainly not pretending to know for sure.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
kentcdoddscommented, Jul 6, 2020

This would make more sense as an assertion that a query. The user will still primarily know what the field is for based on it’s label and will therefore “query” the screen based on the label. But you could definitely use jest-dom’s toHaveAttribute assertion to get confidence that the autocomplete is set up properly.

0reactions
kentcdoddscommented, Jul 6, 2020

I just really don’t think the use case is strong enough to justify an official query. This is what custom queries support is for. For that reason, I’m going to close this. Thanks for the discussion.

Read more comments on GitHub >

github_iconTop Results From Across the Web

HTML attribute: autocomplete - MDN Web Docs
The HTML autocomplete attribute lets web developers specify what if any permission the user agent has to provide automated assistance in ...
Read more >
HTML autocomplete Attribute - W3Schools
The autocomplete attribute specifies whether a form or an input field should have autocomplete on or off. Autocomplete allows the browser to predict...
Read more >
Autocomplete attribute has valid value | ACT Rule | WAI - W3C
This rule checks that the HTML autocomplete attribute has a correct value. ... In some user agents, querying the value of the autocomplete...
Read more >
HTML Autocomplete Attribute - W3docs
Note: The autocomplete attribute works with such <input> types, like text, search, tel, url, password, email, range, and color. Syntax. <input autocomplete="on| ...
Read more >
HTML | <select> autocomplete Attribute - GeeksforGeeks
The HTML <select> autocomplete attribute is used to specify that the form has autocompleted on or off. When the autocomplete attribute is ...
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