diff --git a/lib/webrat/core/scope.rb b/lib/webrat/core/scope.rb index 600a7b0..fd99f64 100644 --- a/lib/webrat/core/scope.rb +++ b/lib/webrat/core/scope.rb @@ -191,6 +191,10 @@ module Webrat @dom ||= Hpricot(scoped_html) end + def element_labeled(label) + find_field(label, TextField, TextareaField, CheckboxField, RadioField, HiddenField) + end + protected def scoped_html diff --git a/lib/webrat/core/session.rb b/lib/webrat/core/session.rb index ab89e50..985f3f4 100644 --- a/lib/webrat/core/session.rb +++ b/lib/webrat/core/session.rb @@ -162,5 +162,6 @@ module Webrat def_delegators :current_scope, :click_button, :clicks_button def_delegators :current_scope, :should_see def_delegators :current_scope, :should_not_see + def_delegators :current_scope, :element_labeled end end diff --git a/spec/api/element_labeled_spec.rb b/spec/api/element_labeled_spec.rb new file mode 100644 index 0000000..547e40b --- /dev/null +++ b/spec/api/element_labeled_spec.rb @@ -0,0 +1,111 @@ +require File.expand_path(File.dirname(__FILE__) + "/../spec_helper") + + +describe "element_labeled" do + + class << self + def using_this_html html + before(:each) do + @session = Webrat::TestSession.new + @session.response_body = html + end + end + + def element_labeled label + @label = label + yield + end + + def should_return_a type, opts + it "should return a textfield" do + @session.element_labeled(opts[:for]).should be_an_instance_of(type) + end + end + + def with_an_id_of id, opts + it "should return an element with the correct id" do + @session.element_labeled(opts[:for]).should match_id(id) + end + end + + def should_raise_error_matching regexp, opts + it "should raise with wrong label" do + lambda { + @session.element_labeled(opts[:for]) + }.should raise_error(regexp) + end + end + end + + def match_id(id) + simple_matcher "element with id #{id.inspect}" do |element, matcher| + element.matches_id? id + end + end + + describe "finding a text field" do + using_this_html <<-EOS +
+ + +
+ EOS + + should_return_a Webrat::TextField, :for => "The Label" + with_an_id_of "element_42", :for => "The Label" + should_raise_error_matching /Could not find .* "Other Label"/, :for => "Other Label" + end + + describe "finding a text field" do + using_this_html <<-EOS +
+ + +
+ EOS + + should_return_a Webrat::HiddenField, :for => "The Label" + with_an_id_of "element_42", :for => "The Label" + should_raise_error_matching /Could not find .* "Other Label"/, :for => "Other Label" + end + + describe "finding a checkbox" do + using_this_html <<-EOS +
+ + +
+ EOS + + should_return_a Webrat::CheckboxField, :for => "The Label" + with_an_id_of "element_42", :for => "The Label" + should_raise_error_matching /Could not find .* "Other Label"/, :for => "Other Label" + end + + describe "finding a radio button" do + using_this_html <<-EOS +
+ + +
+ EOS + + should_return_a Webrat::RadioField, :for => "The Label" + with_an_id_of "element_42", :for => "The Label" + should_raise_error_matching /Could not find .* "Other Label"/, :for => "Other Label" + end + + + describe "finding a text area" do + using_this_html <<-EOS +
+ + +
+ EOS + + should_return_a Webrat::TextareaField, :for => "The Label" + with_an_id_of "element_42", :for => "The Label" + should_raise_error_matching /Could not find .* "Other Label"/, :for => "Other Label" + end +end