Skip to content

Commit

Permalink
Expose Node#wait_until as Node#synchronize
Browse files Browse the repository at this point in the history
  • Loading branch information
jnicklas committed Feb 1, 2012
1 parent cc04222 commit b100d1c
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 98 deletions.
3 changes: 0 additions & 3 deletions lib/capybara/driver/base.rb
Expand Up @@ -51,9 +51,6 @@ def wait?
false
end

def wait_until(*args)
end

def reset!
end

Expand Down
7 changes: 4 additions & 3 deletions lib/capybara/node/base.rb
Expand Up @@ -33,13 +33,12 @@ def initialize(session, base)
@base = base
end

# overridden in subclasses, e.g. Capybara::Node::Element
def reload
self
end

protected

def wait_until(seconds=Capybara.default_wait_time)
def synchronize(seconds=Capybara.default_wait_time)
retries = (seconds.to_f / 0.05).round

begin
Expand All @@ -55,6 +54,8 @@ def wait_until(seconds=Capybara.default_wait_time)
end
end

protected

def driver
session.driver
end
Expand Down
36 changes: 18 additions & 18 deletions lib/capybara/node/element.rb
Expand Up @@ -33,15 +33,15 @@ def initialize(session, base, parent, selector)
# @return [Object] The native element from the driver, this allows access to driver specific methods
#
def native
wait_until { base.native }
synchronize { base.native }
end

##
#
# @return [String] The text of the element
#
def text
wait_until { base.text }
synchronize { base.text }
end

##
Expand All @@ -54,15 +54,15 @@ def text
# @return [String] The value of the attribute
#
def [](attribute)
wait_until { base[attribute] }
synchronize { base[attribute] }
end

##
#
# @return [String] The value of the form element
#
def value
wait_until { base.value }
synchronize { base.value }
end

##
Expand All @@ -72,39 +72,39 @@ def value
# @param [String] value The new value
#
def set(value)
wait_until { base.set(value) }
synchronize { base.set(value) }
end

##
#
# Select this node if is an option element inside a select tag
#
def select_option
wait_until { base.select_option }
synchronize { base.select_option }
end

##
#
# Unselect this node if is an option element inside a multiple select tag
#
def unselect_option
wait_until { base.unselect_option }
synchronize { base.unselect_option }
end

##
#
# Click the Element
#
def click
wait_until { base.click }
synchronize { base.click }
end

##
#
# @return [String] The tag name of the element
#
def tag_name
wait_until { base.tag_name }
synchronize { base.tag_name }
end

##
Expand All @@ -115,7 +115,7 @@ def tag_name
# @return [Boolean] Whether the element is visible
#
def visible?
wait_until { base.visible? }
synchronize { base.visible? }
end

##
Expand All @@ -125,7 +125,7 @@ def visible?
# @return [Boolean] Whether the element is checked
#
def checked?
wait_until { base.checked? }
synchronize { base.checked? }
end

##
Expand All @@ -135,7 +135,7 @@ def checked?
# @return [Boolean] Whether the element is selected
#
def selected?
wait_until { base.selected? }
synchronize { base.selected? }
end

##
Expand All @@ -145,7 +145,7 @@ def selected?
# @return [String] An XPath expression
#
def path
wait_until { base.path }
synchronize { base.path }
end

##
Expand All @@ -156,7 +156,7 @@ def path
# @param [String] event The name of the event to trigger
#
def trigger(event)
wait_until { base.trigger(event) }
synchronize { base.trigger(event) }
end

##
Expand All @@ -170,19 +170,19 @@ def trigger(event)
# @param [Capybara::Element] node The element to drag to
#
def drag_to(node)
wait_until { base.drag_to(node.base) }
synchronize { base.drag_to(node.base) }
end

def find(*args)
wait_until { super }
synchronize { super }
end

def first(*args)
wait_until { super }
synchronize { super }
end

def all(*args)
wait_until { super }
synchronize { super }
end

def reload
Expand Down
2 changes: 1 addition & 1 deletion lib/capybara/node/finders.rb
Expand Up @@ -24,7 +24,7 @@ module Finders
# @raise [Capybara::ElementNotFound] If the element can't be found before time expires
#
def find(*args)
wait_until do
synchronize do
results = all(*args)
raise_find_error(*args) unless results.length == 1
results.first
Expand Down
8 changes: 4 additions & 4 deletions lib/capybara/node/matchers.rb
Expand Up @@ -34,7 +34,7 @@ module Matchers
#
def has_selector?(*args)
options = if args.last.is_a?(Hash) then args.last else {} end
wait_until do
synchronize do
results = all(*args)
query(*args).matches_count?(results) or raise Capybara::ExpectationNotMet
results
Expand All @@ -53,7 +53,7 @@ def has_selector?(*args)
#
def has_no_selector?(*args)
options = if args.last.is_a?(Hash) then args.last else {} end
wait_until do
synchronize do
results = all(*args)
query(*args).matches_count?(results) and raise Capybara::ExpectationNotMet
results
Expand Down Expand Up @@ -160,7 +160,7 @@ def has_no_css?(path, options={})
def has_text?(content)
normalized_content = normalize_whitespace(content)

wait_until do
synchronize do
normalize_whitespace(text).include?(normalized_content) or
raise ExpectationNotMet
end
Expand All @@ -183,7 +183,7 @@ def has_text?(content)
def has_no_text?(content)
normalized_content = normalize_whitespace(content)

wait_until do
synchronize do
!normalize_whitespace(text).include?(normalized_content) or
raise ExpectationNotMet
end
Expand Down
8 changes: 4 additions & 4 deletions lib/capybara/node/simple.rb
Expand Up @@ -118,15 +118,15 @@ def selected?
native[:selected]
end

def synchronize
yield # simple nodes don't need to wait
end

protected

def find_in_base(query)
native.xpath(query.xpath).map { |node| self.class.new(node) }
end

def wait_until
yield # simple nodes don't need to wait
end
end
end
end
12 changes: 1 addition & 11 deletions lib/capybara/session.rb
Expand Up @@ -40,7 +40,7 @@ class Session
]
SESSION_METHODS = [
:body, :html, :current_url, :current_host, :evaluate_script, :source,
:visit, :wait_until, :within, :within_fieldset, :within_table,
:visit, :within, :within_fieldset, :within_table,
:within_frame, :within_window, :current_path, :save_page,
:save_and_open_page, :reset_session!
]
Expand Down Expand Up @@ -244,16 +244,6 @@ def within_window(handle, &blk)
driver.within_window(handle, &blk)
end

##
#
# Retry executing the block until a truthy result is returned or the timeout time is exceeded
#
# @param [Integer] timeout The amount of seconds to retry executing the given block
#
def wait_until(timeout = Capybara.default_wait_time)
Capybara.timeout(timeout,driver) { yield }
end

##
#
# Execute the given script, not returning a result. This is useful for scripts that return
Expand Down
52 changes: 0 additions & 52 deletions lib/capybara/spec/session/javascript.rb
Expand Up @@ -133,58 +133,6 @@
end
end

describe '#wait_until' do
before do
@default_timeout = Capybara.default_wait_time
end

after do
Capybara.default_wait_time = @default_wait_time
end

it "should wait for block to return true" do
@session.visit('/with_js')
@session.select('My Waiting Option', :from => 'waiter')
@session.evaluate_script('activeRequests == 1').should be_true
@session.wait_until do
@session.evaluate_script('activeRequests == 0')
end
@session.evaluate_script('activeRequests == 0').should be_true
end

it "should raise Capybara::TimeoutError if block doesn't return true within timeout" do
@session.visit('/with_html')
Proc.new do
@session.wait_until(0.1) do
@session.all('//div[@id="nosuchthing"]').first
end
end.should raise_error(::Capybara::TimeoutError)
end

it "should accept custom timeout in seconds" do
start = Time.now
Capybara.default_wait_time = 5
begin
@session.wait_until(0.1) { false }
rescue Capybara::TimeoutError; end
(Time.now - start).should be_within(0.1).of(0.1)
end

it "should default to Capybara.default_wait_time before timeout" do
@session.driver # init the driver to exclude init timing from test
start = Time.now
Capybara.default_wait_time = 0.2
begin
@session.wait_until { false }
rescue Capybara::TimeoutError; end
if @session.driver.has_shortcircuit_timeout?
(Time.now - start).should be_within(0.1).of(0)
else
(Time.now - start).should be_within(0.1).of(0.2)
end
end
end

describe '#click_link_or_button' do
it "should wait for asynchronous load" do
@session.visit('/with_js')
Expand Down
2 changes: 0 additions & 2 deletions lib/capybara/util/timeout.rb
Expand Up @@ -17,8 +17,6 @@ def timeout(seconds = 1, driver = nil, error_message = nil, &block)
raise TimeoutError, error_message || "timed out"
end

driver && driver.wait_until(delay)

sleep(0.05)
end
end
Expand Down

0 comments on commit b100d1c

Please sign in to comment.