From bdf6192eb4e9c1777b5c47b8a284afb7aff606e2 Mon Sep 17 00:00:00 2001 From: Markus Graf Date: Wed, 25 Sep 2013 15:07:02 +0200 Subject: [PATCH] fixes #1168 --- lib/capybara/query.rb | 1 + lib/capybara/selector.rb | 2 +- spec/rspec/matchers_spec.rb | 25 ++++++++++++++++++++++++- 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/lib/capybara/query.rb b/lib/capybara/query.rb index 17abd1c4..21eb9330 100644 --- a/lib/capybara/query.rb +++ b/lib/capybara/query.rb @@ -32,6 +32,7 @@ module Capybara def description @description = "#{label} #{locator.inspect}" @description << " with text #{options[:text].inspect}" if options[:text] + @description << " with value #{options[:with].inspect}" if options[:with] @description end diff --git a/lib/capybara/selector.rb b/lib/capybara/selector.rb index a1b25722..58195e45 100644 --- a/lib/capybara/selector.rb +++ b/lib/capybara/selector.rb @@ -103,7 +103,7 @@ Capybara.add_selector(:field) do filter(:checked) { |node, value| not(value ^ node.checked?) } filter(:unchecked) { |node, value| (value ^ node.checked?) } filter(:disabled, :default => false) { |node, value| not(value ^ node.disabled?) } - filter(:with) { |node, with| node.value == with } + filter(:with) { |node, with| node.value == with.to_s } filter(:type) do |node, type| if ['textarea', 'select'].include?(type) node.tag_name == type diff --git a/spec/rspec/matchers_spec.rb b/spec/rspec/matchers_spec.rb index cb6a9779..2914bc67 100644 --- a/spec/rspec/matchers_spec.rb +++ b/spec/rspec/matchers_spec.rb @@ -519,21 +519,44 @@ describe Capybara::RSpecMatchers do end describe "have_field matcher" do - let(:html) { '

' } + let(:html) { '

' } it "gives proper description" do have_field('Text field').description.should == "have field \"Text field\"" end + it "gives proper description for a given value" do + have_field('Text field', with: 'some value').description.should == "have field \"Text field\" with value \"some value\"" + end + it "passes if there is such a field" do html.should have_field('Text field') end + it "passes if there is such a field with value" do + html.should have_field('Text field', with: 'some value') + end + it "fails if there is no such field" do expect do html.should have_field('No such Field') end.to raise_error(/expected to find field "No such Field"/) end + + it "fails if there is such field but with false value" do + expect do + html.should have_field('Text field', with: 'false value') + end.to raise_error(/expected to find field "Text field"/) + end + + it "treats a given value as a string" do + class Foo + def to_s + "some value" + end + end + html.should have_field('Text field', with: Foo.new) + end end describe "have_checked_field matcher" do