class Pincers::Core::SearchContext

Attributes

parent[R]
query[R]

Public Class Methods

new(_elements, _parent, _query) click to toggle source
# 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

Public Instance Methods

[](*args) click to toggle source
# 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) click to toggle source
# File lib/pincers/core/search_context.rb, line 79
def []=(key, value)
  attribute key, value
end
attribute(_name, _value=nil) click to toggle source
# 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
backend() click to toggle source
# File lib/pincers/core/search_context.rb, line 40
def backend
  root.backend
end
click(*_modifiers) click to toggle source
# File lib/pincers/core/search_context.rb, line 141
def click(*_modifiers)
  perform_action { |el| backend.click_on_element el, _modifiers }
end
double_click() click to toggle source
# 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) click to toggle source
# 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
each() { |wrap_siblings [el]| ... } click to toggle source
# File lib/pincers/core/search_context.rb, line 67
def each
  elements.each { |el| yield wrap_siblings [el] }
end
element() click to toggle source
# File lib/pincers/core/search_context.rb, line 49
def element
  reload_elements :single
  @elements.first
end
element!() click to toggle source
# 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
elements() click to toggle source
# File lib/pincers/core/search_context.rb, line 44
def elements
  reload_elements :all
  @elements
end
first() click to toggle source
# 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!() click to toggle source
# File lib/pincers/core/search_context.rb, line 88
def first!
  wrap_siblings [element!]
end
frozen?() click to toggle source
# File lib/pincers/core/search_context.rb, line 28
def frozen?
  !backend.javascript_enabled? || @query.nil?
end
goto() click to toggle source

context related

# File lib/pincers/core/search_context.rb, line 191
def goto
  root.goto frame: self
end
hover() click to toggle source
# File lib/pincers/core/search_context.rb, line 153
def hover
  perform_action { |el| backend.hover_over_element el }
end
last() click to toggle source
# File lib/pincers/core/search_context.rb, line 92
def last
  if elements.last.nil? then nil else wrap_siblings [elements.last] end
end
reload() click to toggle source
# 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
replicate() click to toggle source
# 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_click() click to toggle source
# File lib/pincers/core/search_context.rb, line 145
def right_click
  perform_action { |el| backend.right_click_on_element el }
end
root() click to toggle source
# File lib/pincers/core/search_context.rb, line 32
def root
  parent.root
end
root?() click to toggle source
# File lib/pincers/core/search_context.rb, line 36
def root?
  false
end
set_text(_value) click to toggle source

input related

# 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) click to toggle source
# 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
tag() click to toggle source
# File lib/pincers/core/search_context.rb, line 117
def tag
  wrap_errors do
    backend.extract_element_tag(element!).downcase
  end
end
text() click to toggle source
# 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_html() click to toggle source
# 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) click to toggle source
# 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) click to toggle source

waiting

# 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

Private Instance Methods

advanced_mode?() click to toggle source
# File lib/pincers/core/search_context.rb, line 237
def advanced_mode?
  root.advanced_mode?
end
ensure_actionable() click to toggle source
# File lib/pincers/core/search_context.rb, line 246
def ensure_actionable
  backend.element_is_actionable? element
end
ensure_block(_block) click to toggle source
# File lib/pincers/core/search_context.rb, line 250
def ensure_block(_block)
  begin
    @waiting = true
    _block.call(self) != false
  rescue Pincers::NavigationError
    return false
  ensure
    @waiting = false
  end
end
ensure_present() click to toggle source
# File lib/pincers/core/search_context.rb, line 241
def ensure_present
  reload if element.nil?
  not element.nil?
end
parent_needs_reload?() click to toggle source
# File lib/pincers/core/search_context.rb, line 288
def parent_needs_reload?
  !parent.frozen? && parent.elements.count == 0
end
perform_action() { |first| ... } click to toggle source
# File lib/pincers/core/search_context.rb, line 271
def perform_action
  wrap_errors do
    wait(:actionable) if should_wait?
    yield elements.first
  end
  self
end
poll_until(_options={}) { || ... } click to toggle source
# File lib/pincers/core/search_context.rb, line 310
def poll_until(_options={})
  timeout = _options.fetch :timeout, root.default_timeout
  interval = _options.fetch :interval, root.default_interval
  end_time = Time.now + timeout

  while Time.now <= end_time
    return true if !!yield
    sleep interval
  end

  return false
end
reload_elements(_scope=nil) click to toggle source
# File lib/pincers/core/search_context.rb, line 292
def reload_elements(_scope=nil)
  case _scope
  when :all
    return if @scope == :all
    @scope = :all
  when :single
    return unless @scope.nil?
    @scope = :single
  end

  if @scope == :single
    @elements = @query.execute parent.elements, 1 # force single record
  else
    @elements = @query.execute parent.elements
    @scope = :all if @scope.nil?
  end
end
should_wait?() click to toggle source
# File lib/pincers/core/search_context.rb, line 233
def should_wait?
  !frozen? && !advanced_mode? && !@waiting
end
wrap_childs(_query) click to toggle source
# File lib/pincers/core/search_context.rb, line 283
def wrap_childs(_query)
  child_elements = if advanced_mode? then _query.execute(elements) else nil end
  SearchContext.new child_elements, self, _query
end
wrap_errors() { || ... } click to toggle source
# File lib/pincers/core/search_context.rb, line 261
def wrap_errors
  begin
    yield
  rescue Pincers::Error
    raise
  rescue Exception => exc
    raise Pincers::BackendError.new(self, exc)
  end
end
wrap_siblings(_elements) click to toggle source
# File lib/pincers/core/search_context.rb, line 279
def wrap_siblings(_elements)
  SearchContext.new _elements, parent, nil
end