Class: Element
- Inherits:
-
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
@driver = Driver.driver
@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
#by ⇒ Object
Returns the value of attribute by.
6
7
8
|
# File 'lib/element.rb', line 6
def by
@by
end
|
#locator ⇒ Object
Returns the value of attribute locator.
6
7
8
|
# File 'lib/element.rb', line 6
def locator
@locator
end
|
#name ⇒ Object
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
|
#clear ⇒ Object
104
105
106
|
# File 'lib/element.rb', line 104
def clear
element.clear
end
|
#click ⇒ Object
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
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_element ⇒ Object
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
begin
elements = @driver.find_elements(@by, @locator)
elements.each do |element|
if element.displayed?
found_element = 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
|
#element ⇒ Object
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
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
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
218
219
220
|
# File 'lib/element.rb', line 218
def find_elements(by, locator)
element.find_elements(by, locator)
end
|
#hover_away ⇒ Object
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)
else
Log.error('Cannot hover away from element. Element is not present.')
end
end
|
#hover_over ⇒ Object
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})...")
if element.enabled?
$verification_passes += 1
ElementExtensions.hover_over(self)
else
Log.error('Cannot hover over element. Element is not present.')
end
end
|
#location ⇒ Object
130
131
132
|
# File 'lib/element.rb', line 130
def location
element.location
end
|
#present? ⇒ 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_screenshot ⇒ Object
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
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
|
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
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
|
#size ⇒ Object
166
167
168
|
# File 'lib/element.rb', line 166
def size
element.size
end
|
#submit ⇒ Object
178
179
180
|
# File 'lib/element.rb', line 178
def submit
element.submit
end
|
#tag_name ⇒ Object
174
175
176
|
# File 'lib/element.rb', line 174
def tag_name
element.tag_name
end
|
#text ⇒ Object
182
183
184
185
|
# File 'lib/element.rb', line 182
def text
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_s ⇒ Object
20
21
22
|
# File 'lib/element.rb', line 20
def to_s
"'#{@name}' (By:#{@by} => '#{@locator}')"
end
|
#value ⇒ Object
192
193
194
195
|
# File 'lib/element.rb', line 192
def value
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
|