Class: Applitools::Selenium::Element

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

Constant Summary collapse

JS_GET_COMPUTED_STYLE_FORMATTED_STR =
"   var elem = arguments[0];\n   var styleProp = '%s';\n   if (window.getComputedStyle) {\n   return window.getComputedStyle(elem, null)\n   .getPropertyValue(styleProp);\n   } else if (elem.currentStyle) {\n   return elem.currentStyle[styleProp];\n   } else {\n   return null;\n   };\n".freeze
JS_GET_SCROLL_LEFT =
'return arguments[0].scrollLeft;'.freeze
JS_GET_SCROLL_TOP =
'return arguments[0].scrollTop;'.freeze
JS_GET_SCROLL_WIDTH =
'return arguments[0].scrollWidth;'.freeze
JS_GET_SCROLL_HEIGHT =
'return arguments[0].scrollHeight;'.freeze
JS_SCROLL_TO_FORMATTED_STR =
"  arguments[0].scrollLeft = %d;\n  arguments[0].scrollTop = %d;\n".freeze
JS_GET_OVERFLOW =
'return arguments[0].style.overflow;'.freeze
JS_SET_OVERFLOW_FORMATTED_STR =
"arguments[0].style.overflow = '%s'".freeze
TRACE_PREFIX =
'EyesWebElement'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(driver, element) ⇒ Element

def_delegators ‘Applitools::EyesLogger’, :logger, :log_handler, :log_handler=



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

def initialize(driver, element)
  super(element)

  @driver = driver
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



55
56
57
58
59
60
61
# File 'lib/applitools/selenium/element.rb', line 55

def ==(other)
  if other.is_a? self.class
    super other.web_element
  else
    super other
  end
end

#border_bottom_widthObject



138
139
140
# File 'lib/applitools/selenium/element.rb', line 138

def border_bottom_width
  computed_style_integer(:'border-bottom-width')
end

#border_left_widthObject



126
127
128
# File 'lib/applitools/selenium/element.rb', line 126

def border_left_width
  computed_style_integer(:'border-left-width')
end

#border_right_widthObject



134
135
136
# File 'lib/applitools/selenium/element.rb', line 134

def border_right_width
  computed_style_integer(:'border-right-width')
end

#border_top_widthObject



130
131
132
# File 'lib/applitools/selenium/element.rb', line 130

def border_top_width
  computed_style_integer(:'border-top-width')
end

#boundsObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/applitools/selenium/element.rb', line 73

def bounds
  point = location
  left = point.x
  top = point.y
  width = 0
  height = 0

  begin
    dimension = size
    width = dimension.width
    height = dimension.height
  rescue => e
    # Not supported on all platforms.
    Applitools::EyesLogger.error("Failed extracting size using JavaScript: (#{e.message})")
  end

  if left < 0
    width = [0, width + left].max
    left = 0
  end

  if top < 0
    height = [0, height + top].max
    top = 0
  end

  Applitools::Base::Region.new(left, top, width, height)
end

#clickObject



45
46
47
48
49
# File 'lib/applitools/selenium/element.rb', line 45

def click
  @driver.add_mouse_trigger(Applitools::MouseTrigger::MOUSE_ACTION[:click], self)
  # logger.info "click(#{bounds})";
  web_element.click
end

#computed_style(prop_style) ⇒ Object



118
119
120
# File 'lib/applitools/selenium/element.rb', line 118

def computed_style(prop_style)
  driver.execute_script(JS_GET_COMPUTED_STYLE_FORMATTED_STR % prop_style, self).to_s
end

#computed_style_integer(prop_style) ⇒ Object



122
123
124
# File 'lib/applitools/selenium/element.rb', line 122

def computed_style_integer(prop_style)
  computed_style(prop_style).gsub(/px/, '').to_i.round
end

#find_element(*args) ⇒ Object



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

def find_element(*args)
  self.class.new driver, super
end

#find_elements(*args) ⇒ Object



106
107
108
# File 'lib/applitools/selenium/element.rb', line 106

def find_elements(*args)
  super(*args).map { |e| self.class.new driver, e }
end

#inspectObject



51
52
53
# File 'lib/applitools/selenium/element.rb', line 51

def inspect
  TRACE_PREFIX + web_element.inspect
end

#overflowObject



110
111
112
# File 'lib/applitools/selenium/element.rb', line 110

def overflow
  driver.execute_script(JS_GET_OVERFLOW, __getobj__).to_s
end

#overflow=(overflow) ⇒ Object



114
115
116
# File 'lib/applitools/selenium/element.rb', line 114

def overflow=(overflow)
  driver.execute_script(JS_SET_OVERFLOW_FORMATTED_STR % overflow, self)
end

#padding_bottom_widthObject



154
155
156
# File 'lib/applitools/selenium/element.rb', line 154

def padding_bottom_width
  computed_style_integer(:'padding-bottom')
end

#padding_left_widthObject



142
143
144
# File 'lib/applitools/selenium/element.rb', line 142

def padding_left_width
  computed_style_integer(:'padding-left')
end

#padding_right_widthObject



146
147
148
# File 'lib/applitools/selenium/element.rb', line 146

def padding_right_width
  computed_style_integer(:'padding-right')
end

#padding_top_widthObject



150
151
152
# File 'lib/applitools/selenium/element.rb', line 150

def padding_top_width
  computed_style_integer(:'padding-top')
end

#scroll_heightObject



170
171
172
# File 'lib/applitools/selenium/element.rb', line 170

def scroll_height
  Integer driver.execute_script(JS_GET_SCROLL_HEIGHT, self).to_s
end

#scroll_leftObject



158
159
160
# File 'lib/applitools/selenium/element.rb', line 158

def scroll_left
  Integer driver.execute_script(JS_GET_SCROLL_LEFT, self).to_s
end

#scroll_to(location) ⇒ Object



174
175
176
# File 'lib/applitools/selenium/element.rb', line 174

def scroll_to(location)
  driver.execute_script format(JS_SCROLL_TO_FORMATTED_STR, location.x, location.y), self
end

#scroll_topObject



162
163
164
# File 'lib/applitools/selenium/element.rb', line 162

def scroll_top
  Integer driver.execute_script(JS_GET_SCROLL_TOP, self).to_s
end

#scroll_widthObject



166
167
168
# File 'lib/applitools/selenium/element.rb', line 166

def scroll_width
  Integer driver.execute_script(JS_GET_SCROLL_WIDTH, self).to_s
end

#send_keys(*args) ⇒ Object Also known as: send_key



65
66
67
68
69
70
# File 'lib/applitools/selenium/element.rb', line 65

def send_keys(*args)
  Selenium::WebDriver::Keys.encode(args).each do |key|
    @driver.add_text_trigger(self, key.to_s)
  end
  web_element.send_keys(*args)
end

#to_hashObject



178
179
180
181
182
183
184
185
# File 'lib/applitools/selenium/element.rb', line 178

def to_hash
  {
    left: location.x.to_i,
    top: location.y.to_i,
    width: size.width.to_i,
    height: size.height.to_i
  }
end