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.
This commit is contained in:
Jo Liss 2012-01-06 22:22:41 +01:00
parent c4247f953b
commit 060fc3a72d
2 changed files with 26 additions and 15 deletions

View File

@ -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 @@ class Capybara::RackTest::Node < Capybara::Driver::Node
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

View File

@ -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