Expect value of a dropdown select based on option text

Question:

We have a dropdown select

<select id="favorite-colors" multiple>
  <option value="R">Red</option>
  <option value="G">Green</option>
  <option value="B">Blue</option>
</select>

In playwright we can use to check the value

expect(locator).to_have_values([re.compile(r"R"), re.compile(r"G")])

See: https://playwright.dev/python/docs/api/class-locatorassertions#locator-assertions-to-have-values

But how can I use the expect to check if the selection is "Red" or "Green" or "Blue" – so instead of the value (R, G, B), base on the text of the options
We do not have indicator for "selected" – how should I expect if an option selected?

Asked By: M András

||

Answers:

<select id="favorite-colors" multiple>
  <option value="Red">Red</option>
  <option value="Green">Green</option>
  <option value="Blue">Blue</option>
</select>

The value is what is sent to the backend, so if you want the value to be the full string (Red, Green, Blue), you have to specify it in the html code.

In playwright, the following code should do it:

expect(locator).to_have_values([re.compile(r"Red"), re.compile(r"Green")])

Answered By: Pedro Silva

Seems, this should work

self.page.locator(dropdown).select_option(option)
dd_selected_value = self.page.locator(dropdown).input_value()
expect(self.page.locator(dropdown)).to_have_value(dd_selected_value)
Answered By: M András