Class: Pincers::Core::SearchContext

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable, Extension::Actions, Extension::Labs, Extension::Queries
Defined in:
lib/pincers/core/search_context.rb

Direct Known Subclasses

RootContext

Constant Summary

Constants included from Extension::Queries

Extension::Queries::TEXT_INPUTS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Extension::Labs

#readonly

Methods included from Extension::Actions

#set

Methods included from Extension::Queries

#checked, #classes, #download, #id, #input_mode, #selected, #value

Constructor Details

#initialize(_elements, _parent, _query) ⇒ SearchContext

Returns a new instance of SearchContext.



20
21
22
23
24
25
26
# File 'lib/pincers/core/search_context.rb', line 20

def initialize(_elements, _parent, _query)
  @elements = _elements
  @scope = if @elements.nil? then nil else :all end
  @parent = _parent
  @query = _query
  @waiting = false
end

Instance Attribute Details

#parentObject (readonly)

Returns the value of attribute parent.



16
17
18
# File 'lib/pincers/core/search_context.rb', line 16

def parent
  @parent
end

#queryObject (readonly)

Returns the value of attribute query.



16
17
18
# File 'lib/pincers/core/search_context.rb', line 16

def query
  @query
end

Instance Method Details

#[](*args) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/pincers/core/search_context.rb', line 71

def [](*args)
  if args[0].is_a? String or args[0].is_a? Symbol
    attribute args[0]
  else
    wrap_siblings Array(elements.send(:[],*args))
  end
end

#[]=(key, value) ⇒ Object



79
80
81
# File 'lib/pincers/core/search_context.rb', line 79

def []=(key, value)
  attribute key, value
end

#attribute(_name, _value = nil) ⇒ Object



107
108
109
110
111
112
113
114
115
# File 'lib/pincers/core/search_context.rb', line 107

def attribute(_name, _value=nil)
  wrap_errors do
    if _value.nil?
      backend.extract_element_attribute element!, _name
    else
      backend.set_element_attribute element!, _name, _value
    end
  end
end

#backendObject



40
41
42
# File 'lib/pincers/core/search_context.rb', line 40

def backend
  root.backend
end

#click(*_modifiers) ⇒ Object



141
142
143
# File 'lib/pincers/core/search_context.rb', line 141

def click(*_modifiers)
  perform_action { |el| backend.click_on_element el, _modifiers }
end

#double_clickObject



149
150
151
# File 'lib/pincers/core/search_context.rb', line 149

def double_click
  perform_action { |el| backend.double_click_on_element el }
end

#drag_to(_element) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
# File 'lib/pincers/core/search_context.rb', line 157

def drag_to(_element)
  wrap_errors do
    if advanced_mode?
      wait_actionable
      _element.wait_actionable
    end

    backend.drag_and_drop element!, _element.element!
  end
  self
end

#eachObject



67
68
69
# File 'lib/pincers/core/search_context.rb', line 67

def each
  elements.each { |el| yield wrap_siblings [el] }
end

#elementObject



49
50
51
52
# File 'lib/pincers/core/search_context.rb', line 49

def element
  reload_elements :single
  @elements.first
end

#element!Object



54
55
56
57
58
# File 'lib/pincers/core/search_context.rb', line 54

def element!
  wait(:present) if should_wait?
  raise Pincers::EmptySetError.new self if element.nil?
  element
end

#elementsObject



44
45
46
47
# File 'lib/pincers/core/search_context.rb', line 44

def elements
  reload_elements :all
  @elements
end

#firstObject



83
84
85
86
# File 'lib/pincers/core/search_context.rb', line 83

def first
  wait?(:present) if should_wait?
  if element.nil? then nil else wrap_siblings [element] end
end

#first!Object



88
89
90
# File 'lib/pincers/core/search_context.rb', line 88

def first!
  wrap_siblings [element!]
end

#frozen?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/pincers/core/search_context.rb', line 28

def frozen?
  !backend.javascript_enabled? || @query.nil?
end

#gotoObject

context related



191
192
193
# File 'lib/pincers/core/search_context.rb', line 191

def goto
  root.goto frame: self
end

#hoverObject



153
154
155
# File 'lib/pincers/core/search_context.rb', line 153

def hover
  perform_action { |el| backend.hover_over_element el }
end

#lastObject



92
93
94
# File 'lib/pincers/core/search_context.rb', line 92

def last
  if elements.last.nil? then nil else wrap_siblings [elements.last] end
end

#reloadObject



60
61
62
63
64
65
# File 'lib/pincers/core/search_context.rb', line 60

def reload
  raise Pincers::FrozenSetError.new self if frozen?
  parent.reload if parent_needs_reload?
  wrap_errors { reload_elements }
  self
end

#replicateObject



176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/pincers/core/search_context.rb', line 176

def replicate
  wrap_errors do
    case tag
    when 'form'
      Replicas::Form.new backend, element!
    when 'a'
      Replicas::Link.new backend, element!
    else
      raise Pincers::MissingFeatureError, "No replica avaliable for #{tag}"
    end
  end
end

#right_clickObject



145
146
147
# File 'lib/pincers/core/search_context.rb', line 145

def right_click
  perform_action { |el| backend.right_click_on_element el }
end

#rootObject



32
33
34
# File 'lib/pincers/core/search_context.rb', line 32

def root
  parent.root
end

#root?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/pincers/core/search_context.rb', line 36

def root?
  false
end

#search(_selector = nil, _options = {}, &_block) ⇒ Object



96
97
98
99
100
101
102
103
104
105
# File 'lib/pincers/core/search_context.rb', line 96

def search(_selector=nil, _options={}, &_block)
  if _selector.is_a? Hash
    _options = _selector
    _selector = nil
  end

  query = Helpers::Query.build_from_options(backend, _selector, _options, &_block)

  wrap_errors { wrap_childs query }
end

#set_text(_value) ⇒ Object

input related



137
138
139
# File 'lib/pincers/core/search_context.rb', line 137

def set_text(_value)
  perform_action { |el| backend.set_element_text el, _value }
end

#submit(&_block) ⇒ Object



169
170
171
172
173
174
# File 'lib/pincers/core/search_context.rb', line 169

def submit(&_block)
  wrap_errors do
    # _block.call(FormSetter.new _element) if _block
    backend.submit_form element!
  end
end

#tagObject



117
118
119
120
121
# File 'lib/pincers/core/search_context.rb', line 117

def tag
  wrap_errors do
    backend.extract_element_tag(element!).downcase
  end
end

#textObject



123
124
125
126
127
# File 'lib/pincers/core/search_context.rb', line 123

def text
  wrap_errors do
    elements.map { |e| backend.extract_element_text e }.join
  end
end

#to_htmlObject



129
130
131
132
133
# File 'lib/pincers/core/search_context.rb', line 129

def to_html
  wrap_errors do
    elements.map { |e| backend.extract_element_html e }.join
  end
end

#wait(_condition = nil, _options = {}, &_block) ⇒ Object



220
221
222
223
224
225
226
227
228
229
# File 'lib/pincers/core/search_context.rb', line 220

def wait(_condition=nil, _options={}, &_block)

  if _condition.is_a? Hash
    _options = _condition
    _condition = nil
  end

  raise Pincers::ConditionTimeoutError.new(self, _condition) unless wait?(_condition, _options, &_block)
  return self
end

#wait?(_condition = nil, _options = {}, &_block) ⇒ Boolean

waiting

Returns:

  • (Boolean)


197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/pincers/core/search_context.rb', line 197

def wait?(_condition=nil, _options={}, &_block)

  if _condition.is_a? Hash
    _options = _condition
    _condition = nil
  end

  poll_until(_options) do
    next ensure_block _block if _block

    case _condition
    when :present
      ensure_present
    when :actionable
      ensure_present and ensure_actionable
    when :enabled
      ensure_present and !attribute(:disabled)
    else
      ensure_present and !!attribute(_condition)
    end
  end
end