2010-11-21 08:37:36 -05:00
module Capybara
module Node
##
#
# A {Capybara::Node::Base} represents either an element on a page through the subclass
# {Capybara::Node::Element} or a document through {Capybara::Node::Document}.
#
# Both types of Node share the same methods, used for interacting with the
# elements on the page. These methods are divided into three categories,
# finders, actions and matchers. These are found in the modules
# {Capybara::Node::Finders}, {Capybara::Node::Actions} and {Capybara::Node::Matchers}
# respectively.
#
# A {Capybara::Session} exposes all methods from {Capybara::Node::Document} directly:
#
# session = Capybara::Session.new(:rack_test, my_app)
# session.visit('/')
# session.fill_in('Foo', :with => 'Bar') # from Capybara::Node::Actions
# bar = session.find('#bar') # from Capybara::Node::Finders
# bar.select('Baz', :from => 'Quox') # from Capybara::Node::Actions
# session.has_css?('#foobar') # from Capybara::Node::Matchers
#
class Base
2011-07-13 09:39:17 -04:00
attr_reader :session , :base , :parent
2010-11-21 08:37:36 -05:00
include Capybara :: Node :: Finders
include Capybara :: Node :: Actions
include Capybara :: Node :: Matchers
def initialize ( session , base )
@session = session
@base = base
end
2012-02-01 08:16:17 -05:00
# overridden in subclasses, e.g. Capybara::Node::Element
2011-07-13 09:39:17 -04:00
def reload
self
end
2012-07-13 14:19:23 -04:00
##
#
# This method is Capybara's primary defence agains asynchonicity
# problems. It works by attempting to run a given block of code until it
# succeeds. The exact behaviour of this method depends on a number of
# factors. Principality there are certain exceptions which, when raised
# from the block, instead of bubbling up, are caught, and the block is
# re-run.
#
# Certain drivers, such as RackTest, have no support for aynchronous
# processes, these drivers run the block, and any error raised bubbles up
# immediately. This allows faster turn around in the case where an
# expectation fails.
#
# Only exceptions that are {Capybara::ElementNotFound} or any subclass
# thereof cause the block to be rerun. Drivers may specify additional
# exceptions which also cause reruns. This usually occurs when a node is
# manipulated which no longer exists on the page. For example, the
# Selenium driver specifies
# `Selenium::WebDriver::Error::ObsoleteElementError`.
#
# As long as any of these exceptions are thrown, the block is re-run,
# until a certain amount of time passes. The amount of time defaults to
# {Capybara.default_wait_time} and can be overriden through the `seconds`
# argument. This time comparison the system time to see how much time has
# passed. If the return value of {Time.now} is stubbed out, Capybara will
# raise `Capybara::FrozenInTime`.
#
# @param [Integer] seconds Number of seconds to retry this block
# @return [Object] The result of the given block
#
2012-02-01 08:16:17 -05:00
def synchronize ( seconds = Capybara . default_wait_time )
2012-07-12 10:21:43 -04:00
start_time = Time . now
2011-08-12 07:52:12 -04:00
begin
yield
rescue = > e
2011-08-12 08:38:42 -04:00
raise e unless driver . wait?
2012-02-01 09:12:15 -05:00
raise e unless driver . invalid_element_errors . include? ( e . class ) or e . is_a? ( Capybara :: ElementNotFound )
2012-07-12 10:21:43 -04:00
raise e if ( Time . now - start_time ) > = seconds
2011-08-12 07:52:12 -04:00
sleep ( 0 . 05 )
2012-07-12 10:21:43 -04:00
raise Capybara :: FrozenInTime , " time appears to be frozen, Capybara does not work with libraries which freeze time, consider using time travelling instead " if Time . now == start_time
2011-08-12 07:52:12 -04:00
reload if Capybara . automatic_reload
retry
end
end
2012-02-01 08:16:17 -05:00
protected
2010-11-21 08:37:36 -05:00
def driver
session . driver
end
end
end
end