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.

selectOptions by option content

See original GitHub issue

https://github.com/testing-library/user-event#selectoptionselement-values

Users do not interact with <select/> elements by the option value because it is not visible. Users typically select by the text content of each option that they can see.

This is possibly a separate issue, but the current example in the readme is unclear about whether the option content or the value is used to select because the values and content are the same.

<option data-testid="val1" value="1">
  1
</option>

userEvent.selectOptions(getByTestId("select-multiple"), ["1", "3"]);

While the example is unclear, there is some text: The values parameter can be either an array of values or a singular scalar value.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:11
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
kentcdoddscommented, May 26, 2020

I think this PR handles this best, so I’ll go ahead and close this.

1reaction
andypearsoncommented, Apr 17, 2020

Hello, I come from Ruby and Rails land, where the syntax for selecting an option using Capybara looks like this:

page.select 'March', from: 'Month'

The option is selected based on the visible text of the <option> not the value.

Docs for this method are here: https://www.rubydoc.info/github/jnicklas/capybara/Capybara%2FNode%2FActions:select

I was, therefore, expecting userEvent.selectOptions to work in a similar fashion and have just spent a good long while trying to work out why my test wasn’t working 😃

I would agree with @jsphstls that the default behaviour should be selecting by the <option> text, and “special” behaviour should be supported by passing in option nodes directly.

Additionally, if you do something like:

userEvent.selectOptions(getByLabelText("Month"), "Monday")

You don’t get a warning that the option value doesn’t exist in the <select>, which meant it wasn’t obvious to work out that “nothing” was being selected when I called selectOptions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

HTMLSelectElement.selectedOptions - Web APIs | MDN
The list of selected options is an HTMLCollection object with one entry per currently selected option. An option is considered selected if it ......
Read more >
How do you select a particular option in a SELECT element in ...
I wanted a solution where selecting from an arbitrarily grouped select options automatically selected another select input on that same page. So for...
Read more >
How do I get the text value of a selected option?
Select elements typically have two values that you want to access. First there's the value to be sent to the server, which is...
Read more >
HTML DOM Select options Collection - W3Schools
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you ......
Read more >
ion-select: Select One or Multiple Value Boxes or Placeholders
If value is set on the <ion-select> , the selected option will be chosen based on ... but the newly retrieved select options...
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