code cleanup

This commit is contained in:
Thomas Walpole 2016-11-18 10:56:47 -08:00
parent bec032d706
commit 097f318893
3 changed files with 82 additions and 95 deletions

View File

@ -90,14 +90,11 @@ module Capybara
# @raise [Capybara::ExpectationNotMet] If the selector does not exist
#
def assert_selector(*args, &optional_filter_block)
query = Capybara::Queries::SelectorQuery.new(*args, &optional_filter_block)
synchronize(query.wait) do
result = query.resolve_for(self)
verify_selector_result(args, optional_filter_block) do |result, query|
unless result.matches_count? && ((!result.empty?) || query.expects_none?)
raise Capybara::ExpectationNotMet, result.failure_message
end
end
return true
end
##
@ -117,14 +114,11 @@ module Capybara
# @raise [Capybara::ExpectationNotMet] If the selector exists
#
def assert_no_selector(*args, &optional_filter_block)
query = Capybara::Queries::SelectorQuery.new(*args, &optional_filter_block)
synchronize(query.wait) do
result = query.resolve_for(self)
verify_selector_result(args, optional_filter_block) do |result, query|
if result.matches_count? && ((!result.empty?) || query.expects_none?)
raise Capybara::ExpectationNotMet, result.negative_failure_message
end
end
return true
end
alias_method :refute_selector, :assert_no_selector
@ -455,25 +449,15 @@ module Capybara
# @raise [Capybara::ExpectationNotMet] If the selector does not match
#
def assert_matches_selector(*args, &optional_filter_block)
query = Capybara::Queries::MatchQuery.new(*args, &optional_filter_block)
synchronize(query.wait) do
result = query.resolve_for(self.query_scope)
unless result.include? self
raise Capybara::ExpectationNotMet, "Item does not match the provided selector"
end
verify_match_result(args, optional_filter_block) do |result|
raise Capybara::ExpectationNotMet, "Item does not match the provided selector" unless result.include? self
end
return true
end
def assert_not_matches_selector(*args, &optional_filter_block)
query = Capybara::Queries::MatchQuery.new(*args, &optional_filter_block)
synchronize(query.wait) do
result = query.resolve_for(self.query_scope)
if result.include? self
raise Capybara::ExpectationNotMet, 'Item matched the provided selector'
end
verify_match_result(args, optional_filter_block) do |result|
raise Capybara::ExpectationNotMet, 'Item matched the provided selector' if result.include? self
end
return true
end
alias_method :refute_matches_selector, :assert_not_matches_selector
@ -573,14 +557,11 @@ module Capybara
# @return [true]
#
def assert_text(*args)
query = Capybara::Queries::TextQuery.new(*args)
synchronize(query.wait) do
count = query.resolve_for(self)
verify_text(args) do |count, query|
unless query.matches_count?(count) && ((count > 0) || query.expects_none?)
raise Capybara::ExpectationNotMet, query.failure_message
end
end
return true
end
##
@ -592,14 +573,11 @@ module Capybara
# @return [true]
#
def assert_no_text(*args)
query = Capybara::Queries::TextQuery.new(*args)
synchronize(query.wait) do
count = query.resolve_for(self)
verify_text(args) do |count, query|
if query.matches_count?(count) && ((count > 0) || query.expects_none?)
raise Capybara::ExpectationNotMet, query.negative_failure_message
end
end
return true
end
##
@ -645,6 +623,35 @@ module Capybara
self.eql?(other) || (other.respond_to?(:base) && base == other.base)
end
private
def verify_selector_result(query_args, optional_filter_block, &result_block)
query = Capybara::Queries::SelectorQuery.new(*query_args, &optional_filter_block)
synchronize(query.wait) do
result = query.resolve_for(self)
result_block.call(result, query)
end
return true
end
def verify_match_result(query_args, optional_filter_block, &result_block)
query = Capybara::Queries::MatchQuery.new(*query_args, &optional_filter_block)
synchronize(query.wait) do
result = query.resolve_for(self.query_scope)
result_block.call(result)
end
return true
end
def verify_text(query_args)
query = Capybara::Queries::TextQuery.new(*query_args)
synchronize(query.wait) do
count = query.resolve_for(self)
yield(count, query)
end
return true
end
end
end
end

View File

@ -11,6 +11,20 @@ module Capybara
Capybara.string(actual.to_s)
end
end
def matches?(actual)
yield(wrap(actual))
rescue Capybara::ExpectationNotMet => e
@failure_message = e.message
return false
end
def does_not_match?(actual)
yield(wrap(actual))
rescue Capybara::ExpectationNotMet => e
@failure_message_when_negated = e.message
return false
end
end
class HaveSelector < Matcher
@ -22,17 +36,11 @@ module Capybara
end
def matches?(actual)
wrap(actual).assert_selector(*@args)
rescue Capybara::ExpectationNotMet => e
@failure_message = e.message
return false
super(actual){ |el| el.assert_selector(*@args) }
end
def does_not_match?(actual)
wrap(actual).assert_no_selector(*@args)
rescue Capybara::ExpectationNotMet => e
@failure_message_when_negated = e.message
return false
super(actual){ |el| el.assert_no_selector(*@args) }
end
def description
@ -63,17 +71,11 @@ module Capybara
end
def matches?(actual)
wrap(actual).assert_text(*@args)
rescue Capybara::ExpectationNotMet => e
@failure_message = e.message
return false
super(actual) { |el| el.assert_text(*@args) }
end
def does_not_match?(actual)
wrap(actual).assert_no_text(*@args)
rescue Capybara::ExpectationNotMet => e
@failure_message_when_negated = e.message
return false
super(actual) { |el| el.assert_no_text(*@args) }
end
def description
@ -103,17 +105,11 @@ module Capybara
end
def matches?(actual)
wrap(actual).assert_title(*@args)
rescue Capybara::ExpectationNotMet => e
@failure_message = e.message
return false
super(actual) { |el| el.assert_title(*@args) }
end
def does_not_match?(actual)
wrap(actual).assert_no_title(*@args)
rescue Capybara::ExpectationNotMet => e
@failure_message_when_negated = e.message
return false
super(actual) { |el| el.assert_no_title(*@args) }
end
def description
@ -138,17 +134,11 @@ module Capybara
end
def matches?(actual)
wrap(actual).assert_current_path(*@args)
rescue Capybara::ExpectationNotMet => e
@failure_message = e.message
return false
super(actual) { |el| el.assert_current_path(*@args) }
end
def does_not_match?(actual)
wrap(actual).assert_no_current_path(*@args)
rescue Capybara::ExpectationNotMet => e
@failure_message_when_negated = e.message
return false
super(actual) { |el| el.assert_no_current_path(*@args) }
end
def description
@ -196,17 +186,11 @@ module Capybara
end
def matches?(actual)
actual.assert_matches_selector(*@args)
rescue Capybara::ExpectationNotMet => e
@failure_message = e.message
return false
super(actual) { |el| el.assert_matches_selector(*@args) }
end
def does_not_match?(actual)
actual.assert_not_matches_selector(*@args)
rescue Capybara::ExpectationNotMet => e
@failure_message_when_negated = e.message
return false
super(actual) { |el| el.assert_not_matches_selector(*@args) }
end
def description

View File

@ -588,11 +588,7 @@ module Capybara
# @raise [Capybara::ModalNotFound] if modal dialog hasn't been found
#
def accept_alert(text_or_options=nil, options={}, &blk)
text_or_options, options = nil, text_or_options if text_or_options.is_a?(Hash)
options[:text] ||= text_or_options unless text_or_options.nil?
options[:wait] ||= Capybara.default_max_wait_time
driver.accept_modal(:alert, options, &blk)
accept_modal(:alert, text_or_options, options, &blk)
end
##
@ -602,11 +598,7 @@ module Capybara
# @macro modal_params
#
def accept_confirm(text_or_options=nil, options={}, &blk)
text_or_options, options = nil, text_or_options if text_or_options.is_a?(Hash)
options[:text] ||= text_or_options unless text_or_options.nil?
options[:wait] ||= Capybara.default_max_wait_time
driver.accept_modal(:confirm, options, &blk)
accept_modal(:confirm, text_or_options, options, &blk)
end
##
@ -616,11 +608,7 @@ module Capybara
# @macro modal_params
#
def dismiss_confirm(text_or_options=nil, options={}, &blk)
text_or_options, options = nil, text_or_options if text_or_options.is_a?(Hash)
options[:text] ||= text_or_options unless text_or_options.nil?
options[:wait] ||= Capybara.default_max_wait_time
driver.dismiss_modal(:confirm, options, &blk)
dismiss_modal(:confirm, text_or_options, options, &blk)
end
##
@ -631,11 +619,7 @@ module Capybara
# @option options [String] :with Response to provide to the prompt
#
def accept_prompt(text_or_options=nil, options={}, &blk)
text_or_options, options = nil, text_or_options if text_or_options.is_a?(Hash)
options[:text] ||= text_or_options unless text_or_options.nil?
options[:wait] ||= Capybara.default_max_wait_time
driver.accept_modal(:prompt, options, &blk)
accept_modal(:prompt, text_or_options, options, &blk)
end
##
@ -645,11 +629,7 @@ module Capybara
# @macro modal_params
#
def dismiss_prompt(text_or_options=nil, options={}, &blk)
text_or_options, options = nil, text_or_options if text_or_options.is_a?(Hash)
options[:text] ||= text_or_options unless text_or_options.nil?
options[:wait] ||= Capybara.default_max_wait_time
driver.dismiss_modal(:prompt, options, &blk)
dismiss_modal(:prompt, text_or_options, options, &blk)
end
##
@ -752,6 +732,22 @@ module Capybara
end
private
def accept_modal(type, text_or_options, options, &blk)
text_or_options, options = nil, text_or_options if text_or_options.is_a?(Hash)
options[:text] ||= text_or_options unless text_or_options.nil?
options[:wait] ||= Capybara.default_max_wait_time
driver.accept_modal(type, options, &blk)
end
def dismiss_modal(type, text_or_options, options, &blk)
text_or_options, options = nil, text_or_options if text_or_options.is_a?(Hash)
options[:text] ||= text_or_options unless text_or_options.nil?
options[:wait] ||= Capybara.default_max_wait_time
driver.dismiss_modal(type, options, &blk)
end
def open_file(path)
begin