From 4a06bb9729c5e9df070b0c8af158ded62bdea8bf Mon Sep 17 00:00:00 2001 From: Thomas Walpole Date: Fri, 3 Aug 2018 09:12:06 -0700 Subject: [PATCH] move normalize_ws behavior to node#text methods --- lib/capybara/node/document.rb | 4 ++-- lib/capybara/node/element.rb | 5 +++-- lib/capybara/node/simple.rb | 5 +++-- lib/capybara/queries/text_query.rb | 20 +++++++++----------- 4 files changed, 17 insertions(+), 17 deletions(-) diff --git a/lib/capybara/node/document.rb b/lib/capybara/node/document.rb index ec558cd9..4b50bb89 100644 --- a/lib/capybara/node/document.rb +++ b/lib/capybara/node/document.rb @@ -20,8 +20,8 @@ module Capybara # # @return [String] The text of the document # - def text(type = nil) - find(:xpath, '/html').text(type) + def text(type = nil, normalize_ws: false) + find(:xpath, '/html').text(type, normalize_ws: normalize_ws) end ## diff --git a/lib/capybara/node/element.rb b/lib/capybara/node/element.rb index 5e2aa43f..05208471 100644 --- a/lib/capybara/node/element.rb +++ b/lib/capybara/node/element.rb @@ -53,9 +53,10 @@ module Capybara # @param type [:all, :visible] Whether to return only visible or all text # @return [String] The text of the element # - def text(type = nil) + def text(type = nil, normalize_ws: false) type ||= :all unless session_options.ignore_hidden_elements || session_options.visible_text_only - synchronize { type == :all ? base.all_text : base.visible_text } + t = synchronize { type == :all ? base.all_text : base.visible_text } + normalize_ws ? t.gsub(/[[:space:]]+/, ' ').strip : t end ## diff --git a/lib/capybara/node/simple.rb b/lib/capybara/node/simple.rb index 7034b6f7..2810fe51 100644 --- a/lib/capybara/node/simple.rb +++ b/lib/capybara/node/simple.rb @@ -28,8 +28,9 @@ module Capybara # # @return [String] The text of the element # - def text(_type = nil) - native.text + def text(_type = nil, normalize_ws: false) + t = native.text + normalize_ws ? t.gsub(/[[:space:]]+/, ' ').strip : t end ## diff --git a/lib/capybara/queries/text_query.rb b/lib/capybara/queries/text_query.rb index c5938cf8..bd8667c4 100644 --- a/lib/capybara/queries/text_query.rb +++ b/lib/capybara/queries/text_query.rb @@ -5,12 +5,7 @@ module Capybara module Queries class TextQuery < BaseQuery def initialize(type = nil, expected_text, session_options:, **options) # rubocop:disable Style/OptionalArguments - @type = if type.nil? - Capybara.ignore_hidden_elements || Capybara.visible_text_only ? :visible : :all - else - type - end - + @type = type.nil? ? default_type : type @expected_text = expected_text.is_a?(Regexp) ? expected_text : expected_text.to_s @options = options super(@options) @@ -23,8 +18,7 @@ module Capybara def resolve_for(node) @node = node - @actual_text = text(node, @type) - @actual_text.gsub!(/[[:space:]]+/, ' ').strip! if options[:normalize_ws] + @actual_text = text @count = @actual_text.scan(@search_regexp).size end @@ -74,7 +68,7 @@ module Capybara end def invisible_message - invisible_text = text(@node, :all) + invisible_text = text(query_type: :all) invisible_count = invisible_text.scan(@search_regexp).size return if invisible_count == @count "it was found #{invisible_count} #{Capybara::Helpers.declension('time', 'times', invisible_count)} including non-visible text" @@ -95,8 +89,12 @@ module Capybara !@expected_text.is_a?(Regexp) end - def text(node, query_type) - node.text(query_type) + def text(node: @node, query_type: @type) + node.text(query_type, normalize_ws: options[:normalize_ws]) + end + + def default_type + Capybara.ignore_hidden_elements || Capybara.visible_text_only ? :visible : :all end end end