Class: Capybara::Node::Base
- Inherits:
-
Object
- Object
- Capybara::Node::Base
- Defined in:
- lib/capybara/node/base.rb
Overview
A Base represents either an element on a page through the subclass Element or a document through 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 Finders, Actions and Matchers respectively.
A Session exposes all methods from Document directly:
session = Capybara::Session.new(:rack_test, my_app)
session.visit('/')
session.fill_in('Foo', :with => 'Bar') # from Capybara::Node::Actions
= session.find('#bar') # from Capybara::Node::Finders
.select('Baz', :from => 'Quox') # from Capybara::Node::Actions
session.has_css?('#foobar') # from Capybara::Node::Matchers
Instance Attribute Summary collapse
-
#base ⇒ Object
readonly
Returns the value of attribute base.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#session ⇒ Object
readonly
Returns the value of attribute session.
Instance Method Summary collapse
-
#initialize(session, base) ⇒ Base
constructor
A new instance of Base.
-
#reload ⇒ Object
overridden in subclasses, e.g.
-
#synchronize(seconds = Capybara.default_wait_time) ⇒ Object
This method is Capybara’s primary defence agains asynchronicity problems.
-
#unsynchronized ⇒ Object
private
Within the given block, prevent synchronize from having any effect.
Methods included from Matchers
#==, #assert_no_selector, #assert_selector, #has_button?, #has_checked_field?, #has_css?, #has_field?, #has_link?, #has_no_button?, #has_no_checked_field?, #has_no_css?, #has_no_field?, #has_no_link?, #has_no_select?, #has_no_selector?, #has_no_table?, #has_no_text?, #has_no_unchecked_field?, #has_no_xpath?, #has_select?, #has_selector?, #has_table?, #has_text?, #has_unchecked_field?, #has_xpath?
Methods included from Actions
#attach_file, #check, #choose, #click_button, #click_link, #click_link_or_button, #fill_in, #select, #uncheck, #unselect
Methods included from Finders
#all, #find, #find_button, #find_by_id, #find_field, #find_link, #first
Constructor Details
#initialize(session, base) ⇒ Base
Returns a new instance of Base.
31 32 33 34 35 |
# File 'lib/capybara/node/base.rb', line 31 def initialize(session, base) @session = session @base = base @unsynchronized = false end |
Instance Attribute Details
#base ⇒ Object (readonly)
Returns the value of attribute base.
25 26 27 |
# File 'lib/capybara/node/base.rb', line 25 def base @base end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
25 26 27 |
# File 'lib/capybara/node/base.rb', line 25 def parent @parent end |
#session ⇒ Object (readonly)
Returns the value of attribute session.
25 26 27 |
# File 'lib/capybara/node/base.rb', line 25 def session @session end |
Instance Method Details
#reload ⇒ Object
overridden in subclasses, e.g. Capybara::Node::Element
38 39 40 |
# File 'lib/capybara/node/base.rb', line 38 def reload self end |
#synchronize(seconds = Capybara.default_wait_time) ⇒ Object
This method is Capybara’s primary defence agains asynchronicity 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. Basically 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 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 is compared with 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`.
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/capybara/node/base.rb', line 74 def synchronize(seconds=Capybara.default_wait_time) start_time = Time.now begin yield rescue => e raise e if @unsynchronized raise e unless driver.wait? raise e unless driver.invalid_element_errors.include?(e.class) || e.is_a?(Capybara::ElementNotFound) raise e if (Time.now - start_time) >= seconds sleep(0.05) 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 reload if Capybara.automatic_reload retry end end |
#unsynchronized ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Within the given block, prevent synchronize from having any effect.
This is an internal method which should not be called unless you are absolutely sure of what you’re doing.
101 102 103 104 105 106 107 |
# File 'lib/capybara/node/base.rb', line 101 def unsynchronized orig = @unsynchronized @unsynchronized = true yield ensure @unsynchronized = orig end |