Skip to content
This repository has been archived by the owner on Jul 30, 2020. It is now read-only.

Commit

Permalink
Allow blocks for searches and polling methods
Browse files Browse the repository at this point in the history
  • Loading branch information
ferrous26 committed Mar 13, 2012
1 parent 466d6d0 commit 521a3d4
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
14 changes: 7 additions & 7 deletions lib/accessibility/dsl.rb
Expand Up @@ -374,11 +374,11 @@ def unregister_notifications
# @options opts [AX::Element] :parent
# @options opts [AX::Element] :ancestor
# @return [AX::Element,nil]
def wait_for element, opts = {}
def wait_for element, opts = {}, &block
if opts.has_key? :ancestor
wait_for_descendant element, opts.delete(:ancestor), opts
wait_for_descendant element, opts.delete(:ancestor), opts, &block
elsif opts.has_key? :parent
wait_for_child element, opts.delete(:parent), opts
wait_for_child element, opts.delete(:parent), opts, &block
else
raise ArgumentError, 'parent/ancestor opt required'
end
Expand All @@ -393,11 +393,11 @@ def wait_for element, opts = {}
# @param [AX::Element]
# @param [Hash]
# @return [AX::Element,nil]
def wait_for_descendant descendant, ancestor, opts
def wait_for_descendant descendant, ancestor, opts, &block
timeout = opts.delete(:timeout) || 15
start = Time.now
until Time.now - start > timeout
result = ancestor.search(descendant, opts)
result = ancestor.search(descendant, opts, &block)
return result unless result.blank?
sleep 0.2
end
Expand All @@ -419,10 +419,10 @@ def wait_for_descendant descendant, ancestor, opts
# @param [AX::Element]
# @param [Hash]
# @return [AX::Element,nil]
def wait_for_child child, parent, opts
def wait_for_child child, parent, opts, &block
timeout = opts.delete(:timeout) || 15
start = Time.now
q = Accessibility::Qualifier.new(child.classify, opts)
q = Accessibility::Qualifier.new(child.classify, opts, &block)
until Time.now - start > timeout
result = parent.attribute(:children).find { |x| q.qualifies? x }
return result unless result.blank?
Expand Down
9 changes: 6 additions & 3 deletions lib/ax/element.rb
Expand Up @@ -244,7 +244,8 @@ def search kind, filters = {}
kind = kind.camelize
klass = kind.singularize
search = klass == kind ? :find : :find_all
qualifier = Accessibility::Qualifier.new(klass, filters)
block = block_given? ? Proc.new : nil
qualifier = Accessibility::Qualifier.new(klass, filters, &block)
tree = Accessibility::Enumerators::BreadthFirst.new(self)

tree.send(search) { |element| qualifier.qualifies? element }
Expand All @@ -265,7 +266,8 @@ def search kind, filters = {}
# @param [Hash{Symbol=>Object}]
# @return [AX::Element]
def ancestor kind, filters = {}
qualifier = Accessibility::Qualifier.new(kind.camelize, filters)
block = block_given? ? Proc.new : nil
qualifier = Accessibility::Qualifier.new(kind.camelize, filters, &block)
element = attribute :parent
until qualifier.qualifies? element
element = element.attribute :parent
Expand Down Expand Up @@ -330,7 +332,8 @@ def method_missing method, *args
end

if @attrs.include? KAXChildrenAttribute
result = search method, *args
block = block_given? ? Proc.new : nil
result = search method, args.last, &block
return result unless result.blank?
raise Accessibility::SearchFailure.new(self, method, args.first)
end
Expand Down
3 changes: 2 additions & 1 deletion lib/ax/row.rb
Expand Up @@ -28,7 +28,8 @@ class AX::Row < AX::Element
# @param [Hash]
# @return [AX::Element]
def child_in_column filters
qualifier = Accessibility::Qualifier.new(:Column, filters)
block = block_given? ? Proc.new : nil
qualifier = Accessibility::Qualifier.new(:Column, filters, &block)
column = self.parent.columns.index { |x| qualifier.qualifies? x }
return self.children.at(column) if column
raise Accessibility::SearchFailure.new(self.parent, 'column', filters)
Expand Down

0 comments on commit 521a3d4

Please sign in to comment.