Class: Locator::Dom::Nokogiri::Element

Inherits:
Element
  • Object
show all
Defined in:
lib/locator/dom/nokogiri/element.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(element) ⇒ Element

Returns a new instance of Element.



7
8
9
10
# File 'lib/locator/dom/nokogiri/element.rb', line 7

def initialize(element)
  @element = element
  @matches = []
end

Instance Attribute Details

#elementObject (readonly)

Returns the value of attribute element.



5
6
7
# File 'lib/locator/dom/nokogiri/element.rb', line 5

def element
  @element
end

#matchesObject (readonly)

Returns the value of attribute matches.



5
6
7
# File 'lib/locator/dom/nokogiri/element.rb', line 5

def matches
  @matches
end

Instance Method Details

#<=>(other) ⇒ Object



12
13
14
# File 'lib/locator/dom/nokogiri/element.rb', line 12

def <=>(other)
  to_s.length <=> other.to_s.length
end

#ancestor_of?(other) ⇒ Boolean

Returns:



84
85
86
# File 'lib/locator/dom/nokogiri/element.rb', line 84

def ancestor_of?(other)
  other.ancestors.include?(element)
end

#ancestorsObject



80
81
82
# File 'lib/locator/dom/nokogiri/element.rb', line 80

def ancestors
  element.ancestors
end

#attribute(name) ⇒ Object



56
57
58
# File 'lib/locator/dom/nokogiri/element.rb', line 56

def attribute(name)
  element.attribute(name).to_s
end

#checkedObject



39
40
41
42
# File 'lib/locator/dom/nokogiri/element.rb', line 39

def checked
  checked = element.attribute('checked')
  checked ? checked.value : nil
end

#contentObject



44
45
46
# File 'lib/locator/dom/nokogiri/element.rb', line 44

def content
  element.content
end

#css_pathObject



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

def css_path
  element.css_path.to_s
end

#element_by_css(rule) ⇒ Object



72
73
74
# File 'lib/locator/dom/nokogiri/element.rb', line 72

def element_by_css(rule)
  elements_by_css(rule).first
end

#element_by_id(id) ⇒ Object



60
61
62
# File 'lib/locator/dom/nokogiri/element.rb', line 60

def element_by_id(id)
  elements_by_xpath("//*[@id='#{id}']").first
end

#element_by_xpath(xpath) ⇒ Object



64
65
66
# File 'lib/locator/dom/nokogiri/element.rb', line 64

def element_by_xpath(xpath)
  elements_by_xpath(xpath).first
end

#elements_by_css(*rules) ⇒ Object



76
77
78
# File 'lib/locator/dom/nokogiri/element.rb', line 76

def elements_by_css(*rules)
  element.css(*rules).map { |element| Element.new(element) }
end

#elements_by_xpath(*xpaths) ⇒ Object



68
69
70
# File 'lib/locator/dom/nokogiri/element.rb', line 68

def elements_by_xpath(*xpaths)
  element.xpath(*xpaths).map { |element| Element.new(element) }
end

#inner_htmlObject



48
49
50
# File 'lib/locator/dom/nokogiri/element.rb', line 48

def inner_html
  element.inner_html
end

#nameObject



16
17
18
# File 'lib/locator/dom/nokogiri/element.rb', line 16

def name
  element.name
end

#to_sObject



52
53
54
# File 'lib/locator/dom/nokogiri/element.rb', line 52

def to_s
  element.to_s
end

#valueObject



28
29
30
31
32
33
34
35
36
37
# File 'lib/locator/dom/nokogiri/element.rb', line 28

def value
  case name
  when 'input'
    element.attribute('value').value
  when 'select'
    selected = element.children.select { |option| option.attribute('selected') }
    values = selected.map { |option| option.attribute('value') || option.content }
    element.attribute('multiple') ? values.strip : values.first.strip
  end
end

#xpathObject



20
21
22
# File 'lib/locator/dom/nokogiri/element.rb', line 20

def xpath
  element.path.to_s
end