Class: Crabfarm::Dsl::Surfer::SearchContext
- Inherits:
-
Object
- Object
- Crabfarm::Dsl::Surfer::SearchContext
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/crabfarm/dsl/surfer/search_context.rb
Direct Known Subclasses
Constant Summary collapse
- TIMEOUT =
Default timeout for waiting operations
10.0
Instance Attribute Summary collapse
-
#elements ⇒ Object
Returns the value of attribute elements.
-
#parent ⇒ Object
Returns the value of attribute parent.
Instance Method Summary collapse
- #[](*args) ⇒ Object
- #classes ⇒ Object
- #each ⇒ Object
- #element! ⇒ Object
-
#fill(_value) ⇒ Object
clears and sends_keys to this context main element.
- #first ⇒ Object
-
#initialize(_elements, _parent) ⇒ SearchContext
constructor
A new instance of SearchContext.
- #last ⇒ Object
-
#method_missing(_method, *_args, &_block) ⇒ Object
Any methods missing are forwarded to the main element (first).
- #respond_to?(_method, _include_all = false) ⇒ Boolean
- #root ⇒ Object
-
#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.
14 15 16 17 |
# File 'lib/crabfarm/dsl/surfer/search_context.rb', line 14 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).
97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/crabfarm/dsl/surfer/search_context.rb', line 97 def method_missing(_method, *_args, &_block) wrap_errors do m = /^(.*)_all$/.match _method.to_s if m then return [] if empty? elements.map { |e| e.send(m[1], *_args, &_block) } else element!.send(_method, *_args, &_block) end end end |
Instance Attribute Details
#elements ⇒ Object
Returns the value of attribute elements.
10 11 12 |
# File 'lib/crabfarm/dsl/surfer/search_context.rb', line 10 def elements @elements end |
#parent ⇒ Object
Returns the value of attribute parent.
10 11 12 |
# File 'lib/crabfarm/dsl/surfer/search_context.rb', line 10 def parent @parent end |
Instance Method Details
#[](*args) ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/crabfarm/dsl/surfer/search_context.rb', line 27 def [](*args) if args[0].is_a? String or args[0].is_a? Symbol attribute args[0] else child_context Array(elements.send(:[],*args)) end end |
#classes ⇒ Object
48 49 50 |
# File 'lib/crabfarm/dsl/surfer/search_context.rb', line 48 def classes wrap_errors { (element!['class'] || '').split(' ') } end |
#each ⇒ Object
23 24 25 |
# File 'lib/crabfarm/dsl/surfer/search_context.rb', line 23 def each elements.each { |el| yield child_context [el] } end |
#element! ⇒ Object
43 44 45 46 |
# File 'lib/crabfarm/dsl/surfer/search_context.rb', line 43 def element! raise EmptySetError.new("This set is empty", self) if empty? elements.first end |
#fill(_value) ⇒ Object
clears and sends_keys to this context main element
89 90 91 92 93 94 |
# File 'lib/crabfarm/dsl/surfer/search_context.rb', line 89 def fill(_value) wrap_errors do element!.clear element!.send_keys _value end end |
#first ⇒ Object
35 36 37 |
# File 'lib/crabfarm/dsl/surfer/search_context.rb', line 35 def first if elements.first.nil? then nil else child_context [elements.first] end end |
#last ⇒ Object
39 40 41 |
# File 'lib/crabfarm/dsl/surfer/search_context.rb', line 39 def last if elements.last.nil? then nil else child_context [elements.last] end end |
#respond_to?(_method, _include_all = false) ⇒ Boolean
109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/crabfarm/dsl/surfer/search_context.rb', line 109 def respond_to?(_method, _include_all=false) return true if super m = /^.*_all$/.match _method.to_s if m then return true if empty? elements.first.respond_to? m[1], _include_all else return true if empty? elements.first.respond_to? _method, _include_all end end |
#root ⇒ Object
19 20 21 |
# File 'lib/crabfarm/dsl/surfer/search_context.rb', line 19 def root @parent.root end |
#search(_selector = nil, _options = {}) ⇒ Object
searches for elements that match a given selector
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/crabfarm/dsl/surfer/search_context.rb', line 53 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 wrap_errors do 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 child_context new_elements if ok end end else child_context search_elements() end end |