diff --git a/lib/capybara/node/matchers.rb b/lib/capybara/node/matchers.rb index b2a60ef5..09e87f03 100644 --- a/lib/capybara/node/matchers.rb +++ b/lib/capybara/node/matchers.rb @@ -253,8 +253,13 @@ module Capybara # # page.has_field?('Name', :with => 'Jonas') # + # It is also possible to filter by the field type attribute: + # + # page.has_field?('Email', :type => 'email') + # # @param [String] locator The label, name or id of a field to check for # @option options [String] :with The text content of the field + # @option options [String] :type The type attribute of the field # @return [Boolean] Whether it exists # def has_field?(locator, options={}) @@ -268,6 +273,7 @@ module Capybara # # @param [String] locator The label, name or id of a field to check for # @option options [String] :with The text content of the field + # @option options [String] :type The type attribute of the field # @return [Boolean] Whether it doesn't exist # def has_no_field?(locator, options={}) diff --git a/lib/capybara/selector.rb b/lib/capybara/selector.rb index 1201a61c..3b101ebc 100644 --- a/lib/capybara/selector.rb +++ b/lib/capybara/selector.rb @@ -80,6 +80,7 @@ Capybara.add_selector(:field) do filter(:checked) { |node, value| not(value ^ node.checked?) } filter(:unchecked) { |node, value| (value ^ node.checked?) } filter(:with) { |node, with| node.value == with } + filter(:type) { |node, type| node[:type] == type } end Capybara.add_selector(:fieldset) do diff --git a/lib/capybara/spec/session/has_field_spec.rb b/lib/capybara/spec/session/has_field_spec.rb index 415e51d7..f1f1ac93 100644 --- a/lib/capybara/spec/session/has_field_spec.rb +++ b/lib/capybara/spec/session/has_field_spec.rb @@ -36,6 +36,20 @@ shared_examples_for "has_field" do @session.should_not have_field('First Name', :with => 'John') end end + + context 'with type' do + it "should be true if a field with the given type is on the page" do + @session.should have_field('First Name', :type => 'text') + @session.should have_field('Html5 Email', :type => 'email') + @session.should have_field('Html5 Tel', :type => 'tel') + end + + it "should be false if the given field is not on the page" do + @session.should_not have_field('First Name', :type => 'email') + @session.should_not have_field('Html5 Email', :type => 'tel') + @session.should_not have_field('Description', :type => '') + end + end end describe '#has_no_field' do @@ -75,6 +89,20 @@ shared_examples_for "has_field" do @session.should have_no_field('First Name', :with => 'John') end end + + context 'with type' do + it "should be false if a field with the given type is on the page" do + @session.should_not have_no_field('First Name', :type => 'text') + @session.should_not have_no_field('Html5 Email', :type => 'email') + @session.should_not have_no_field('Html5 Tel', :type => 'tel') + end + + it "should be true if the given field is not on the page" do + @session.should have_no_field('First Name', :type => 'email') + @session.should have_no_field('Html5 Email', :type => 'tel') + @session.should have_no_field('Description', :type => '') + end + end end describe '#has_checked_field?' do