mirror of
https://github.com/teamcapybara/capybara.git
synced 2022-11-09 12:08:07 -05:00
Add option to text
which makes it possible to return all text, not just invisible text, closes #960
This commit is contained in:
parent
e22c937884
commit
0f9206fd42
7 changed files with 68 additions and 7 deletions
|
@ -16,7 +16,7 @@ module Capybara
|
||||||
|
|
||||||
class << self
|
class << self
|
||||||
attr_accessor :asset_root, :app_host, :run_server, :default_host, :always_include_port
|
attr_accessor :asset_root, :app_host, :run_server, :default_host, :always_include_port
|
||||||
attr_accessor :server_host, :server_port, :exact, :match, :exact_options
|
attr_accessor :server_host, :server_port, :exact, :match, :exact_options, :visible_text_only
|
||||||
attr_accessor :default_selector, :default_wait_time, :ignore_hidden_elements
|
attr_accessor :default_selector, :default_wait_time, :ignore_hidden_elements
|
||||||
attr_accessor :save_and_open_page_path, :automatic_reload, :raise_server_errors
|
attr_accessor :save_and_open_page_path, :automatic_reload, :raise_server_errors
|
||||||
attr_writer :default_driver, :current_driver, :javascript_driver, :session_name
|
attr_writer :default_driver, :current_driver, :javascript_driver, :session_name
|
||||||
|
@ -347,6 +347,7 @@ Capybara.configure do |config|
|
||||||
config.match = :smart
|
config.match = :smart
|
||||||
config.exact = false
|
config.exact = false
|
||||||
config.raise_server_errors = true
|
config.raise_server_errors = true
|
||||||
|
config.visible_text_only = false
|
||||||
end
|
end
|
||||||
|
|
||||||
Capybara.register_driver :rack_test do |app|
|
Capybara.register_driver :rack_test do |app|
|
||||||
|
|
|
@ -8,7 +8,11 @@ module Capybara
|
||||||
@native = native
|
@native = native
|
||||||
end
|
end
|
||||||
|
|
||||||
def text
|
def all_text
|
||||||
|
raise NotImplementedError
|
||||||
|
end
|
||||||
|
|
||||||
|
def visible_text
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -64,7 +68,7 @@ module Capybara
|
||||||
def path
|
def path
|
||||||
raise NotSupportedByDriverError
|
raise NotSupportedByDriverError
|
||||||
end
|
end
|
||||||
|
|
||||||
def trigger(event)
|
def trigger(event)
|
||||||
raise NotSupportedByDriverError
|
raise NotSupportedByDriverError
|
||||||
end
|
end
|
||||||
|
|
|
@ -42,10 +42,26 @@ module Capybara
|
||||||
|
|
||||||
##
|
##
|
||||||
#
|
#
|
||||||
# @return [String] The text of the element
|
# Retrieve the text of the element. If `Capybara.ignore_hidden_elements`
|
||||||
|
# is `true`, which it is by default, then this will return only text
|
||||||
|
# which is visible. The exact semantics of this may differ between
|
||||||
|
# drivers, but generally any text within elements with `display:none` is
|
||||||
|
# ignored. This behaviour can be overridden by passing `:all` to this
|
||||||
|
# method.
|
||||||
#
|
#
|
||||||
def text
|
# @param [:all, :visible] Whether to return only visible or all text
|
||||||
synchronize { base.text }
|
#
|
||||||
|
# @return [String] The text of the element
|
||||||
|
#
|
||||||
|
def text(type=nil)
|
||||||
|
type ||= :all unless Capybara.ignore_hidden_elements or Capybara.visible_text_only
|
||||||
|
synchronize do
|
||||||
|
if type == :all
|
||||||
|
base.all_text
|
||||||
|
else
|
||||||
|
base.visible_text
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
##
|
##
|
||||||
|
|
|
@ -1,5 +1,9 @@
|
||||||
class Capybara::RackTest::Node < Capybara::Driver::Node
|
class Capybara::RackTest::Node < Capybara::Driver::Node
|
||||||
def text
|
def all_text
|
||||||
|
Capybara::Helpers.normalize_whitespace(native.text)
|
||||||
|
end
|
||||||
|
|
||||||
|
def visible_text
|
||||||
Capybara::Helpers.normalize_whitespace(unnormalized_text)
|
Capybara::Helpers.normalize_whitespace(unnormalized_text)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -4,6 +4,37 @@ Capybara::SpecHelper.spec '#text' do
|
||||||
@session.text.should == 'Bar'
|
@session.text.should == 'Bar'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "ignores invisible text by default" do
|
||||||
|
@session.visit('/with_html')
|
||||||
|
@session.find(:id, "hidden-text").text.should == 'Some of this text is'
|
||||||
|
end
|
||||||
|
|
||||||
|
it "shows invisible text if `:all` given" do
|
||||||
|
@session.visit('/with_html')
|
||||||
|
@session.find(:id, "hidden-text").text(:all).should == 'Some of this text is hidden!'
|
||||||
|
end
|
||||||
|
|
||||||
|
it "ignores invisible text if `:visible` given" do
|
||||||
|
Capybara.ignore_hidden_elements = false
|
||||||
|
@session.visit('/with_html')
|
||||||
|
@session.find(:id, "hidden-text").text(:visible).should == 'Some of this text is'
|
||||||
|
end
|
||||||
|
|
||||||
|
it "ignores invisible text if `Capybara.ignore_hidden_elements = true`" do
|
||||||
|
@session.visit('/with_html')
|
||||||
|
@session.find(:id, "hidden-text").text.should == 'Some of this text is'
|
||||||
|
Capybara.ignore_hidden_elements = false
|
||||||
|
@session.find(:id, "hidden-text").text.should == 'Some of this text is hidden!'
|
||||||
|
end
|
||||||
|
|
||||||
|
it "ignores invisible text if `Capybara.visible_text_only = true`" do
|
||||||
|
@session.visit('/with_html')
|
||||||
|
Capybara.visible_text_only = true
|
||||||
|
@session.find(:id, "hidden-text").text.should == 'Some of this text is'
|
||||||
|
Capybara.ignore_hidden_elements = false
|
||||||
|
@session.find(:id, "hidden-text").text.should == 'Some of this text is'
|
||||||
|
end
|
||||||
|
|
||||||
context "with css as default selector" do
|
context "with css as default selector" do
|
||||||
before { Capybara.default_selector = :css }
|
before { Capybara.default_selector = :css }
|
||||||
it "should print the text of the page" do
|
it "should print the text of the page" do
|
||||||
|
|
|
@ -23,6 +23,7 @@ module Capybara
|
||||||
Capybara.exact = false
|
Capybara.exact = false
|
||||||
Capybara.exact_options = false
|
Capybara.exact_options = false
|
||||||
Capybara.raise_server_errors = true
|
Capybara.raise_server_errors = true
|
||||||
|
Capybara.visible_text_only = false
|
||||||
Capybara.match = :smart
|
Capybara.match = :smart
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -61,6 +61,10 @@ banana</textarea>
|
||||||
<a href="/with_simple_html" title="hidden link" class="simple">hidden link</a>
|
<a href="/with_simple_html" title="hidden link" class="simple">hidden link</a>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<div id="hidden-text">
|
||||||
|
Some of this text is <em style="display:none">hidden!</em>
|
||||||
|
</div>
|
||||||
|
|
||||||
<div style="display: none;">
|
<div style="display: none;">
|
||||||
<a id="first_invisble" class="hidden">first hidden link</a>
|
<a id="first_invisble" class="hidden">first hidden link</a>
|
||||||
</div>
|
</div>
|
||||||
|
|
Loading…
Add table
Reference in a new issue