awetestlib 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,1179 @@
1
+ module Validations
2
+
3
+ def self.included(mod)
4
+ # puts "RegressionSupport::Validations extended by #{mod}"
5
+ end
6
+
7
+ def modal_exists?(browser, button = nil)
8
+ rtrn = nil
9
+ if @browserAbbrev == 'IE'
10
+ Timeout::timeout(2) do
11
+ begin
12
+ if browser.enabled_popup
13
+ hwnd = browser.enabled_popup(5)
14
+ debug_to_log("Modal popup with handle #{hwnd} found. (#{__LINE__})")
15
+ wc = WinClicker.new
16
+ wc.makeWindowActive(hwnd)
17
+ rtrn = wc.getWindowTitle(hwnd)
18
+ if button
19
+ wc.clickWindowsButton_hWnd(hwnd, button)
20
+ end
21
+ wc = nil
22
+ end
23
+ rescue Timeout::Error
24
+ debug_to_log("No Modal popup found. (#{__LINE__})")
25
+ return rtrn
26
+ end
27
+ return rtrn
28
+ end
29
+ rtrn
30
+ else
31
+ rtrn
32
+ end
33
+ end
34
+
35
+ def validate_message(browser, message)
36
+ if validate(browser, @myName, __LINE__)
37
+ message_to_log(message)
38
+ end
39
+ end
40
+
41
+ def validate_style_value(browser, element, how, what, type, expected, desc = '')
42
+ #TODO: works only with watir-webdriver
43
+ msg = "Expected CSS style #{type} value '#{expected}' in #{element} with #{how} = #{what}"
44
+ msg << " #{desc}" if desc.length > 0
45
+ case element
46
+ when :link
47
+ actual = browser.link(how => what).style type
48
+ when :button
49
+ actual = browser.button(how => what).style type
50
+ when :image
51
+ actual = browser.image(how => what).style type
52
+ when :span
53
+ actual = browser.span(how => what).style type
54
+ when :div
55
+ actual = browser.div(how => what).style type
56
+ end
57
+ if expected == actual
58
+ passed_to_log(msg)
59
+ else
60
+ failed_to_log(msg)
61
+ end
62
+ rescue
63
+ failed_to_log( "Unable to validate #{msg} '#{$!}'")
64
+ end
65
+
66
+ ##### begin core validation methods #####
67
+
68
+ def arrays_match?(exp, act, dir, col, org = nil)
69
+ if exp == act
70
+ passed_to_log("Click on #{dir} column '#{col}' produces expected sorted list.")
71
+ true
72
+ else
73
+ failed_to_log("Click on #{dir} column '#{col}' fails to produce expected sorted list.")
74
+ debug_to_log("Original order ['#{org.join("', '")}']") if org
75
+ debug_to_log("Expected order ['#{exp.join("', '")}']")
76
+ debug_to_log(" Actual order ['#{act.join("', '")}']")
77
+ end
78
+ end
79
+
80
+ alias arrays_match arrays_match?
81
+
82
+ def enabled?(browser, element, how, what, desc = '')
83
+ msg = "#{element.to_s.titlecase} by #{how}=>'#{what}' is enabled.}"
84
+ msg << " #{desc}" if desc.length > 0
85
+ case element
86
+ when :textfield, :textarea, :text_area, :text_field
87
+ rtrn = browser.text_field(how, what).enabled? and not browser.text_field(how, what).readonly?
88
+ when :select_list, :selectlist
89
+ rtrn = browser.select_list(how, what).enabled?
90
+ else
91
+ rtrn = browser.element(how, what).enabled?
92
+ end
93
+ if rtrn
94
+ passed_to_log("#{msg}")
95
+ true
96
+ else
97
+ failed_to_log("#{msg}")
98
+ end
99
+ rtrn
100
+ rescue
101
+ failed_to_log("#Unable to verify that #{msg}': '#{$!}")
102
+ end
103
+
104
+ alias validate_enabled enabled?
105
+
106
+ def date_string_equals?(actual, expected, desc = '', fail_on_format = true)
107
+ rtrn = false
108
+ msg = "Assert actual date '#{actual}' equals expected date '#{expected}'. #{desc} "
109
+ if actual == expected
110
+ rtrn = true
111
+ elsif DateTime.parse(actual).to_s == DateTime.parse(expected).to_s
112
+ msg << " with different formatting. "
113
+ if not fail_on_format
114
+ rtrn = true
115
+ end
116
+ end
117
+ msg << " #{desc}" if desc.length > 0
118
+ if rtrn
119
+ passed_to_log("#{msg}")
120
+ else
121
+ failed_to_log("#{msg}")
122
+ end
123
+ rtrn
124
+ rescue
125
+ failed_to_log("Unable to #{msg}. #{$!}")
126
+ end
127
+
128
+ def disabled?(browser, element, how, what, desc = '')
129
+ msg = "#{element.to_s.titlecase} by #{how}=>'#{what}' is disabled. #{desc}"
130
+ case element
131
+ when :textfield, :textarea, :text_area, :text_field
132
+ rtrn = browser.text_field(how, what).disabled? ||
133
+ browser.text_field(how, what).readonly?
134
+ when :select_list, :selectlist
135
+ rtrn = browser.select_list(how, what).disabled?
136
+ when :checkbox
137
+ rtrn = browser.checkbox(how, what).disabled?
138
+ when :radio
139
+ rtrn = browser.radio(how, what).disabled?
140
+ when :button
141
+ rtrn = browser.button(how, value).disabled?
142
+ else
143
+ msg = "#{__method__} does not yet support '#{element}'. #{desc}"
144
+ debug_to_log(msg)
145
+ raise msg
146
+ end
147
+ if rtrn
148
+ passed_to_log("#{msg}")
149
+ true
150
+ else
151
+ failed_to_log("#{msg}")
152
+ end
153
+ rtrn
154
+ rescue
155
+ failed_to_log("#Unable to verify that #{msg}: '#{$!}'")
156
+ end
157
+
158
+ alias validate_not_enabled disabled?
159
+ alias validate_disabled disabled?
160
+
161
+ def verify_text_in_table_with_text(table, text, value)
162
+ #TODO This needs clarification, renaming
163
+ msg = "Table :id=>#{table.id} with text '#{text} contains '#{value}."
164
+ index = get_index_of_row_with_text(table, text)
165
+ if table[index].text =~ value
166
+ passed_to_log(msg)
167
+ true
168
+ else
169
+ failed_to_log(msg)
170
+ end
171
+ end
172
+
173
+ def visible?(browser, element, how, what, desc = '')
174
+ msg = "#{element.to_s.titlecase} #{how}=>'#{what}' is visible. #{desc}"
175
+ rtrn = false
176
+ case how
177
+ when :index
178
+ target = get_element(browser, element, how, what)
179
+ if target.visible?
180
+ rtrn = true
181
+ end
182
+ else
183
+ if browser.element(how, what).visible?
184
+ rtrn = true
185
+ end
186
+ end
187
+ if rtrn
188
+ passed_to_log("#{msg}")
189
+ else
190
+ failed_to_log("#{msg}")
191
+ end
192
+ rtrn
193
+ rescue
194
+ failed_to_log("Unable to verify that #{msg}': '#{$!}'")
195
+ end
196
+
197
+ alias validate_visible visible?
198
+
199
+ def not_visible?(browser, element, how, what, desc = '')
200
+ msg = "#{element.to_s.titlecase} #{how}=>'#{what}' is not visible. #{desc}"
201
+ rtrn = false
202
+ case how
203
+ when :index
204
+ target = get_element(browser, element, how, what)
205
+ if not target.visible?
206
+ rtrn = true
207
+ end
208
+ else
209
+ if not browser.element(how, what).visible?
210
+ rtrn = true
211
+ end
212
+ end
213
+ if rtrn
214
+ passed_to_log("#{msg}")
215
+ else
216
+ failed_to_log("#{msg}")
217
+ end
218
+ rtrn
219
+ rescue
220
+ failed_to_log("Unable to verify that #{msg}': '#{$!}' #{desc}")
221
+ end
222
+
223
+ alias validate_not_visible not_visible?
224
+
225
+ def checked?(browser, how, what, desc = '')
226
+ msg = "Checkbox #{how}=>#{what} is checked."
227
+ msg << " #{desc}" if desc.length > 0
228
+ if browser.checkbox(how, what).checked?
229
+ if validate(browser, @myName, __LINE__)
230
+ passed_to_log(msg)
231
+ true
232
+ end
233
+ else
234
+ failed_to_log(msg)
235
+ end
236
+ rescue
237
+ failed_to_log("Unable to validate #{msg}: '#{$!}'")
238
+ end
239
+
240
+ alias checkbox_checked? checked?
241
+ alias checkbox_set? checked?
242
+
243
+ def not_checked?(browser, how, what, desc = '')
244
+ msg = "Checkbox #{how}=>#{what} is not checked."
245
+ msg << " #{desc}" if desc.length > 0
246
+ if not browser.checkbox(how, what).checked?
247
+ if validate(browser, @myName, __LINE__)
248
+ passed_to_log(msg)
249
+ true
250
+ end
251
+ else
252
+ failed_to_log(msg)
253
+ end
254
+ rescue
255
+ failed_to_log("Unable to validate #{msg}: '#{$!}'")
256
+ end
257
+
258
+ alias checkbox_checked? checked?
259
+ alias checkbox_set? checked?
260
+
261
+ def exists?(browser, element, how, what, value = nil, desc = '')
262
+ msg = "#{element.to_s.titlecase} with #{how}=>'#{what}' "
263
+ msg << "and value=>'#{value}' " if value
264
+ msg << "exists"
265
+ e = get_element(browser, element, how, what, value)
266
+ if e
267
+ passed_to_log("#{msg}? #{desc}")
268
+ true
269
+ else
270
+ failed_to_log("#{msg}? #{desc} [#{get_callers(1)}]")
271
+ end
272
+ rescue
273
+ failed_to_log("Unable to determine if #{msg}. #{desc} '#{$!}' [#{get_callers(1)}]")
274
+ end
275
+
276
+ def does_not_exist?(browser, element, how, what, value = nil, desc = '')
277
+ msg = "#{element.to_s.titlecase} with #{how}=>'#{what}' "
278
+ msg << "and value=>'#{value}' " if value
279
+ msg << "does not exist."
280
+ msg << " #{desc}" if desc.length > 0
281
+ if browser.element(how, what).exists?
282
+ failed_to_log(msg)
283
+ else
284
+ passed_to_log(msg)
285
+ true
286
+ end
287
+ rescue
288
+ failed_to_log("Unable to verify that #{msg}': '#{$!}' #{desc}")
289
+ end
290
+
291
+ alias not_exist? does_not_exist?
292
+
293
+ def set?(browser, how, what, desc = '', no_fail = false)
294
+ #TODO Needs to handle radio value as well
295
+ msg = "Radio #{how}=>#{what} is selected."
296
+ msg << " #{desc}" if desc.length > 0
297
+ if browser.radio(how, what).set?
298
+ if validate(browser, @myName, __LINE__)
299
+ passed_to_log(msg)
300
+ true
301
+ end
302
+ else
303
+ if no_fail
304
+ passed_to_log("Radio #{how}=>#{what} is not selected.")
305
+ else
306
+ failed_to_log(msg)
307
+ end
308
+ end
309
+ rescue
310
+ failed_to_log("Unable to validate #{msg}: '#{$!}'")
311
+ end
312
+
313
+ alias radio_set? set?
314
+ alias radio_checked? set?
315
+ alias radio_selected? set?
316
+
317
+ def not_set?(browser, how, what, desc = '', no_fail = false)
318
+ #TODO Needs to handle radio value as well
319
+ msg = "Radio #{how}=>#{what} is not selectedd."
320
+ msg << " #{desc}" if desc.length > 0
321
+ if not browser.radio(how, what).set?
322
+ if validate(browser, @myName, __LINE__)
323
+ passed_to_log(msg)
324
+ true
325
+ end
326
+ else
327
+ if no_fail
328
+ passed_to_log("Radio #{how}=>#{what} is not selected.")
329
+ else
330
+ failed_to_log(msg)
331
+ end
332
+ end
333
+ rescue
334
+ failed_to_log("Unable to validate #{msg}: '#{$!}'")
335
+ end
336
+
337
+ alias radio_not_set? not_set?
338
+ alias radio_not_checked? not_set?
339
+ alias radio_not_selected? not_set?
340
+
341
+ def radio_with_value_set?(browser, how, what, value, desc = '', no_fail = false)
342
+ msg = "Radio #{how}=>#{what} :value=>#{value} is selected."
343
+ msg << " #{desc}" if desc.length > 0
344
+ if browser.radio(how, what, value).set?
345
+ if validate(browser, @myName, __LINE__)
346
+ passed_to_log(msg)
347
+ true
348
+ end
349
+ else
350
+ if no_fail
351
+ passed_to_log("Radio #{how}=>#{what} :value=>#{value} is not selected.")
352
+ else
353
+ failed_to_log(msg)
354
+ end
355
+ end
356
+ rescue
357
+ failed_to_log("Unable to validate #{msg}: '#{$!}'")
358
+ end
359
+
360
+ alias radio_set_with_value? radio_with_value_set?
361
+
362
+ def select_list_includes?(browser, how, what, option, desc = '')
363
+ msg = "Select list #{how}=>#{what} includes option '#{option}'."
364
+ msg << " #{desc}" if desc.length > 0
365
+ select_list = browser.select_list(how, what)
366
+ options = select_list.options
367
+ if option
368
+ if options.include?(option)
369
+ passed_to_log(msg)
370
+ true
371
+ else
372
+ failed_to_log(msg)
373
+ nil
374
+ end
375
+ end
376
+ rescue
377
+ failed_to_log("Unable to verify #{msg}. '#{$!}'")
378
+ end
379
+
380
+ alias validate_select_list_contains select_list_includes?
381
+ alias select_list_contains? select_list_includes?
382
+
383
+ def select_list_does_not_include?(browser, how, what, option, desc = '')
384
+ msg = "Select list #{how}=>#{what} does not include option '#{option}'."
385
+ msg << " #{desc}" if desc.length > 0
386
+ select_list = browser.select_list(how, what)
387
+ options = select_list.options
388
+ if option
389
+ if not options.include?(option)
390
+ passed_to_log(msg)
391
+ true
392
+ else
393
+ failed_to_log(msg)
394
+ nil
395
+ end
396
+ end
397
+ rescue
398
+ failed_to_log("Unable to verify #{msg}. '#{$!}'")
399
+ end
400
+
401
+ def string_equals?(actual, target, desc = '')
402
+ msg = "Assert actual '#{actual}' equals expected '#{target}'. #{desc} "
403
+ if actual == target
404
+ passed_to_log("#{msg}")
405
+ true
406
+ else
407
+ failed_to_log("#{msg}")
408
+ end
409
+ rescue
410
+ failed_to_log("Unable to #{msg}. #{$!}")
411
+ end
412
+
413
+ alias validate_string_equal string_equals?
414
+ alias validate_string_equals string_equals?
415
+ alias text_equals string_equals?
416
+ alias text_equals? string_equals?
417
+
418
+ def string_does_not_equal?(strg, target, desc = '')
419
+ msg = "String '#{strg}' does not equal '#{target}'."
420
+ msg << " '#{desc}' " if desc.length > 0
421
+ if strg == target
422
+ failed_to_log("#{msg} (#{__LINE__})")
423
+ true
424
+ else
425
+ passed_to_log("#{msg} (#{__LINE__})")
426
+ end
427
+ end
428
+
429
+ alias validate_string_not_equal string_does_not_equal?
430
+ alias validate_string_does_not_equal string_does_not_equal?
431
+
432
+ def read_only?(browser, element, how, what, value = nil, desc = '')
433
+ msg = "#{element.to_s.titlecase} with #{how}=>'#{what}' "
434
+ msg << "and value=>'#{value}' " if value
435
+ msg << "read only"
436
+ e = get_element(browser, element, how, what, value)
437
+ if e
438
+ if e.readonly?
439
+ passed_to_log("#{msg}? #{desc}")
440
+ true
441
+ else
442
+ failed_to_log("#{msg}? #{desc} [#{get_callers(1)}]")
443
+ end
444
+ end
445
+ rescue
446
+ failed_to_log("Unable to determine if #{msg}. #{desc} '#{$!}' [#{get_callers(1)}]")
447
+ end
448
+
449
+ def not_read_only?(browser, element, how, what, value = nil, desc = '')
450
+ msg = "#{element.to_s.titlecase} with #{how}=>'#{what}' "
451
+ msg << "and value=>'#{value}' " if value
452
+ msg << "is not read only"
453
+ e = get_element(browser, element, how, what, value)
454
+ if e
455
+ if e.readonly?
456
+ failed_to_log("#{msg}? #{desc} [#{get_callers(1)}]")
457
+ else
458
+ passed_to_log("#{msg}? #{desc}")
459
+ true
460
+ end
461
+ end
462
+ rescue
463
+ failed_to_log("Unable to determine if #{msg}. #{desc} '#{$!}' [#{get_callers(1)}]")
464
+ end
465
+
466
+ def ready?(browser, element, how, what, value = '', desc = '')
467
+ msg = "#{element.to_s.titlecase} with #{how}=>'#{what}' "
468
+ msg << "and value=>'#{value}' " if value
469
+ e = get_element(browser, element, how, what, value)
470
+ if e and e.enabled?
471
+ passed_to_log("#{msg}? #{desc}")
472
+ true
473
+ else
474
+ failed_to_log("#{msg}? #{desc} [#{get_callers(1)}]")
475
+ end
476
+ rescue
477
+ failed_to_log("Unable to determine if #{msg}. #{desc} '#{$!}' [#{get_callers(1)}]")
478
+ end
479
+
480
+ ##### end core validation methods #####
481
+
482
+ ##### begin methods using @ai #####
483
+
484
+ def window_exists?(title)
485
+ title = translate_popup_title(title)
486
+ if @ai.WinExists(title) == 1
487
+ passed_to_log("Window title:'#{title}' exists")
488
+ true
489
+ else
490
+ failed_to_log("Window title:'#{title}' does not exist")
491
+ end
492
+ end
493
+
494
+ alias window_exists window_exists?
495
+
496
+ def window_does_not_exist?(title)
497
+ title = translate_popup_title(title)
498
+ if @ai.WinExists(title) == 1
499
+ failed_to_log("Window title:'#{title}' exists")
500
+ else
501
+ passed_to_log("Window title:'#{title}' does not exist")
502
+ true
503
+ end
504
+ end
505
+
506
+ alias window_no_exists window_does_not_exist?
507
+
508
+ ##### end methods using @ai #####
509
+
510
+ ##### backward compatible methods #####
511
+
512
+ def validate_link_exist(browser, link, logit = true, desc = '')
513
+ exists?(browser, :link, :text, link, nil, desc)
514
+ end
515
+
516
+ def link_not_exist?(browser, link, desc = '')
517
+ does_not_exist?(browser, :link, :text, link, nil, desc)
518
+ end
519
+
520
+ alias validate_link_not_exist link_not_exist?
521
+
522
+ def validate_div_visible_by_id(browser, strg)
523
+ visible?(browser, :div, :id, strg)
524
+ end
525
+
526
+ def validate_div_not_visible_by_id(browser, strg, desc = '')
527
+ not_visible?(browser, :div, :id, strg, desc)
528
+ end
529
+
530
+ ##### end backward compatible methods #####
531
+
532
+
533
+ def link_enabled?(browser, strg)
534
+ #TODO Use enabled?()
535
+ count = string_count_in_string(browser.text, strg)
536
+ if count > 0
537
+ if browser.link(:text, strg).enabled?
538
+ if validate(browser, @myName, __LINE__)
539
+ passed_to_log(strg + " is enabled. (#{__LINE__})")
540
+ true
541
+ end
542
+ else
543
+ failed_to_log(strg + " is not enabled.")
544
+ end
545
+ else
546
+ failed_to_log("Link '#{strg.to_s}' (by :text) not found. Cannot validate if enabled. (#{__LINE__}) " + desc)
547
+ end
548
+ rescue
549
+ failed_to_log("Unable to validate that link with :text '#{text}' is enabled: '#{$!}'. (#{__LINE__})")
550
+ debug_to_log("#{strg} appears #{count} times in browser.text.")
551
+ end
552
+
553
+ alias validate_link_enabled link_enabled?
554
+
555
+ def link_disabled?(browser, strg)
556
+ #TODO use disabled?()
557
+ count = string_count_in_string(browser.text, strg)
558
+ if count > 0
559
+ if browser.link(:text, strg).enabled?
560
+ if validate(browser, @myName, __LINE__)
561
+ failed_to_log(strg + " is enabled. (#{__LINE__})")
562
+ end
563
+ else
564
+ passed_to_log(strg + " is not enabled.")
565
+ true
566
+ end
567
+ else
568
+ failed_to_log("Link '#{strg.to_s}' (by :text) not found. Cannot validate if disabled. (#{__LINE__}) " + desc)
569
+ end
570
+ rescue
571
+ failed_to_log("Unable to validate that link with :text '#{text}' is enabled: '#{$!}'. (#{__LINE__})")
572
+ debug_to_log("#{strg} appears #{count} times in browser.text.")
573
+ end
574
+
575
+ alias validate_link_not_enabled link_disabled?
576
+
577
+ def popup_exists?(popup, message=nil)
578
+ if not message
579
+ message = "Popup: #{popup.title}"
580
+ end
581
+ if is_browser?(popup)
582
+ passed_to_log("#{message}: found.")
583
+ debug_to_log("\n"+popup.text+"\n")
584
+ true
585
+ else
586
+ failed_to_log("#{message}: not found." + " (#{__LINE__})")
587
+ end
588
+ rescue
589
+ failed_to_log("Unable to validate existence of popup: '#{$!}'. (#{__LINE__})")
590
+ end
591
+
592
+ alias popup_exist popup_exists?
593
+ alias popup_exists popup_exists?
594
+ alias popup_exist? popup_exists?
595
+ alias iepopup_exist popup_exists?
596
+ alias iepopup_exist? popup_exists?
597
+ alias iepopup_exists popup_exists?
598
+ alias iepopup_exists? popup_exists?
599
+
600
+ def validate_drag_drop(err, tol, exp, act)
601
+ ary = [false, "failed, expected: #{exp}, actual: #{act}, err: #{err}"]
602
+ if err == 0
603
+ ary = [true, 'succeeded ']
604
+ elsif err.abs <= tol
605
+ ary = [true, "within tolerance (+-#{tol}px) "]
606
+ end
607
+ ary
608
+ end
609
+
610
+ def validate_list(browser, listId, text, message)
611
+ message_to_log("Method validate_list() is deprecated: use validate_list_by_xxx instead")
612
+ validate_list_by_id(browser, listId, text, message)
613
+ end
614
+
615
+ #Validate select list contains text
616
+ def validate_list_by_id(browser, strg, text, message, select_if_present=true)
617
+ #TODO Use select_list_includes?() ?
618
+ if browser.select_list(:id, strg).exists?
619
+ select_list = browser.select_list(:id, strg)
620
+ if select_list.include?(text)
621
+ if select_if_present
622
+ if select_option_by_id_and_option_text(browser, strg, text)
623
+ if validate(browser, @myName, __LINE__)
624
+ passed_to_log(message)
625
+ true
626
+ end
627
+ else
628
+ failed_to_log(message + " (#{__LINE__})")
629
+ end
630
+ else
631
+ if validate(browser, @myName, __LINE__)
632
+ passed_to_log(message)
633
+ true
634
+ end
635
+ end
636
+ else
637
+ failed_to_log(message + " Not found. (#{__LINE__})")
638
+ end
639
+ else
640
+ failed_to_log("Select list with id='#{strg} not found. (#{__LINE__})")
641
+ end
642
+ rescue
643
+ failed_to_log("Unable to validate selectlist with id='#{strg}: '#{$!}'. (#{__LINE__})")
644
+ end
645
+
646
+ #Validate select list contains text
647
+ def validate_list_by_name(browser, strg, text, message, select_if_present=true)
648
+ #TODO Use select_list_includes?() ?
649
+ if browser.select_list(:name, strg).exists?
650
+ select_list = browser.select_list(:name, strg)
651
+ if select_list.include?(text)
652
+ if select_if_present
653
+ if select_option_by_name_and_option_text(browser, strg, text)
654
+ if validate(browser, @myName, __LINE__)
655
+ passed_to_log(message)
656
+ true
657
+ end
658
+ else
659
+ failed_to_log(message + " (#{__LINE__})")
660
+ end
661
+ else
662
+ if validate(browser, @myName, __LINE__)
663
+ passed_to_log(message)
664
+ true
665
+ end
666
+ end
667
+ else
668
+ failed_to_log(message + " Not found. (#{__LINE__})")
669
+ end
670
+ else
671
+ failed_to_log("Select list with name='#{strg} not found. (#{__LINE__})")
672
+ end
673
+ rescue
674
+ failed_to_log("Unable to validate that '#{text}' appeared in select list with name='#{strg}: '#{$!}'. (#{__LINE__})")
675
+ end
676
+
677
+ #Validate select list does not contain text
678
+ def validate_no_list(browser, id, text, desc = '')
679
+ select_list_does_not_include?(browser, :id, id, text, desc)
680
+ end
681
+
682
+ def validate_text(browser, ptrn, desc = '', skip_fail = false, skip_sleep = false)
683
+ cls = browser.class.to_s
684
+ cls.gsub!('Watir::', '')
685
+ cls.gsub!('IE', 'Browser')
686
+ msg = "#{cls} text contains '#{ptrn}'."
687
+ msg << " #{desc}" if desc.length > 0
688
+ if ptrn.is_a?(Regexp)
689
+ target = ptrn
690
+ else
691
+ target = Regexp.new(Regexp.escape(ptrn))
692
+ end
693
+ sleep_for(2) unless skip_sleep
694
+ myText = browser.text
695
+ if not myText.match(target)
696
+ sleep_for(2) unless skip_sleep #TODO try a wait_until here
697
+ myText = browser.text
698
+ end
699
+ if myText.match(target)
700
+ #if myText.match(ptrn)
701
+ if validate(browser, @myName, __LINE__)
702
+ passed_to_log("#{msg}")
703
+ true
704
+ end
705
+ else
706
+ if skip_fail
707
+ debug_to_log("#{cls} text does not contain the text: '#{ptrn}'. #{desc}")
708
+ else
709
+ failed_to_log("#{msg}")
710
+ end
711
+ #debug_to_log("\n#{myText}")
712
+ end
713
+ rescue
714
+ failed_to_log("Unable to validate #{msg} '#{$!}'")
715
+ end
716
+
717
+ alias validate_link validate_text
718
+
719
+ def text_in_element_equals?(browser, element, how, what, expected, desc = '')
720
+ msg = "Expected exact text '#{expected}' in #{element} :#{how}=>#{what}."
721
+ msg << " #{desc}" if desc.length > 0
722
+ text = ''
723
+ who = browser.element(how, what)
724
+ if who
725
+ text = who.text
726
+ if text == expected
727
+ passed_to_log(msg)
728
+ true
729
+ else
730
+ debug_to_log("exp: [#{expected.gsub(' ', '^')}]")
731
+ debug_to_log("act: [#{text.gsub(' ', '^')}]")
732
+ failed_to_log("#{msg} Found '#{text}'.")
733
+ end
734
+ end
735
+ rescue
736
+ failed_to_log("Unable to verify #{msg} '#{$!}'")
737
+ end
738
+
739
+ def text_in_span_equals?(browser, how, what, expected, desc = '')
740
+ text_in_element_equals?(browser, :span, how, what, expected, desc)
741
+ end
742
+
743
+ def element_contains_text?(browser, element, how, what, expected, desc = '')
744
+ msg = "Element #{element} :{how}=>#{what} contains text '#{expected}'."
745
+ msg << " #{desc}" if desc.length > 0
746
+ who = browser.element(how, what)
747
+ if who
748
+ text = who.text
749
+ if expected and expected.length > 0
750
+ rgx = Regexp.new(Regexp.escape(expected))
751
+ if text =~ rgx
752
+ passed_to_log(msg)
753
+ true
754
+ else
755
+ debug_to_log("exp: [#{expected.gsub(' ', '^')}]")
756
+ debug_to_log("act: [#{text.gsub(' ', '^')}]")
757
+ failed_to_log("#{msg} Found '#{text}'. #{desc}")
758
+ end
759
+ else
760
+ if text.length > 0
761
+ debug_to_log("exp: [#{expected.gsub(' ', '^')}]")
762
+ debug_to_log("act: [#{text.gsub(' ', '^')}]")
763
+ failed_to_log("#{msg} Found '#{text}'. #{desc}")
764
+ else
765
+ passed_to_log(msg)
766
+ true
767
+ end
768
+ end
769
+ end
770
+ rescue
771
+ failed_to_log("Unable to verify #{msg} '#{$!}'")
772
+ end
773
+
774
+ def span_contains_text?(browser, how, what, expected, desc = '')
775
+ element_contains_text?(browser, :span, how, what, expected, desc)
776
+ end
777
+
778
+ alias valid_text_in_span span_contains_text?
779
+
780
+ def validate_text_in_span_by_id(browser, id, strg = '', desc = '')
781
+ element_contains_text?(browser, :span, :id, id, strg, desc)
782
+ end
783
+
784
+ def validate_url(browser, url, message = '')
785
+ if browser.url.to_s.match(url)
786
+ if validate(browser, @myName, __LINE__)
787
+ passed_to_log('Found "'+url.to_s+'" ' + message)
788
+ true
789
+ end
790
+ else
791
+ failed_to_log('Did not find "'+url.to_s+'" ' + message + " (#{__LINE__})")
792
+ end
793
+ rescue
794
+ failed_to_log("Unable to validate that current url is '#{url}': '#{$!}'. (#{__LINE__})")
795
+ end
796
+
797
+ def validate_select_list(browser, how, what, opt_type, list = nil, multiple = false, ignore = ['Select One'], limit = 5)
798
+ mark_testlevel("#{__method__.to_s.titleize} (#{how}=>#{what})", 2)
799
+ ok = true
800
+ select_list = browser.select_list(how, what)
801
+ options = select_list.options
802
+ if list
803
+ if options == list
804
+ passed_to_log("Select list options list equals expected list #{list}")
805
+ else
806
+ debug_to_report("actual:\n#{nice_array(options, true)}")
807
+ debug_to_report("expected:\n#{nice_array(list, true)}")
808
+ failed_to_log("Select list options list #{nice_array(options, true)} "+
809
+ "does not equal expected list #{nice_array(list, true)}")
810
+ end
811
+ end
812
+
813
+ #single selections
814
+ cnt = 0
815
+ options.each do |opt|
816
+ if not ignore.include?(opt)
817
+ cnt += 1
818
+ ok = select_option(select_list, opt_type, opt)
819
+ break if not ok
820
+ select_list.clear
821
+ break if limit > 0 and cnt >= limit
822
+ end
823
+ end
824
+
825
+ sleep_for(0.5)
826
+ select_list.clear
827
+ if ok and multiple
828
+ if options.length > 2
829
+ targets = list.slice(1, 2)
830
+ select_option(select_list, opt_type, options[1])
831
+ select_option(select_list, opt_type, options[2])
832
+ selected = select_list.selected_options
833
+ if selected == targets
834
+ passed_to_log("Select list selected options equals expected #{targets}")
835
+ else
836
+ failed_to_log("Select list selected options #{selected} does not equal expected list #{targets.to_a}")
837
+ end
838
+ else
839
+ debug_to_log("Too few options to test multiple selection (need 2 or more): '#{options}", __LINE__)
840
+ end
841
+ end
842
+ rescue
843
+ failed_to_log("Unable to validate select_list: '#{$!}'", __LINE__)
844
+ end
845
+
846
+ def validate_select_list_contents(browser, how, what, list)
847
+ mark_testlevel("#{__method__.to_s.titleize} (#{what})", 2)
848
+ select_list = browser.select_list(how, what)
849
+ options = select_list.options
850
+ if list
851
+ if options == list
852
+ passed_to_log("Select list options list equals expected list #{list}")
853
+ options
854
+ else
855
+ failed_to_log("Select list options list #{options} does not equal expected list #{list}")
856
+ nil
857
+ end
858
+ end
859
+ rescue
860
+ failed_to_log("Unable to validate select_list contents: '#{$!}'", __LINE__)
861
+ end
862
+
863
+ def validate_selected_options(browser, how, what, list, desc = '')
864
+ select_list = browser.select_list(how, what)
865
+ selected = select_list.selected_options.sort
866
+ if list.is_a?(Array)
867
+ if selected == list.sort
868
+ passed_to_log("Expected options [#{list.sort}] are selected [#{selected}]. #{desc}")
869
+ else
870
+ failed_to_log("Selected options [#{selected}] do not match expected [#{list.sort}]. #{desc}")
871
+ true
872
+ end
873
+ else
874
+ if selected.length == 1
875
+ if selected[0] =~ /#{list}/
876
+ passed_to_log("Expected option [#{list}] was selected. #{desc}")
877
+ true
878
+ else
879
+ failed_to_log("Expected option [#{list}] was not selected. Found [#{selected}]. #{desc}")
880
+ end
881
+ else
882
+ if selected.include?(list)
883
+ failed_to_log("Expected option [#{list}] was found among multiple selections [#{selected}]. #{desc}")
884
+ else
885
+ failed_to_log("Expected option [#{list}] was not found among multiple selections [#{selected}]. #{desc}")
886
+ end
887
+ end
888
+ end
889
+
890
+ rescue
891
+ failed_to_log("Unable to validate selected option(s): '#{$!}' #{desc}", __LINE__)
892
+ end
893
+
894
+ alias validate_selections validate_selected_options
895
+ alias validate_select_list_selections validate_selected_options
896
+
897
+ def string_contains?(strg, target, desc = '')
898
+ msg = "String '#{strg}' contains '#{target}'."
899
+ msg << " '#{desc}' " if desc.length > 0
900
+ if strg.match(target)
901
+ passed_to_log("#{msg} (#{__LINE__})")
902
+ true
903
+ else
904
+ failed_to_log("#{msg} (#{__LINE__})")
905
+ end
906
+ end
907
+
908
+ alias validate_string string_contains?
909
+ alias validate_string_contains string_contains?
910
+
911
+ def string_does_not_contain?(strg, target, desc = '')
912
+ msg = "String '#{strg}' does not contain '#{target}'."
913
+ msg << " '#{desc}' " if desc.length > 0
914
+ if strg.match(target)
915
+ failed_to_log("#{msg} (#{__LINE__})")
916
+ true
917
+ else
918
+ passed_to_log("#{msg} (#{__LINE__})")
919
+ end
920
+ end
921
+
922
+ alias validate_string_not_contains string_does_not_contain?
923
+ alias validate_string_not_contain string_does_not_contain?
924
+ alias validate_string_does_not_contain string_does_not_contain?
925
+
926
+ def validate_no_text(browser, ptrn, desc = '')
927
+ cls = browser.class.to_s
928
+ cls.gsub!('Watir::', '')
929
+ cls.gsub!('IE', 'Browser')
930
+ msg = "#{cls} does not contain text '#{ptrn}'."
931
+ msg << " #{desc}" if desc.length > 0
932
+ if ptrn.is_a?(Regexp)
933
+ target = ptrn
934
+ else
935
+ target = Regexp.new(Regexp.escape(ptrn))
936
+ end
937
+ browser_text = browser.text
938
+ if browser_text.match(target)
939
+ if validate(browser, @myName, __LINE__)
940
+ failed_to_log("#{msg} [#{browser_text.match(target)[0]}]")
941
+ end
942
+ else
943
+ passed_to_log(msg)
944
+ true
945
+ end
946
+ rescue
947
+ failed_to_log("Unable to validate #{msg}: '#{$!}'")
948
+ end
949
+
950
+ def textfield_does_not_equal?(browser, how, what, expected, desc = '')
951
+ msg = "Text field #{how}=>#{what} does not equal '#{expected}'"
952
+ msg << " #{desc}" if desc.length > 0
953
+ if not browser.text_field(how, what).value == expected
954
+ if validate(browser, @myName, __LINE__)
955
+ passed_to_log(msg)
956
+ true
957
+ end
958
+ else
959
+ failed_to_log(msg)
960
+ end
961
+ rescue
962
+ failed_to_log("Unable to validate that #{msg}: '#{$!}'")
963
+ end
964
+
965
+ alias validate_textfield_not_value textfield_does_not_equal?
966
+
967
+ ###################################
968
+ def validate_textfield_not_value_by_name(browser, name, value, desc = '')
969
+ textfield_does_not_equal?(browser, :name, name, value, desc)
970
+ end
971
+
972
+ alias validate_textfield_no_value_by_name validate_textfield_not_value_by_name
973
+
974
+ ###################################
975
+ def validate_textfield_not_value_by_id(browser, id, value, desc = '')
976
+ textfield_does_not_equal?(browser, :id, id, value, desc)
977
+ end
978
+
979
+ alias validate_textfield_no_value_by_id validate_textfield_not_value_by_id
980
+
981
+ def textfield_empty?(browser, how, what, desc = '')
982
+ msg = "Text field #{how}=>#{what} is empty."
983
+ msg << desc if desc.length > 0
984
+ value = browser.text_field(how, what).value
985
+ if value.to_s.length == 0
986
+ if validate(browser, @myName, __LINE__)
987
+ passed_to_log(msg)
988
+ true
989
+ end
990
+ else
991
+ failed_to_log("#{msg} Contains '#{value}'")
992
+ end
993
+ rescue
994
+ failed_to_log("Unable to validate #{msg} '#{$!}'")
995
+ end
996
+
997
+ alias validate_textfield_empty textfield_empty?
998
+ alias text_field_empty? textfield_empty?
999
+
1000
+ def validate_textfield_empty_by_name(browser, name, message = '')
1001
+ validate_textfield_empty(browser, :name, name, message)
1002
+ end
1003
+
1004
+ def validate_textfield_empty_by_id(browser, id, message = '')
1005
+ validate_textfield_empty(browser, :id, id, message)
1006
+ end
1007
+
1008
+ def validate_textfield_empty_by_title(browser, title, message = '')
1009
+ validate_textfield_empty(browser, :title, title, message)
1010
+ end
1011
+
1012
+ def textfield_equals?(browser, how, what, expected, desc = '')
1013
+ msg = "Expected '#{expected}' in textfield #{how}=>'#{what}'. #{desc}"
1014
+ actual = browser.text_field(how, what).value
1015
+ if actual.is_a?(Array)
1016
+ actual = actual[0].to_s
1017
+ end
1018
+ #debug_to_report("#{actual.inspect}")
1019
+ #debug_to_report("#{actual}")
1020
+ if actual == expected
1021
+ if validate(browser, @myName, __LINE__)
1022
+ passed_to_log("#{msg}")
1023
+ true
1024
+ end
1025
+ else
1026
+ act_s = actual.strip
1027
+ exp_s = expected.strip
1028
+ if act_s == exp_s
1029
+ if validate(browser, @myName, __LINE__)
1030
+ passed_to_log("#{msg} (stripped)")
1031
+ true
1032
+ end
1033
+ else
1034
+ debug_to_report(
1035
+ "#{__method__} (spaces underscored):\n "+
1036
+ "expected:[#{expected.gsub(' ', '_')}] (#{expected.length})\n "+
1037
+ "actual:[#{actual.gsub(' ', '_')}] (#{actual.length})"
1038
+ )
1039
+ failed_to_log("#{msg}. Found: '#{actual}'")
1040
+ end
1041
+ end
1042
+ rescue
1043
+ failed_to_log("Unable to validate #{msg}: '#{$!}")
1044
+ end
1045
+
1046
+ alias validate_textfield_value textfield_equals?
1047
+ alias text_field_equals? textfield_equals?
1048
+
1049
+ def validate_textfield_dollar_value(browser, how, what, expected, with_cents = true, desc = '')
1050
+ desc << " Dollar formatting"
1051
+ if with_cents
1052
+ expected << '.00' if not expected =~ /\.00$/
1053
+ desc << ' without cents.'
1054
+ else
1055
+ expected.gsub!(/\.00$/, '')
1056
+ desc << ' with cents.'
1057
+ end
1058
+ textfield_equals?(browser, how, what, expected, desc)
1059
+ end
1060
+
1061
+ def validate_textfield_value_by_name(browser, name, expected, desc = '')
1062
+ textfield_equals?(browser, :name, name, expected, desc)
1063
+ end
1064
+
1065
+ def validate_textfield_value_by_id(browser, id, expected, desc = '')
1066
+ textfield_equals?(browser, :id, id, expected, desc)
1067
+ end
1068
+
1069
+ def validate_textfield_visible_by_name(browser, strg, desc = '')
1070
+ visible?(browser, :text_field, :name, strg, desc)
1071
+ end
1072
+
1073
+ alias visible_textfield_by_name validate_textfield_visible_by_name
1074
+
1075
+ def validate_textfield_disabled_by_name(browser, strg, desc = '')
1076
+ disabled?(browser, :text_field, :name, strg, desc)
1077
+ end
1078
+
1079
+ alias disabled_textfield_by_name validate_textfield_disabled_by_name
1080
+
1081
+ def validate_textfield_enabled_by_name(browser, strg, desc = '')
1082
+ enabled?(browser, :text_field, :name, strg, desc)
1083
+ end
1084
+
1085
+ alias enabled_textfield_by_name validate_textfield_enabled_by_name
1086
+
1087
+ def validate_textfield_not_visible_by_name(browser, strg, desc = '')
1088
+ not_visible?(browser, :text_field, :name, strg, desc)
1089
+ end
1090
+
1091
+ alias visible_no_textfield_by_name validate_textfield_not_visible_by_name
1092
+
1093
+ def validate_radio_not_set(browser, radio, message)
1094
+ if browser.radio(:id, radio).checked?
1095
+ if validate(browser, @myName, __LINE__)
1096
+ failed_to_log(message + " (#{__LINE__})")
1097
+ end
1098
+ else
1099
+ passed_to_log(message)
1100
+ true
1101
+ end
1102
+ rescue
1103
+ failed_to_log("Unable to validate that radio with id='#{radio} is clear': '#{$!}'. (#{__LINE__})")
1104
+ end
1105
+
1106
+ alias validate_not_radioset validate_radio_not_set
1107
+
1108
+ def radio_is_set?(browser, radio, message)
1109
+ if browser.radio(:id, radio).checked?
1110
+ if validate(browser, @myName, __LINE__)
1111
+ passed_to_log(message)
1112
+ true
1113
+ end
1114
+ else
1115
+ failed_to_log(message + " (#{__LINE__})")
1116
+ end
1117
+ rescue
1118
+ failed_to_log("Unable to validate that radio with id='#{radio} is clear': '#{$!}'. (#{__LINE__})")
1119
+ end
1120
+
1121
+ alias validate_radioset radio_is_set?
1122
+ alias validate_radio_set radio_is_set?
1123
+
1124
+ def validate_radioset_by_name(browser, radio, message)
1125
+ if browser.radio(:name, radio).checked?
1126
+ if validate(browser, @myName, __LINE__)
1127
+ passed_to_log(message)
1128
+ true
1129
+ end
1130
+ else
1131
+ failed_to_log(message + " (#{__LINE__})")
1132
+ end
1133
+ rescue
1134
+ failed_to_log("Unable to validate that radio with name='#{radio} is clear': '#{$!}'. (#{__LINE__})")
1135
+ end
1136
+
1137
+ def checked_by_id?(browser, strg, desc = '')
1138
+ checked?(browser, :id, strg, desc)
1139
+ end
1140
+
1141
+ alias validate_check checked_by_id?
1142
+ alias checkbox_is_checked? checked_by_id?
1143
+
1144
+ def checkbox_is_enabled?(browser, strg, desc = '')
1145
+ enabled?(browser, :checkbox, :id, strg, desc)
1146
+ end
1147
+
1148
+ alias validate_check_enabled checkbox_is_enabled?
1149
+
1150
+ def checkbox_is_disabled?(browser, strg, desc = '')
1151
+ disabled?(browser, :checkbox, :id, strg, desc)
1152
+ end
1153
+
1154
+ alias validate_check_disabled checkbox_is_disabled?
1155
+
1156
+ def validate_check_by_class(browser, strg, desc)
1157
+ checked?(browser, :class, strg, desc)
1158
+ end
1159
+
1160
+ def checkbox_not_checked?(browser, strg, desc)
1161
+ not_checked?(browser, :id, strg, desc)
1162
+ end
1163
+
1164
+ alias validate_not_check checkbox_not_checked?
1165
+
1166
+ def validate_image(browser, source, desc = '', nofail=false)
1167
+ if browser.image(:src, source).exists?
1168
+ if validate(browser, @myName, __LINE__)
1169
+ passed_to_log("Found '#{source}' image. #{desc}")
1170
+ true
1171
+ end
1172
+ else
1173
+ failed_to_log("Did not find '#{source}' image. #{desc} (#{__LINE__})") unless nofail
1174
+ end
1175
+ rescue
1176
+ failed_to_log("Unable to validate that '#{+source}' image appeared in page: '#{$!}'. (#{__LINE__})")
1177
+ end
1178
+
1179
+ end