Cucumber step to test whether a select field is sorted

This step will pass if the specified select is sorted.

Then /^the "(.*?)" select should be sorted$/ do |label, negate|
  select = find_field(label)
  options = select.all('option').reject { |o| o.value.nil? }

  options.collect(&:text).each_cons(2) do |a,b|
    (a.text.downcase <=> b.text.downcase).should <= 0
  end
end

In conjunction with this custom matcher, the each_cons block can be replaced with:

options.should be_naturally_sorted

Usage

Then the "Organizations" select should be sorted

Depends on Capybara.


This step is now part of Spreewald Show archive.org snapshot .

Dominik Schöler