From 7c08390bb8ac0e62c28a9543d91f1336f846837c Mon Sep 17 00:00:00 2001 From: Zach Dennis Date: Tue, 14 Apr 2009 17:09:46 -0400 Subject: [PATCH] Updated regex for LabelLocator and FieldLabeledLocator to work with labels whose text ends in a non-word character like \? or # --- .../core/locators/field_labeled_locator.rb | 2 +- lib/webrat/core/locators/label_locator.rb | 2 +- spec/public/locators/field_labeled_spec.rb | 15 +++++++++ spec/public/select_date_spec.rb | 24 ++++++++++++++ spec/public/select_datetime_spec.rb | 31 +++++++++++++++++++ spec/public/select_time_spec.rb | 21 +++++++++++++ 6 files changed, 93 insertions(+), 2 deletions(-) diff --git a/lib/webrat/core/locators/field_labeled_locator.rb b/lib/webrat/core/locators/field_labeled_locator.rb index 1e09923..8071be2 100644 --- a/lib/webrat/core/locators/field_labeled_locator.rb +++ b/lib/webrat/core/locators/field_labeled_locator.rb @@ -20,7 +20,7 @@ module Webrat def matching_label_elements label_elements.select do |label_element| - text(label_element) =~ /^\W*#{Regexp.escape(@value.to_s)}\b/i + text(label_element) =~ /^\W*#{Regexp.escape(@value.to_s)}(\b|\Z)/i end end diff --git a/lib/webrat/core/locators/label_locator.rb b/lib/webrat/core/locators/label_locator.rb index a79eb85..19a40e6 100644 --- a/lib/webrat/core/locators/label_locator.rb +++ b/lib/webrat/core/locators/label_locator.rb @@ -12,7 +12,7 @@ module Webrat def label_element label_elements.detect do |label_element| - text(label_element) =~ /^\W*#{Regexp.escape(@value.to_s)}\b/i + text(label_element) =~ /^\W*#{Regexp.escape(@value.to_s)}(\b|\Z)/i end end diff --git a/spec/public/locators/field_labeled_spec.rb b/spec/public/locators/field_labeled_spec.rb index a612ef9..6828a9b 100644 --- a/spec/public/locators/field_labeled_spec.rb +++ b/spec/public/locators/field_labeled_spec.rb @@ -153,5 +153,20 @@ describe "field_labeled" do should_return_a Webrat::TextField, :for => "The Label" with_an_id_of "element_42", :for => "The Label" end + + describe "finding a field whose label ends with an non word character" do + using_this_html <<-HTML + +
+ + +
+ + HTML + + should_return_a Webrat::TextField, :for => "License #" + with_an_id_of "element_42", :for => "License #" + should_raise_error_matching /Could not find .* "Other License #"/, :for => "Other License #" + end end diff --git a/spec/public/select_date_spec.rb b/spec/public/select_date_spec.rb index fb51205..1649059 100644 --- a/spec/public/select_date_spec.rb +++ b/spec/public/select_date_spec.rb @@ -71,6 +71,30 @@ describe "select_date" do select_date "December 25, 2003" click_button end + + it "should work when the label ends in a non word character" do + with_html <<-HTML + +
+
+ + + + +
+ + HTML + webrat_session.should_receive(:post).with("/appointments", + "appointment" => {"date(1i)" => '2003', "date(2i)" => "12", "date(3i)" => "25"}) + select_date Date.parse("December 25, 2003"), :from => "date ?" + click_button + end it "should fail if the specified label is not found" do with_html <<-HTML diff --git a/spec/public/select_datetime_spec.rb b/spec/public/select_datetime_spec.rb index dcd1923..0297177 100644 --- a/spec/public/select_datetime_spec.rb +++ b/spec/public/select_datetime_spec.rb @@ -61,6 +61,37 @@ describe "select_datetime" do click_button end + it "should work when the label ends in a non word character" do + with_html <<-HTML + +
+
+ + + + + : + +
+ + HTML + webrat_session.should_receive(:post).with("/appointments", + "appointment" => {"time(1i)" => '2003', "time(2i)" => "12", "time(3i)" => "25", "time(4i)" => "09", "time(5i)" => "30"}) + select_datetime "December 25, 2003 9:30", :from => "Time ?" + click_button + end + + it "should work when no label is specified" do with_html <<-HTML diff --git a/spec/public/select_time_spec.rb b/spec/public/select_time_spec.rb index 3da5bb3..3eafbaa 100644 --- a/spec/public/select_time_spec.rb +++ b/spec/public/select_time_spec.rb @@ -43,6 +43,27 @@ describe "select_time" do click_button end + it "should work when the label ends in a non word character" do + with_html <<-HTML + +
+
+ + : + +
+ + HTML + webrat_session.should_receive(:post).with("/appointments", + "appointment" => {"time(4i)" => "09", "time(5i)" => "30"}) + select_time "9:30AM", :from => "Time #" + click_button + end + it "should work when no label is specified" do with_html <<-HTML