I need to select a value in a select
. The select is a list of countries, displaying their names, with a two-letter country code as the value for each option. We surface the most selected countries to the top, while also leaving them in their alphabetical position. This means the items surfaced to the top are repeated twice.
<select>
<option value="gb">UK</option>
<option value="us">USA</option>
<option value="af">Afganistan</option>
<option value="ai">Aland Islands</option>
...
<option value="us">USA</option>
...
<option value="us">UK</option>
...
</select>
I'm selecting a value like this:
cy.getSelect().select('gb')
However, this raises an error:
CypressError: Timed out retrying: cy.select() matched more than one option by value or text: gb
This makes sense as the value for 'UK' is gb
and it appears at the top of the list and within the list in its alphabetical position.
How can I tell Cypress to ignore the duplicate value and select the first match?
Note that I cannot guarantee the index of any country and that I have lots of other tests that select different countries. I need a way to tell Cypress to select the first match.