class SeleniumSurfer::SearchContext
### WebDriver Element wrapper
Provides jQuery-like access to elements.
Constants
- TIMEOUT
Public Class Methods
new(_elements, _parent)
click to toggle source
# File lib/selenium_surfer/search_context.rb, line 15 def initialize(_elements, _parent) @elements = _elements @parent = _parent end
Public Instance Methods
explode(&_block)
click to toggle source
yield individual SearchContext
for each element contained in this result
# File lib/selenium_surfer/search_context.rb, line 35 def explode(&_block) return enum_for(__method__) if _block.nil? context.each do |el| _block.call SearchContext.new([el], self) end end
fill(_value)
click to toggle source
clears and sends_keys to this context main element
# File lib/selenium_surfer/search_context.rb, line 77 def fill(_value) raise EmptySetError.new('Cannot call \'fill\' on an empty set', self) if empty? wrap_errors do context.first.clear context.first.send_keys _value end end
method_missing(_method, *_args, &_block)
click to toggle source
Any methods missing are forwarded to the main element (first).
# File lib/selenium_surfer/search_context.rb, line 86 def method_missing(_method, *_args, &_block) wrap_errors do m = /^(.*)_all$/.match _method.to_s if m then return [] if empty? context.map { |e| e.send(m[1], *_args, &_block) } else raise EmptySetError.new("Cannot call '#{_method}' on an empty set", self) if empty? context.first.send(_method, *_args, &_block) end end end
parent_context()
click to toggle source
return the context's parent context
# File lib/selenium_surfer/search_context.rb, line 27 def parent_context @parent end
respond_to?(_method, _include_all=false)
click to toggle source
Calls superclass method
# File lib/selenium_surfer/search_context.rb, line 99 def respond_to?(_method, _include_all=false) return true if super m = /^.*_all$/.match _method.to_s if m then return true if empty? context.first.respond_to? m[1], _include_all else return true if empty? context.first.respond_to? _method, _include_all end end
root_context()
click to toggle source
return the context's root context
# File lib/selenium_surfer/search_context.rb, line 21 def root_context return @parent.root_context if @parent self end
search(_selector=nil, _options={})
click to toggle source
searches for elements that match a given selector
# File lib/selenium_surfer/search_context.rb, line 43 def search(_selector=nil, _options={}) _options[:css] = _selector if _selector wait_mode = _options.delete :wait if wait_mode # retrieve timeout timeout = _options.delete :timeout timeout = TIMEOUT if timeout.nil? # use a selenium timeout wait = Selenium::WebDriver::Wait.new(timeout: timeout) wait.until do new_elements = search_elements _options # test wait condition ok = case wait_mode when :present then (new_elements.length > 0) when :visible then (new_elements.length > 0 and new_elements.first.displayed?) when :enabled then (new_elements.length > 0 and new_elements.first.displayed? and new_elements.first.enabled?) when :not_present then (new_elements.length == 0) when :not_visible then (not new_elements.any? { |e| e.displayed? }) else raise SetupError.new "Invalid wait mode '#{wait_mode}'" end SearchContext.new new_elements, self if ok end else SearchContext.new search_elements(_options), self end end
Private Instance Methods
context()
click to toggle source
returns the current context
# File lib/selenium_surfer/search_context.rb, line 132 def context @elements end
search_elements(_options)
click to toggle source
base filtering method, expands current context
# File lib/selenium_surfer/search_context.rb, line 123 def search_elements(_options) wrap_errors do context.inject([]) do |r, element| r + element.find_elements(_options) end end end
wrap_errors() { || ... }
click to toggle source
wrap every selenium errors that happen inside block.
# File lib/selenium_surfer/search_context.rb, line 114 def wrap_errors begin yield rescue Selenium::WebDriver::Error::WebDriverError => e raise WebDriverError.new e, self end end