testcentricity_web 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,59 @@
1
+ module TestCentricity
2
+ class UIElement
3
+ def set_siebel_checkbox_state(state)
4
+ obj, _ = find_element
5
+ object_not_found_exception(obj, 'Siebel checkbox')
6
+ raise "#{locator} is not a Siebel CheckBox object" unless get_siebel_object_type == 'JCheckBox'
7
+ expected = state.to_bool
8
+ obj.click unless expected == obj.checked?
9
+ end
10
+
11
+ def choose_siebel_option(option)
12
+ Capybara.wait_on_first_by_default = true
13
+ invoke_siebel_popup
14
+ first(:xpath, "//li[@class='ui-menu-item']", :exact => true, :match => :prefer_exact,text: option).click
15
+ end
16
+
17
+ def get_siebel_options
18
+ invoke_siebel_popup
19
+ sleep(0.5)
20
+ options = page.all(:xpath, "//li[@class='ui-menu-item']").collect(&:text)
21
+ obj, _ = find_element
22
+ obj.native.send_keys(:escape)
23
+ options
24
+ end
25
+
26
+ def verify_siebel_options(expected, enqueue = false)
27
+ invoke_siebel_popup
28
+ sleep(0.5)
29
+ actual = page.all(:xpath, "//li[@class='ui-menu-item']").collect(&:text)
30
+ enqueue ?
31
+ ExceptionQueue.enqueue_assert_equal(expected, actual, "Expected list of options in list #{@locator}") :
32
+ assert_equal(expected, actual, "Expected list of options in list #{@locator} to be #{expected} but found #{actual}")
33
+ obj, _ = find_element
34
+ obj.native.send_keys(:escape)
35
+ end
36
+
37
+ def invoke_siebel_dialog(popup)
38
+ invoke_siebel_popup
39
+ popup.wait_until_exists(15)
40
+ end
41
+
42
+ def get_siebel_object_type
43
+ obj, _ = find_element
44
+ object_not_found_exception(obj, 'Siebel object')
45
+ obj.native.attribute('ot')
46
+ end
47
+
48
+ private
49
+
50
+ def invoke_siebel_popup
51
+ obj, _ = find_element
52
+ object_not_found_exception(obj, 'Siebel object')
53
+ trigger_name = obj.native.attribute('aria-describedby').strip
54
+ trigger = "//span[@id='#{trigger_name}']"
55
+ trigger = "#{@parent.get_locator}#{trigger}" if @context == :section && !@parent.get_locator.nil?
56
+ first(:xpath, trigger).click
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,465 @@
1
+ Capybara::Node::Element.class_eval do
2
+ def click_at(x, y)
3
+ right = x - (native.size.width / 2)
4
+ top = y - (native.size.height / 2)
5
+ driver.browser.action.move_to(native).move_by(right.to_i, top.to_i).click.perform
6
+ end
7
+
8
+ def get_width
9
+ native.size.width
10
+ end
11
+
12
+ def get_height
13
+ native.size.height
14
+ end
15
+ end
16
+
17
+
18
+ module TestCentricity
19
+ class UIElement
20
+ include Capybara::DSL
21
+ include Test::Unit::Assertions
22
+
23
+ attr_reader :parent, :locator, :context, :type
24
+ attr_accessor :alt_locator
25
+
26
+ def initialize(parent, locator, context, type = nil)
27
+ @parent = parent
28
+ @locator = locator
29
+ @context = context
30
+ @type = type
31
+ @alt_locator = nil
32
+ end
33
+
34
+ def get_locator
35
+ @locator
36
+ end
37
+
38
+ def set_alt_locator(temp_locator)
39
+ @alt_locator = temp_locator
40
+ end
41
+
42
+ def clear_alt_locator
43
+ @alt_locator = nil
44
+ end
45
+
46
+ def click
47
+ obj, _ = find_element
48
+ object_not_found_exception(obj, nil)
49
+ begin
50
+ obj.click
51
+ rescue
52
+ obj.click_at(10, 10)
53
+ end
54
+ end
55
+
56
+ def double_click
57
+ obj, _ = find_element
58
+ object_not_found_exception(obj, nil)
59
+ page.driver.browser.mouse.double_click(obj.native)
60
+ end
61
+
62
+ def click_at(x, y)
63
+ obj, _ = find_element
64
+ raise "Object #{@locator} not found" unless obj
65
+ obj.click_at(x, y)
66
+ end
67
+
68
+ def set(value)
69
+ obj, _ = find_element
70
+ object_not_found_exception(obj, nil)
71
+ obj.set(value)
72
+ end
73
+
74
+ def send_keys(*keys)
75
+ obj, _ = find_element
76
+ object_not_found_exception(obj, nil)
77
+ obj.send_keys(*keys)
78
+ end
79
+
80
+ def exists?
81
+ obj, _ = find_element
82
+ obj != nil
83
+ end
84
+
85
+ def visible?
86
+ obj, type = find_element
87
+ exists = obj
88
+ invisible = false
89
+ if type == :css
90
+ Capybara.using_wait_time 0.1 do
91
+ # is object itself hidden with .ui-helper-hidden class?
92
+ self_hidden = page.has_css?("#{@locator}.ui-helper-hidden")
93
+ # is parent of object hidden, thus hiding the object?
94
+ parent_hidden = page.has_css?(".ui-helper-hidden > #{@locator}")
95
+ # is grandparent of object, or any other ancestor, hidden?
96
+ other_ancestor_hidden = page.has_css?(".ui-helper-hidden * #{@locator}")
97
+ # if any of the above conditions are true, then object is invisible
98
+ invisible = self_hidden || parent_hidden || other_ancestor_hidden
99
+ end
100
+ else
101
+ invisible = !obj.visible? if exists
102
+ end
103
+ # the object is visible if it exists and it is not invisible
104
+ (exists && !invisible) ? true : false
105
+ end
106
+
107
+ def hidden?
108
+ not visible?
109
+ end
110
+
111
+ def enabled?
112
+ not disabled?
113
+ end
114
+
115
+ def disabled?
116
+ obj, _ = find_element
117
+ object_not_found_exception(obj, nil)
118
+ obj.disabled?
119
+ end
120
+
121
+ def read_only?
122
+ obj, _ = find_element
123
+ object_not_found_exception(obj, nil)
124
+ !!obj.native.attribute('readonly')
125
+ end
126
+
127
+ def get_max_length
128
+ obj, _ = find_element
129
+ object_not_found_exception(obj, nil)
130
+ obj.native.attribute('maxlength')
131
+ end
132
+
133
+ def checked?
134
+ obj, _ = find_element
135
+ object_not_found_exception(obj, 'Checkbox')
136
+ obj.checked?
137
+ end
138
+
139
+ def set_checkbox_state(state)
140
+ obj, _ = find_element
141
+ object_not_found_exception(obj, 'Checkbox')
142
+ invalid_object_type_exception(obj, 'checkbox')
143
+ obj.set(state)
144
+ end
145
+
146
+ def verify_check_state(state, enqueue = false)
147
+ actual = checked?
148
+ enqueue ?
149
+ ExceptionQueue.enqueue_assert_equal(state, actual, "Expected #{@locator}") :
150
+ assert_equal(state, actual, "Expected #{@locator} to be #{state} but found #{actual} instead")
151
+ end
152
+
153
+ def wait_until_exists(seconds)
154
+ timeout = seconds.nil? ? Capybara.default_max_wait_time : seconds
155
+ wait = Selenium::WebDriver::Wait.new(timeout: timeout)
156
+ wait.until { exists? }
157
+ rescue
158
+ raise "Could not find element #{@locator} after #{timeout} seconds" unless exists?
159
+ end
160
+
161
+ def wait_until_gone(seconds)
162
+ timeout = seconds.nil? ? Capybara.default_max_wait_time : seconds
163
+ wait = Selenium::WebDriver::Wait.new(timeout: timeout)
164
+ wait.until { !exists? }
165
+ rescue
166
+ raise "Element #{@locator} remained visible after #{timeout} seconds" if exists?
167
+ end
168
+
169
+ def wait_until_value_is(value, seconds)
170
+ timeout = seconds.nil? ? Capybara.default_max_wait_time : seconds
171
+ wait = Selenium::WebDriver::Wait.new(timeout: timeout)
172
+ wait.until { get_value == value }
173
+ rescue
174
+ raise "Value of UI element #{@locator} failed to equal '#{value}' after #{timeout} seconds" unless exists?
175
+ end
176
+
177
+ def wait_until_value_changes(seconds)
178
+ value = get_value
179
+ timeout = seconds.nil? ? Capybara.default_max_wait_time : seconds
180
+ wait = Selenium::WebDriver::Wait.new(timeout: timeout)
181
+ wait.until { get_value != value }
182
+ rescue
183
+ raise "Value of UI element #{@locator} failed to change from '#{value}' after #{timeout} seconds" unless exists?
184
+ end
185
+
186
+ def get_value
187
+ obj, _ = find_element
188
+ object_not_found_exception(obj, nil)
189
+ case obj.tag_name.downcase
190
+ when 'input', 'select', 'textarea'
191
+ obj.value
192
+ else
193
+ obj.text
194
+ end
195
+ end
196
+
197
+ def verify_value(expected, enqueue = false)
198
+ actual = get_value
199
+ enqueue ?
200
+ ExceptionQueue.enqueue_assert_equal(expected.strip, actual.strip, "Expected #{@locator}") :
201
+ assert_equal(expected.strip, actual.strip, "Expected #{@locator} to display '#{expected}' but found '#{actual}'")
202
+ end
203
+
204
+ def hover
205
+ obj, _ = find_element
206
+ object_not_found_exception(obj, nil)
207
+ obj.hover
208
+ end
209
+
210
+ def drag_by(right_offset, down_offset)
211
+ obj, _ = find_element
212
+ object_not_found_exception(obj, nil)
213
+ obj.drag_by(right_offset, down_offset)
214
+ end
215
+
216
+ def attach_file(file_path)
217
+ Capybara.ignore_hidden_elements = false
218
+ page.attach_file(@locator, file_path)
219
+ Capybara.ignore_hidden_elements = true
220
+ end
221
+
222
+ def choose_option(option)
223
+ obj, _ = find_element
224
+ object_not_found_exception(obj, nil)
225
+ obj.click
226
+ if first(:css, 'li.active-result')
227
+ if option.is_a?(Array)
228
+ option.each do |item|
229
+ page.find(:css, 'li.active-result', text: item.strip).click
230
+ end
231
+ else
232
+ first(:css, 'li.active-result', text: option).click
233
+ end
234
+ else
235
+ if option.is_a?(Array)
236
+ option.each do |item|
237
+ obj.select item
238
+ end
239
+ else
240
+ obj.select option
241
+ end
242
+ end
243
+ end
244
+
245
+ def get_options
246
+ obj, _ = find_element
247
+ object_not_found_exception(obj, nil)
248
+ obj.all('option').collect(&:text)
249
+ end
250
+
251
+ def get_row_count
252
+ wait_until_exists(5)
253
+ row_count = page.all(:xpath, "#{@locator}/tbody/tr", :visible => :all).count
254
+ row_count
255
+ end
256
+
257
+ def get_column_count
258
+ row_count = get_row_count
259
+ if row_count == 0
260
+ page.all(:xpath, "#{@locator}/thead/tr/th", :visible => :all).count
261
+ else
262
+ (row_count == 1) ?
263
+ page.all(:xpath, "#{@locator}/tbody/tr/td", :visible => :all).count :
264
+ page.all(:xpath, "#{@locator}/tbody/tr[2]/td", :visible => :all).count
265
+ end
266
+ end
267
+
268
+ def click_table_cell(row, column)
269
+ row_count = get_row_count
270
+ raise "Row #{row} exceeds number of rows (#{row_count}) in table #{@locator}" if row > row_count
271
+ column_count = get_column_count
272
+ raise "Column #{column} exceeds number of columns (#{column_count}) in table #{@locator}" if column > column_count
273
+ set_table_cell_locator(row, column)
274
+ click
275
+ clear_alt_locator
276
+ end
277
+
278
+ def double_click_table_cell(row, column)
279
+ row_count = get_row_count
280
+ raise "Row #{row} exceeds number of rows (#{row_count}) in table #{@locator}" if row > row_count
281
+ column_count = get_column_count
282
+ raise "Column #{column} exceeds number of columns (#{column_count}) in table #{@locator}" if column > column_count
283
+ set_table_cell_locator(row, column)
284
+ double_click
285
+ clear_alt_locator
286
+ end
287
+
288
+ def click_table_cell_link(row, column)
289
+ row_count = get_row_count
290
+ raise "Row #{row} exceeds number of rows (#{row_count}) in table #{@locator}" if row > row_count
291
+ column_count = get_column_count
292
+ raise "Column #{column} exceeds number of columns (#{column_count}) in table #{@locator}" if column > column_count
293
+ set_table_cell_locator(row, column)
294
+ saved_locator = @alt_locator
295
+ set_alt_locator("#{@alt_locator}/a")
296
+ set_alt_locator("#{saved_locator}/span/a") unless exists?
297
+ # if link not present, check for text entry fields and try to dismiss by tabbing out
298
+ unless exists?
299
+ set_alt_locator("#{saved_locator}/input")
300
+ set_alt_locator("#{saved_locator}/textarea") unless exists?
301
+ send_keys(:tab) if exists?
302
+ set_alt_locator("#{saved_locator}/a")
303
+ set_alt_locator("#{saved_locator}/span/a") unless exists?
304
+ send_keys(:tab) unless exists?
305
+ end
306
+ wait_until_exists(1)
307
+ click
308
+ clear_alt_locator
309
+ end
310
+
311
+ def get_table_row(row)
312
+ columns = []
313
+ row_count = get_row_count
314
+ raise "Row #{row} exceeds number of rows (#{row_count}) in table #{@locator}" if row > row_count
315
+ column_count = get_column_count
316
+ (1..column_count).each do |column|
317
+ value = ''
318
+ set_table_cell_locator(row, column)
319
+ saved_locator = @alt_locator
320
+ set_alt_locator("#{saved_locator}/input")
321
+ unless exists?
322
+ set_alt_locator("#{saved_locator}/textarea")
323
+ unless exists?
324
+ set_alt_locator(saved_locator)
325
+ end
326
+ end
327
+ value = get_value if exists?
328
+ columns.push(value)
329
+ end
330
+ clear_alt_locator
331
+ columns
332
+ end
333
+
334
+ def get_row_data(row)
335
+ row_count = get_row_count
336
+ raise "Row #{row} exceeds number of rows (#{row_count}) in table #{@locator}" if row > row_count
337
+ (row > 1) ?
338
+ set_alt_locator("#{@locator}/tbody/tr[#{row}]") :
339
+ set_alt_locator("#{@locator}/tbody/tr")
340
+ value = get_value if exists?
341
+ clear_alt_locator
342
+ value
343
+ end
344
+
345
+ def get_table_cell(row, column)
346
+ row_count = get_row_count
347
+ raise "Row #{row} exceeds number of rows (#{row_count}) in table #{@locator}" if row > row_count
348
+ column_count = get_column_count
349
+ raise "Column #{column} exceeds number of columns (#{column_count}) in table #{@locator}" if column > column_count
350
+ set_table_cell_locator(row, column)
351
+ saved_locator = @alt_locator
352
+ set_alt_locator("#{saved_locator}/input")
353
+ unless exists?
354
+ set_alt_locator("#{saved_locator}/textarea")
355
+ unless exists?
356
+ set_alt_locator(saved_locator)
357
+ end
358
+ end
359
+ value = get_value
360
+ clear_alt_locator
361
+ value
362
+ end
363
+
364
+ def verify_table_cell(row, column, expected, enqueue = false)
365
+ actual = get_table_cell(row, column)
366
+ enqueue ?
367
+ ExceptionQueue.enqueue_assert_equal(expected.strip, actual.strip, "Expected #{@locator} row #{row}/column #{column}") :
368
+ assert_equal(expected.strip, actual.strip, "Expected #{@locator} row #{row}/column #{column} to display '#{expected}' but found '#{actual}'")
369
+ end
370
+
371
+ def set_table_cell(row, column, value)
372
+ row_count = get_row_count
373
+ raise "Row #{row} exceeds number of rows (#{row_count}) in table #{@locator}" if row > row_count
374
+ # column_count = get_column_count
375
+ # raise "Column #{column} exceeds number of columns (#{column_count}) in table #{@locator}" if column > column_count
376
+ set_table_cell_locator(row, column)
377
+ click if exists?
378
+ saved_locator = @alt_locator
379
+ set_alt_locator("#{saved_locator}/input")
380
+ set_alt_locator("#{saved_locator}/textarea") unless exists?
381
+ set(value)
382
+ clear_alt_locator
383
+ end
384
+
385
+ def click_header_column(column)
386
+ column_count = get_column_count
387
+ raise "Column #{column} exceeds number of columns (#{column_count}) in table header #{@locator}" if column > column_count
388
+ (column > 1) ?
389
+ set_alt_locator("#{@locator}/thead/tr/th[#{column}]") :
390
+ set_alt_locator("#{@locator}/thead/tr/th")
391
+ click
392
+ clear_alt_locator
393
+ end
394
+
395
+ def get_header_column(column)
396
+ column_count = get_column_count
397
+ raise "Column #{column} exceeds number of columns (#{column_count}) in table header #{@locator}" if column > column_count
398
+ (column > 1) ?
399
+ set_alt_locator("#{@locator}/thead/tr/th[#{column}]") :
400
+ set_alt_locator("#{@locator}/thead/tr/th")
401
+ value = get_value
402
+ clear_alt_locator
403
+ value
404
+ end
405
+
406
+ def get_header_columns
407
+ columns = []
408
+ column_count = get_column_count
409
+ (1..column_count).each do |column|
410
+ (column > 1) ?
411
+ set_alt_locator("#{@locator}/thead/tr/th[#{column}]") :
412
+ set_alt_locator("#{@locator}/thead/tr/th")
413
+ columns.push(get_value)
414
+ end
415
+ clear_alt_locator
416
+ columns
417
+ end
418
+
419
+ def get_list_items(item_locator)
420
+ obj, _ = find_element
421
+ object_not_found_exception(obj, nil)
422
+ obj.all(item_locator).collect(&:text)
423
+ end
424
+
425
+ private
426
+
427
+ def find_element
428
+ @alt_locator.nil? ? locator = @locator : locator = @alt_locator
429
+ locator = "#{@parent.get_locator}#{locator}" if @context == :section && !@parent.get_locator.nil?
430
+ saved_wait_time = Capybara.default_max_wait_time
431
+ Capybara.default_max_wait_time = 0.01
432
+ tries ||= 4
433
+ attributes = [:text, :name, :id, :css, :xpath]
434
+ type = attributes[tries]
435
+ obj = page.find(type, locator)
436
+ [obj, type]
437
+ rescue
438
+ Capybara.default_max_wait_time = saved_wait_time
439
+ retry if (tries -= 1) > 0
440
+ [nil, nil]
441
+ ensure
442
+ Capybara.default_max_wait_time = saved_wait_time
443
+ end
444
+
445
+ def object_not_found_exception(obj, obj_type)
446
+ @alt_locator.nil? ? locator = @locator : locator = @alt_locator
447
+ obj_type.nil? ? object_type = "Object" : object_type = obj_type
448
+ raise "#{object_type} #{locator} not found" unless obj
449
+ end
450
+
451
+ def invalid_object_type_exception(obj, obj_type)
452
+ unless obj.tag_name == obj_type || obj.native.attribute('type') == obj_type
453
+ @alt_locator.nil? ? locator = @locator : locator = @alt_locator
454
+ raise "#{locator} is not a #{obj_type} element"
455
+ end
456
+ end
457
+
458
+ def set_table_cell_locator(row, column)
459
+ row_spec = "#{@locator}/tbody/tr"
460
+ row_spec = "#{row_spec}[#{row}]" if row > 1
461
+ column_spec = "/td[#{column}]"
462
+ set_alt_locator("#{row_spec}#{column_spec}")
463
+ end
464
+ end
465
+ end