Class: SeleniumSurfer::SearchContext
- Inherits:
-
Object
- Object
- SeleniumSurfer::SearchContext
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/selenium_surfer/search_context.rb
Overview
### WebDriver Element wrapper
Provides jQuery-like access to elements.
Direct Known Subclasses
Constant Summary collapse
- TIMEOUT =
Default timeout for waiting operations
10.0
Instance Method Summary collapse
-
#explode(&_block) ⇒ Object
yield individual SearchContext for each element contained in this result.
-
#fill(_value) ⇒ Object
clears and sends_keys to this context main element.
-
#initialize(_elements, _parent) ⇒ SearchContext
constructor
A new instance of SearchContext.
-
#method_missing(_method, *_args, &_block) ⇒ Object
Any methods missing are forwarded to the main element (first).
-
#parent_context ⇒ Object
return the context’s parent context.
- #respond_to?(_method, _include_all = false) ⇒ Boolean
-
#root_context ⇒ Object
return the context’s root context.
-
#search(_selector = nil, _options = {}) ⇒ Object
searches for elements that match a given selector.
Constructor Details
#initialize(_elements, _parent) ⇒ SearchContext
Returns a new instance of SearchContext.
15 16 17 18 |
# File 'lib/selenium_surfer/search_context.rb', line 15 def initialize(_elements, _parent) @elements = _elements @parent = _parent end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(_method, *_args, &_block) ⇒ Object
Any methods missing are forwarded to the main element (first).
86 87 88 89 90 91 92 93 94 95 96 97 |
# 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 |
Instance Method Details
#explode(&_block) ⇒ Object
yield individual SearchContext for each element contained in this result
35 36 37 38 39 40 |
# 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) ⇒ Object
clears and sends_keys to this context main element
77 78 79 80 81 82 83 |
# 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 |
#parent_context ⇒ Object
return the context’s parent context
27 28 29 |
# File 'lib/selenium_surfer/search_context.rb', line 27 def parent_context @parent end |
#respond_to?(_method, _include_all = false) ⇒ Boolean
99 100 101 102 103 104 105 106 107 108 109 |
# 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 ⇒ Object
return the context’s root context
21 22 23 24 |
# File 'lib/selenium_surfer/search_context.rb', line 21 def root_context return @parent.root_context if @parent self end |
#search(_selector = nil, _options = {}) ⇒ Object
searches for elements that match a given selector
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/selenium_surfer/search_context.rb', line 43 def search(_selector=nil, ={}) [:css] = _selector if _selector wait_mode = .delete :wait if wait_mode # retrieve timeout timeout = .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 # 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(), self end end |