Ruby and Watir; randomly select option from select_list that does NOT have "disabled" in the attribute

256 Views Asked by At

Running Watir tests where I am looking to select a random value from a select_list element. By default, one of the options has the attribute disabled value="true"

Currently I am putting all the options into an array like such:

@browser.select_list(:class, 'preset-select').options.to_a.sample.click

Normally this works in selecting a single option, but I occasionally run into an issue where it tries to click on the disabled object instead.

How would I validate this to ensure the object returned is not a disabled one?

1

There are 1 best solutions below

4
Justin Ko On

You can filter the list of options to those that are enabled:

enabled_options = @browser.select_list(:class, 'preset-select').options.select(&:enabled?)
enabled_options.sample.click