Class: SK::Element

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

Direct Known Subclasses

Cell, Clickable, Dropdown, Field, Row, Table

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arg, src = "xxx") ⇒ Element

Returns a new instance of Element.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/element.rb', line 3

def initialize(arg,src="xxx")
  # puts "element init arg=#{arg} src=#{src}"
  if !arg
    # sometimes we want to create an element from
    # a find that fails... so create a dummy element
    @locator = { src: src }
    @el = false
  elsif arg.instance_of?(Selenium::WebDriver::Element)
    @locator = { src: src }
    @el = arg
  else # it is a locator
    @locator = arg # remember
    @el = SK::Browser.find(arg)
    SK::Trace.debug "element locator=#{arg} el=#{@el}"
    SK::Trace.warn "element failed to initialize with locator = #{arg}" unless @el
  end
end

Instance Attribute Details

#elObject (readonly)

Returns the value of attribute el.



21
22
23
# File 'lib/element.rb', line 21

def el
  @el
end

#locatorObject (readonly)

Returns the value of attribute locator.



22
23
24
# File 'lib/element.rb', line 22

def locator
  @locator
end

Instance Method Details

#children(locator, klass = SK::Element) ⇒ Object



81
82
83
84
85
86
# File 'lib/element.rb', line 81

def children(locator,klass=SK::Element)
    # get the matching children elements
    els = SK::Browser.rescue_exceptions { self.el.find_elements(locator) } 
    # but return as one of our class types
    els.map { |el| klass.new(el) }
end

#displayed?Boolean

Returns:

  • (Boolean)


33
34
35
36
# File 'lib/element.rb', line 33

def displayed?
  puts "+++ element displayed?"
  SK::Browser.rescue_exceptions { self.el.displayed? } 
end

#enabled?Boolean

Returns:

  • (Boolean)


43
44
45
46
47
48
# File 'lib/element.rb', line 43

def enabled?
  puts "enabled? 1:#{exists?}"
  false unless exists?
  puts "enabled? 2:#{el.enabled?}"
  el.enabled?
end

#exists?Boolean Also known as: exist?

Returns:

  • (Boolean)


28
29
30
# File 'lib/element.rb', line 28

def exists?
  !!el
end

#find(locator, klass = SK::Element) ⇒ Object Also known as: child



57
58
59
60
61
62
# File 'lib/element.rb', line 57

def find(locator,klass=SK::Element)
  # trace "find #{locator} in #{self.locator}"
  child_el = find_child_el(locator)
  # trace "child el = #{child_el}"
  klass.new(child_el,"#{locator}")
end

#find_child_el(locator) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/element.rb', line 65

def find_child_el(locator)
    # trace "find child of #{self.locator}. locator=#{locator}"
    # find an element by locator inside another element (base)
    case
    when locator.has_key?(:value)
      # trace "... locate by value: #{locator[:value]}}"
      search(:value,locator[:value]) 
    when locator.has_key?(:type)
      # trace "... locate by type: #{locator[:type]}" 
      search(:type,locator[:type]) 
    else
      # a standard selenium locator so just use selenium
      SK::Browser.rescue_exceptions { self.el.find_element(locator) } 
    end
end

#htmlObject



38
39
40
41
# File 'lib/element.rb', line 38

def html
  # useful to see for debugging
  self.el.attribute('innerHTML')
end

#parent_elObject



100
101
102
# File 'lib/element.rb', line 100

def parent_el
  self.el.find_element({xpath: ".."})
end

#search(key, val) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/element.rb', line 88

def search(key,val)
  trace "element#search key=#{key} val=#{val}"
  # selenium does not support a locator search by type
  # so we need a specialized function to find the element
  elements = self.el.find_elements({})
  #elements = all({tag_name: 'input'})
  elements.find do |e| 
      # trace "> #{e.attribute(key)} :: #{val}"
      e.attribute(key) == val
  end
end

#textObject



50
51
52
53
54
55
# File 'lib/element.rb', line 50

def text
  # useful to see contents for debugging
  str1 = self.el ? self.el.text : ''
  # str2 = str1.gsub(/\n\s+/, " ") # shrink white space
  str1.gsub(/\n+/, " ").strip # remove new lines
end

#to_sObject



24
25
26
# File 'lib/element.rb', line 24

def to_s
  "<SK::Element #{locator}>"
end