lib/capybara/rspec/matchers.rb in capybara-2.2.1 vs lib/capybara/rspec/matchers.rb in capybara-2.3.0

- old
+ new

@@ -49,20 +49,24 @@ def does_not_match?(actual) @actual = wrap(actual) @actual.has_no_text?(type, content, options) end - def failure_message_for_should + def failure_message message = Capybara::Helpers.failure_message(description, options) message << " in #{format(@actual.text(type))}" message end - def failure_message_for_should_not - failure_message_for_should.sub(/(to find)/, 'not \1') + def failure_message_when_negated + failure_message.sub(/(to find)/, 'not \1') end + # RSpec 2 compatibility: + alias_method :failure_message_for_should, :failure_message + alias_method :failure_message_for_should_not, :failure_message_when_negated + def description "text #{format(content)}" end def format(content) @@ -86,23 +90,54 @@ def does_not_match?(actual) @actual = wrap(actual) @actual.has_no_title?(title) end - def failure_message_for_should + def failure_message "expected there to be title #{title.inspect} in #{@actual.title.inspect}" end - def failure_message_for_should_not + def failure_message_when_negated "expected there not to be title #{title.inspect} in #{@actual.title.inspect}" end + # RSpec 2 compatibility: + alias_method :failure_message_for_should, :failure_message + alias_method :failure_message_for_should_not, :failure_message_when_negated + def description "have title #{title.inspect}" end end + class BecomeClosed + def initialize(options) + @wait_time = Capybara::Query.new(options).wait + end + + def matches?(window) + @window = window + start_time = Time.now + while window.exists? && (Time.now - start_time) < @wait_time + sleep 0.05 + end + window.closed? + end + + def failure_message + "expected #{@window.inspect} to become closed after #{@wait_time} seconds" + end + + def failure_message_when_negated + "expected #{@window.inspect} not to become closed after #{@wait_time} seconds" + end + + # RSpec 2 compatibility: + alias_method :failure_message_for_should, :failure_message + alias_method :failure_message_for_should_not, :failure_message_when_negated + end + def have_selector(*args) HaveSelector.new(*args) end def have_xpath(xpath, options={}) @@ -146,8 +181,18 @@ HaveSelector.new(:select, locator, options) end def have_table(locator, options={}) HaveSelector.new(:table, locator, options) + end + + ## + # Wait for window to become closed. + # @example + # expect(window).to become_closed(wait: 0.8) + # @param options [Hash] optional param + # @option options [Numeric] :wait (Capybara.default_wait_time) wait time + def become_closed(options = {}) + BecomeClosed.new(options) end end end