Class: WebViewer::ElementReader

Inherits:
Object
  • Object
show all
Defined in:
lib/element_reader.rb

Overview

ElementReader fetches a Selenium::WebDriver::Element for a particular selector. If the selector requires parameters, ElementReader defers reading from WebDriver until the parameters are provided using the [] method.

Instance Method Summary collapse

Constructor Details

#initialize(viewer, selector, selector_type) ⇒ ElementReader

Returns a new instance of ElementReader.



8
9
10
11
12
# File 'lib/element_reader.rb', line 8

def initialize(viewer, selector, selector_type)
  @viewer = viewer
  @selector = selector
  @selector_type = selector_type
end

Instance Method Details

#[](*arguments) ⇒ Object



32
33
34
35
# File 'lib/element_reader.rb', line 32

def [](*arguments)
  selector_text = @selector.call(*arguments)
  self.element(selector_text)
end

#callable_selector?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/element_reader.rb', line 18

def callable_selector?
  @selector.respond_to?(:call) 
end

#element(selector_text = @selector) ⇒ Object



37
38
39
# File 'lib/element_reader.rb', line 37

def element(selector_text = @selector)
  @viewer.find_element(@selector_type, selector_text)
end

#get(*arguments) ⇒ Object

return either self or element depending on whether selector needs parameters



23
24
25
26
27
28
29
30
# File 'lib/element_reader.rb', line 23

def get(*arguments)
  return element unless callable_selector?
  if (arguments.length > 0) || !requires_parameters?
    element(@selector.call(*arguments))
  else
    self
  end
end

#requires_parameters?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/element_reader.rb', line 14

def requires_parameters?
  @selector.respond_to?(:call) && @selector.respond_to?(:arity) && @selector.arity > 0
end