Class: DomGlancy::DOMElement

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(h = {}) ⇒ DOMElement

Returns a new instance of DOMElement.



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/dom_glancy/element.rb', line 17

def initialize(h = {})
  @tag        = h['tag']
  @left       = h['left']
  @top        = h['top']
  @height     = h['height']
  @width      = h['width']
  @klass      = h['class']
  @id         = h['id']
  @style      = h['style']
  @visible    = h['visible']
  @similarity = h['similarity'] || 0
end

Instance Attribute Details

#heightObject

Returns the value of attribute height.



10
11
12
# File 'lib/dom_glancy/element.rb', line 10

def height
  @height
end

#idObject

element looks like this in archive. “height”=>238, “visible”=>true, “tag”=>“DIV”, “width”=>720, “class”=>“grid”, “left”=>43, “top”=>14



6
7
8
# File 'lib/dom_glancy/element.rb', line 6

def id
  @id
end

#klassObject

Returns the value of attribute klass.



12
13
14
# File 'lib/dom_glancy/element.rb', line 12

def klass
  @klass
end

#leftObject

Returns the value of attribute left.



8
9
10
# File 'lib/dom_glancy/element.rb', line 8

def left
  @left
end

#similarityObject

Returns the value of attribute similarity.



15
16
17
# File 'lib/dom_glancy/element.rb', line 15

def similarity
  @similarity
end

#styleObject

Returns the value of attribute style.



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

def style
  @style
end

#tagObject

Returns the value of attribute tag.



7
8
9
# File 'lib/dom_glancy/element.rb', line 7

def tag
  @tag
end

#topObject

Returns the value of attribute top.



9
10
11
# File 'lib/dom_glancy/element.rb', line 9

def top
  @top
end

#visibleObject

Returns the value of attribute visible.



13
14
15
# File 'lib/dom_glancy/element.rb', line 13

def visible
  @visible
end

#widthObject

Returns the value of attribute width.



11
12
13
# File 'lib/dom_glancy/element.rb', line 11

def width
  @width
end

Instance Method Details

#all_same?(anOther) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
39
40
41
42
# File 'lib/dom_glancy/element.rb', line 36

def all_same?(anOther)
  same = same_element?(anOther)     &&
      similar_size?(anOther, 0)     &&
      similar_location?(anOther, 0) &&
      same_size?(anOther)           &&
      same_visibility?(anOther)
end

#change_info(anOther) ⇒ Object



119
120
121
122
123
124
125
126
127
128
# File 'lib/dom_glancy/element.rb', line 119

def change_info(anOther)
  info = []
  if same_element?(anOther)
    info << 'left' unless similar_left?(anOther, @similarity)
    info << 'top' unless similar_top?(anOther, @similarity)
    info << 'height' unless similar_height?(anOther, @similarity)
    info << 'width' unless similar_width?(anOther, @similarity)
  end
  info
end

#change_level(anOther) ⇒ Object



115
116
117
# File 'lib/dom_glancy/element.rb', line 115

def change_level(anOther)
  change_info(anOther).length
end

#close_enough?(anOther) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
47
48
49
50
# File 'lib/dom_glancy/element.rb', line 44

def close_enough?(anOther)
  r = same_element?(anOther)                  &&
      similar_location?(anOther, @similarity) &&
      similar_size?(anOther, @similarity)     &&
      same_style?(anOther)                    &&
      same_visibility?(anOther)
end

#same_class?(anOther) ⇒ Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/dom_glancy/element.rb', line 90

def same_class?(anOther)
  @klass == anOther.klass
end

#same_element?(anOther) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
33
34
# File 'lib/dom_glancy/element.rb', line 30

def same_element?(anOther)
  same = same_tag?(anOther)   &&
         same_id?(anOther)    &&
         same_class?(anOther)
end

#same_id?(anOther) ⇒ Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/dom_glancy/element.rb', line 94

def same_id?(anOther)
  @id == anOther.id
end

#same_location?(anOther) ⇒ Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/dom_glancy/element.rb', line 82

def same_location?(anOther)
  (@left == anOther.left) && (@top == anOther.top)
end

#same_size?(anOther) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/dom_glancy/element.rb', line 86

def same_size?(anOther)
  (@height == anOther.height) && (@width == anOther.width)
end

#same_style?(anOther) ⇒ Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/dom_glancy/element.rb', line 102

def same_style?(anOther)
  @style == anOther.style
end

#same_tag?(anOther) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/dom_glancy/element.rb', line 78

def same_tag?(anOther)
  @tag == anOther.tag
end

#same_visibility?(anOther) ⇒ Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/dom_glancy/element.rb', line 98

def same_visibility?(anOther)
  @visible == anOther.visible
end

#similar_height?(anOther, similarity = 0) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/dom_glancy/element.rb', line 74

def similar_height?(anOther, similarity = 0)
  (@height - anOther.height).abs <= similarity
end

#similar_left?(anOther, similarity = 0) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/dom_glancy/element.rb', line 66

def similar_left?(anOther, similarity = 0)
  (@left - anOther.left).abs <= similarity
end

#similar_location?(anOther, similarity = 0) ⇒ Boolean

Returns:

  • (Boolean)


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

def similar_location?(anOther, similarity = 0)
  similar = (@top - anOther.top).abs <= similarity
  similar && (@left - anOther.left).abs <= similarity
end

#similar_size?(anOther, similarity = 0) ⇒ Boolean

Returns:

  • (Boolean)


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

def similar_size?(anOther, similarity = 0)
  similar = (@height - anOther.height).abs <= similarity
  similar && (@width - anOther.width).abs <= similarity
end

#similar_top?(anOther, similarity = 0) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/dom_glancy/element.rb', line 62

def similar_top?(anOther, similarity = 0)
  (@top - anOther.top).abs <= similarity
end

#similar_width?(anOther, similarity = 0) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/dom_glancy/element.rb', line 70

def similar_width?(anOther, similarity = 0)
  (@width - anOther.width).abs <= similarity
end

#similarity_level(anOther) ⇒ Object



106
107
108
109
110
111
112
113
# File 'lib/dom_glancy/element.rb', line 106

def similarity_level(anOther)
  level = 0
  if same_element?(anOther) && same_style?(anOther)
    level += 1 if similar_location?(anOther, @similarity) and !same_location?(anOther)
    level += 1 if similar_size?(anOther, @similarity)     and !same_size?(anOther)
  end
  level
end

#to_hashObject



130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/dom_glancy/element.rb', line 130

def to_hash
  {
      'id'      => @id,
      'height'  => @height,
      'visible' => @visible,
      'tag'     => @tag,
      'width'   => @width,
      'class'   => @klass,
      'left'    => @left,
      'top'     => @top
  }
end