awetestlib 0.1.29pre4-x86-mingw32 → 0.1.29-x86-mingw32

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