Class: Element

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, by, locator) ⇒ Element

Returns a new instance of Element.



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/element.rb', line 8

def initialize(name, by, locator)
  @name = name
  @by = by
  @locator = locator

  # wrapped driver
  @driver = Driver.driver

  # selenium web element
  @element = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_sym, *arguments, &block) ⇒ Object



245
246
247
248
249
250
251
252
# File 'lib/element.rb', line 245

def method_missing(method_sym, *arguments, &block)
  Log.debug("called #{method_sym} on element #{@locator} by #{@by_type}")
  if @element.respond_to?(method_sym)
    @element.method(method_sym).call(*arguments, &block)
  else
    super
  end
end

Instance Attribute Details

#byObject (readonly)

Returns the value of attribute by.



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

def by
  @by
end

#locatorObject (readonly)

Returns the value of attribute locator.



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

def locator
  @locator
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

Instance Method Details

#attribute(name) ⇒ Object



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

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

#clearObject



104
105
106
# File 'lib/element.rb', line 104

def clear
  element.clear
end

#clickObject



108
109
110
111
112
113
114
115
116
117
# File 'lib/element.rb', line 108

def click
  Log.debug("Clicking on #{self}")
  if element.enabled?
    ElementExtensions.highlight(self) if Gridium.config.highlight_verifications
    $verification_passes += 1
    element.click
  else
    Log.error('Cannot click on element.  Element is not present.')
  end
end

#displayed?Boolean

Returns:

  • (Boolean)


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

def displayed?
  begin
    return element.displayed?
  rescue Exception => e
    return false
  end
end

#displayed_elementObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/element.rb', line 41

def displayed_element
  found_element = nil
  #Found an issue where the element would go stale after it's found
  begin
    elements = @driver.find_elements(@by, @locator)
    elements.each do |element|
      if element.displayed? #removed check for element.enabled
        found_element = element; #this will always return the last displayed element
      end
    end
  rescue Exception => e
    if found_element
      Log.warn("An element was found, but it was not displayed on the page. Gridium.config.visible_elements_only set to: #{Gridium.config.visible_elements_only} Element: #{self.to_s}")
    else
      Log.warn("Could not find Element: #{self.to_s}")
    end
  end

  found_element
end

#elementObject



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/element.rb', line 24

def element
  if @element.nil? or is_stale?
    wait = Selenium::WebDriver::Wait.new :timeout => Gridium.config.element_timeout, :interval => 1
    if Gridium.config.visible_elements_only
      wait.until { @element = displayed_element }
    else
      wait.until { @element = @driver.find_element(@by, @locator); Log.debug("Finding element #{self}..."); @element.enabled? }
    end

  end
  @element
end

#element=(e) ⇒ Object



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

def element=(e)
  @element = e
end

#enabled?Boolean

Returns:

  • (Boolean)


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

def enabled?
  element.enabled?
end

#find_element(by, locator) ⇒ Element

Search for an element within this element

Parameters:

  • by (Symbol)

    (:css or :xpath)

  • locator (String)

Returns:



205
206
207
208
# File 'lib/element.rb', line 205

def find_element(by, locator)
  Log.debug('Finding element...')
  element.find_element(by, locator)
end

#find_elements(by, locator) ⇒ Array

Search for an elements within this element

Parameters:

  • by (Symbol)

    (:css or :xpath)

  • locator (String)

Returns:

  • (Array)

    elements



218
219
220
# File 'lib/element.rb', line 218

def find_elements(by, locator)
  element.find_elements(by, locator)
end

#hover_awayObject



147
148
149
150
151
152
153
154
155
# File 'lib/element.rb', line 147

def hover_away
  Log.debug("Hovering away from element (#{self.to_s})...")
  if element.enabled?
    $verification_passes += 1
    ElementExtensions.hover_away(self) # Javascript workaround to above issue
  else
    Log.error('Cannot hover away from element.  Element is not present.')
  end
end

#hover_overObject



134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/element.rb', line 134

def hover_over
  Log.debug("Hovering over element (#{self.to_s})...")
  # @driver.mouse.move_to(element)            # Note: Doesn't work with Selenium 2.42 bindings for Firefox v31
  # @driver.action.move_to(element).perform
  # @driver.mouse_over(@locator)
  if element.enabled?
    $verification_passes += 1
    ElementExtensions.hover_over(self) # Javascript workaround to above issue
  else
    Log.error('Cannot hover over element.  Element is not present.')
  end
end

#locationObject



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

def location
  element.location
end

#present?Boolean

Returns:

  • (Boolean)


84
85
86
87
88
89
90
# File 'lib/element.rb', line 84

def present?
  begin
    return element.enabled?
  rescue Exception => e
    return false
  end
end

#save_element_screenshotObject



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/element.rb', line 222

def save_element_screenshot
  Log.debug ("Capturing screenshot of element...")
  self.scroll_into_view

  timestamp = Time.now.strftime("%Y_%m_%d__%H_%M_%S")
  name = self.name.gsub(' ', '_')
  screenshot_path = File.join($current_run_dir, "#{name}__#{timestamp}.png")
  @driver.save_screenshot(screenshot_path)

  location_x = self.location.x
  location_y = self.location.y
  element_width = self.size.width
  element_height = self.size.height

  # ChunkyPNG commands tap into oily_png (performance-enhanced version of chunky_png)
  image = ChunkyPNG::Image.from_file(screenshot_path.to_s)
  image1 = image.crop(location_x, location_y, element_width, element_height)
  image2 = image1.to_image
  element_screenshot_path = File.join($current_run_dir, "#{name}__#{timestamp}.png")
  image2.save(element_screenshot_path)
  SpecData.screenshots_captured.push("#{name}__#{timestamp}.png")
end

#scroll_into_viewObject



157
158
159
160
161
162
163
164
# File 'lib/element.rb', line 157

def scroll_into_view
  if element.enabled?
    $verification_passes += 1
    ElementExtensions.scroll_to(self)
  else
    Log.error('Cannot scroll element into view.  Element is not present.')
  end
end

#selected?Boolean

Returns:

  • (Boolean)


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

def selected?
  element.selected?
end

#send_keys(*args) ⇒ Object



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

def send_keys(*args)
  Log.debug("Typing: #{args} into element: (#{self}).")
  if element.enabled?
    ElementExtensions.highlight(self) if Gridium.config.highlight_verifications
    $verification_passes += 1
    element.send_keys *args
  else
    Log.error('Cannot type into element.  Element is not present.')
  end
end

#sizeObject



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

def size
  element.size
end

#submitObject



178
179
180
# File 'lib/element.rb', line 178

def submit
  element.submit
end

#tag_nameObject



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

def tag_name
  element.tag_name
end

#textObject



182
183
184
185
# File 'lib/element.rb', line 182

def text
  #this is used for text based elements
  element.text
end

#text=(text) ⇒ Object



187
188
189
190
# File 'lib/element.rb', line 187

def text=(text)
  element.clear
  element.send_keys(text)
end

#to_sObject



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

def to_s
  "'#{@name}' (By:#{@by} => '#{@locator}')"
end

#valueObject



192
193
194
195
# File 'lib/element.rb', line 192

def value
  #this is used for inputs and forms
  element.attribute("value")
end

#verify(timeout = nil) ⇒ Object

soft failure, will not kill test immediately



67
68
69
70
71
# File 'lib/element.rb', line 67

def verify(timeout=nil)
  Log.debug('Verifying new element...')
  timeout = Gridium.config.element_timeout if timeout.nil?
  ElementVerification.new(self, timeout)
end

#wait_until(timeout = nil) ⇒ Object

hard failure, will kill test immediately



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

def wait_until(timeout=nil)
  Log.debug('Waiting for new element...')
  timeout = Gridium.config.element_timeout if timeout.nil?
  ElementVerification.new(self, timeout, fail_test=true)
end