From 2fee0ddb0ecf4cf1d1f20cbd7d5f65414686625f Mon Sep 17 00:00:00 2001 From: Kyle Hargraves Date: Sat, 12 Apr 2008 16:22:20 -0500 Subject: [PATCH] Ability to select options by regexp --- lib/webrat/select_option.rb | 8 ++++++-- test/selects_test.rb | 27 +++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/lib/webrat/select_option.rb b/lib/webrat/select_option.rb index f687941..587c52b 100644 --- a/lib/webrat/select_option.rb +++ b/lib/webrat/select_option.rb @@ -6,8 +6,12 @@ module Webrat @element = element end - def matches_text?(text) - @element.innerHTML == text.to_s + def matches_text?(text) + if text.is_a?(Regexp) + @element.innerHTML =~ text + else + @element.innerHTML == text.to_s + end end def choose diff --git a/test/selects_test.rb b/test/selects_test.rb index bee63cf..feef07a 100644 --- a/test/selects_test.rb +++ b/test/selects_test.rb @@ -108,4 +108,31 @@ class SelectsTest < Test::Unit::TestCase @session.selects "January", :from => "month" @session.clicks_button end + + def test_should_find_option_by_regexp + @response.stubs(:body).returns(<<-EOS) +
+ + +
+ EOS + @session.expects(:post_via_redirect).with("/login", "month" => "January") + @session.selects(/jan/i) + @session.clicks_button + end + + def test_should_find_option_by_regexp_in_list_specified_by_label + @response.stubs(:body).returns(<<-EOS) +
+ + + + + +
+ EOS + @session.expects(:post_via_redirect).with("/login", "start_month" => "s1", "end_month" => "e1") + @session.selects(/jan/i, :from => "End Month") + @session.clicks_button + end end