Permalink
Browse files

RackTest::Node#text now normalizes whitespace like Selenium

Previously Rack::Test's #text method might have returned
"   some  text ", whereas now it returns "some text", just like
Selenium.
  • Loading branch information...
1 parent c4247f9 commit 060fc3a72d0cd18e1ad3376017123c0ceb29aaa4 @joliss joliss committed Jan 6, 2012
Showing with 26 additions and 15 deletions.
  1. +17 −11 lib/capybara/rack_test/node.rb
  2. +9 −4 lib/capybara/spec/session/text_spec.rb
@@ -1,16 +1,6 @@
class Capybara::RackTest::Node < Capybara::Driver::Node
def text
- if !visible?
- ''
- elsif native.text?
- native.text
- elsif native.element?
- native.children.map do |child|
- Capybara::RackTest::Node.new(driver, child).text
- end.join
- else
- ''
- end
+ unnormalized_text.strip.gsub(/\s+/, ' ')
end
def [](name)
@@ -94,6 +84,22 @@ def find(locator)
native.xpath(locator).map { |n| self.class.new(driver, n) }
end
+protected
+
+ def unnormalized_text
+ if !visible?
+ ''
+ elsif native.text?
+ native.text
+ elsif native.element?
+ native.children.map do |child|
+ Capybara::RackTest::Node.new(driver, child).unnormalized_text
+ end.join
+ else
+ ''
+ end
+ end
+
private
def string_node
@@ -1,19 +1,24 @@
shared_examples_for "text" do
describe '#text' do
- before do
- @session.visit('/with_simple_html')
- end
-
it "should print the text of the page" do
+ @session.visit('/with_simple_html')
@session.text.should == 'Bar'
end
context "with css as default selector" do
before { Capybara.default_selector = :css }
it "should print the text of the page" do
+ @session.visit('/with_simple_html')
@session.text.should == 'Bar'
end
after { Capybara.default_selector = :xpath }
end
+
+ it "should strip whitespace" do
+ @session.visit('/with_html')
+ n = @session.find(:css, '#second')
+ @session.find(:css, '#second').text.should =~ \
+ /\ADuis aute .* text with whitespace .* est laborum\.\z/
+ end
end
end

0 comments on commit 060fc3a

Please sign in to comment.