awetestlib 0.0.3-x86-mingw32 → 0.1.0-x86-mingw32

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,2423 +1,5 @@
1
1
  module Legacy
2
2
 
3
- def run
4
- setup
5
- set_script_variables
6
- run_test
7
- rescue
8
- fatal_to_log("(#{__LINE__}) #{$!}")
9
- browser.close
10
- raise
11
- end
12
-
13
- # Place holder to prevent method not found error in scripts
14
- def set_script_variables
15
- # TODO: replace with method_missing?
16
- end
17
-
18
- def setup
19
- # if @os_sysname =~ /Windows.+Server\s+2003/
20
- ## 'Microsoft(R) Windows(R) Server 2003, Enterprise Edition'
21
- # @vertical_hack_ie = 110
22
- # @vertical_hack_ff = 138
23
- # @horizontal_hack_ie = 5
24
- # @horizontal_hack_ff = 4
25
- # elsif @os_sysname =~ /Windows XP Professional/
26
- # 'Microsoft Windows XP Professional'
27
- @vertical_hack_ie = 118
28
- @vertical_hack_ff = 144
29
- @horizontal_hack_ie = 5
30
- @horizontal_hack_ff = 4
31
- #end
32
-
33
- @settings_display_ids = Hash[
34
- "Currency" => "row-currencyName",
35
- "Description" => "row-description",
36
- "Tx Date" => "row-fmtDate",
37
- "Total" => "row-amount",
38
- "[currencyCode]" => "row-currencyCode",
39
- "Date in Millis" => "row-dateInMilliseconds",
40
- ]
41
- @column_data_display_ids = Hash[
42
- "Currency" => "yui-dt0-th-currencyName",
43
- "Description" => "yui-dt0-th-description",
44
- "Tx Date" => "yui-dt0-th-fmtDate",
45
- "Total" => "yui-dt0-th-fmtDate",
46
- "[currencyCode]" => "yui-dt0-th-currencyCode",
47
- "Date in Millis" => "yui-dt0-th-dateInMilliseconds",
48
- ]
49
- @settings_panel_index = 0
50
- @x_tolerance = 4
51
- @y_tolerance = 4
52
- end
53
-
54
- # :category: Waits
55
- def sleep_for(seconds, dbg = true, desc = '')
56
- msg = "Sleeping for #{seconds} seconds."
57
- msg << " #{desc}" if desc.length > 0
58
- msg << "\n#{get_debug_list}" if dbg
59
- info_to_log(msg)
60
- sleep(seconds)
61
- end
62
-
63
- # :category: Helpers
64
- def get_mdyy(t = Time.now)
65
- "#{t.month}/#{t.day}/#{t.year}"
66
- end
67
-
68
- # :category: Helpers
69
- def get_prefix(strg, offset)
70
- a_slice = strg.slice(0, offset)
71
- a_slice.downcase
72
- end
73
-
74
- # :category: Helpers
75
- def get_timestamp(format = 'long', offset = nil, offset_unit = :years)
76
- t = DateTime.now
77
- if offset
78
- t = t.advance(offset_unit => offset)
79
- end
80
- case format
81
- when 'dateonly'
82
- t.strftime("%m/%d/%Y")
83
- when 'condensed'
84
- t.strftime("%Y%m%d%H%M")
85
- when 'condensed_seconds'
86
- t.strftime("%Y%m%d%H%M%S")
87
- when 'long'
88
- t.strftime("%m/%d/%Y %I:%M %p")
89
- when 'mdyy'
90
- get_mdyy(t)
91
- when 'm/d/y'
92
- get_mdyy(t)
93
- else
94
- Time.now.strftime("%m/%d/%Y %H:%M:%S")
95
- end
96
- end
97
-
98
- def token_auth(browser, role, token, id = 'token_pass')
99
- set_textfield_by_id(browser, id, token)
100
- click_button_by_value(browser, 'Continue')
101
- if validate_text(browser, 'The requested page requires authentication\.\s*Please enter your Passcode below', nil, true)
102
- bail_out(browser, __LINE__, "Token authorization failed on '#{token}'")
103
- end
104
- end
105
-
106
- # :category: Helpers
107
- def calc_index(index, every = 1)
108
- (index / every) + (every - 1)
109
- end
110
-
111
- =begin rdoc
112
- :category: Page Data
113
- :tags: data, DOM, page
114
-
115
- *browser* is any container element. Best to use is the smallest that contains the desired data.
116
-
117
- *types* defaults to all of: :text, :textarea, :select_list, :span, :hidden, :checkbox, and :radio.
118
- Set types to array of subset of these if fewer elements are desired.
119
-
120
- No positive validations are reported but failure is rescued and reported.
121
- =end
122
- def capture_page_data(browser, types = [:text, :textarea, :select_list, :span, :hidden, :checkbox, :radio])
123
- start = Time.now
124
- debug_to_log("Begin #{__method__}")
125
- data = Hash.new
126
- data[:id] = Hash.new
127
- data[:name] = Hash.new
128
- data[:index] = Hash.new
129
- types.each do |type|
130
- #debug_to_log("#{__method__}: #{type}. . .")
131
- data[:id][type], data[:name][type], data[:index][type] = parse_elements(browser, type)
132
- end
133
- data
134
- rescue
135
- failed_to_log("#{__method__}: '#{$!}'")
136
- ensure
137
- stop = Time.now
138
- passed_to_log("#{__method__.to_s.titleize} finished. (#{"%.5f" % (stop - start)} secs)")
139
- #debug_to_log("End #{__method__}")
140
- end
141
-
142
- def compare_page_data(before, after, how, desc = '')
143
- [:text, :textarea, :select_list, :span, :checkbox, :radio].each do |type|
144
- before[how][type].each_key do |what|
145
- msg = "#{desc} #{type} #{how}=#{what}: Expected '#{before[how][type][what]}'."
146
- if after[how][type][what] == before[how][type][what]
147
- passed_to_log(msg)
148
- else
149
- failed_to_log("#{msg} Found '#{after[how][type][what]}'")
150
- end
151
- end
152
- end
153
- rescue
154
- failed_to_log("Unable to compare before and after page data. '#{$!}'")
155
- end
156
-
157
- =begin rdoc
158
- :category: Page Data
159
- :tags:data, DOM
160
-
161
- *data* is output of capture_page_data().
162
-
163
- *how* is one of :id, :name, :index
164
-
165
- *what* is the target value for how
166
-
167
- *type* is one of :text,:textarea,:select_list,:span,:hidden,:checkbox,:radio
168
-
169
- *get_text* is used for select_list to choose selected option value or text (default)
170
-
171
- Note that multiple selections will be captured as arrays so value and text.
172
-
173
- =end
174
- def fetch_page_data(data, how, what, type, get_text = true)
175
- rslt = data[how][type][what]
176
- if type == :select_list
177
- value, text = rslt.split('::')
178
- if get_text
179
- rslt = text
180
- else
181
- rslt = value
182
- end
183
- end
184
- rslt
185
- end
186
-
187
- # :category: Helpers
188
- def capture_screen(browser, ts)
189
- browser.maximize
190
- browser.bring_to_front
191
- caller = get_caller
192
- caller.match(/:(\d+):/)
193
- lnbr = $1
194
- path = "#{@myRoot}/screenshot/"
195
- screenfile = "#{@myName}_#{@myRun.id}_#{lnbr.to_s}_#{ts.to_f.to_s}.scrsht.jpg"
196
- info_to_log("path:#{path} screenfile:#{screenfile}")
197
- screenSpec = '"' + path + screenfile + '"'
198
- screenSpec.gsub!('/', '\\')
199
- screen_capture(screenSpec)
200
- screenfile
201
- end
202
-
203
- # :category: Helpers
204
- def bail_out(browser, lnbr, msg)
205
- ts = Time.new
206
- msg = "Bailing out at util line #{lnbr} #{ts} " + msg
207
- puts "#{msg}"
208
- fatal_to_log(msg, nil, 1, lnbr)
209
- debug_to_log(dump_caller(lnbr))
210
- if is_browser?(browser)
211
- if @browserAbbrev == 'IE'
212
- hwnd = browser.hwnd
213
- kill_browser(hwnd, lnbr, browser)
214
- raise(RuntimeError, msg, caller)
215
- elsif @browserAbbrev == 'FF'
216
- debug_to_log("#{browser.inspect}")
217
- debug_to_log("#{browser.to_s}")
218
- raise(RuntimeError, msg, caller)
219
- end
220
- end
221
- @status = 'bailout'
222
- raise(RuntimeError, msg, caller)
223
- end
224
-
225
- # :category: Helpers
226
- def parse_cookies(browser)
227
- cookies = Hash.new
228
- strg = browser.document.cookie
229
- ary = strg.split(';')
230
- ary.each do |c|
231
- key, value = c.split('=')
232
- cookies[key.lstrip] = value
233
- end
234
- cookies
235
- end
236
-
237
- =begin rdoc
238
- :category: Page Data
239
- :tags:data, DOM
240
-
241
- *browser* is any container element. best to use is the smallest that contains the desired data.
242
-
243
- *type* is one of these symbols: :text,:textarea,:select_list,:span,:hidden,:checkbox,:radio
244
-
245
- Returns three hashes: id[type][id] = value, name[type][id] = value, index[type][id] = value
246
-
247
- A given element appears once in the set of hashes depending on how is is found: id first
248
- then name, then index.
249
-
250
- Select list value is in the form 'value::text'. parse with x.split('::')
251
-
252
- No positive validations are reported but failure is rescued and reported.
253
- =end
254
- def parse_elements(browser, type)
255
- id = Hash.new
256
- name = Hash.new
257
- index = Hash.new
258
- idx = 0
259
- #debug_to_log("#{__method__}: #{type}")
260
- case type
261
- when :span
262
- collection = browser.spans
263
- when :select_list
264
- collection = browser.select_lists
265
- when :radio
266
- collection = browser.radios
267
- when :checkbox
268
- collection = browser.checkboxes
269
- else
270
- collection = browser.elements(:type, type.to_s)
271
- end
272
- #debug_to_log("#{__method__}: collection: #{collection.inspect}")
273
- collection.each do |e|
274
- case type
275
- when :span
276
- vlu = e.text
277
- when :select_list
278
- vlu = "#{e.value}::#{e.selected_options[0]}"
279
- when :radio
280
- vlu = e.set?
281
- when :checkbox
282
- vlu = e.set?
283
- else
284
- vlu = e.value
285
- end
286
- idx += 1
287
- if e.id.length > 0 and not e.id =~ /^__[A-Z]/
288
- id[e.id] = vlu
289
- elsif e.name.length > 0 and not e.name =~ /^__[A-Z]/
290
- name[e.name] = vlu
291
- else
292
- index[idx] = vlu if not type == :hidden
293
- end
294
- end
295
- [id, name, index]
296
-
297
- rescue
298
- failed_to_log("#{__method__}: '#{$!}'")
299
- end
300
-
301
- # :category: GUI
302
- def verify_no_element_overlap(browser, above_element, above_how, above_what, below_element, below_how, below_what, side, desc = '')
303
- mark_testlevel("#{__method__.to_s.titleize}", 3)
304
- msg = "#{above_element.to_s.titleize} #{above_how}=>#{above_what} does not overlap "+
305
- "#{below_element.to_s.titleize} #{below_how}=>#{below_what} at the #{side}."
306
- msg << " #{desc}" if desc.length > 0
307
- above = browser.element(above_how, above_what)
308
- below = browser.element(below_how, below_what)
309
- if overlay?(above, below, side)
310
- failed_to_log(msg)
311
- else
312
- passed_to_log(msg)
313
- true
314
- end
315
- rescue
316
- failed_to_log("Unable to verify that #{msg} '#{$!}'")
317
- end
318
-
319
- # :category: GUI
320
- def verify_element_inside(inner_element, outer_element, desc = '')
321
- mark_testlevel("#{__method__.to_s.titleize}", 3)
322
- msg = "#{inner_element.class.to_s} (:id=#{inner_element.id}) is fully enclosed by #{outer_element.class.to_s} (:id=#{outer_element.id})."
323
- msg << " #{desc}" if desc.length > 0
324
- if overlay?(inner_element, outer_element, :inside)
325
- failed_to_log(msg)
326
- else
327
- passed_to_log(msg)
328
- true
329
- end
330
- rescue
331
- failed_to_log("Unable to verify that #{msg} '#{$!}'")
332
- end
333
-
334
- # :category: GUI
335
- def overlay?(inner, outer, side = :bottom)
336
- #mark_testlevel("#{__method__.to_s.titleize}", 3)
337
- inner_t, inner_b, inner_l, inner_r = inner.bounding_rectangle_offsets
338
- outer_t, outer_b, outer_l, outer_r = outer.bounding_rectangle_offsets
339
- #overlay = false
340
- case side
341
- when :bottom
342
- overlay = inner_b > outer_t
343
- when :top
344
- overlay = inner_t > outer_t
345
- when :left
346
- overlay = inner_l < outer_r
347
- when :right
348
- overlay = inner_r > outer_r
349
- when :inside
350
- overlay = !(inner_t > outer_t and
351
- inner_r < outer_r and
352
- inner_l > outer_l and
353
- inner_b < outer_b)
354
- else
355
- overlay = (inner_t > outer_b or
356
- inner_r > outer_l or
357
- inner_l < outer_r or
358
- inner_b < outer_t)
359
- end
360
- overlay
361
- rescue
362
- failed_to_log("Unable to determine overlay. '#{$!}'")
363
- end
364
-
365
- # :category: Page Data
366
- def pdf_to_text(file, noblank = true)
367
- spec = file.sub(/\.pdf$/, '')
368
- `pdftotext #{spec}.pdf`
369
- file = File.new("#{spec}.txt")
370
- text = []
371
- file.readlines.each do |l|
372
- l.chomp! if noblank
373
- if l.length > 0
374
- text << l
375
- end
376
- end
377
- file.close
378
- text
379
- end
380
-
381
- # :category: Bullet-Proofing
382
- def kill_browser(hwnd, lnbr, browser = nil, doflag = false)
383
- # TODO Firefox
384
- logit = false
385
- if @browserAbbrev == 'FF'
386
- if is_browser?(browser) # and browser.url.length > 1
387
- logit = true
388
- here = __LINE__
389
- url = browser.url
390
- capture_screen(browser, Time.new.to_f) if @screenCaptureOn
391
- browser.close if url.length > 0
392
- @status = 'killbrowser'
393
- fatal_to_log("Kill browser called from line #{lnbr}")
394
- end
395
- elsif hwnd
396
- pid = Watir::IE::Process.process_id_from_hwnd(hwnd)
397
- if pid and pid > 0 and pid < 538976288
398
- if browser.exists?
399
- here = __LINE__
400
- logit = true
401
- url = browser.url
402
- capture_screen(browser, Time.new.to_f) if @screenCaptureOn
403
- browser.close
404
- sleep(2)
405
- if browser.exists?
406
- do_taskkill(FATAL, pid)
407
- end
408
- @status = 'killbrowser'
409
- end
410
- end
411
- if logit
412
- debug_to_log("#{@browserName} window hwnd #{hwnd} pid #{pid} #{url} (#{here})")
413
- fatal_to_log("Kill browser called from line #{lnbr}")
414
- end
415
- end
416
- end
417
-
418
- # :category: Bullet-Proofing
419
- def do_taskkill(severity, pid)
420
- if pid and pid > 0 and pid < 538976288
421
- info_to_log("Executing taskkill for pid #{pid}")
422
- log_message(severity, %x[taskkill /t /f /pid #{pid}])
423
- end
424
- rescue
425
- error_to_log("#{$!} (#{__LINE__})")
426
- end
427
-
428
- # :category: Bullet-Proofing
429
- def check_for_other_browsers
430
- cnt1 = find_other_browsers
431
- cnt2 = Watir::Process.count 'iexplore.exe'
432
- debug_to_log("check_for_other_browsers: cnt1: #{cnt1} cnt2: #{cnt2}")
433
- rescue
434
- error_to_log("#{$!} (#{__LINE__})\n#{Kernel.caller.to_yaml}")
435
- end
436
-
437
- # :category: Bullet-Proofing
438
- def check_for_and_clear_other_browsers
439
- if @targetBrowser.abbrev == 'IE'
440
- debug_to_log("#{__method__}:")
441
- cnt1 = find_other_browsers
442
- cnt2 = Watir::IE.process_count
443
- debug_to_log("#{__method__}: cnt1: #{cnt1} cnt2: #{cnt2}")
444
- begin
445
- Watir::IE.each do |ie|
446
- pid = Watir::IE::Process.process_id_from_hwnd(ie.hwnd)
447
- debug_to_log("#{__method__}: Killing browser process: hwnd #{ie.hwnd} pid #{pid} title '#{ie.title}' (#{__LINE__})")
448
- do_taskkill(INFO, pid)
449
- sleep_for(10)
450
- end
451
- #Watir::IE.close_all()
452
- rescue
453
- debug_to_log("#{__method__}: #{$!} (#{__LINE__})")
454
- end
455
- sleep(3)
456
- cnt1 = find_other_browsers
457
- cnt2 = Watir::IE.process_count
458
- if cnt1 > 0 or cnt2 > 0
459
- debug_to_log("#{__method__}:cnt1: #{cnt1} cnt2: #{cnt2}")
460
- begin
461
- Watir::IE.each do |ie|
462
- pid = Watir::IE::Process.process_id_from_hwnd(ie.hwnd)
463
- debug_to_log("#{__method__}: Killing browser process: hwnd #{ie.hwnd} pid #{pid} title '#{ie.title}' (#{__LINE__})")
464
- do_taskkill(INFO, pid)
465
- sleep_for(10)
466
- end
467
- #Watir::IE.close_all()
468
- rescue
469
- debug_to_log("#{__method__}:#{$!} (#{__LINE__})")
470
- end
471
- end
472
- end
473
- rescue
474
- error_to_log("#{__method__}: #{$!} (#{__LINE__})\n#{Kernel.caller.to_yaml}")
475
- end
476
-
477
- #--
478
- #def attach_browser(browser, how, what)
479
- # debug_to_log("Attaching browser window :#{how}=>'#{what}' ")
480
- # uri_decoded_pattern = URI.encode(what.to_s.gsub('(?-mix:', '').gsub(')', ''))
481
- # case @browserAbbrev
482
- # when 'IE'
483
- # tmpbrowser = Watir::IE.attach(how, what)
484
- # browser.visible = true
485
- # tmpbrowser.visible = true
486
- # tmpbrowser.speed = :fast
487
- # tmpbrowser
488
- # when 'FF'
489
- # tmpbrowser = FireWatir::Firefox.attach(how, /#{uri_decoded_pattern}/)
490
- # when 'S'
491
- # Watir::Safari.attach(how, what)
492
- # tmpbrowser = browser
493
- # when 'C'
494
- # browser.window(how, /#{uri_decoded_pattern}/).use
495
- # tmpbrowser = browser
496
- # end
497
- # debug_to_log("#{__method__}: tmpbrowser:#{tmpbrowser.inspect}")
498
- # tmpbrowser
499
- #end
500
- #++
501
-
502
- # :category: Navigation
503
- def attach_browser(browser, how, what, desc = '')
504
- debug_to_log("Attaching browser window :#{how}=>'#{what}' #{desc}")
505
- uri_decoded_pattern = URI.encode(what.to_s.gsub('(?-mix:', '').gsub(')', ''))
506
- case @browserAbbrev
507
- when 'IE'
508
- tmpbrowser = Watir::IE.attach(how, what)
509
- browser.visible = true
510
- if tmpbrowser
511
- tmpbrowser.visible = true
512
- tmpbrowser.speed = :fast
513
- else
514
- raise "Browser window :#{how}=>'#{what}' has at least one doc not in completed ready state."
515
- end
516
- when 'FF'
517
- #TODO: This may be dependent on Firefox version if webdriver doesn't support 3.6.17 and below
518
- browser.driver.switch_to.window(browser.driver.window_handles[0])
519
- browser.window(how, /#{uri_decoded_pattern}/).use
520
- tmpbrowser = browser
521
- when 'S'
522
- Watir::Safari.attach(how, what)
523
- tmpbrowser = browser
524
- when 'C'
525
- browser.window(how, /#{uri_decoded_pattern}/).use
526
- tmpbrowser = browser
527
- end
528
- debug_to_log("#{__method__}: tmpbrowser:#{tmpbrowser.inspect}")
529
- tmpbrowser
530
- end
531
-
532
- # :category: Navigation
533
- def attach_browser_by_url(browser, pattern, desc = '')
534
- attach_browser(browser, :url, pattern, desc)
535
- end
536
-
537
- alias attach_browser_with_url attach_browser_by_url
538
-
539
- # :category: Navigation
540
- def attach_popup(browser, how, what, desc = '')
541
- msg = "Attach popup :#{how}=>'#{what}'. #{desc}"
542
- popup = attach_browser(browser, how, what, desc)
543
- sleep_for(1)
544
- debug_to_log("#{popup.inspect}")
545
- if is_browser?(popup)
546
- title = popup.title
547
- passed_to_log("#{msg} title='#{title}'")
548
- return popup
549
- else
550
- failed_to_log(msg)
551
- end
552
- rescue
553
- failed_to_log("Unable to attach popup :#{how}=>'#{what}'. #{desc} '#{$!}' (#{__LINE__})")
554
- end
555
-
556
- # :category: Navigation
557
- def attach_popup_by_title(browser, strg, desc = '')
558
- attach_popup(browser, :title, strg, desc)
559
- end
560
-
561
- # :category: Navigation
562
- def attach_popup_by_url(browser, pattern, desc = '')
563
- attach_popup(browser, :url, pattern, desc)
564
- end
565
-
566
- alias get_popup_with_url attach_popup_by_url
567
- alias attach_popup_with_url attach_popup_by_url
568
- alias attach_iepopup attach_popup_by_url
569
-
570
- def clear_checkbox(browser, how, what, value = nil, desc = '')
571
- msg = "Clear checkbox #{how}=>'#{what}'"
572
- msg << " ('#{value}')" if value
573
- msg << " #{desc}'" if desc.length > 0
574
- browser.checkbox(how, what).clear
575
- if validate(browser, @myName, __LINE__)
576
- passed_to_log(msg)
577
- true
578
- end
579
- rescue
580
- failed_to_log("Unable to #{msg} '#{$!}'")
581
- end
582
-
583
- def clear_checkbox_by_name(browser, strg, value = nil, desc = '')
584
- clear_checkbox(browser, :name, strg, value, desc)
585
- end
586
-
587
- def clear_checkbox_by_id(browser, strg, value = nil, desc = '')
588
- clear_checkbox(browser, :id, strg, value, desc)
589
- end
590
-
591
- # :category: Bullet-Proofing
592
- def find_other_browsers
593
- cnt = 0
594
- if @targetBrowser.abbrev == 'IE'
595
- Watir::IE.each do |ie|
596
- debug_to_log("#{ie.inspect}")
597
- ie.close()
598
- cnt = cnt + 1
599
- end
600
- end
601
- debug_to_log("Found #{cnt} IE browser(s).")
602
- return cnt
603
- rescue
604
- error_to_log("#{$!} (#{__LINE__})\n#{Kernel.caller.to_yaml}", __LINE__)
605
- return 0
606
- end
607
-
608
- # :category: Debug
609
- def get_trace(lnbr)
610
- callertrace = "\nCaller trace: (#{lnbr})\n"
611
- Kernel.caller.each_index do |x|
612
- callertrace << ' >> ' + Kernel.caller[x].to_s + "\n"
613
- end
614
- callertrace
615
- end
616
-
617
- alias dump_caller get_trace
618
-
619
- # :category: Helpers
620
- def translate_var_list(key)
621
- if @var[key] and @var[key].length > 0
622
- list = @var[key].dup
623
- unless list =~ /^\[.+\]$/
624
- list = "[#{list}]"
625
- end
626
- eval(list)
627
- end
628
- rescue
629
- failed_to_log("#{__method__}: '#{$!}'")
630
- end
631
-
632
- # :category: Helpers
633
- def get_variables(file, login = :role, dbg = true)
634
- debug_to_log("#{__method__}: file = #{file}")
635
- debug_to_log("#{__method__}: role = #{login}")
636
-
637
- @var = Hash.new
638
- workbook = Excel.new(file)
639
- data_index = find_sheet_with_name(workbook, 'Data')
640
- workbook.default_sheet = workbook.sheets[data_index]
641
- var_col = 0
642
-
643
- 2.upto(workbook.last_column) do |col|
644
- scriptName = workbook.cell(1, col)
645
- if scriptName == @myName
646
- var_col = col
647
- break
648
- end
649
- end
650
-
651
- 2.upto(workbook.last_row) do |line|
652
- name = workbook.cell(line, 'A')
653
- value = workbook.cell(line, var_col).to_s.strip
654
- @var[name] = value
655
- end
656
-
657
- @var.keys.sort.each do |name|
658
- message_tolog("@var #{name}: '#{@var[name]}'")
659
- end if dbg
660
-
661
- @login = Hash.new
662
- login_col = 0
663
- role_col = 0
664
- userid_col = 0
665
- password_col = 0
666
- url_col = 0
667
- name_col = 0
668
- role_index = find_sheet_with_name(workbook, 'Login')
669
- if role_index >= 0
670
- workbook.default_sheet = workbook.sheets[role_index]
671
-
672
- 1.upto(workbook.last_column) do |col|
673
- a_cell = workbook.cell(1, col)
674
- case a_cell
675
- when @myName
676
- login_col = col
677
- break
678
- when 'role'
679
- role_col = col
680
- when 'userid'
681
- userid_col = col
682
- when 'password'
683
- password_col = col
684
- when 'url'
685
- url_col = col
686
- when 'name'
687
- name_col = col
688
- end
689
- end
690
-
691
- 2.upto(workbook.last_row) do |line|
692
- role = workbook.cell(line, role_col)
693
- userid = workbook.cell(line, userid_col)
694
- password = workbook.cell(line, password_col)
695
- url = workbook.cell(line, url_col)
696
- username = workbook.cell(line, name_col)
697
- enabled = workbook.cell(line, login_col).to_s
698
-
699
- case login
700
- when :id
701
- key = userid
702
- when :role
703
- key = role
704
- else
705
- key = role
706
- end
707
-
708
- @login[key] = Hash.new
709
- @login[key]['role'] = role
710
- @login[key]['userid'] = userid
711
- @login[key]['password'] = password
712
- @login[key]['url'] = url
713
- @login[key]['name'] = username
714
- @login[key]['enabled'] = enabled
715
-
716
- end
717
-
718
- @login.keys.sort.each do |key|
719
- message_tolog("@login (by #{login}): #{key}=>'#{@login[key].to_yaml}'")
720
- end if dbg
721
- end
722
-
723
- rescue
724
- fatal_to_log("#{__method__}: '#{$!}'")
725
- end
726
-
727
- # :category: Debug
728
- def grab_window_list(strg)
729
- @ai.AutoItSetOption("WinTitleMatchMode", 2)
730
- list = @ai.WinList(strg)
731
- stuff = ''
732
- names = list[0]
733
- handles = list[1]
734
- max = names.length - 1
735
- rng = Range.new(0, max)
736
- rng.each do |idx|
737
- window_handle = "[HANDLE:#{handles[idx]}]"
738
- full_text = @ai.WinGetText(window_handle)
739
- stuff << "[#{handles[idx]}]=>#{names[idx]}=>'#{full_text}'\n"
740
- end
741
- debug_to_log("\n#{stuff}")
742
- @ai.AutoItSetOption("WinTitleMatchMode", 1)
743
- stuff
744
- end
745
-
746
- # :category: Debug
747
- def debug_call_list(msg)
748
- call_array = get_call_array
749
- debug_to_log("#{msg}\n#{dump_array(call_array)}")
750
- end
751
-
752
- #--
753
- ## TODO ugly, gotta lose the hardcoding...
754
- #def decode_options(strg)
755
- # idx = 0
756
- # @options = Hash.new
757
- # strg.each_char do |c|
758
- # idx = idx + 1
759
- # case idx
760
- # when 1
761
- # @options['load'] = c.to_i
762
- # when 2
763
- # @options['screenshot'] = c.to_i
764
- # if c.to_i > 0
765
- # @screenCaptureOn = true
766
- # end
767
- # when 3
768
- # @options['hiderun'] = c.to_i
769
- # # when 4
770
- # # @options['another'] = c.to_i
771
- # end
772
- #
773
- # end
774
- # # puts @options.to_yaml
775
- #end
776
- #++
777
-
778
- # :category: User Input
779
- def click_popup_button(title, button, waitTime= 9, user_input=nil)
780
- #TODO: is winclicker still viable/available?
781
- wc = WinClicker.new
782
- if wc.clickWindowsButton(title, button, waitTime)
783
- passed_to_log("Window '#{title}' button '#{button}' found and clicked.")
784
- true
785
- else
786
- failed_to_log("Window '#{title}' button '#{button}' not found. (#{__LINE__})")
787
- end
788
- wc = nil
789
- # get a handle if one exists
790
- # hwnd = $ie.enabled_popup(waitTime)
791
- # if (hwnd) # yes there is a popup
792
- # w = WinClicker.new
793
- # if ( user_input )
794
- # w.setTextValueForFileNameField( hwnd, "#{user_input}" )
795
- # end
796
- # # I put this in to see the text being input it is not necessary to work
797
- # sleep 3
798
- # # "OK" or whatever the name on the button is
799
- # w.clickWindowsButton_hwnd( hwnd, "#{button}" )
800
- # #
801
- # # this is just cleanup
802
- # w=nil
803
- # end
804
- end
805
-
806
- # :category: Locate Elements
807
- def get_select_list(browser, how, what, desc = '')
808
- list = browser.select_list(how, what)
809
- if validate(browser, @myName, __LINE__)
810
- passed_to_log("Select list #{how}='#{what}' found and returned.")
811
- return list
812
- end
813
- rescue
814
- failed_to_log("Unable to return select list #{how}='#{what}': '#{$!}' (#{__LINE__})")
815
- end
816
-
817
- # :category: Page Data
818
- def get_select_options(browser, how, what, dump = false)
819
- list = browser.select_list(how, what)
820
- dump_select_list_options(list) if dump
821
- list.options
822
- rescue
823
- failed_to_log("Unable to get select options for #{how}=>#{what}. '#{$!}'")
824
- end
825
-
826
- # :category: Page Data
827
- def get_select_options_by_id(browser, strg, dump = false)
828
- get_select_options(browser, :id, strg, dump)
829
- end
830
-
831
- # :category: Page Data
832
- def get_select_options_by_name(browser, strg, dump = false)
833
- get_select_options(browser, :name, strg, dump)
834
- end
835
-
836
- # :category: Page Data
837
- def get_selected_options(browser, how, what)
838
- begin
839
- list = browser.select_list(how, what)
840
- rescue => e
841
- if not rescue_me(e, __method__, "browser.select_list(#{how}, '#{what}')", "#{browser.class}")
842
- raise e
843
- end
844
- end
845
- list.selected_options
846
- end
847
-
848
- # :category: Page Data
849
- def get_selected_options_by_id(browser, strg)
850
- get_selected_options(browser, :id, strg)
851
- end
852
-
853
- alias get_selected_option_by_id get_selected_options_by_id
854
-
855
- # :category: Page Data
856
- def get_selected_options_by_name(browser, strg)
857
- get_selected_options(browser, :name, strg)
858
- end
859
-
860
- alias get_selected_option_by_name get_selected_options_by_name
861
-
862
- # :category: Helpers
863
- def sec2hms(s)
864
- Time.at(s.to_i).gmtime.strftime('%H:%M:%S')
865
- end
866
-
867
- # :category: User Input
868
- def select(browser, how, what, which, value, desc = '')
869
- msg = "Select option #{which}='#{value}' from list #{how}=#{what}. #{desc}"
870
- list = browser.select_list(how, what)
871
- case which
872
- when :text
873
- list.select(value)
874
- when :value
875
- list.select_value(value)
876
- when :index
877
- all = list.getAllContents
878
- txt = all[value]
879
- list.select(txt)
880
- else
881
- na = "#{__method__} cannot support select by '#{which}'. (#{msg})"
882
- debug_to_log(na, __LINE__, true)
883
- raise na
884
- end
885
- passed_to_log(msg)
886
- rescue
887
- failed_to_log("#Unable to #{msg}': '#{$!}'")
888
- end
889
-
890
- # :category: User Input
891
- def select_option_from_list(list, what, what_strg, desc = '')
892
- ok = true
893
- msg = "#{__method__.to_s.titleize} "
894
- if list
895
- msg << "list id=#{list.id}: "
896
- case what
897
- when :text
898
- list.select(what_strg) #TODO: regex?
899
- when :value
900
- list.select_value(what_strg) #TODO: regex?
901
- when :index
902
- list.select(list.getAllContents[what_strg.to_i])
903
- else
904
- msg << "select by #{what} not supported. #{desc} (#{__LINE__})"
905
- failed_to_log(msg)
906
- ok = false
907
- end
908
- if ok
909
- msg << "#{what}='#{what_strg}' selected. #{desc}"
910
- passed_to_log(msg)
911
- true
912
- end
913
- else
914
- failed_to_log("#{__method__.to_s.titleize} list not found. #{desc} (#{__LINE__})")
915
- end
916
- rescue
917
- failed_to_log("#{__method__.to_s.titleize}: #{what}='#{what_strg}' could not be selected: '#{$!}'. #{desc} (#{__LINE__})")
918
- end
919
-
920
- # :category: User Input
921
- def select_option_by_id_and_option_text(browser, strg, option, nofail=false, desc = '')
922
- msg = "Select list id=#{strg} option text='#{option}' selected."
923
- msg << " #{desc}" if desc.length > 0
924
- list = browser.select_list(:id, strg)
925
- list.select(option)
926
- # browser.select_list(:id, strg).select(option) #(browser.select_list(:id, strg).getAllContents[option])
927
- if validate(browser, @myName, __LINE__)
928
- passed_to_log(msg)
929
- true
930
- end
931
- rescue
932
- if !nofail
933
- failed_to_log("#{msg} '#{$!}'")
934
- end
935
- end
936
-
937
- alias select_option_by_id select_option_by_id_and_option_text
938
- alias select_option_by_id_and_text select_option_by_id_and_option_text
939
-
940
- # :category: User Input
941
- def select_option_by_name_and_option_text(browser, strg, option, desc = '')
942
- msg = "Select list name=#{strg} option text='#{option}' selected."
943
- msg << " #{desc}" if desc.length > 0
944
- begin
945
- list = browser.select_list(:name, strg)
946
- rescue => e
947
- if not rescue_me(e, __method__, "#{__LINE__}: select_list(:name,'#{strg}')", "#{browser.class}")
948
- raise e
949
- end
950
- end
951
- begin
952
- list.select(option)
953
- rescue => e
954
- if not rescue_me(e, __method__, "#{__LINE__}: select_list#select('#{option}')", "#{browser.class}")
955
- raise e
956
- end
957
- end
958
- if validate(browser, @myName, __LINE__)
959
- passed_to_log(msg)
960
- true
961
- end
962
- rescue
963
- failed_to_log("#{msg} '#{$!}'")
964
- end
965
-
966
- alias select_option_by_name select_option_by_name_and_option_text
967
-
968
- # :category: User Input
969
- def select_option_by_title_and_option_text(browser, strg, option, desc = '')
970
- msg = "Select list name=#{strg} option text='#{option}' selected."
971
- msg << " #{desc}" if desc.length > 0
972
- browser.select_list(:title, strg).select(option)
973
- if validate(browser, @myName, __LINE__)
974
- passed_to_log(msg)
975
- end
976
- rescue
977
- failed_to_log("#{msg} '#{$!}'")
978
- end
979
-
980
- # :category: User Input
981
- def select_option_by_class_and_option_text(browser, strg, option, desc = '')
982
- msg = "Select list class=#{strg} option text='#{option}' selected."
983
- msg << " #{desc}" if desc.length > 0
984
- browser.select_list(:class, strg).select(option)
985
- if validate(browser, @myName, __LINE__)
986
- passed_to_log(msg)
987
- true
988
- end
989
- rescue
990
- failed_to_log("#{msg} '#{$!}'")
991
- end
992
-
993
- # :category: User Input
994
- def select_option_by_name_and_option_value(browser, strg, option, desc = '')
995
- msg = "Select list name=#{strg} option value='#{option}' selected."
996
- msg << " #{desc}" if desc.length > 0
997
- begin
998
- list = browser.select_list(:name, strg)
999
- rescue => e
1000
- if not rescue_me(e, __method__, "#{__LINE__}: select_list(:name,'#{strg}')", "#{browser.class}")
1001
- raise e
1002
- end
1003
- end
1004
- begin
1005
- list.select_value(option)
1006
- rescue => e
1007
- if not rescue_me(e, __method__, "#{__LINE__}: select_list#select_value('#{option}')", "#{browser.class}")
1008
- raise e
1009
- end
1010
- end
1011
- if validate(browser, @myName, __LINE__)
1012
- passed_to_log(msg)
1013
- true
1014
- end
1015
- rescue
1016
- failed_to_log("#{msg} '#{$!}'")
1017
- end
1018
-
1019
- # :category: User Input
1020
- def select_option_by_id_and_option_value(browser, strg, option, desc = '')
1021
- msg = "Select list name=#{strg} option value='#{option}' selected."
1022
- msg << " #{desc}" if desc.length > 0
1023
- begin
1024
- list = browser.select_list(:id, strg)
1025
- rescue => e
1026
- if not rescue_me(e, __method__, "#{__LINE__}: select_list(:text,'#{strg}')", "#{browser.class}")
1027
- raise e
1028
- end
1029
- end
1030
- sleep(0.5) unless @targetBrowser.abbrev == 'IE'
1031
- begin
1032
- list.select_value(option)
1033
- rescue => e
1034
- if not rescue_me(e, __method__, "#{__LINE__}: select_list#select_value('#{option}')", "#{browser.class}")
1035
- raise e
1036
- end
1037
- end
1038
- if validate(browser, @myName, __LINE__)
1039
- passed_to_log(msg)
1040
- true
1041
- end
1042
- rescue
1043
- failed_to_log("#{msg} '#{$!}'")
1044
- end
1045
-
1046
- def select_option_by_id_and_index(browser, strg, idx, desc = '')
1047
- msg = "Select list id=#{strg} index='#{idx}' selected."
1048
- msg << " #{desc}" if desc.length > 0
1049
- list = browser.select_list(:id, strg)
1050
- all = list.getAllContents
1051
- txt = all[idx]
1052
- browser.select_list(:id, strg).set(browser.select_list(:id, strg).getAllContents[idx])
1053
- if validate(browser, @myName, __LINE__)
1054
- passed_to_log(msg)
1055
- true
1056
- end
1057
- rescue
1058
- failed_to_log("#{msg} '#{$!}'")
1059
- end
1060
-
1061
- # TODO add check that both list and option exist
1062
- def select_option_by_name_and_index(browser, strg, idx)
1063
- msg = "Select list name=#{strg} index='#{idx}' selected."
1064
- msg << " #{desc}" if desc.length > 0
1065
- browser.select_list(:name, strg).set(browser.select_list(:name, strg).getAllContents[idx])
1066
- if validate(browser, @myName, __LINE__)
1067
- passed_to_log(msg)
1068
- true
1069
- end
1070
- rescue
1071
- failed_to_log("#{msg} '#{$!}'")
1072
- end
1073
-
1074
- def select_option_by_xpath_and_index(browser, strg, idx)
1075
- msg = "Select list xpath=#{strg} index='#{idx}' selected."
1076
- msg << " #{desc}" if desc.length > 0
1077
- browser.select_list(:xpath, strg).set(browser.select_list(:xpath, strg).getAllContents[idx])
1078
- if validate(browser, nil, __LINE__)
1079
- passed_to_log(msg)
1080
- true
1081
- end
1082
- rescue
1083
- failed_to_log("#{msg} '#{$!}'")
1084
- end
1085
-
1086
- # :category: User Input
1087
- def set(browser, element, how, what, value = nil, desc = '')
1088
- msg = "Set #{element} #{how}=>'#{what}' "
1089
- msg << "('#{value.to_s}')" if value
1090
- msg << " '#{desc}' " if desc.length > 0
1091
- case element
1092
- when :radio
1093
- browser.radio(how, what, value).set
1094
- when :checkbox
1095
- browser.checkbox(how, what, value).set
1096
- else
1097
- failed_to_log("#{__method__}: #{element} not supported")
1098
- end
1099
- if validate(browser, @myName, __LINE__)
1100
- passed_to_log(msg)
1101
- true
1102
- end
1103
- rescue
1104
- failed_to_log("#{msg} '#{$!}'")
1105
- end
1106
-
1107
- # :category: User Input
1108
- def set_checkbox(browser, how, what, desc = '')
1109
- set(browser, :checkbox, how, what, nil, desc)
1110
- end
1111
-
1112
- # :category: User Input
1113
- def set_checkbox_by_class(browser, strg, value = nil, desc = '')
1114
- set(browser, :checkbox, :class, strg, value, desc)
1115
- end
1116
-
1117
- # :category: User Input
1118
- def set_checkbox_by_id(browser, strg, value = nil, desc = '')
1119
- set(browser, :checkbox, :id, strg, value, desc)
1120
- end
1121
-
1122
- # :category: User Input
1123
- def set_checkbox_by_name(browser, strg, value = nil, desc = '')
1124
- set(browser, :checkbox, :name, strg, value, desc)
1125
- end
1126
-
1127
- # :category: User Input
1128
- def set_checkbox_by_title(browser, strg, value = nil, desc = '')
1129
- set(browser, :checkbox, :title, strg, value, desc)
1130
- end
1131
-
1132
- # :category: User Input
1133
- def set_checkbox_by_value(browser, strg, desc = '')
1134
- set(browser, :checkbox, :value, strg, nil, desc)
1135
- end
1136
-
1137
- # :category: User Input
1138
- def set_radio(browser, how, what, value = nil, desc = '')
1139
- if how == :value
1140
- set_radio_by_value(browser, what, desc)
1141
- else
1142
- set(browser, :radio, how, what, value, desc)
1143
- end
1144
- rescue
1145
- failed_to_log("#{msg} '#{$!}'")
1146
- end
1147
-
1148
- # :category: User Input
1149
- def set_radio_two_attributes(browser, how1, what1, how2, what2, desc = '')
1150
- msg = "Set radio #{how1}='#{what1}', #{how2}= #{what2}"
1151
- msg << " '#{desc}' " if desc.length > 0
1152
- browser.radio(how1 => what1, how2 => what2).set
1153
- if validate(browser, @myName, __LINE__)
1154
- passed_to_log(msg)
1155
- true
1156
- end
1157
- rescue
1158
- failed_to_log("#{msg} '#{$!}'")
1159
- end
1160
-
1161
- # :category: User Input
1162
- def set_radio_by_class(browser, strg, value = nil, desc = '')
1163
- set(browser, :radio, :class, strg, value, desc)
1164
- end
1165
-
1166
- # :category: User Input
1167
- def set_radio_by_id(browser, strg, value = nil, desc = '')
1168
- set(browser, :radio, :id, strg, value, desc)
1169
- end
1170
-
1171
- # :category: User Input
1172
- def set_radio_by_index(browser, index, desc = '')
1173
- set(browser, :radio, :index, index, nil, desc)
1174
- end
1175
-
1176
- # :category: User Input
1177
- def set_radio_by_name(browser, strg, value = nil, desc = '')
1178
- set(browser, :radio, :name, strg, value, desc)
1179
- end
1180
-
1181
- # :category: User Input
1182
- def set_radio_by_title(browser, strg, value = nil, desc = '')
1183
- set(browser, :radio, :title, strg, value, desc)
1184
- end
1185
-
1186
- # :category: User Input
1187
- def set_radio_no_wait_by_index(browser, index, desc = '')
1188
- #TODO: Not supported by Watir 1.8.x
1189
- msg = "Radio :index=#{index} "
1190
- radios = browser.radios
1191
- debug_to_log("\n#{radios}")
1192
- radio = radios[index]
1193
- debug_to_log("\n#{radio}")
1194
- radio.click_no_wait
1195
- if validate(browser)
1196
- msg << 'set ' + desc
1197
- passed_to_log(msg)
1198
- true
1199
- end
1200
- rescue
1201
- msg << 'not found ' + desc
1202
- failed_to_log("#{msg} (#{__LINE__})")
1203
- end
1204
-
1205
- def set_radio_by_value(browser, strg, desc = '')
1206
- msg = "Radio :value=>'#{strg}' "
1207
- msg << " '#{desc}' " if desc.length > 0
1208
- browser.radio(:value, strg).set
1209
- if validate(browser, @myName, __LINE__)
1210
- passed_to_log(msg)
1211
- true
1212
- end
1213
- rescue
1214
- failed_to_log("#{msg} '#{$!}'")
1215
- end
1216
-
1217
- def set_radio_by_name_and_index(browser, name, index, desc = '')
1218
- set_radio_two_attributes(browser, :name, name, :index, index, desc)
1219
- end
1220
-
1221
- def set_radio_by_name_and_text(browser, name, text, desc = '')
1222
- set_radio_two_attributes(browser, :name, name, :text, text, desc)
1223
- end
1224
-
1225
- def set_radio_by_value_and_index(browser, value, index, desc = '')
1226
- set_radio_two_attributes(browser, :value, value, :index, index, desc)
1227
- end
1228
-
1229
- def set_radio_by_name_and_value(browser, strg, value, desc = '')
1230
- set(browser, :radio, :name, strg, value, desc)
1231
- end
1232
-
1233
- # No validation
1234
- def clear(browser, element, how, what, value = nil, desc = '')
1235
- msg = "Clear #{element} #{how}=>'#{what}' "
1236
- msg << "('#{value.to_s}')" if value
1237
- msg << " '#{desc}' " if desc.length > 0
1238
- case element
1239
- when :radio
1240
- browser.radio(how, what, value).clear
1241
- when :checkbox
1242
- browser.checkbox(how, what, value).clear
1243
- when :text_field
1244
- browser.text_field(how, what).set('')
1245
- else
1246
- failed_to_log("#{__method__}: #{element} not supported")
1247
- end
1248
- if validate(browser, @myName, __LINE__)
1249
- passed_to_log(msg)
1250
- true
1251
- end
1252
- rescue
1253
- failed_to_log("#{msg} '#{$!}'")
1254
- end
1255
-
1256
- def clear_radio(browser, how, what, value = nil, desc = '')
1257
- msg = "Clear radio #{how}=>'#{what.to_s}' "
1258
- msg << "('#{value.to_s}')" if value
1259
- msg << " '#{desc}' " if desc.length > 0
1260
- radio = browser.radio(how, what, value)
1261
- radio.clear
1262
- if validate(browser, @myName, __LINE__)
1263
- passed_to_log(msg)
1264
- true
1265
- end
1266
- rescue
1267
- failed_to_log("#{msg} '#{$!}'")
1268
- end
1269
-
1270
- # Set skip_value_check = true when string is altered by application and/or
1271
- # this method will be followed by validate_text
1272
- def clear_textfield(browser, how, which, skip_value_check = false)
1273
- if browser.text_field(how, which).exists?
1274
- tf = browser.text_field(how, which)
1275
- if validate(browser, @myName, __LINE__)
1276
- tf.clear
1277
- if validate(browser, @myName, __LINE__)
1278
- if tf.value == ''
1279
- passed_to_log("Textfield #{how}='#{which}' cleared.")
1280
- true
1281
- elsif skip_value_check
1282
- passed_to_log("Textfield #{how}='#{which}' cleared. (skip value check)")
1283
- true
1284
- else
1285
- failed_to_log("Textfield #{how}='#{which}' not cleared: Found:'#{tf.value}'. (#{__LINE__})")
1286
- end
1287
- end
1288
- end
1289
- else
1290
- failed_to_log("Textfield id='#{id}' to clear. (#{__LINE__})")
1291
- end
1292
- rescue
1293
- failed_to_log("Textfield id='#{id}' could not be cleared: '#{$!}'. (#{__LINE__})")
1294
- end
1295
-
1296
- def close_window_by_title(browser, title, desc = '', text = '')
1297
- msg = "Window '#{title}':"
1298
- if @ai.WinWait(title, text, WAIT) > 0
1299
- passed_to_log("#{msg} appeared. #{desc}")
1300
- myHandle = @ai.WinGetHandle(title, text)
1301
- full_text = @ai.WinGetText(title)
1302
- debug_to_log("#{msg} hwnd: #{myHandle.inspect}")
1303
- debug_to_log("#{msg} title: '#{title}' text: '#{full_text}'")
1304
- if @ai.WinClose(title, text) > 0
1305
- passed_to_log("#{msg} closed successfully. #{desc}")
1306
- else
1307
- failed_to_log("#{msg} close failed. (#{__LINE__}) #{desc}")
1308
- end
1309
- else
1310
- failed_to_log("#{msg} did not appear after #{WAIT} seconds. (#{__LINE__}) #{desc}")
1311
- end
1312
- rescue
1313
- failed_to_log("#{msg}: Unable to close: '#{$!}'. (#{__LINE__}) #{desc}")
1314
- end
1315
-
1316
- def set_file_field(browser, how, what, filespec, desc = '')
1317
- msg = "Set file field #{how}=>#{what} to '#{filespec}."
1318
- msg << " #{desc}" if desc.length > 0
1319
- ff = browser.file_field(how, what)
1320
- if ff
1321
- ff.set filespec
1322
- sleep_for(8)
1323
- if validate(browser, @myName, __LINE__)
1324
- passed_to_log(msg)
1325
- true
1326
- end
1327
- else
1328
- failed_to_log("#{msg} File field not found.")
1329
- end
1330
- rescue
1331
- failed_to_log("Unable to #{msg} '#{$!}'")
1332
- end
1333
-
1334
- def set_file_field_by_name(browser, strg, path, desc = '')
1335
- set_file_field(browser, :name, strg, path, desc)
1336
- end
1337
-
1338
- def set_file_field_by_id(browser, strg, path, desc = '')
1339
- set_file_field(browser, :id, strg, path, desc)
1340
- end
1341
-
1342
- def set_text_field(browser, how, what, value, desc = '', skip_value_check = false)
1343
- #TODO: fix this to handle Safari password field
1344
- msg = "Set textfield #{how}='#{what}' to '#{value}'"
1345
- msg << " #{desc}" if desc.length > 0
1346
- msg << " (Skip value check)" if skip_value_check
1347
- if browser.text_field(how, what).exists?
1348
- tf = browser.text_field(how, what)
1349
- debug_to_log("#{tf.inspect}")
1350
- if validate(browser, @myName, __LINE__)
1351
- tf.set(value)
1352
- if validate(browser, @myName, __LINE__)
1353
- if tf.value == value
1354
- passed_to_log(msg)
1355
- true
1356
- elsif skip_value_check
1357
- passed_to_log(msg)
1358
- true
1359
- else
1360
- failed_to_log("#{msg}: Found:'#{tf.value}'.")
1361
- end
1362
- end
1363
- end
1364
- else
1365
- failed_to_log("Textfield #{how}='#{what}' not found to set to '#{value}''")
1366
- end
1367
- rescue
1368
- failed_to_log("Unable to '#{msg}': '#{$!}'")
1369
- end
1370
-
1371
- alias set_textfield set_text_field
1372
-
1373
- def set_textfield_by_name(browser, name, value, desc = '', skip_value_check = false)
1374
- if browser.text_field(:name, name).exists?
1375
- tf = browser.text_field(:name, name)
1376
- # Workaround because browser.text_field doesn't work for password fields in Safari
1377
- elsif @browserAbbrev.eql?("S")
1378
- tf = browser.password(:name, name)
1379
- end
1380
- if tf.exists?
1381
- if validate(browser, @myName, __LINE__)
1382
- tf.set(value)
1383
- if validate(browser, @myName, __LINE__)
1384
- if tf.value == value
1385
- passed_to_log("Set textfield name='#{name}' to '#{value}' #{desc}")
1386
- true
1387
- elsif skip_value_check
1388
- passed_to_log("Set textfield name='#{name}' to '#{value}' #{desc} (skip value check)")
1389
- true
1390
- else
1391
- failed_to_log("Set textfield name='#{name}' to '#{value}': Found:'#{tf.value}'. #{desc} (#{__LINE__})")
1392
- end
1393
- end
1394
- end
1395
- else
1396
- failed_to_log("Textfield name='#{name}' not found to set to '#{value}'. #{desc} (#{__LINE__})")
1397
- end
1398
- rescue
1399
- failed_to_log("Textfield name='#{name}' could not be set to '#{value}': '#{$!}'. #{desc} (#{__LINE__})")
1400
- end
1401
-
1402
- # Set skip_value_check = true when string is altered by application and/or
1403
- # this method will be followed by validate_text
1404
- def set_textfield_by_id(browser, id, value, desc = '', skip_value_check = false)
1405
- set_text_field(browser, :id, id, value, desc, skip_value_check)
1406
- end
1407
-
1408
- def set_textfield_by_title(browser, title, value, desc = '', skip_value_check = false)
1409
- set_text_field(browser, :title, title, value, desc, skip_value_check)
1410
- end
1411
-
1412
- def set_textfield_by_class(browser, strg, value, desc = '', skip_value_check = false)
1413
- set_text_field(browser, :class, strg, value, desc, skip_value_check)
1414
- end
1415
-
1416
- def set_text_field_and_validate(browser, how, what, value, desc = '', valid_value = nil)
1417
- #NOTE: use when value and valid_value differ as with dollar reformatting
1418
- if set_text_field(browser, how, what, value, desc, true)
1419
- expected = valid_value ? valid_value : value
1420
- validate_textfield_value(browser, how, what, expected)
1421
- end
1422
- rescue
1423
- failed_to_log("Unable to '#{msg}': '#{$!}'")
1424
- end
1425
-
1426
- =begin rdoc
1427
- :category: Basic
1428
- :tags:logon, login, user, password, url
1429
- TODO: Needs to be more flexible about finding login id and password textfields
1430
- TODO: Parameterize url and remove references to environment
1431
- =end
1432
- def login(browser, user, password)
1433
- myURL = @myAppEnv.url
1434
- runenv = @myAppEnv.nodename
1435
- message_tolog("URL: #{myURL}")
1436
- message_tolog("Beginning login: User: #{user} Environment: #{runenv}")
1437
- if validate(browser, @myName, __LINE__)
1438
- browser.goto(myURL)
1439
- if validate(browser, @myName)
1440
- set_textfield_by_name(browser, 'loginId', user)
1441
- set_textfield_by_name(browser, 'password', password)
1442
- click_button_by_value(browser, 'Login')
1443
- if validate(browser, @myName)
1444
- passed_to_log("Login successful.")
1445
- end
1446
- else
1447
- failed_to_log("Unable to login to application: '#{$!}'")
1448
- # screen_capture( "#{@myRoot}/screens/#{myName}_#{@runid}_#{__LINE__.to_s}_#{Time.new.to_f.to_s}.jpg")
1449
- end
1450
- end
1451
- rescue
1452
- failed_to_log("Unable to login to application: '#{$!}'")
1453
- end
1454
-
1455
- =begin rdoc
1456
- category: Logon
1457
- :tags:logon, login, user, password, url, basic authorization
1458
- =end
1459
- def basic_auth(browser, user, pswd, url, bypass_validate = false)
1460
- mark_testlevel("Basic Authorization Login", 0)
1461
-
1462
- message_to_report ("Login: #{user}")
1463
- message_to_report ("URL: #{url}")
1464
- message_to_report ("Password: #{pswd}")
1465
-
1466
- @login_title = "Connect to"
1467
-
1468
- a = Thread.new {
1469
- browser.goto(url)
1470
- }
1471
-
1472
- sleep_for(2)
1473
- message_to_log("#{@login_title}...")
1474
-
1475
- if (@ai.WinWait(@login_title, "", 90) > 0)
1476
- win_title = @ai.WinGetTitle(@login_title)
1477
- debug_to_log("Basic Auth Login window appeared: '#{win_title}'")
1478
- @ai.WinActivate(@login_title)
1479
- @ai.ControlSend(@login_title, '', "[CLASS:Edit; INSTANCE:2]", '!u')
1480
- @ai.ControlSend(@login_title, '', "[CLASS:Edit; INSTANCE:2]", user, 1)
1481
- @ai.ControlSend(@login_title, '', "[CLASS:Edit; INSTANCE:3]", pswd.gsub(/!/, '{!}'), 1)
1482
- @ai.ControlClick(@login_title, "", '[CLASS:Button; INSTANCE:1]')
1483
- else
1484
- debug_to_log("Basic Auth Login window did not appear.")
1485
- end
1486
- a.join
1487
-
1488
- validate(browser, @myName) unless bypass_validate
1489
-
1490
- message_to_report("URL: [#{browser.url}] User: [#{user}]")
1491
-
1492
- end
1493
-
1494
- def logout(browser, where = @myName, lnbr = __LINE__)
1495
- #TODO Firewatir 1.6.5 does not implement .exists for FireWatir::Firefox class
1496
- debug_to_log("Logging out in #{where} at line #{lnbr}.", lnbr, true)
1497
- debug_to_log("#{__method__}: browser: #{browser.inspect} (#{__LINE__})")
1498
-
1499
- if ['FF', 'S'].include?(@browserAbbrev) || browser.exists?
1500
- case @browserAbbrev
1501
- when 'FF'
1502
- if is_browser?(browser)
1503
- url = browser.url
1504
- title = browser.title
1505
- debug_to_log("#{__method__}: Firefox browser url: [#{url}]")
1506
- debug_to_log("#{__method__}: Firefox browser title: [#{title}]")
1507
- debug_to_log("#{__method__}: Closing browser: #{where} (#{lnbr})")
1508
- if url and url.length > 1
1509
- browser.close
1510
- else
1511
- browser = FireWatir::Firefox.attach(:title, title)
1512
- browser.close
1513
- end
1514
-
1515
- end
1516
- when 'IE'
1517
- hwnd = browser.hwnd
1518
- pid = Watir::IE::Process.process_id_from_hwnd(hwnd)
1519
- debug_to_log("#{__method__}: Closing browser: hwnd #{hwnd} pid #{pid} #{where} (#{lnbr}) (#{__LINE__})")
1520
- browser.close
1521
- if browser.exists? and pid > 0 and pid < 538976288 # value of uninitialized memory location
1522
- debug_to_log("Retry close browser: hwnd #{hwnd} pid #{pid} #{where} #{lnbr} (#{__LINE__})")
1523
- browser.close
1524
- end
1525
- if browser.exists? and pid > 0 and pid < 538976288 # value of uninitialized memory location
1526
- kill_browser(browser.hwnd, __LINE__, browser, true)
1527
- end
1528
- when 'S'
1529
- if is_browser?(browser)
1530
- url = browser.url
1531
- title = browser.title
1532
- debug_to_log("Safari browser url: [#{url}]")
1533
- debug_to_log("Safari browser title: [#{title}]")
1534
- debug_to_log("Closing browser: #{where} (#{lnbr})")
1535
- close_modal_s # to close any leftover modal dialogs
1536
- browser.close
1537
- end
1538
- when 'C'
1539
- if is_browser?(browser)
1540
- url = browser.url
1541
- title = browser.title
1542
- debug_to_log("Chrome browser url: [#{url}]")
1543
- debug_to_log("Chrome browser title: [#{title}]")
1544
- debug_to_log("Closing browser: #{where} (#{lnbr})")
1545
- if url and url.length > 1
1546
- browser.close
1547
- #else
1548
- #browser = FireWatir::Firefox.attach(:title, title)
1549
- #browser.close
1550
- end
1551
-
1552
- end
1553
- else
1554
- raise "Unsupported browser: '#{@browserAbbrev}'"
1555
- end
1556
- end
1557
- # rescue => e
1558
- # if not e.is_a?(Vapir::WindowGoneException)
1559
- # raise e
1560
- # end
1561
- end
1562
-
1563
- #close popup in new window
1564
- def close_new_window_popup(popup)
1565
- if is_browser?(popup)
1566
- url = popup.url
1567
- debug_to_log("Closing popup '#{url}' ")
1568
- popup.close
1569
-
1570
- end
1571
- end
1572
-
1573
- def close_panel_by_text(browser, panel, strg = 'Close')
1574
- if validate(browser, @myName, __LINE__)
1575
- if @browserAbbrev == 'IE'
1576
- panel.link(:text, strg).click!
1577
- elsif $USE_FIREWATIR
1578
- begin
1579
- panel.link(:text, strg).click
1580
- rescue => e
1581
- if not rescue_me(e, __method__, "link(:text,'#{strg}').click", "#{panel.class}")
1582
- raise e
1583
- end
1584
- end
1585
- else
1586
- panel.link(:text, strg).click(:wait => false)
1587
- end
1588
- sleep_for(1)
1589
- if validate(browser, @myName, __LINE__)
1590
- passed_to_log("Panel '#{strg}' (by :text) closed.")
1591
- true
1592
- end
1593
- else
1594
- failed_to_log("Panel '#{strg}' (by :text) still open.")
1595
- end
1596
- rescue
1597
- failed_to_log("Click on '#{strg}'(by :text) failed: '#{$!}' (#{__LINE__})")
1598
- end
1599
-
1600
- # def close_modal_ie(title="", button="OK", text='', side = 'primary', wait = WAIT)
1601
- def close_popup(title, button = "OK", text = '', side = 'primary', wait = WAIT, desc = '', quiet = false)
1602
- #TODO needs simplifying and debug code cleaned up
1603
- title = translate_popup_title(title)
1604
- msg = "'#{title}'"
1605
- msg << " with text '#{text}'" if text.length > 0
1606
- msg << " (#{desc})" if desc.length > 0
1607
- @ai.Opt("WinSearchChildren", 1) # Match any substring in the title
1608
- if @ai.WinWait(title, text, wait) > 0
1609
- myHandle = @ai.WinGetHandle(title, text)
1610
- full_text = @ai.WinGetText(title)
1611
- #debug_to_report("Found popup handle:'#{myHandle}', title:'#{title}', text:'#{full_text}'")
1612
- if myHandle.length > 0
1613
- debug_to_log("hwnd: #{myHandle.inspect}")
1614
- passed_to_log("#{msg} appeared.") unless quiet
1615
- sleep_for(0.5)
1616
- @ai.WinActivate(title, text)
1617
- if @ai.WinActive(title, text) # > 0 #Hack to prevent fail when windows session locked
1618
- debug_to_log("#{msg} activated.")
1619
- if @ai.ControlFocus(title, text, button) # > 0
1620
- controlHandle = @ai.ControlGetHandle(title, '', "[CLASS:Button; TEXT:#{button}]")
1621
- if not controlHandle
1622
- button = "&#{button}"
1623
- controlHandle = @ai.ControlGetHandle(title, '', "[CLASS:Button; TEXT:#{button}]")
1624
- end
1625
- debug_to_log("Handle for button '#{button}': [#{controlHandle}]")
1626
- debug_to_log("#{msg} focus gained.")
1627
- # sleep_for(2)
1628
- if @ai.ControlClick(title, text, button, side) # > 0
1629
- # if @ai.ControlClick(title, text, "[Handle:#{controlHandle}]", side) > 0
1630
- # debug_to_log("#{msg} #{side} click on 'Handle:#{controlHandle}'." )
1631
- debug_to_log("#{msg} #{side} click on '#{button}' successful.")
1632
- sleep_for(1)
1633
- if @ai.WinExists(title, text) > 0
1634
- debug_to_log("#{msg} close popup failed on click '#{button}'. Trying WinClose. (#{__LINE__})")
1635
- @ai.WinClose(title, text)
1636
- if @ai.WinExists(title, text) > 0
1637
- debug_to_log("#{msg} close popup failed with WinClose('#{title}','#{text}'). (#{__LINE__})")
1638
- @ai.WinKill(title, text)
1639
- if @ai.WinExists(title, text) > 0
1640
- debug_to_log("#{msg} close popup failed with WinKill('#{title}','#{text}'). (#{__LINE__})")
1641
- else
1642
- debug_to_log("#{msg} closed successfully with WinKill('#{title}','#{text}').")
1643
- end
1644
- else
1645
- debug_to_log("#{msg} closed successfully with WinClose('#{title}','#{text}').")
1646
- end
1647
- else
1648
- passed_to_log("#{msg} closed successfully.") unless quiet
1649
- end
1650
- else
1651
- failed_to_log("#{msg} #{side} click on '#{button}' failed. (#{__LINE__})")
1652
- end
1653
- else
1654
- failed_to_log("#{msg} Unable to gain focus on button (#{__LINE__})")
1655
- end
1656
- else
1657
- failed_to_log("#{msg} Unable to activate (#{__LINE__})")
1658
- end
1659
- else
1660
- failed_to_log("#{msg} did not appear after #{wait} seconds. (#{__LINE__})")
1661
- end
1662
- else
1663
- failed_to_log("#{msg} did not appear after #{wait} seconds. (#{__LINE__})")
1664
- end
1665
- rescue
1666
- failed_to_log("Close popup title=#{title} failed: '#{$!}' (#{__LINE__})")
1667
- end
1668
-
1669
- alias close_popup_validate_text close_popup
1670
-
1671
- def close_popup_by_text(popup, strg = 'Close', desc = '')
1672
- count = 0
1673
- url = popup.url
1674
- if validate(popup, @myName, __LINE__)
1675
- count = string_count_in_string(popup.text, strg)
1676
- if count > 0
1677
- # @waiter.wait_until( browser.link(:text, strg).exists? ) if @waiter
1678
- begin
1679
- popup.link(:text, strg).click
1680
- rescue => e
1681
- if not rescue_me(e, __method__, "link(:text,'#{strg}')", "#{popup.class}")
1682
- raise e
1683
- end
1684
- end
1685
- passed_to_log("Popup #{url} closed by clicking link with text '#{strg}'. #{desc}")
1686
- true
1687
- else
1688
- failed_to_log("Link :text=>'#{strg}' for popup #{url} not found. #{desc}")
1689
- end
1690
- end
1691
- rescue
1692
- failed_to_log("Close popup #{url} with click link :text+>'#{strg}' failed: '#{$!}' (#{__LINE__})")
1693
- debug_to_log("#{strg} appears #{count} times in popup.text.")
1694
- raise
1695
- end
1696
-
1697
- # #close a modal dialog
1698
- def close_modal(browser, title="", button="OK", text='', side = 'primary', wait = WAIT)
1699
- case @targetBrowser.abbrev
1700
- when 'IE'
1701
- close_modal_ie(browser, title, button, text, side, wait)
1702
- when 'FF'
1703
- close_modal_ff(browser, title, button, text, side)
1704
- when 'S'
1705
- close_modal_s
1706
- when 'C', 'GC'
1707
- close_modal_c(browser, title)
1708
- end
1709
- end
1710
-
1711
- # TODO: Logging
1712
- def close_modal_c(browser, title)
1713
- browser.window(:url, title).close
1714
- end
1715
-
1716
- # TODO: Logging
1717
- def close_modal_s
1718
- # simply closes the frontmost Safari dialog
1719
- Appscript.app("Safari").activate; Appscript.app("System Events").processes["Safari"].key_code(52)
1720
- end
1721
-
1722
- def close_modal_ie(browser, title="", button="OK", text='', side = 'primary', wait = WAIT, desc = '', quiet = false)
1723
- #TODO needs simplifying, incorporating text verification, and debug code cleaned up
1724
- title = translate_popup_title(title)
1725
- msg = "Modal window (popup) '#{title}'"
1726
- if @ai.WinWait(title, text, wait)
1727
- myHandle = @ai.WinGetHandle(title, text)
1728
- if myHandle.length > 0
1729
- debug_to_log("hwnd: #{myHandle.inspect}")
1730
- passed_to_log("#{msg} appeared.") unless quiet
1731
- window_handle = "[HANDLE:#{myHandle}]"
1732
- sleep_for(0.5)
1733
- @ai.WinActivate(window_handle)
1734
- if @ai.WinActive(window_handle)
1735
- debug_to_log("#{msg} activated.")
1736
- controlHandle = @ai.ControlGetHandle(title, '', "[CLASS:Button; TEXT:#{button}]")
1737
- if not controlHandle.length > 0
1738
- button = "&#{button}"
1739
- controlHandle = @ai.ControlGetHandle(title, '', "[CLASS:Button; TEXT:#{button}]")
1740
- end
1741
- debug_to_log("Handle for button '#{button}': [#{controlHandle}]")
1742
- debug_to_log("#{msg} focus gained.")
1743
- if @ai.ControlClick(title, '', "[CLASS:Button; TEXT:#{button}]")
1744
- passed_to_log("#{msg} #{side} click on '[CLASS:Button; TEXT:#{button}]' successful.")
1745
- sleep_for(0.5)
1746
- if @ai.WinExists(window_handle)
1747
- debug_to_log("#{msg} close popup failed on click '#{button}'. Trying WinClose. (#{__LINE__})")
1748
- @ai.WinClose(title, text)
1749
- if @ai.WinExists(window_handle)
1750
- debug_to_log("#{msg} close popup failed with WinClose(#{window_handle}). (#{__LINE__})")
1751
- @ai.WinKill(window_handle)
1752
- if @ai.WinExists(window_handle)
1753
- debug_to_log("#{msg} close popup failed with WinKill(#{window_handle}). (#{__LINE__})")
1754
- else
1755
- debug_to_log("#{msg} closed successfully with WinKill(#{window_handle}).")
1756
- end
1757
- else
1758
- debug_to_log("#{msg} closed successfully with WinClose(#{window_handle}).")
1759
- end
1760
- else
1761
- passed_to_log("#{msg} closed successfully.")
1762
- end
1763
- else
1764
- failed_to_log("#{msg} #{side} click on '[CLASS:Button; TEXT:#{button}]' failed. (#{window_handle}) (#{__LINE__})")
1765
- end
1766
- else
1767
- failed_to_log("#{msg} Unable to activate (#{window_handle}) (#{__LINE__})")
1768
- end
1769
- else
1770
- failed_to_log("#{msg} did not appear after #{wait} seconds. (#{window_handle}) (#{__LINE__})")
1771
- end
1772
- else
1773
- failed_to_log("#{msg} did not appear after #{wait} seconds.(#{window_handle}) (#{__LINE__})")
1774
- end
1775
- rescue
1776
- failed_to_log("Close popup title=#{title} failed: '#{$!}' (#{__LINE__})")
1777
- end
1778
-
1779
- # private :close_modal_ie
1780
-
1781
- def close_modal_ff(browser, title="", button=nil, text="", side='')
1782
- title = translate_popup_title(title)
1783
- msg = "Modal dialog (popup): title=#{title} button='#{button}' text='#{text}' side='#{side}':"
1784
- modal = browser.modal_dialog(:timeout => WAIT)
1785
- if modal.exists?
1786
- modal_text = modal.text
1787
- if text.length > 0
1788
- if modal_text =~ /#{text}/
1789
- passed_to_log("#{msg} appeared with match on '#{text}'.")
1790
- else
1791
- failed_to_log("#{msg} appeared but did not match '#{text}' ('#{modal_text}).")
1792
- end
1793
- else
1794
- passed_to_log("#{msg} appeared.")
1795
- end
1796
- if button
1797
- modal.click_button(button)
1798
- else
1799
- modal.close
1800
- end
1801
- if modal.exists?
1802
- failed_to_log("#{msg} close failed. (#{__LINE__})")
1803
- else
1804
- passed_to_log("#{msg} closed successfully.")
1805
- end
1806
- else
1807
- failed_to_log("#{msg} did not appear after #{WAIT} seconds. (#{__LINE__})")
1808
- end
1809
- rescue
1810
- failed_to_log("#{msg} Unable to validate modal popup: '#{$!}'. (#{__LINE__})")
1811
- end
1812
-
1813
- def handle_popup(title, text = '', button= 'OK', side = 'primary', wait = WAIT, desc = '')
1814
- title = translate_popup_title(title)
1815
- msg = "'#{title}'"
1816
- if text.length > 0
1817
- msg << " with text '#{text}'"
1818
- end
1819
- @ai.Opt("WinSearchChildren", 1) # match title from start, forcing default
1820
-
1821
- if button and button.length > 0
1822
- if button =~ /ok|yes/i
1823
- id = '1'
1824
- else
1825
- id = '2'
1826
- end
1827
- else
1828
- id = ''
1829
- end
1830
-
1831
- if @ai.WinWait(title, '', wait) > 0
1832
- myHandle = @ai.WinGetHandle(title, '')
1833
- window_handle = "[HANDLE:#{myHandle}]"
1834
- full_text = @ai.WinGetText(window_handle)
1835
- debug_to_log("Found popup handle:'#{myHandle}', title:'#{title}', text:'#{full_text}'")
1836
-
1837
- controlHandle = @ai.ControlGetHandle(window_handle, '', "[CLASS:Button; TEXT:#{button}]")
1838
- if not controlHandle
1839
- # button = "&#{button}"
1840
- controlHandle = @ai.ControlGetHandle(window_handle, '', "[CLASS:Button; TEXT:&#{button}]")
1841
- end
1842
-
1843
- if text.length > 0
1844
- if full_text =~ /#{text}/
1845
- passed_to_log("Found popup handle:'#{myHandle}', title:'#{title}', text includes '#{text}'. #{desc}")
1846
- else
1847
- failed_to_log("Found popup handle:'#{myHandle}', title:'#{title}', text does not include '#{text}'. Closing it. #{desc}")
1848
- end
1849
- end
1850
-
1851
- @ai.WinActivate(window_handle, '')
1852
- @ai.ControlClick(window_handle, '', id, side)
1853
- if @ai.WinExists(title, '') > 0
1854
- debug_to_log("#{msg} @ai.ControlClick on '#{button}' (ID:#{id}) with handle '#{window_handle}' failed to close window. Trying title.")
1855
- @ai.ControlClick(title, '', id, side)
1856
- if @ai.WinExists(title, '') > 0
1857
- debug_to_report("#{msg} @ai.ControlClick on '#{button}' (ID:#{id}) with title '#{title}' failed to close window. Forcing closed.")
1858
- @ai.WinClose(title, '')
1859
- if @ai.WinExists(title, '') > 0
1860
- debug_to_report("#{msg} @ai.WinClose on title '#{title}' failed to close window. Killing window.")
1861
- @ai.WinKill(title, '')
1862
- if @ai.WinExists(title, '') > 0
1863
- failed_to_log("#{msg} @ai.WinKill on title '#{title}' failed to close window")
1864
- else
1865
- passed_to_log("Killed: popup handle:'#{myHandle}', title:'#{title}'. #{desc}")
1866
- true
1867
- end
1868
- else
1869
- passed_to_log("Forced closed: popup handle:'#{myHandle}', title:'#{title}'. #{desc}")
1870
- true
1871
- end
1872
- else
1873
- passed_to_log("Closed on '#{button}': popup handle:'#{myHandle}', title:'#{title}'. #{desc}")
1874
- true
1875
- end
1876
- else
1877
- passed_to_log("Closed on '#{button}': popup handle:'#{myHandle}', title:'#{title}'. #{desc}")
1878
- true
1879
- end
1880
-
1881
- else
1882
- failed_to_log("#{msg} did not appear after #{wait} seconds. #{desc} (#{__LINE__})")
1883
- end
1884
- rescue
1885
- failed_to_log("Unable to handle popup #{msg}: '#{$!}' #{desc} (#{__LINE__})")
1886
-
1887
- end
1888
-
1889
- # howLong is integer, whatFor is a browser object
1890
- =begin rdoc
1891
- :category: Waits
1892
- :tags:wait
1893
- howLong is the number of seconds, text is a string to be found, threshold is the number of seconds
1894
- after which a fail message is generated even though the text was detected within the howLong limit.
1895
- Use this in place of wait_until_by_text when the wait time needs to be longer than the test automation default.
1896
- =end
1897
- def hold_for_text(browser, howLong, text, desc = '', threshold = 20, interval = 0.25)
1898
- countdown = howLong
1899
- while ((not browser.contains_text(text)) and countdown > 0)
1900
- sleep(interval)
1901
- countdown = countdown - interval
1902
- end
1903
- if countdown < howLong
1904
- waittime = howLong - countdown
1905
- passed_tolog("#{__method__} '#{text}' found after #{waittime} second(s) #{desc}")
1906
- if waittime > threshold
1907
- failed_tolog("#{__method__} '#{text}' took #{waittime} second(s). (threshold: #{threshold} seconds) #{desc}")
1908
- end
1909
- true
1910
- else
1911
- failed_tolog("#{__method__} '#{text}' not found after #{howLong} second(s) #{desc}")
1912
- false
1913
- end
1914
- rescue
1915
- failed_tolog("Unable to #{__method__} '#{text}'. '#{$!}' #{desc}")
1916
- end
1917
-
1918
- alias wait_for_text hold_for_text
1919
-
1920
- def hover(browser, element, wait = 2)
1921
- w1, h1, x1, y1, xc1, yc1, xlr1, ylr1 = get_element_coordinates(browser, element, true)
1922
- @ai.MoveMouse(xc1, yc1)
1923
- sleep_for(1)
1924
- end
1925
-
1926
- def get_save_file_path(root, filename)
1927
- filespec = "#{root}/file/#{filename}"
1928
- filespec.gsub!('/', '\\')
1929
- end
1930
-
1931
- def save_file_orig(filepath, desc = '', wait = WAIT)
1932
- # title = translate_popup_title(title)
1933
- @ai.WinWait("File Download", "", wait)
1934
- @ai.ControlFocus("File Download", "", "&Save")
1935
- sleep 1
1936
- @ai.ControlClick("File Download", "", "&Save", "left")
1937
- @ai.WinWait("Save As", "", wait)
1938
- sleep 1
1939
- @ai.ControlSend("Save As", "", "Edit1", filepath)
1940
- @ai.ControlClick("Save As", "", "&Save", "left")
1941
- sleep 1
1942
- @ai.WinWait("Download complete", "", wait)
1943
- @ai.ControlClick("Download complete", "", "Close")
1944
- end
1945
-
1946
- #TODO This and save_file2 have to be combined somehow.
1947
- def save_file1(filepath, title = "File Download", desc = '', wait = WAIT)
1948
- title = translate_popup_title(title)
1949
- @ai.WinWait(title, '', wait)
1950
- @ai.WinActivate(title, '')
1951
- sleep 1
1952
- @ai.ControlFocus(title, "", "&Save")
1953
- sleep 3
1954
- @ai.ControlClick(title, "", "&Save", "primary")
1955
- sleep 2
1956
- @ai.ControlClick(title, "", "Save", "primary")
1957
-
1958
- @ai.WinWait("Save As", "", wait)
1959
- sleep 1
1960
- @ai.ControlSend("Save As", "", "Edit1", filepath)
1961
- @ai.ControlFocus("Save As", "", "&Save")
1962
- @ai.ControlClick("Save As", "", "&Save", "primary")
1963
- @ai.ControlClick("Save As", "", "Save", "primary")
1964
-
1965
- @ai.WinWait("Download complete", "", wait)
1966
- passed_to_log("Save file '#{filepath}' succeeded. #{desc}")
1967
- @ai.ControlClick("Download complete", "", "Close")
1968
- rescue
1969
- failed_to_log("Save file failed: #{desc} '#{$!}'. (#{__LINE__})")
1970
- end
1971
-
1972
- def save_file2(filepath, title = "File Download - Security Warning", desc = '', wait = WAIT)
1973
- title = translate_popup_title(title)
1974
- sleep(1)
1975
- @ai.WinWait(title, '', wait)
1976
- dl_hndl = @ai.WinGetHandle(title, '')
1977
- dl_sv_hndl = @ai.ControlGetHandle(title, '', "&Save")
1978
- @ai.WinActivate(title, '')
1979
- sleep 1
1980
- @ai.ControlFocus(title, "", "&Save")
1981
- sleep 1
1982
- @ai.ControlFocus(title, "", "Save")
1983
- sleep 1
1984
- @ai.ControlClick(title, "", "&Save", "primary")
1985
- sleep 1
1986
- @ai.ControlClick(title, "", "Save", "primary")
1987
- sleep 1
1988
- w = WinClicker.new
1989
- w.clickButtonWithHandle(dl_sv_hndl)
1990
- sleep 1
1991
- w.clickWindowsButton_hwnd(dl_hndl, "Save")
1992
- sleep 1
1993
- w.clickWindowsButton_hwnd(dl_hndl, "&Save")
1994
-
1995
- @ai.WinWait("Save As", "", wait)
1996
- sleep 1
1997
- @ai.ControlSend("Save As", "", "Edit1", filepath)
1998
- @ai.ControlFocus("Save As", "", "&Save")
1999
- @ai.ControlClick("Save As", "", "&Save", "primary")
2000
-
2001
- @ai.WinWait("Download complete", "", wait)
2002
- passed_to_log("Save file '#{filepath}' succeeded. #{desc}")
2003
- @ai.ControlClick("Download complete", "", "Close")
2004
- rescue
2005
- failed_to_log("Save file failed: #{desc} '#{$!}'. (#{__LINE__})")
2006
- end
2007
-
2008
- #method for handling save dialog
2009
- #use click_no_wait on the action that triggers the save dialog
2010
- def save_file(filepath, download_title = "File Download - Security Warning")
2011
- # TODO need version for Firefox
2012
- # TODO need to handle first character underline, e.g. 'Cancel' and '&Cancel'
2013
- download_title = translate_popup_title(download_title)
2014
- download_text = ''
2015
- download_control = "&Save"
2016
- saveas_title = 'Save As'
2017
- saveas_text = ''
2018
- saveas_control = "Edit1"
2019
- dnld_cmplt_title = "Download Complete"
2020
- dnld_cmplt_title = translate_popup_title(dnld_cmplt_title)
2021
- dnld_cmplt_text = ""
2022
- # save_title = ""
2023
- side = 'primary'
2024
- msgdl = "Window '#{download_title}':"
2025
- msgsa = "Window '#{saveas_title}':"
2026
- msgdc = "Window '#{dnld_cmplt_title}':"
2027
- begin
2028
- if @ai.WinWait(download_title, download_text, WAIT)
2029
- @ai.WinActivate(download_title, download_text)
2030
- if @ai.WinActive(download_title, download_text)
2031
- dl_title = @ai.WinGetTitle(download_title, download_text)
2032
- # dl_hndl = @ai.WinGetHandle(download_title, download_text)
2033
- # dl_text = @ai.WinGetText(download_title, download_text)
2034
- # dl_sv_hndl = @ai.ControlGetHandle(dl_title, '', download_control)
2035
- # dl_op_hndl = @ai.ControlGetHandle(dl_title, '', '&Open')
2036
- # dl_cn_hndl = @ai.ControlGetHandle(dl_title, '', 'Cancel')
2037
- debug_to_log("#{msgdl} activated. (#{__LINE__})")
2038
-
2039
- if @ai.ControlFocus(dl_title, download_text, download_control)
2040
- debug_to_log("#{msgdl} focus gained. (#{__LINE__})")
2041
-
2042
- @ai.Send("S")
2043
- # @ai.ControlSend(dl_Stitle, download_text, download_control, "{ENTER}")
2044
- sleep_for 1
2045
-
2046
- if @ai.ControlClick(dl_title, download_text, download_control, side)
2047
- debug_to_log("#{msgdl} click succeeded on '#{download_control}'. (#{__LINE__})")
2048
-
2049
- if @ai.WinWait(saveas_title, saveas_text, WAIT)
2050
- debug_to_log("#{msgsa} appeared. (#{__LINE__})")
2051
- sleep_for 1
2052
- if @ai.ControlSend(saveas_title, saveas_text, saveas_control, filepath)
2053
- debug_to_log("#{msgsa} controlsend of '#{saveas_control}' succeeded. (#{__LINE__})")
2054
-
2055
- @ai.Send("S")
2056
- @ai.ControlSend(saveas_title, saveas_text, saveas_control, "{ENTER}")
2057
- sleep_for 1
2058
-
2059
- if @ai.ControlClick(saveas_title, saveas_text, saveas_control, side)
2060
- passed_to_log("#{msgsa} click succeeded on '#{saveas_control}'. (#{__LINE__})")
2061
- if @ai.WinWait(dnld_cmplt_title, dnld_cmplt_text, WAIT)
2062
- debug_to_log("#{msgdc} appeared. (#{__LINE__})")
2063
- sleep_for 1
2064
- if @ai.ControlClick(dnld_cmplt_title, dnld_cmplt_text, "Close", side)
2065
- passed_to_log("Save file for #{filepath} succeeded.")
2066
- else
2067
- failed_to_log("#{msgdc} click failed on 'Close'. (#{__LINE__})")
2068
- end
2069
- else
2070
- failed_to_log("#{msgdc} did not appear after #{WAIT} seconds. (#{__LINE__})")
2071
- end
2072
- else
2073
- failed_to_log("#{msgsa} click failed on '#{saveas_control}'. (#{__LINE__})")
2074
- end
2075
- else
2076
- failed_to_log("#{msgsa} controlsend of '#{saveas_control}' failed. (#{__LINE__})")
2077
- end
2078
- else
2079
- failed_to_log("#{msgsa} did not appear after #{WAIT} seconds. (#{__LINE__})")
2080
- end
2081
- else
2082
- failed_to_log("#{msgdl} click failed on '#{download_control}'. (#{__LINE__})")
2083
- end
2084
- else
2085
- failed_to_log("#{msgdl} Unable to gain focus on control '#{dl_title}'. (#{__LINE__})")
2086
- end
2087
- else
2088
- failed_to_log("#{msgdl} Unable to activate. (#{__LINE__})")
2089
- end
2090
- else
2091
- failed_to_log("#{msgdl} did not appear after #{WAIT} seconds. (#{__LINE__})")
2092
- end
2093
- rescue
2094
- failed_to_log("Save file failed: '#{$!}'. (#{__LINE__})")
2095
- end
2096
- end
2097
-
2098
- def close_log(scriptName, lnbr = '')
2099
- cmplTS = Time.now.to_f.to_s
2100
- puts ("#{scriptName} finished. Closing log. #{lnbr.to_s}")
2101
- passed_to_log("#{scriptName} run complete [#{cmplTS}]")
2102
- @myLog.close()
2103
- sleep(2)
2104
- end
2105
-
2106
- protected :close_log
2107
-
2108
- #method for cancelling Print window
2109
- # TODO need to handle 'Cancel' and '&Cancel' (first character underlined)
2110
- def close_print(title = 'Print', text = '', button = '&Cancel', side = 'left')
2111
- msg = "Popup: title=#{title} button='#{button}' text='#{text}' side='#{side}':"
2112
- if @ai.WinWait(title, text, WAIT)
2113
- passed_to_log("#{msg} found.")
2114
- @ai.WinActivate(title)
2115
- if @ai.WinActive(title, text)
2116
- passed_to_log("#{msg} activated.")
2117
- if @ai.ControlFocus(title, text, button)
2118
- passed_to_log("#{msg} focus attained.")
2119
- if @ai.ControlClick(title, text, button, side)
2120
- passed_to_log("#{msg} closed successfully.")
2121
- else
2122
- failed_to_log("#{msg} click failed on button (#{__LINE__})")
2123
- end
2124
- else
2125
- failed_to_log("#{msg} Unable to gain focus on button (#{__LINE__})")
2126
- end
2127
- else
2128
- failed_to_log("#{msg} Unable to activate (#{__LINE__})")
2129
- end
2130
- else
2131
- failed_to_log("#{msg} did not appear after #{WAIT} seconds. (#{__LINE__})")
2132
- end
2133
- rescue
2134
- failed_to_log("Close #{msg}: '#{$!}'. (#{__LINE__})")
2135
- end
2136
-
2137
- #method for handling file download dialog
2138
- #use click_no_wait on the action that triggers the save dialog
2139
- # TODO need version for Firefox
2140
- # TODO need to handle 'Cancel' and '&Cancel' (first character underlined)
2141
- # TODO replace call to close_modal_ie with actual file download
2142
- def file_download(browser = nil)
2143
- title = 'File Download'
2144
- title = translate_popup_title(title)
2145
- text = ''
2146
- button = 'Cancel'
2147
- if @browserAbbrev == 'IE'
2148
- close_popup(title, button, text)
2149
- else
2150
-
2151
- end
2152
- end
2153
-
2154
- #method for handling file upload dialog
2155
- #use click_no_wait on the action that triggers the save dialog
2156
- # TODO need version for Firefox
2157
- def file_upload(filepath)
2158
- title = 'Choose File'
2159
- title = translate_popup_title(title)
2160
- text = ''
2161
- button = "&Open"
2162
- control = "Edit1"
2163
- side = 'primary'
2164
- msg = "Window title=#{title} button='#{button}' text='#{text}' side='#{side}':"
2165
- begin
2166
- if @ai.WinWait(title, text, WAIT)
2167
- passed_to_log("#{msg} found.")
2168
- @ai.WinActivate(title, text)
2169
- if @ai.WinActive(title, text)
2170
- passed_to_log("#{msg} activated.")
2171
- if @ai.ControlSend(title, text, control, filepath)
2172
- passed_to_log("#{msg} #{control} command sent.")
2173
- sleep_for 1
2174
- if @ai.ControlClick(title, text, button, "primary")
2175
- passed_to_log("#{msg} Upload of #{filepath} succeeded.")
2176
- else
2177
- failed_to_log("#{msg} Upload of #{filepath} failed. (#{__LINE__})")
2178
- end
2179
- else
2180
- failed_to_log("#{msg} Unable to select #{filepath}. (#{__LINE__})")
2181
- end
2182
- else
2183
- failed_to_log("#{msg} Unable to activate. (#{__LINE__})")
2184
- end
2185
- else
2186
- failed_to_log("#{msg} did not appear after #{WAIT} seconds. (#{__LINE__})")
2187
- end
2188
- rescue
2189
- failed_to_log("#{msg} Unable to upload: '#{$!}'. (#{__LINE__})")
2190
- end
2191
-
2192
- end
2193
-
2194
- def find_popup(browser, how, what, desc = '')
2195
- msg = "Find popup :#{how}=>'#{what}'. #{desc}"
2196
- popup = Watir::IE.find(how, what) # TODO: too browser specific
2197
- sleep_for(1)
2198
- debug_to_log("#{popup.inspect}")
2199
- if is_browser?(popup)
2200
- # title = popup.title
2201
- passed_to_log(msg)
2202
- return popup
2203
- else
2204
- failed_to_log(msg)
2205
- end
2206
- rescue
2207
- failed_to_log("Unable to find popup :#{how}=>'#{what}'. #{desc} '#{$!}' (#{__LINE__})")
2208
- end
2209
-
2210
- def find_sheet_with_name(workbook, sheet_name)
2211
- sheets = workbook.sheets
2212
- idx = 0
2213
- found = false
2214
- sheets.each do |s|
2215
- if s == sheet_name
2216
- found = true
2217
- break
2218
- end
2219
- idx += 1
2220
- end
2221
- if found
2222
- idx
2223
- else
2224
- -1
2225
- end
2226
- end
2227
-
2228
- # howLong is integer, whatFor is a browser object
2229
- def wait_for_text(browser, howLong, text)
2230
- countdown = howLong
2231
- while ((not browser.contains_text(text)) and countdown > 0)
2232
- sleep(1)
2233
- countdown = countdown - 1
2234
- end
2235
- if countdown
2236
- passed_tolog("wait_for_text '#{text}' found after #{howLong} second(s)")
2237
- else
2238
- failed_tolog("wait_for_text '#{text}' not foundafter #{howLong} second(s)")
2239
- end
2240
- countdown
2241
- end
2242
-
2243
- def wait_for_element_to_reappear(browser, how, what, desc = '', timeout = 20)
2244
- msg = "Element #{how}=#{what} exists. #{desc}"
2245
- wait_while(browser, "While: #{msg}", timeout) { browser.element(how, what).exists? }
2246
- wait_until(browser, "Until: #{msg}", timeout) { browser.element(how, what).exists? }
2247
- end
2248
-
2249
- # howLong is integer, whatFor is a browser object
2250
- def wait_for_exists(howLong, whatFor)
2251
- wait_for(howLong, whatFor)
2252
- end
2253
-
2254
- def wait_for(howLong, whatFor)
2255
- countdown = howLong
2256
- while ((not whatFor.exists?) and countdown > 0)
2257
- sleep(1)
2258
- puts whatFor.inspect+':'+countdown.to_s
2259
- countdown = countdown - 1
2260
- end
2261
- if countdown
2262
- puts 'found '+whatFor.inspect
2263
- passed_tolog("wait_for (#{howLong} found "+whatFor.inspect)
2264
- else
2265
- puts 'Did not find '+whatFor.inspect
2266
- failed_tolog("wait_for (#{howLong} did not find "+whatFor.inspect)
2267
- end
2268
- countdown
2269
- end
2270
-
2271
- def wait_the_hard_way(browser, how, what, wait = 6, intvl = 0.25)
2272
- count = (wait / intvl).to_i + 1
2273
- tally = 0
2274
- ok = (1 / intvl).to_i + 1
2275
- debug_to_log("#{__method__}: wait: #{wait} secs; intvl: #{intvl} secs; count; #{count}; thresh: #{ok}")
2276
- (1..count).each do |x|
2277
- begin
2278
- if browser.element(how, what).exists?
2279
- tally += 1
2280
- debug_to_log("#{x}: #{(x - 1) * intvl}: #{what} exists.")
2281
- else
2282
- tally = 0
2283
- debug_to_log("#{x}: #{(x - 1) * intvl}: #{what} does not exist.")
2284
- end
2285
- rescue
2286
- tally = 0
2287
- debug_to_log("#{x}: #{(x - 1) * intvl}: #{what} rescue: #{$!}")
2288
- end
2289
- if tally >= ok
2290
- return true
2291
- end
2292
- sleep(intvl)
2293
- end
2294
- end
2295
-
2296
- def is_browser?(browser)
2297
- myClass = browser.class.to_s
2298
- case @targetBrowser.abbrev
2299
- when 'IE'
2300
- myClass =~ /Watir::/i # TODO: should this be /Watir::IE/i ?
2301
- when 'FF'
2302
- if @version.to_f < 4.0
2303
- myClass =~ /FireWatir::/i
2304
- else
2305
- myClass =~ /Watir::Browser/i
2306
- end
2307
- when 'S'
2308
- myClass =~ /Watir::Safari/i
2309
- when 'C'
2310
- myClass =~ /Watir::Browser/i
2311
- end
2312
- end
2313
-
2314
- alias is_browser is_browser?
2315
-
2316
- def nice_array(arr, space_to_underscore = false)
2317
- new_arr = Array.new
2318
- if space_to_underscore
2319
- arr.each do |nty|
2320
- new_arr << nty.gsub(/\s/, '_')
2321
- end
2322
- else
2323
- new_arr = arr
2324
- end
2325
- "['#{new_arr.join("','")}']"
2326
- end
2327
-
2328
- def open_browser(url = nil)
2329
- debug_to_log("Opening browser: #{@targetBrowser.name}")
2330
- debug_to_log("#{__method__}: [#{get_caller_line}] #{get_callers}")
2331
- case @targetBrowser.abbrev
2332
- when 'IE'
2333
- @myBrowser = open_ie
2334
- @myHwnd = @myBrowser.hwnd
2335
- #@waiter = Watir::Waiter.new(WAIT)
2336
- when 'FF'
2337
- #version = "11"
2338
- #@myBrowser = open_ff_for_version(version)
2339
- @myBrowser = open_ff_for_version
2340
- when 'S'
2341
- debug_to_log("Opening browser: #{@targetBrowser.name} legacy.rb:#{__LINE__}")
2342
- aBrowser = Watir::Safari.new
2343
- debug_to_log("Browser instantiated")
2344
- @myBrowser = aBrowser
2345
- #require 'shamisen/awetest_legacy/safari_waiter'
2346
- #@waiter = Watir::Waiter
2347
- when 'C', 'GC'
2348
- @myBrowser = open_chrome
2349
- ##require 'shamisen/awetest_legacy/webdriver_waiter'
2350
- #require 'shamisen/support/webdriver_ext/browser'
2351
- #@waiter = Watir::Waiter
2352
-
2353
- else
2354
- raise "Unsupported browser: #{@targetBrowser.name}"
2355
- end
2356
- get_browser_version(@myBrowser)
2357
- if url
2358
- go_to_url(@myBrowser, url)
2359
- end
2360
- @myBrowser
2361
- end
2362
-
2363
- def open_ie(process = true)
2364
- check_for_and_clear_other_browsers
2365
- Watir::Browser.default = 'ie'
2366
- #if process && !IS_WIN_2008
2367
- # browser = Watir::IE.new_process
2368
- #else
2369
- browser = Watir::IE.new
2370
- #end
2371
- browser
2372
- end
2373
-
2374
- def open_ff_for_version(version = @targetVersion)
2375
- if version.to_f < 4.0
2376
- browser = open_ff
2377
- #waiter = Watir::Waiter.new(WAIT)
2378
- else
2379
- browser = Watir::Browser.new(:firefox)
2380
- #require 'shamisen/awetest_legacy/webdriver_waiter'
2381
- #waiter = Watir::Waiter
2382
- end
2383
- browser
2384
- end
2385
-
2386
- def open_ff
2387
- Watir::Browser.default = 'firefox'
2388
- browser = Watir::Browser.new
2389
- end
2390
-
2391
- def open_chrome
2392
- browser = Watir::Browser.new(:chrome)
2393
- end
2394
-
2395
- #Get the browser to navigate to a given url. If not supplied in the second argument,
2396
- #defaults to value of FullScript.myURL, which is populated from ApplicationEnvironment.url.
2397
-
2398
- def go_to_url(browser, url = nil, redirect = nil)
2399
- if url
2400
- @myURL = url
2401
- end
2402
- message_tolog("URL: #{@myURL}")
2403
- browser.goto(@myURL)
2404
- if validate(browser, @myName, __LINE__)
2405
- # TODO .url method returns blank in Firewatir
2406
- if redirect
2407
- passed_to_log("Redirected to url '#{browser.url}'.")
2408
- true
2409
- elsif browser.url =~ /#{@myURL}/i # or @browserAbbrev == 'FF'
2410
- passed_to_log("Navigated to url '#{@myURL}'.")
2411
- true
2412
- else
2413
- failed_to_log("Navigated to url '#{browser.url}' but expected '#{@myURL}'.")
2414
- end
2415
- end
2416
- rescue
2417
- fatal_to_log("Unable to navigate to '#{@myURL}': '#{$!}'")
2418
- end
2419
-
2420
-
2421
3
  #--
2422
4
  ##def open_log
2423
5
  # start = Time.now.to_f.to_s
@@ -2441,2283 +23,18 @@ Use this in place of wait_until_by_text when the wait time needs to be longer th
2441
23
  #end
2442
24
  #++
2443
25
 
2444
- # :category: User Input
2445
- def click(browser, element, how, what, desc = '')
2446
- #debug_to_log("#{__method__}: #{element}, #{how}, #{what}")
2447
- msg = "Click #{element} :#{how}=>'#{what}'"
2448
- msg << ", '#{desc}'" if desc.length > 0
2449
- msg1 = "#{element}(#{how}, '#{what}')"
2450
- begin
2451
- case element
2452
- when :link
2453
- browser.link(how, what).click
2454
- when :button
2455
- browser.button(how, what).click
2456
- when :image
2457
- browser.image(how, what).click
2458
- when :radio
2459
- case how
2460
- when :index
2461
- set_radio_by_index(browser, what, desc)
2462
- else
2463
- browser.radio(how, what).set
2464
- end
2465
- when :span
2466
- browser.span(how, what).click
2467
- when :div
2468
- browser.div(how, what).click
2469
- when :cell
2470
- browser.cell(how, what).click
2471
- else
2472
- browser.element(how, what).click
2473
- end
2474
- rescue => e
2475
- if not rescue_me(e, __method__, "browser(#{msg1}).click", "#{browser.class}")
2476
- raise e
2477
- end
2478
- end
2479
- if validate(browser, @myName, __LINE__)
2480
- passed_to_log(msg)
2481
- true
2482
- end
2483
- rescue
2484
- failed_to_log("Unable to #{msg}. '#{$!}'")
2485
- end
2486
-
2487
- # :category: User Input
2488
- def click_no_wait(browser, element, how, what, desc = '')
2489
- debug_to_log("#{__method__}: #{element}, #{how}, #{what}")
2490
- msg = "Click no wait #{element} :#{how}=>'#{what}'"
2491
- msg << ", '#{desc}'" if desc.length > 0
2492
- msg1 = "#{element}(#{how}, '#{what}'"
2493
- begin
2494
- case element
2495
- when :link
2496
- browser.link(how, what).click_no_wait
2497
- when :button
2498
- browser.button(how, what).click_no_wait
2499
- when :image
2500
- browser.image(how, what).click_no_wait
2501
- when :radio
2502
- case how
2503
- when :index
2504
- set_radio_no_wait_by_index(browser, what, desc)
2505
- else
2506
- browser.radio(how, what).click_no_wait
2507
- end
2508
- when :span
2509
- browser.span(how, what).click_no_wait
2510
- when :div
2511
- browser.div(how, what).click_no_wait
2512
- when :checkbox
2513
- browser.checkbox(how, what).click_no_wait
2514
- when :cell
2515
- browser.cell(how, what).click_no_wait
2516
- else
2517
- browser.element(how, what).click_no_wait
2518
- end
2519
- rescue => e
2520
- if not rescue_me(e, __method__, "browser(#{msg1}').click_no_wait", "#{browser.class}")
2521
- raise e
2522
- end
2523
- end
2524
- if validate(browser, @myName, __LINE__)
2525
- passed_to_log(msg)
2526
- true
2527
- end
2528
- rescue
2529
- failed_to_log("Unable to #{msg} '#{$!}'")
2530
- sleep_for(1)
2531
- end
2532
-
2533
- # :category: User Input
2534
- def click_button_by_id(browser, strg, desc = '')
2535
- click(browser, :button, :id, strg, desc)
2536
- end
2537
- # :category: User Input
2538
- def click_link_by_index(browser, strg, desc = '')
2539
- click(browser, :link, :index, strg, desc)
2540
- end
2541
- # :category: User Input
2542
- def click_link_by_href(browser, strg, desc = '')
2543
- click(browser, :link, :href, strg, desc)
2544
- end
2545
-
2546
- alias click_href click_link_by_href
2547
- # :category: User Input
2548
- def click_link_no_wait_by_href(browser, strg, desc = '')
2549
- click_no_wait(browser, :link, :href, strg, desc)
2550
- end
2551
-
2552
- alias click_href_no_wait click_link_no_wait_by_href
2553
- # :category: User Input
2554
- def click_button_by_index(browser, index, desc = '')
2555
- click(browser, :button, :index, index, desc)
2556
- end
2557
- # :category: User Input
2558
- def click_button_by_name(browser, strg, desc = '')
2559
- click(browser, :button, :name, strg, desc)
2560
- end
2561
- # :category: User Input
2562
- def click_button_by_text(browser, strg, desc = '')
2563
- click(browser, :button, :text, strg, desc)
2564
- end
2565
- # :category: User Input
2566
- def click_button_by_class(browser, strg, desc = '')
2567
- click(browser, :button, :class, strg, desc)
2568
- end
2569
- # :category: User Input
2570
- def click_button_no_wait_by_id(browser, strg, desc = '')
2571
- click_no_wait(browser, :button, :id, strg, desc)
2572
- end
2573
-
2574
- alias click_button_by_id_no_wait click_button_no_wait_by_id
2575
- # :category: User Input
2576
- def click_button_no_wait_by_name(browser, strg, desc = '')
2577
- click_no_wait(browser, :button, :name, strg, desc)
2578
- end
2579
- # :category: User Input
2580
- def click_button_no_wait_by_class(browser, strg, desc = '')
2581
- click_no_wait(browser, :button, :class, strg, desc)
2582
- end
2583
-
2584
- alias click_button_by_class_no_wait click_button_no_wait_by_class
2585
- # :category: User Input
2586
- def click_button_by_value(browser, strg, desc = '')
2587
- click(browser, :button, :value, strg, desc)
2588
- end
2589
- # :category: User Input
2590
- def click_button_by_title(browser, strg, desc = '')
2591
- click(browser, :button, :title, strg, desc)
2592
- end
2593
- # :category: User Input
2594
- def click_button_by_xpath_and_id(browser, strg, desc = '')
2595
- msg = "Click button by xpath and id '#{strg}' #{desc}"
2596
- if browser.button(:xpath, "//a[@id = '#{strg}']").click
2597
- passed_to_log(msg)
2598
- true
2599
- else
2600
- failed_to_log(msg)
2601
- end
2602
- rescue
2603
- failed_to_log("Unable to click button by xpath and id '#{strg}' #{desc} '#{$!}' (#{__LINE__})")
2604
- end
2605
-
2606
- alias click_button_by_xpath click_button_by_xpath_and_id
2607
- # :category: User Input
2608
- def click_link_by_id(browser, strg, desc = '')
2609
- click(browser, :link, :id, strg, desc)
2610
- end
2611
-
2612
- alias click_id click_link_by_id
2613
- # :category: User Input
2614
- def click_link_by_name(browser, strg, desc = '')
2615
- click(browser, :link, :name, strg, desc)
2616
- end
2617
-
2618
- alias click_name click_link_by_name
2619
- # :category: User Input
2620
- def click_link_by_xpath_and_id(browser, strg, desc = '')
2621
- msg = "Click link by xpath and id '#{strg}' #{desc}"
2622
- if browser.link(:xpath, "//a[@id = '#{strg}']").click
2623
- passed_to_log(msg)
2624
- true
2625
- else
2626
- failed_to_log(msg)
2627
- end
2628
- rescue
2629
- failed_to_log("Unable click on link by xpath and id '#{strg}' #{desc} '#{$!}' (#{__LINE__})")
2630
- end
2631
-
2632
- alias click_link_by_xpath click_link_by_xpath_and_id
2633
- # :category: User Input
2634
- def click_link_no_wait_by_id(browser, strg, desc = '')
2635
- click_no_wait(browser, :link, :id, strg, desc)
2636
- end
2637
-
2638
- alias click_no_wait_id click_link_no_wait_by_id
2639
- alias click_no_wait_by_id click_link_no_wait_by_id
2640
- alias click_id_no_wait click_link_no_wait_by_id
2641
- alias click_no_wait_link_by_id click_link_no_wait_by_id
2642
- # :category: User Input
2643
- def click_file_field_by_id(browser, strg, desc = '')
2644
- click(browser, :file_field, :id, strg, desc)
2645
- end
2646
- # :category: User Input
2647
- def click_img_by_alt(browser, strg, desc = '')
2648
- click(browser, :image, :alt, strg, desc)
2649
- end
2650
- # :category: User Input
2651
- def click_img_by_title(browser, strg, desc = '')
2652
- click(browser, :image, :title, strg, desc)
2653
- end
2654
- # :category: User Input
2655
- def click_img_by_xpath_and_name(browser, strg, desc = '')
2656
- msg = "Click image by xpath where name='#{strg}' #{desc}"
2657
- if browser.link(:xpath, "//input[@name = '#{strg}']").click
2658
- passed_to_log(msg)
2659
- true
2660
- else
2661
- failed_to_log(msg)
2662
- end
2663
- rescue
2664
- failed_to_log("Unable to click image by xpath where name='#{strg}' #{desc} '#{$!}'")
2665
- end
2666
-
2667
- alias click_img_by_xpath click_img_by_xpath_and_name
2668
- alias click_image_by_xpath click_img_by_xpath_and_name
2669
- alias click_image_by_xpath_and_name click_img_by_xpath_and_name
2670
- # :category: User Input
2671
- def click_img_no_wait_by_alt(browser, strg, desc = '')
2672
- click_no_wait(browser, :image, :alt, strg, desc)
2673
- end
2674
-
2675
- alias click_img_by_alt_no_wait click_img_no_wait_by_alt
2676
- # :category: User Input
2677
- def click_img_by_src(browser, strg, desc = '')
2678
- click(browser, :image, :src, strg, desc)
2679
- end
2680
- # :category: User Input
2681
- def click_img_by_src_and_index(browser, strg, index, desc = '')
2682
- msg = "Click image by src='#{strg}' and index=#{index}"
2683
- msg << " #{desc}" if desc.length > 0
2684
- browser.image(:src => strg, :index => index).click
2685
- if validate(browser, @myName, __LINE__)
2686
- passed_to_log(msg)
2687
- true
2688
- end
2689
- rescue
2690
- failed_to_log("Unable to #{msg} '#{$!}'")
2691
- end
2692
- # :category: User Input
2693
- def click_link_by_value(browser, strg, desc = '')
2694
- click(browser, :link, :value, strg, desc)
2695
- end
2696
- # :category: User Input
2697
- def click_link_by_text(browser, strg, desc = '')
2698
- click(browser, :link, :text, strg, desc)
2699
- end
2700
-
2701
- alias click_link click_link_by_text
2702
- alias click_text click_link_by_text
2703
- alias click_js_button click_link_by_text
2704
- # :category: User Input
2705
- def click_link_by_class(browser, strg, desc = '')
2706
- click(browser, :link, :class, strg, desc)
2707
- end
2708
-
2709
- alias click_class click_link_by_class
2710
- # :category: User Input
2711
- def click_button_no_wait_by_text(browser, strg, desc = '')
2712
- click_no_wait(browser, :button, :text, strg, desc)
2713
- end
2714
- # :category: User Input
2715
- def click_button_no_wait_by_value(browser, strg, desc = '')
2716
- click_no_wait(browser, :button, :value, strg, desc)
2717
- end
2718
- # :category: User Input
2719
- def click_link_by_name_no_wait(browser, strg, desc = '')
2720
- click_no_wait(browser, :link, :name, strg, desc)
2721
- end
2722
-
2723
- alias click_no_wait_name click_link_by_name_no_wait
2724
- alias click_name_no_wait click_link_by_name_no_wait
2725
- # :category: User Input
2726
- def click_link_by_text_no_wait(browser, strg, desc = '')
2727
- click_no_wait(browser, :link, :text, strg, desc)
2728
- end
2729
-
2730
- alias click_no_wait_text click_link_by_text_no_wait
2731
- alias click_text_no_wait click_link_by_text_no_wait
2732
- # :category: User Input
2733
- def click_span_by_text(browser, strg, desc = '')
2734
- if not desc and not strg.match(/Save|Open|Close|Submit|Cancel/)
2735
- desc = 'to navigate to selection'
2736
- end
2737
- msg = "Click span containing text '#{strg}'."
2738
- msg << " #{desc}" if desc.length > 0
2739
- if validate(browser, @myName, __LINE__)
2740
- passed_to_log("#{msg}")
2741
- end
2742
- rescue
2743
- failed_to_log("Unable to #{msg}: '#{$!}'")
2744
- end
2745
-
2746
- # TODO no logging yet. slow.# :category: User Input
2747
- def click_span_with_text(browser, trgt, desc = '')
2748
- msg = "Find and click span containing text '#{trgt}'."
2749
- msg << " #{desc}" if desc.length > 0
2750
- spans = browser.spans
2751
- x = 0
2752
- spans.each do |span|
2753
- x += 1
2754
- debug_to_log("Span #{x}: #{span.text}")
2755
- aText = span.text
2756
- if aText and aText.size > 0
2757
- if aText =~ /#{trgt}/
2758
- break
2759
- end
2760
- end
2761
- end
2762
- spans[x].click
2763
- end
2764
- # :category: User Input
2765
- def click_link_by_title(browser, strg, desc = '')
2766
- click(browser, :link, :title, strg, desc)
2767
- end
2768
-
2769
- alias click_title click_link_by_title
2770
- # :category: User Input
2771
- def click_title_no_wait(browser, strg, desc = '')
2772
- click_no_wait(browser, :link, :title, strg, desc)
2773
- end
2774
- # :category: User Input
2775
- def click_table_row_with_text_by_id(browser, ptrn, strg, column = nil)
2776
- msg = "id=#{ptrn} row with text='#{strg}"
2777
- table = get_table_by_id(browser, /#{ptrn}/)
2778
- if table
2779
- index = get_index_of_row_with_text(table, strg, column)
2780
- if index
2781
- table[index].click
2782
- if validate(browser, @myName, __LINE__)
2783
- passed_to_log("Click #{msg} row index=#{index}.")
2784
- index
2785
- end
2786
- else
2787
- failed_to_log("Table #{msg} not found to click.")
2788
- end
2789
- else
2790
- failed_to_log("Table id=#{ptrn} not found.")
2791
- end
2792
- rescue
2793
- failed_to_log("Unable to click table #{msg}: '#{$!}' (#{__LINE__}) ")
2794
- end
2795
- # :category: User Input
2796
- def click_table_row_with_text_by_index(browser, idx, strg, column = nil)
2797
- msg = "index=#{idx} row with text='#{strg}"
2798
- table = get_table_by_index(browser, idx)
2799
- if table
2800
- index = get_index_of_row_with_text(table, strg, column)
2801
- if index
2802
- table[index].click
2803
- if validate(browser, @myName, __LINE__)
2804
- passed_to_log("Click #{msg} row index=#{index}.")
2805
- index
2806
- end
2807
- else
2808
- failed_to_log("Table #{msg} not found to click.")
2809
- end
2810
- else
2811
- failed_to_log("Table id=#{ptrn} not found.")
2812
- end
2813
- rescue
2814
- failed_to_log("Unable to click table #{msg}: '#{$!}' (#{__LINE__}) ")
2815
- end
2816
-
2817
- def double_click_table_row_with_text_by_id(browser, ptrn, strg, column = nil)
2818
- msg = "id=#{ptrn} row with text='#{strg}"
2819
- table = get_table_by_id(browser, /#{ptrn}/)
2820
- if table
2821
- index = get_index_of_row_with_text(table, strg, column)
2822
- if index
2823
- table[index].fire_event('ondblclick')
2824
- if validate(browser, @myName, __LINE__)
2825
- passed_to_log("Double click #{msg} row index=#{index}.")
2826
- index
2827
- end
2828
- else
2829
- failed_to_log("Table #{msg} not found to double click.")
2830
- end
2831
- else
2832
- failed_to_log("Table id=#{ptrn} not found.")
2833
- end
2834
- rescue
2835
- failed_to_log("Unable to double click table #{msg}: '#{$!}' (#{__LINE__}) ")
2836
- end
2837
-
2838
- def double_click_table_row_with_text_by_index(browser, idx, strg, column = nil)
2839
- msg = "index=#{idx} row with text='#{strg}"
2840
- table = get_table_by_index(browser, idx)
2841
- if table
2842
- index = get_index_of_row_with_text(table, strg, column)
2843
- if index
2844
- row = table[index]
2845
- table[index].fire_event('ondblclick')
2846
- row.fire_event('ondblclick')
2847
- if validate(browser, @myName, __LINE__)
2848
- passed_to_log("Double click #{msg} row index=#{index}.")
2849
- index
2850
- end
2851
- else
2852
- failed_to_log("Table #{msg} not found to double click.")
2853
- end
2854
- else
2855
- failed_to_log("Table id=#{ptrn} not found.")
2856
- end
2857
- rescue
2858
- failed_to_log("Unable to double click table #{msg}: '#{$!}' (#{__LINE__}) ")
2859
- end
2860
-
2861
- def flash_text(browser, strg, count, desc = '')
2862
- msg = "Flash link text='#{strg}' #{count} times."
2863
- msg << " #{desc}" if desc.length > 0
2864
- strgCnt = string_count_in_string(browser.text, strg)
2865
- if strgCnt > 0
2866
- browser.link(:text, strg).flash(count)
2867
- if validate(browser, @myName, __LINE__)
2868
- passed_to_log(msg)
2869
- true
2870
- end
2871
- else
2872
- failed_to_log("#{msg} Link not found.")
2873
- end
2874
- rescue
2875
- failed_to_log("Unable to #{msg} '#{$!}'")
2876
- end
2877
-
2878
- def flash_id(browser, strg, count)
2879
- msg = "Flash link id='#{strg}' #{count} times."
2880
- msg << " #{desc}" if desc.length > 0
2881
- browser.link(:id, strg).flash(count)
2882
- if validate(browser, @myName, __LINE__)
2883
- passed_to_log(msg)
2884
- true
2885
- end
2886
- rescue
2887
- failed_to_log("Unable to #{msg} '#{$!}'")
2888
- end
2889
-
2890
- def flash(element, count = 4)
2891
- element.flash(count)
2892
- debug_to_log("'#{element.inspect}' flashed #{count} times.")
2893
- true
2894
- rescue
2895
- debug_to_log("Flash '#{element.inspect}' failed: '#{$!}' (#{__LINE__})")
2896
- end
2897
-
2898
- def upload_file(data_path)
2899
- limit = 180 # .seconds
2900
- Timeout::timeout(limit) {
2901
- wait = 20
2902
- @ai.WinWait("Choose File to Upload", "", wait)
2903
- sleep 1
2904
- @ai.ControlSend("Choose File to Upload", "", "Edit1", data_path)
2905
- @ai.ControlClick("Choose File to Upload", "", "[CLASS:Button; INSTANCE:2]", "left")
2906
- sleep 4
2907
- #sleep 1
2908
- }
2909
- failed_to_log("Choose File to Upload not found after #{limit} '#{$!}'")
2910
- rescue Timeout::Error
2911
- failed_to_log("File Upload timeout after #{limit} '#{$!}'")
2912
- end
2913
-
2914
- #TODO put rescue inside the do loop
2915
- #parameters: browser and a list of column link text values
2916
- #example: exercise_sorting(browser,['Division', 'Payee', 'Date'], 'Sortable columns on this page')
2917
-
2918
- def exercise_sorting(browser, columnList, desc = '')
2919
- columnList.each do |column|
2920
- click(browser, :link, :text, column, desc)
2921
- end
2922
- end
2923
-
2924
- alias validate_sorting exercise_sorting
2925
-
2926
- def focus_on_textfield_by_id(browser, strg, desc = '')
2927
- msg = "Set focus on textfield name='#{strg}' "
2928
- msg << " #{desc}" if desc.length > 0
2929
- tf = browser.text_field(:id, strg)
2930
- if validate(browser, @myName, __LINE__)
2931
- tf.focus
2932
- if validate(browser, @myName, __LINE__)
2933
- passed_to_log(msg)
2934
- true
2935
- end
2936
- end
2937
- rescue
2938
- failed_to_log("Unable to #{msg} '#{$!}'")
2939
- end
2940
-
2941
- def open_popup_through_link_title(browser, title, pattern, name)
2942
- click_title(browser, title)
2943
- #TODO need some kind of wait for process here
2944
- sleep_for 2
2945
- attach_iepopup(browser, pattern, name)
2946
- rescue
2947
- failed_to_log("Unable to open popup '#{name}': '#{$!}' (#{__LINE__})")
2948
- end
2949
-
2950
- def get_div(browser, how, what, desc = '', dbg = false)
2951
- msg = "Get division #{how}=>#{what}."
2952
- msg << " #{desc}" if desc.length > 0
2953
- Watir::Wait.until { browser.div(how, what).exists? }
2954
- div = browser.div(how, what)
2955
- debug_to_log(div.inspect) if dbg
2956
- if validate(browser, @myName, __LINE__)
2957
- if div
2958
- passed_to_log(msg)
2959
- return div
2960
- else
2961
- failed_to_log(msg)
2962
- end
2963
- end
2964
- rescue
2965
- failed_to_log("Unable to '#{msg}' '#{$!}'")
2966
- end
2967
-
2968
- def get_div_by_id(browser, strg, desc = '', dbg = false)
2969
- get_div(browser, :id, strg, desc, dbg)
2970
- end
2971
-
2972
- def get_div_by_class(browser, strg, desc = '', dbg = false)
2973
- get_div(browser, :class, strg, desc, dbg)
2974
- end
2975
-
2976
- def get_div_by_text(browser, strg, desc = '', dbg = false)
2977
- get_div(browser, :text, strg, desc, dbg)
2978
- end
2979
-
2980
- def get_form(browser, how, strg)
2981
- begin
2982
- # Watir::Wait.until( browser.form(how, strg).exists? ) # fails in wait_until
2983
- rescue => e
2984
- if not rescue_me(e, __method__, "browser.form(#{how}, '#{strg}').exists?", "#{browser.class}")
2985
- raise e
2986
- end
2987
- end
2988
- myForm = browser.form(how, strg)
2989
- if validate(browser, @myName, __LINE__)
2990
- passed_to_log("Form #{how}='#{strg}' found and returned.")
2991
- return myForm
2992
- end
2993
- rescue
2994
- failed_to_log("Unable to return form #{how}='#{strg}': '#{$!}' (#{__LINE__})")
2995
- end
2996
-
2997
- def get_form_by_id(browser, strg)
2998
- get_form(browser, :id, strg)
2999
- end
3000
-
3001
- def get_frame(browser, how, strg, desc = '')
3002
- # begin
3003
- # Watir::Wait.until(browser.frame(how, strg).exists?) # fails in wait_until
3004
- # rescue => e
3005
- # if not rescue_me(e, __method__, "browser.frame(#{how}, '#{strg}').exists?", "#{browser.class}")
3006
- # raise e
3007
- # end
3008
- # end
3009
- begin
3010
- frame = browser.frame(how, strg)
3011
- rescue => e
3012
- if not rescue_me(e, __method__, "browser.frame(#{how}, '#{strg}').exists?", "#{browser.class}")
3013
- raise e
3014
- end
3015
- end
3016
- if validate(browser, @myName, __LINE__)
3017
- passed_to_log("Frame #{how}='#{strg}' found and returned. #{desc}")
3018
- return frame
3019
- end
3020
- rescue
3021
- failed_to_log("Unable to return frame #{how}='#{strg}'. #{desc}: '#{$!}' (#{__LINE__})")
3022
- end
3023
-
3024
- def get_frame_by_id(browser, strg, desc = '')
3025
- get_frame(browser, :id, strg, desc)
3026
- end
3027
-
3028
- def get_frame_by_index(browser, index, desc = '')
3029
- get_frame(browser, :index, index, desc)
3030
- end
3031
-
3032
- def get_frame_by_name(browser, strg, desc = '')
3033
- get_frame(browser, :name, strg, desc)
3034
- end
3035
-
3036
- def get_span(browser, how, strg, desc = '')
3037
- begin
3038
- #TODO: use LegacyExtensions#wait_until
3039
- Watir::Wait.until { browser.span(how, strg).exists? }
3040
- rescue => e
3041
- if not rescue_me(e, __method__, "browser.span(#{how}, '#{strg}').exists?", "#{browser.class}")
3042
- raise e
3043
- end
3044
- end
3045
- begin
3046
- span = browser.span(how, strg)
3047
- rescue => e
3048
- if not rescue_me(e, __method__, "browser.span(#{how}, '#{strg}').exists?", "#{browser.class}")
3049
- raise e
3050
- end
3051
- end
3052
- if validate(browser, @myName, __LINE__)
3053
- passed_to_log("Span #{how}='#{strg}' found and returned. #{desc}")
3054
- return span
3055
- end
3056
- rescue
3057
- failed_to_log("Unable to return span #{how}='#{strg}'. #{desc}: '#{$!}' (#{__LINE__})")
3058
- end
3059
-
3060
- def get_span_by_id(browser, strg, desc = '')
3061
- get_span(browser, :id, strg, desc)
3062
- end
3063
-
3064
- def get_table(browser, how, what, desc = '')
3065
- msg = "Return table :#{how}='#{what}'. #{desc}"
3066
- tbl = browser.table(how, what)
3067
- if validate(browser, @myName, __LINE__)
3068
- passed_to_log(msg)
3069
- tbl
3070
- end
3071
- rescue
3072
- failed_to_log("#{msg}': '#{$!}'")
3073
- end
3074
-
3075
- def get_table_by_id(browser, strg, desc = '')
3076
- get_table(browser, :id, strg, desc)
3077
- end
3078
-
3079
- def get_table_by_index(browser, idx)
3080
- get_table(browser, :index, idx, desc)
3081
- end
3082
-
3083
- def get_table_by_text(browser, strg)
3084
- get_table(browser, :text, strg, desc)
3085
- end
3086
-
3087
- def get_table_headers(table, header_index = 1)
3088
- headers = Hash.new
3089
- headers['index'] = Hash.new
3090
- headers['name'] = Hash.new
3091
- count = 1
3092
- table[header_index].each do |cell|
3093
- if cell.text.length > 0
3094
- name = cell.text.gsub(/\s+/, ' ')
3095
- headers['index'][count] = name
3096
- headers['name'][name] = count
3097
- end
3098
- count += 1
3099
- end
3100
- #debug_to_log("#{__method__}:****** headers:\n#{headers.to_yaml}")
3101
- headers
3102
- rescue
3103
- failed_to_log("Unable to get content headers. '#{$!}'")
3104
- end
3105
-
3106
- def get_textfield_value(browser, how, what, desc = '')
3107
- msg = "Return value in textfield #{how}='#{what}'"
3108
- msg << " #{desc}" if desc.length > 0
3109
- tf = browser.text_field(how, what)
3110
- if validate(browser, @myName, __LINE__)
3111
- if tf
3112
- debug_to_log("#{tf.inspect}")
3113
- vlu = tf.value
3114
- passed_to_log("#{msg} Value='#{vlu}'")
3115
- vlu
3116
- else
3117
- failed_to_log("#{msg}")
3118
- end
3119
- end
3120
- rescue
3121
- failed_to_log("Unable to #{msg}: '#{$!}'")
3122
- end
3123
-
3124
- def get_textfield_value_by_name(browser, strg, desc = '')
3125
- get_textfield_value(browser, :name, strg, desc)
3126
- end
3127
-
3128
- def get_textfield_value_by_id(browser, strg)
3129
- get_textfield_value(browser, :id, strg)
3130
- end
3131
-
3132
- def string_count_in_string(strg, substrg)
3133
- count = strg.scan(substrg).length
3134
- count
3135
- end
3136
-
3137
- # :category: Helpers
3138
- def translate_popup_title(title)
3139
- new_title = title
3140
- case @browserAbbrev
3141
- when 'IE'
3142
- if @browserVersion
3143
- case @browserVersion
3144
- when '8.0'
3145
- case title
3146
- when "Microsoft Internet Explorer"
3147
- new_title = "Message from webpage"
3148
- when "The page at"
3149
- new_title = "Message from webpage"
3150
- end
3151
- when '7.0'
3152
- case title
3153
- when "Message from webpage"
3154
- new_title = "Microsoft Internet Explorer"
3155
- when "The page at"
3156
- new_title = "Windows Internet Explorer"
3157
- end
3158
- when '6.0'
3159
- case title
3160
- when "Message from webpage"
3161
- new_title = "Microsoft Internet Explorer"
3162
- when "The page at"
3163
- new_title = "Microsoft Internet Explorer"
3164
- end
3165
- else
3166
- case title
3167
- when "Microsoft Internet Explorer"
3168
- new_title = "Message from webpage"
3169
- when "The page at"
3170
- new_title = "Message from webpage"
3171
- end
3172
- end
3173
- else
3174
- case title
3175
- when "Microsoft Internet Explorer"
3176
- new_title = "Message from webpage"
3177
- when "The page at"
3178
- new_title = "Message from webpage"
3179
- end
3180
- end
3181
- when 'FF'
3182
- case title
3183
- when 'File Download'
3184
- new_title = 'Opening'
3185
- when "Microsoft Internet Explorer"
3186
- new_title = 'The page at'
3187
- when "Message from webpage"
3188
- new_title = 'The page at'
3189
- end
3190
- when 'C'
3191
- case title
3192
- when 'File Download'
3193
- new_title = 'Save As'
3194
- when "Microsoft Internet Explorer"
3195
- new_title = 'The page at'
3196
- when "Message from webpage"
3197
- new_title = 'The page at'
3198
- end
3199
- end
3200
- new_title
3201
- end
3202
-
3203
- #--
3204
- #def translate_popup_title(title)
3205
- # new_title = title
3206
- # case @browserAbbrev
3207
- # when 'IE'
3208
- #
3209
- #
3210
- # case title
3211
- # when "Microsoft Internet Explorer"
3212
- # new_title = "Message from webpage"
3213
- # when "The page at"
3214
- # new_title = "Message from webpage"
3215
- # end
3216
- #
3217
- #
3218
- #
3219
- #
3220
- # when 'FF'
3221
- # case title
3222
- # when 'File Download'
3223
- # new_title = 'Opening'
3224
- # when "Microsoft Internet Explorer"
3225
- # new_title = 'The page at'
3226
- # when "Message from webpage"
3227
- # new_title = 'The page at'
3228
- # end
3229
- #
3230
- # when 'C'
3231
- # case title
3232
- # when 'File Download'
3233
- # new_title = 'Save As'
3234
- # when "Microsoft Internet Explorer"
3235
- # new_title = 'The page at'
3236
- # when "Message from webpage"
3237
- # new_title = 'The page at'
3238
- # end
3239
- # end
3240
- # new_title
26
+ #def find_me(where, how, what)
27
+ # me = where.element(how, what)
28
+ # puts me.inspect
29
+ #rescue
30
+ # error_to_log("#{where.inspect} doesn't seem to respond to element() #{$!}")
3241
31
  #end
3242
- #++
3243
-
3244
- def get_browser_version(browser)
3245
- debug_to_log("starting get_browser_version")
3246
- case @targetBrowser.abbrev
3247
- when 'IE'
3248
- @browserAbbrev = 'IE'
3249
- @browserName = 'Internet Explorer'
3250
- @browserAppInfo = browser.document.invoke('parentWindow').navigator.appVersion
3251
- @browserAppInfo =~ /MSIE\s(.*?);/
3252
- @browserVersion = $1
3253
- when 'FF'
3254
- #@browserAbbrev = 'FF'
3255
- #@browserName = 'Firefox'
3256
- #js_stuff = <<-end_js_stuff
3257
- #var info = Components.classes["@mozilla.org/xre/app-info;1"]
3258
- #.getService(Components.interfaces.nsIXULAppInfo);
3259
- #[info, info.name, info.version];
3260
- #end_js_stuff
3261
- #js_stuff.gsub!("\n", " ")
3262
- #info = browser.execute_script(js_stuff)
3263
- #info, aName, @browserVersion = info.split(',')
3264
- #debug_to_log("FF info: [#{info}]")
3265
- #debug_to_log("FF name: [#{aName}]")
3266
- #debug_to_log("FF vrsn: [#{@browserVersion}]")
3267
- @browserAbbrev = 'FF'
3268
- @browserName = 'Firefox'
3269
- @browserVersion = '6.01' #TODO: get actual version from browser
3270
- debug_to_log("Firefox, in get_browser_version (#{@browserVersion})")
3271
- when 'S'
3272
- @browserAbbrev = 'S'
3273
- @browserName = 'Safari'
3274
- @browserVersion = '5.0.4' #TODO: get actual version from browser itself
3275
- debug_to_log("Safari, in get_browser_version (#{@browserVersion})")
3276
- when 'C'
3277
- @browserAbbrev = 'C'
3278
- @browserName = 'Chrome'
3279
- @browserVersion = '11.0' #TODO: get actual version from browser
3280
- debug_to_log("Chrome, in get_browser_version (#{@browserVersion})")
3281
- end
3282
- # if [notify_queue, notify_class, notify_id].all?
3283
- # Resque::Job.create(notify_queue, notify_class, :id => notify_id, :browser_used => "#{@browserName} #{@browserVersion}")
3284
- #end
3285
- rescue
3286
- debug_to_log("Unable to determine #{@browserAbbrev} browser version: '#{$!}' (#{__LINE__})")
3287
-
3288
- # TODO: can we get rid of this?
3289
- # js for getting firefox version information
3290
- # function getAppID() {
3291
- # var id;
3292
- # if("@mozilla.org/xre/app-info;1" in Components.classes) {
3293
- # // running under Mozilla 1.8 or later
3294
- # id = Components.classes["@mozilla.org/xre/app-info;1"]
3295
- # .getService(Components.interfaces.nsIXULAppInfo).ID;
3296
- # } else {
3297
- # try {
3298
- # id = Components.classes["@mozilla.org/preferences-service;1"]
3299
- # .getService(Components.interfaces.nsIPrefBranch)
3300
- # .getCharPref("app.id");
3301
- # } catch(e) {
3302
- # // very old version
3303
- # dump(e);
3304
- # }
3305
- # }
3306
- # return id;
3307
- # }
3308
- # alert(getAppID());
3309
- # another snippet that shows getting attributes from object
3310
- # var info = Components.classes["@mozilla.org/xre/app-info;1"]
3311
- # .getService(Components.interfaces.nsIXULAppInfo);
3312
- # // Get the name of the application running us
3313
- # info.name; // Returns "Firefox" for Firefox
3314
- # info.version; // Returns "2.0.0.1" for Firefox version 2.0.0.1
3315
- ensure
3316
- message_to_log("Browser: [#{@browserAbbrev} #{@browserVersion}]")
3317
- end
3318
-
3319
- protected :get_browser_version
3320
-
3321
- def fire_event(browser, element, how, what, event, desc = '')
3322
- msg = "#{element.to_s.titlecase}: #{how}=>'#{what}' event:'#{event}'"
3323
- msg1 = "#{element.to_s.titlecase}(#{how}, '#{what}')"
3324
- begin
3325
- case element
3326
- when :link
3327
- browser.link(how, what).fire_event(event)
3328
- when :button
3329
- browser.button(how, what).fire_event(event)
3330
- when :image
3331
- browser.image(how, what).fire_event(event)
3332
- when :span
3333
- browser.span(how, what).fire_event(event)
3334
- when :div
3335
- browser.div(how, what).fire_event(event)
3336
- else
3337
- browser.element(how, what).fire_event(event)
3338
- end
3339
- rescue => e
3340
- if not rescue_me(e, __method__, "browser(#{msg1}).fire_event('#{event}')", "#{browser.class}")
3341
- raise e
3342
- end
3343
- end
3344
- if validate(browser, @myName, __LINE__)
3345
- passed_to_log("Fire event: #{msg}. #{desc}")
3346
- true
3347
- end
3348
- rescue
3349
- failed_to_log("Unable to fire event: #{msg}. '#{$!}' #{desc}")
3350
- end
3351
-
3352
- def fire_event_on_link_by_text(browser, strg, event = 'onclick', desc = '')
3353
- fire_event(browser, :link, :text, strg, event, desc)
3354
- end
3355
-
3356
- alias fire_event_text fire_event_on_link_by_text
3357
- alias fire_event_by_text fire_event_on_link_by_text
3358
-
3359
- def fire_event_on_link_by_id(browser, strg, event = 'onclick', desc = '')
3360
- fire_event(browser, :link, :id, strg, event, desc)
3361
- end
3362
-
3363
- alias fire_event_id fire_event_on_link_by_id
3364
- alias fire_event_by_id fire_event_on_link_by_id
3365
-
3366
- def fire_event_on_image_by_src(browser, strg, event = 'onclick', desc = '')
3367
- fire_event(browser, :img, :src, strg, event, desc)
3368
- end
3369
-
3370
- alias fire_event_src fire_event_on_image_by_src
3371
- alias fire_event_image_by_src fire_event_on_image_by_src
3372
-
3373
- # :category: Waits
3374
- def wait_until_exists(browser, element, how, what, desc = '')
3375
- msg = "Wait until (#{element} :#{how}=>#{what}) exists."
3376
- msg << " #{desc}" if desc.length > 0
3377
- start = Time.now.to_f
3378
- # TODO: try Watir::Wait.until { browser.element(how, what).exists? } instead of this (cumbersome) case statement
3379
- # TODO: above fails on frame
3380
- begin
3381
- case element
3382
- when :link
3383
- Watir::Wait.until { browser.link(how, what).exists? }
3384
- when :button
3385
- Watir::Wait.until { browser.button(how, what).exists? }
3386
- when :radio
3387
- Watir::Wait.until { browser.radio(how, what).exists? }
3388
- when :checkbox
3389
- Watir::Wait.until { browser.checkbox(how, what).exists? }
3390
- when :div
3391
- Watir::Wait.until { browser.div(how, what).exists? }
3392
- when :select_list
3393
- Watir::Wait.until { browser.select_list(how, what).exists? }
3394
- when :text_field
3395
- Watir::Wait.until { browser.text_field(how, what).exists? }
3396
- when :frame
3397
- Watir::Wait.until { browser.frame(how, what).exists? }
3398
- when :form
3399
- Watir::Wait.until { browser.form(how, what).exists? }
3400
- when :cell
3401
- Watir::Wait.until { browser.cell(how, what).exists? }
3402
- when :image
3403
- Watir::Wait.until { browser.image(how, what).exists? }
3404
- else
3405
- Watir::Wait.until { browser.element(how, what).exists? }
3406
- end
3407
- rescue => e
3408
- if e.class.to_s =~ /TimeOutException/
3409
- failed_to_log("#{msg}: '#{$!}'")
3410
- return false
3411
- elsif not rescue_me(e, __method__, "#{block.to_s}", "#{browser.class}")
3412
- raise e
3413
- end
3414
- end
3415
- stop = Time.now.to_f
3416
- #debug_to_log("#{__method__}: start:#{start} stop:#{stop}")
3417
- # sleep 1
3418
- if validate(browser, @myName, __LINE__)
3419
- passed_to_log("#{msg} (#{stop - start} seconds)")
3420
- true
3421
- end
3422
- rescue
3423
- failed_to_log("Unable to complete #{msg}: '#{$!}'")
3424
- end
3425
-
3426
- # :category: Waits
3427
- def wait_while(browser, desc, timeout = 45, &block)
3428
- #TODO: Would like to be able to see the block code in the log message instead of the identification
3429
- msg = "Wait while #{desc}:"
3430
- start = Time.now.to_f
3431
- begin
3432
- #Watir::Wait.until(timeout) { block.call(nil) }
3433
- if block.call(nil)
3434
- Watir::Wait.while(timeout) { block.call(nil) }
3435
- end
3436
- rescue => e
3437
- if e.class.to_s =~ /TimeOutException/ or e.message =~ /timed out/
3438
- failed_to_log("#{msg}: '#{$!}' ")
3439
- return false
3440
- elsif not rescue_me(e, __method__, "#{block.to_s}", "#{browser.class}")
3441
- raise e
3442
- end
3443
- end
3444
- stop = Time.now.to_f
3445
- #debug_to_log("#{__method__}: start:#{start} stop:#{stop} block: #{block.to_s}")
3446
- # sleep 1
3447
- if validate(browser, @myName, __LINE__)
3448
- passed_to_log("#{msg} (#{"%.5f" % (stop - start)} seconds)") # {#{block.to_s}}")
3449
- true
3450
- end
3451
- rescue
3452
- failed_to_log("Unable to complete #{msg}. '#{$!}'")
3453
- end
3454
-
3455
- alias wait_while_true wait_while
3456
-
3457
- # :category: Waits
3458
- def wait_until(browser, desc, timeout = 45, skip_pass = false, &block)
3459
- #TODO: Would like to be able to see the block code in the log message instead of the identification
3460
- msg = "Wait until #{desc}"
3461
- start = Time.now.to_f
3462
- begin
3463
- Watir::Wait.until(timeout) { block.call(nil) }
3464
- rescue => e
3465
- if e.class.to_s =~ /TimeOutException/ or e.message =~ /timed out/
3466
- failed_to_log("#{msg} '#{$!}'")
3467
- return false
3468
- elsif not rescue_me(e, __method__, "#{block.to_s}", "#{browser.class}")
3469
- raise e
3470
- end
3471
- end
3472
- stop = Time.now.to_f
3473
- #debug_to_log("#{__method__}: start:#{start} stop:#{stop} block: #{block.to_s}")
3474
- # sleep 1
3475
- if validate(browser, @myName, __LINE__)
3476
- passed_to_log("#{msg} (#{"%.5f" % (stop - start)} seconds)") unless skip_pass # {#{block.to_s}}")
3477
- true
3478
- end
3479
- rescue
3480
- failed_to_log("Unable to complete #{msg} '#{$!}'")
3481
- end
3482
-
3483
- alias wait_until_true wait_until
3484
-
3485
- # :category: Waits
3486
- def wait_until_by_radio_value(browser, strg, desc = '')
3487
- wait_until_exists(browser, :radio, :value, strg, desc)
3488
- end
3489
-
3490
- # :category: Waits
3491
- def wait_until_ready(browser, how, what, desc = '', timeout = 90, verbose = false)
3492
- msg = "#{__method__.to_s.titleize}: element: #{how}='#{what}'"
3493
- msg << " #{desc}" if desc.length > 0
3494
- proc_exists = Proc.new { browser.element(how, what).exists? }
3495
- proc_enabled = Proc.new { browser.element(how, what).enabled? }
3496
- case how
3497
- when :href
3498
- proc_exists = Proc.new { browser.link(how, what).exists? }
3499
- proc_enabled = Proc.new { browser.link(how, what).enabled? }
3500
- end
3501
- if verbose
3502
- if wait_until(browser, "#{msg} Element exists.", timeout) { proc_exists.call(nil) }
3503
- if wait_until(browser, "#{msg} Element enabled.", timeout) { proc_enabled.call(nil) }
3504
- passed_to_log(msg)
3505
- true
3506
- else
3507
- failed_to_log(msg)
3508
- end
3509
- else
3510
- failed_to_log(msg)
3511
- end
3512
- else
3513
- start = Time.now.to_f
3514
- if Watir::Wait.until(timeout) { proc_exists.call(nil) }
3515
- if Watir::Wait.until(timeout) { proc_enabled.call(nil) }
3516
- stop = Time.now.to_f
3517
- #debug_to_log("#{__method__}: start:#{"%.5f" % start} stop:#{"%.5f" % stop}")
3518
- passed_to_log("#{msg} (#{"%.5f" % (stop - start)} seconds)")
3519
- true
3520
- else
3521
- failed_to_log(msg)
3522
- end
3523
- else
3524
- failed_to_log(msg)
3525
- end
3526
- end
3527
- rescue
3528
- failed_to_log("Unable to #{msg}. '#{$!}'")
3529
- end
3530
-
3531
- # :category: Waits
3532
- def wait_until_ready_quiet(browser, how, what, desc = '', timeout = 45, quiet = true)
3533
- msg = "#{__method__.to_s.titleize}: element: #{how}='#{what}'"
3534
- msg << " #{desc}" if desc.length > 0
3535
- proc_exists = Proc.new { browser.element(how, what).exists? }
3536
- proc_enabled = Proc.new { browser.element(how, what).enabled? }
3537
- case how
3538
- when :href
3539
- proc_exists = Proc.new { browser.link(how, what).exists? }
3540
- proc_enabled = Proc.new { browser.link(how, what).enabled? }
3541
- end
3542
- start = Time.now.to_f
3543
- if Watir::Wait.until(timeout) { proc_exists.call(nil) }
3544
- if Watir::Wait.until(timeout) { proc_enabled.call(nil) }
3545
- stop = Time.now.to_f
3546
- #debug_to_log("#{msg}: start:#{"%.5f" % start} stop:#{"%.5f" % stop}")
3547
- passed_to_log("#{msg} (#{"%.5f" % (stop - start)} seconds)") unless quiet
3548
- true
3549
- else
3550
- failed_to_log(msg)
3551
- end
3552
- else
3553
- failed_to_log(msg)
3554
- end
3555
- rescue
3556
- failed_to_log("Unable to #{msg}. '#{$!}'")
3557
- end
3558
-
3559
- # :category: Waits
3560
- def wait_until_text(browser, strg, desc = '', timeout = 60)
3561
- if not strg.class.to_s.match('String')
3562
- raise "#{__method__} requires String for search target. #{strg.class} is not supported."
3563
- end
3564
- wait_until(browser, "'#{strg}' #{desc}", timeout) { browser.text.include? strg }
3565
- end
3566
-
3567
- alias wait_until_by_text wait_until_text
3568
-
3569
- # :category: Waits
3570
- def wait_until_by_link_text(browser, strg, desc = '')
3571
- wait_until_exists(browser, :link, :text, strg, desc)
3572
- end
3573
-
3574
- # :category: Waits
3575
- def wait_until_enabled(browser, what, how, value, desc = '')
3576
- # TODO: This can be simplified
3577
- start = Time.now.to_f
3578
- begin
3579
- case what
3580
- when :link
3581
- Watir::Wait.until { browser.link(how, value).enabled? }
3582
- when :button
3583
- Watir::Wait.until { browser.button(how, value).enabled? }
3584
- when :radio
3585
- Watir::Wait.until { browser.radio(how, value).enabled? }
3586
- when :checkbox
3587
- Watir::Wait.until { browser.checkbox(how, value).enabled? }
3588
- when :div
3589
- Watir::Wait.until { browser.div(how, value).enabled? }
3590
- when :select_list
3591
- Watir::Wait.until { browser.select_list(how, value).enabled? }
3592
- when :text_field
3593
- Watir::Wait.until { browser.text_field(how, value).enabled? }
3594
- when :table
3595
- Watir::Wait.until { browser.table(how, value).enabled? }
3596
- else
3597
- raise "#{__method__}: Element #{what} not supported."
3598
- end
3599
- rescue => e
3600
- if e.class.to_s =~ /TimeOutException/
3601
- failed_to_log("Wait until (#{what} :#{how}=>#{value}) enabled. #{desc}: '#{$!}' #{desc}")
3602
- return false
3603
- elsif not rescue_me(e, __method__, "#{block.to_s}", "#{browser.class}")
3604
- raise e
3605
- end
3606
- end
3607
- stop = Time.now.to_f
3608
- #debug_to_log("#{__method__}: start:#{start} stop:#{stop}")
3609
- # sleep 1
3610
- if validate(browser, @myName, __LINE__)
3611
- passed_to_log("Wait until (#{what} :#{how}=>#{value}) enabled. #{desc} (#{stop - start} seconds)")
3612
- true
3613
- end
3614
- rescue
3615
- failed_to_log("Unable to complete wait until (#{what} :#{how}=>#{value}) enabled. #{desc}: '#{$!}'")
3616
- end
3617
-
3618
- # :category: Waits
3619
- def wait_until_visible(browser, element, how, what, desc = '')
3620
- start = Time.now.to_f
3621
- Watir::Wait.until(20) { browser.element(how, what).exists? }
3622
- begin
3623
- case element
3624
- when :link
3625
- Watir::Wait.until { browser.link(how, what).visible? }
3626
- when :button
3627
- Watir::Wait.until { browser.button(how, what).visible? }
3628
- when :radio
3629
- Watir::Wait.until { browser.radio(how, what).visible? }
3630
- when :checkbox
3631
- Watir::Wait.until { browser.checkbox(how, what).visible? }
3632
- when :div
3633
- Watir::Wait.until { browser.div(how, what).visible? }
3634
- when :select_list
3635
- Watir::Wait.until { browser.select_list(how, what).visible? }
3636
- when :text_field
3637
- Watir::Wait.until { browser.text_field(how, what).visible? }
3638
- else
3639
- Watir::Wait.until { browser.element(how, what).visible? }
3640
- # raise "#{__method__}: Element #{what} not supported."
3641
- end
3642
- rescue => e
3643
- if e.class.to_s =~ /TimeOutException/
3644
- failed_to_log("Wait until (#{what} :#{how}=>#{what}) visible. #{desc}: '#{$!}' #{desc}")
3645
- return false
3646
- elsif not rescue_me(e, __method__, '', "#{browser.class}")
3647
- raise e
3648
- end
3649
- end
3650
- stop = Time.now.to_f
3651
- #debug_to_log("#{__method__}: start:#{start} stop:#{stop}")
3652
- # sleep 1
3653
- if validate(browser, @myName, __LINE__)
3654
- passed_to_log("Wait until (#{element} :#{how}=>#{what}) visible. #{desc} (#{stop - start} seconds)")
3655
- true
3656
- end
3657
- rescue
3658
- failed_to_log("Unable to complete wait until (#{element} :#{how}=>#{what}) visible. #{desc}: '#{$!}'")
3659
- end
3660
-
3661
- # :category: Bullet-Proofing
3662
- def rescue_me(e, me = nil, what = nil, where = nil, who = nil)
3663
- #TODO: these are rescues from exceptions raised in Watir/Firewatir
3664
- debug_to_log("#{__method__}: Enter")
3665
- ok = false
3666
- begin
3667
- gaak = who.inspect
3668
- located = gaak =~ /located=true/i
3669
- rescue
3670
- debug_to_log("#{__method__}: gaak: '#{gaak}'")
3671
- end
3672
- msg = e.message
3673
- debug_to_log("#{__method__}: msg = #{msg}")
3674
- if msg =~ /undefined method\s+.join.\s+for/i # firewatir to_s implementation error
3675
- ok = true
3676
- elsif msg =~ /undefined method\s+.match.\s+for.+WIN32OLERuntimeError/i # watir and firewatir
3677
- ok = true
3678
- elsif msg =~ /undefined method\s+.match.\s+for.+UnknownObjectException/i # watir
3679
- ok = true
3680
- elsif msg =~ /window\.getBrowser is not a function/i # firewatir
3681
- ok = true
3682
- elsif msg =~ /WIN32OLERuntimeError/i # watir
3683
- ok = true
3684
- elsif msg =~ /undefined method\s+.match.\s+for/i # watir
3685
- ok = true
3686
- elsif msg =~ /wrong number of arguments \(1 for 0\)/i
3687
- ok = true
3688
- elsif (msg =~ /unable to locate element/i)
3689
- if located
3690
- ok = true
3691
- elsif where == 'Watir::Div'
3692
- ok = true
3693
- end
3694
- elsif (msg =~ /HRESULT error code:0x80070005/)
3695
- ok = true
3696
- #elsif msg =~ /missing\s+\;\s+before statement/
3697
- # ok = true
3698
- end
3699
- if ok
3700
- debug_to_log("#{__method__}: RESCUED: \n#{who.to_yaml}=> #{what} in #{me}()\n=> '#{$!}'")
3701
- debug_to_log("#{__method__}: #{who.inspect}") if who
3702
- debug_to_log("#{__method__}: #{where.inspect}")
3703
- debug_to_log("#{__method__}: #{get_callers(6, true)}")
3704
- else
3705
- debug_to_log("#{__method__}: NO RESCUE: #{e.message}")
3706
- debug_to_log("#{__method__}: NO RESCUE: \n#{get_callers(6, true)}")
3707
- end
3708
- debug_to_log("#{__method__}: Exit")
3709
- ok
3710
- end
3711
-
3712
- # :category: Browser
3713
- def close_popup_by_button_title(popup, strg, desc = '')
3714
- click(popup, :link, :title, strg, desc)
3715
- end
3716
-
3717
- # :category: GUI
3718
- def move_element_with_handle(browser, element, handle_id, dx, dy)
3719
- # msg = "Move element "
3720
- # w1, h1, x1, y1, xc1, yc1, xlr1, ylr1 = get_element_coordinates(browser, element, true)
3721
- # newx = w1 + dx
3722
- # newy = h1 + dy
3723
- # msg << " by [#{dx}, #{dy}] to expected [[#{newx}, #{newy}] "
3724
- # handle = get_resize_handle(element, handle_id)
3725
- # hw, hh, hx, hy, hxc, hyc, hxlr, hylr = get_element_coordinates(browser, handle, true)
3726
-
3727
- # drag_and_drop(hxc, hyc, dx, dy)
3728
-
3729
- # w2, h2, x2, y2, xc2, yc2, xlr2, ylr2 = get_element_coordinates(browser, element, true)
3730
-
3731
- # xerr = x2 - newx
3732
- # yerr = y2 - newy
3733
- # xdsp = (x1 - x2).abs
3734
- # ydsp = (y1 - y2).abs
3735
-
3736
- # if x2 == newx and y2 == newy
3737
- # msg << "succeeded."
3738
- # passed_to_log(msg)
3739
- # else
3740
- # msg << "failed. "
3741
- # failed_to_log(msg)
3742
- # debug_to_log("x: actual #{x2}, error #{xerr}, displace #{xdsp}. y: actual #{y2}, error #{yerr}, displace #{ydsp}.")
3743
- # end
3744
-
3745
- end
3746
-
3747
- # :category: GUI
3748
- def resize_element_with_handle(browser, element, target, dx, dy=nil)
3749
- #TODO enhance to accept differing percentages in each direction
3750
- msg = "Resize element "
3751
- w1, h1, x1, y1, xc1, yc1, xlr1, ylr1 = get_element_coordinates(browser, element, true)
3752
- if dy
3753
- deltax = dx
3754
- deltay = dy
3755
- neww = w1 + dx
3756
- newh = h1 + dy
3757
- msg << " by [#{dx}, #{dy}] " #"" to expected dimension [#{neww}, #{newh}] "
3758
- else
3759
- deltax, deltay, neww, newh = adjust_dimensions_by_percent(w1, h1, dx, true)
3760
- msg << "by #{dx} percent " #"" to expected dimension [#{neww}, #{newh}] "
3761
- end
3762
- handle = get_resize_handle_by_class(element, target) #, true)
3763
- sleep_for(0.5)
3764
- hw, hh, hx, hy, hxc, hyc, hxlr, hylr = get_element_coordinates(browser, handle, true)
3765
- hxlr_diff = 0
3766
- hylr_diff = 0
3767
-
3768
- # TODO These adjustments are adhoc and empirical. Need to be derived more rigorously
3769
- if @browserAbbrev == 'IE'
3770
- hxlr_diff = (xlr1 - hxlr)
3771
- hylr_diff = (ylr1 - hylr)
3772
- x_start = hxlr - 2
3773
- y_start = hylr - 2
3774
- else
3775
- hxlr_diff = (xlr1 - hxlr) / 2 unless (xlr1 - hxlr) == 0
3776
- hylr_diff = (ylr1 - hylr) / 2 unless (ylr1 - hylr) == 0
3777
- x_start = hxlr
3778
- y_start = hylr
3779
- end
3780
-
3781
- newxlr = xlr1 + deltax
3782
- newylr = ylr1 + deltay
3783
- # msg << ", lower right [#{newxlr}, #{newylr}] - "
3784
- sleep_for(0.5)
3785
-
3786
- drag_and_drop(x_start, y_start, deltax, deltay)
3787
-
3788
- sleep_for(1.5)
3789
- w2, h2, x2, y2, xc2, yc2, xlr2, ylr2 = get_element_coordinates(browser, element, true)
3790
-
3791
- werr = w2 - neww
3792
- herr = h2 - newh
3793
-
3794
- # TODO This adjustment is adhoc and empirical. Needs to be derived more rigorously
3795
- xlrerr = xlr2 - newxlr + hxlr_diff
3796
- ylrerr = ylr2 - newylr + hylr_diff
3797
-
3798
- xlrdsp = (xlr1 - xlr2).abs
3799
- ylrdsp = (ylr1 - ylr2).abs
3800
-
3801
- debug_to_log("\n" +
3802
- "\t\t hxlr_diff: #{hxlr_diff}\n" +
3803
- "\t\t hylr_diff: #{hylr_diff}\n" +
3804
- "\t\t werr: #{werr}\n" +
3805
- "\t\t herr: #{herr}\n" +
3806
- "\t\t xlrerr: #{xlrerr}\n" +
3807
- "\t\t ylrerr: #{ylrerr}\n" +
3808
- "\t\t xlrdsp: #{xlrdsp}\n" +
3809
- "\t\t ylrdsp: #{ylrdsp}\n" +
3810
- "\t\t @min_width: #{@min_width}\n" +
3811
- "\t\t@min_height: #{@min_height}\n" +
3812
- "\t\t x tol: #{@x_tolerance}\n" +
3813
- "\t\t y tol: #{@y_tolerance}\n"
3814
- )
3815
-
3816
- #TODO Add check that window _was_ resized.
3817
- x_ok, x_msg = validate_move(w2, xlrerr, @x_tolerance, @min_width, xlr2)
3818
- y_ok, y_msg = validate_move(h2, ylrerr, @y_tolerance, @min_height, ylr2)
3819
- msg = msg + "x: #{x_msg}, y: #{y_msg}"
3820
-
3821
- if x_ok and y_ok
3822
- passed_to_log(msg)
3823
- else
3824
- failed_to_log(msg)
3825
- debug_to_log("x - actual #{xlr2}, error #{xlrerr}, displace #{xlrdsp}, y - actual #{ylr2}, error #{ylrerr}, displace #{ylrdsp}.")
3826
- end
3827
- sleep_for(1)
3828
- rescue
3829
- failed_to_log("Unable to validate resize. #{$!} (#{__LINE__})")
3830
- sleep_for(1)
3831
- end
3832
-
3833
- # :category: GUI
3834
- def get_resize_handle_by_id(element, id, dbg=nil)
3835
- handle = get_div_by_id(element, id, dbg)
3836
- sleep_for(1)
3837
- handle.flash(5)
3838
- return handle
3839
- end
3840
-
3841
- # :category: GUI
3842
- def get_resize_handle_by_class(element, strg, dbg=nil)
3843
- handle = get_div_by_class(element, strg, dbg)
3844
- sleep_for(0.5)
3845
- handle.flash(5)
3846
- return handle
3847
- end
3848
-
3849
- # :category: GUI
3850
- def get_element_coordinates(browser, element, dbg=nil)
3851
- bx, by, bw, bh = get_browser_coord(browser, dbg)
3852
- if @browserAbbrev == 'IE'
3853
- x_hack = @horizontal_hack_ie
3854
- y_hack = @vertical_hack_ie
3855
- elsif @browserAbbrev == 'FF'
3856
- x_hack = @horizontal_hack_ff
3857
- y_hack = @vertical_hack_ff
3858
- end
3859
- sleep_for(1)
3860
- w, h = element.dimensions.to_a
3861
- xc, yc = element.client_offset.to_a
3862
- # xcc, ycc = element.client_center.to_a
3863
- xcc = xc + w/2
3864
- ycc = yc + h/2
3865
- # screen offset:
3866
- xs = bx + x_hack + xc - 1
3867
- ys = by + y_hack + yc - 1
3868
- # screen center:
3869
- xsc = xs + w/2
3870
- ysc = ys + h/2
3871
- xslr = xs + w
3872
- yslr = ys + h
3873
- if dbg
3874
- debug_to_log(
3875
- "\n\t\tElement: #{element.inspect}"+
3876
- "\n\t\tbrowser screen offset: x: #{bx} y: #{by}"+
3877
- "\n\t\t dimensions: x: #{w} y: #{h}"+
3878
- "\n\t\t client offset x: #{xc} y: #{yc}"+
3879
- "\n\t\t screen offset x: #{xs} y: #{ys}"+
3880
- "\n\t\t client center x: #{xcc} y: #{ycc}"+
3881
- "\n\t\t screen center x: #{xsc} y: #{ysc}"+
3882
- "\n\t\t screen lower right x: #{xslr} y: #{yslr}")
3883
- end
3884
- [w, h, xs, ys, xsc, ysc, xslr, yslr]
3885
- end
3886
-
3887
- def get_element(browser, element, how, what, value = nil)
3888
- target = nil
3889
- what = Regexp.new(Regexp.escape(what)) unless how == :index or what.is_a?(Regexp)
3890
- case element
3891
- when :link
3892
- target = browser.link(how, what)
3893
- when :button
3894
- target = browser.button(how, what)
3895
- when :div
3896
- target = browser.div(how, what)
3897
- when :checkbox
3898
- target = browser.checkbox(how, what, value)
3899
- when :text_field, :textfield
3900
- target = browser.text_field(how, what)
3901
- when :image
3902
- target = browser.image(how, what)
3903
- when :file_field, :filefield
3904
- target = browser.file_field(how, what)
3905
- when :form
3906
- target = browser.form(how, what)
3907
- when :frame
3908
- target = browser.frame(how, what)
3909
- when :radio
3910
- target = browser.radio(how, what, value)
3911
- when :span
3912
- target = browser.span(how, what)
3913
- when :table
3914
- target = browser.table(how, what)
3915
- when :li
3916
- target = browser.li(how, what)
3917
- when :select_list, :selectlist
3918
- target = browser.select_list(how, what)
3919
- when :hidden
3920
- target = browser.hidden(how, what)
3921
- when :area
3922
- target = browser.area(how, what)
3923
- end
3924
- if target.exists?
3925
- target
3926
- else
3927
- nil
3928
- end
3929
- rescue => e
3930
- if not rescue_me(e, __method__, "browser.#{element}(#{how}, '#{what}')", "#{browser.class}", target)
3931
- raise e
3932
- end
3933
- end
3934
-
3935
- def get_element_text(browser, element, how, what, desc = '')
3936
- msg = "Return text in #{element} #{how}='#{what}'"
3937
- msg << " #{desc}" if desc.length > 0
3938
- text = browser.element(how, what).text
3939
- if validate(browser, @myName, __LINE__)
3940
- passed_to_log("#{msg} text='#{text}'")
3941
- text
3942
- end
3943
- rescue
3944
- failed_to_log("Unable to #{msg}: '#{$!}'")
3945
- end
3946
-
3947
- # :category: GUI
3948
- def adjust_dimensions_by_percent(w, h, p, returnnew=nil)
3949
- p += 100
3950
- nw = (w * (p/100.0)).to_i
3951
- nh = (h * (p/100.0)).to_i
3952
- deltaw = nw - w
3953
- deltah = nh - h
3954
- if returnnew
3955
- [deltaw, deltah, nw, nh]
3956
- else
3957
- [deltaw, deltah]
3958
- end
3959
- end
3960
-
3961
- # :category: GUI
3962
- def get_browser_coord(browser=nil, dbg=nil)
3963
- browser = @myBrowser if not browser
3964
- title = browser.title
3965
- x = @ai.WinGetPosX(title)
3966
- y = @ai.WinGetPosY(title)
3967
- w = @ai.WinGetPosWidth(title)
3968
- h = @ai.WinGetPosHeight(title)
3969
- if dbg
3970
- debug_to_log("\n\t\tBrowser #{browser.inspect}\n"+
3971
- "\t\tdimensions: x: #{w} y: #{h}"+
3972
- "\t\tscreen offset x: #{x} y: #{y}")
3973
- end
3974
- [x, y, w, h]
3975
- end
3976
-
3977
- # :category: Table Handling
3978
- def get_index_for_column_head(panel, table_index, strg)
3979
- rgx = Regexp.new(strg)
3980
- panel.tables[table_index].each do |row|
3981
- if row.text =~ rgx
3982
- index = 1
3983
- row.each do |cell|
3984
- if cell.text =~ rgx
3985
- return index
3986
- end
3987
- index += 1
3988
- end
3989
- end
3990
- end
3991
- end
3992
-
3993
- # :category: Table Handling
3994
- def get_index_of_last_row(table, pad = 2, every = 1)
3995
- index = calc_index(table.row_count, every)
3996
- index = index.to_s.rjust(pad, '0')
3997
- #debug_to_log("#{__method__}: index='#{index}' row_count=#{table.row_count} pad=#{pad} every=#{every}")
3998
- index
3999
- end
4000
-
4001
- alias get_index_for_last_row get_index_of_last_row
4002
-
4003
- # :category: Table Handling
4004
- def get_index_of_last_row_with_text(table, strg, column_index = nil)
4005
- debug_to_log("#{__method__}: #{get_callers(5)}")
4006
- msg = "Find last row in table :id=#{table.id} with text '#{strg}'"
4007
- msg << " in column #{column_index}" if column_index
4008
- dbg = "#{__method__}: #{table.id} text by row "
4009
- dbg << "in column #{column_index}" if column_index
4010
- index = 0
4011
- found = false
4012
- at_index = 0
4013
- #row_count = table.row_count
4014
- table.rows.each do |row|
4015
- cell_count = get_cell_count(row)
4016
- index += 1
4017
- text = ''
4018
- if column_index
4019
- col_idx = column_index.to_i
4020
- if cell_count >= col_idx
4021
- text = row[col_idx].text
4022
- end
4023
- else
4024
- text = row.text
4025
- end
4026
- dbg << "\n#{index}. [#{text}]"
4027
- if text =~ /#{strg}/
4028
- found = true
4029
- at_index = index
4030
- end
4031
- end
4032
- debug_to_log(dbg)
4033
- if found
4034
- passed_to_log("#{msg} at index #{index}.")
4035
- at_index
4036
- else
4037
- failed_to_log("#{msg}")
4038
- nil
4039
- end
4040
- rescue
4041
- failed_to_log("Unable to #{msg}. '#{$!}'")
4042
- end
4043
-
4044
- alias get_index_for_last_row_with_text get_index_of_last_row_with_text
4045
-
4046
- # :category: Table Handling
4047
- def get_index_of_row_with_text(table, strg, column_index = nil, fail_if_found = false)
4048
- debug_to_log("#{__method__}: #{get_callers(5)}")
4049
- if fail_if_found
4050
- msg = 'No '
4051
- else
4052
- msg = 'Find '
4053
- end
4054
- msg << "row in table :id=#{table.id} with text '#{strg}'"
4055
- msg << " in column #{column_index}" if column_index
4056
- dbg = "#{__method__}: #{table.id} text by row "
4057
- dbg << "in column #{column_index}" if column_index
4058
- index = 0
4059
- found = false
4060
- table.rows.each do |row|
4061
- cell_count = row.cells.length
4062
- index += 1
4063
- text = ''
4064
- if column_index
4065
- col_idx = column_index.to_i
4066
- if cell_count >= col_idx
4067
- text = row[col_idx].text
4068
- end
4069
- else
4070
- text = row.text
4071
- end
4072
- dbg << "\n#{index}. [#{text}]"
4073
- if text =~ /#{strg}/
4074
- found = true
4075
- break
4076
- end
4077
- end
4078
- debug_to_log(dbg)
4079
- if found
4080
- if fail_if_found
4081
- failed_to_log("#{msg} at index #{index}.")
4082
- else
4083
- passed_to_log("#{msg} at index #{index}.")
4084
- end
4085
- index
4086
- else
4087
- if fail_if_found
4088
- passed_to_log("#{msg}")
4089
- else
4090
- failed_to_log("#{msg}")
4091
- end
4092
- nil
4093
- end
4094
- rescue
4095
- failed_to_log("Unable to #{msg}. '#{$!}'")
4096
- end
4097
-
4098
- # :category: Table Handling
4099
- def get_index_of_row_with_textfield_value(table, strg, how, what, column_index = nil)
4100
- msg = "Find row in table :id=#{table.id} with value '#{strg}' in text_field #{how}=>'#{what} "
4101
- msg << " in column #{column_index}" if column_index
4102
- index = 0
4103
- found = false
4104
- table.rows.each do |row|
4105
- cell_count = get_cell_count(row)
4106
- index += 1
4107
- text = ''
4108
- if column_index
4109
- col_idx = column_index.to_i
4110
- if cell_count >= col_idx
4111
- if row[col_idx].text_field(how, what).exists?
4112
- value = row[col_idx].text_field(how, what).value
4113
- end
4114
- end
4115
- else
4116
- if row.text_field(how, what).exists?
4117
- value = row.text_field(how, what).value
4118
- sleep(0.25)
4119
- end
4120
- end
4121
- if value and value =~ /#{strg}/
4122
- found = true
4123
- break
4124
- end
4125
- end
4126
- if found
4127
- passed_to_log("#{msg} at index #{index}.")
4128
- else
4129
- failed_to_log("#{msg}")
4130
- end
4131
- index
4132
- rescue
4133
- failed_to_log("Unable to #{msg}. '#{$!}'")
4134
- end
4135
-
4136
- # :category: Table Handling
4137
- def get_index_for_table_containing_text(browser, strg, ordinal = 1)
4138
- msg = "Get index for table containing text '#{strg}'"
4139
- index = 0
4140
- found = 0
4141
- browser.tables.each do |t|
4142
- index += 1
4143
- if t.text =~ /#{strg}/
4144
- found += 1
4145
- if ordinal > 0 and found == ordinal
4146
- break
4147
- end
4148
- end
4149
- end
4150
- if found
4151
- passed_to_log("#{msg}: #{index}")
4152
- index
4153
- else
4154
- passed_to_log("#{msg}.")
4155
- nil
4156
- end
4157
- rescue
4158
- failed_to_log("Unable to find index of table containing text '#{strg}' '#{$!}' ")
4159
- end
4160
-
4161
- def get_table_containing_text(browser, strg, ordinal = 1)
4162
- msg = "Get table #{ordinal} containing text '#{strg}'"
4163
- index = get_index_for_table_containing_text(browser, strg, ordinal)
4164
- if index
4165
- passed_to_log(msg)
4166
- browser.tables[index]
4167
- else
4168
- failed_to_log(msg)
4169
- nil
4170
- end
4171
- rescue
4172
- failed_to_log("Unable to find index of table containing text '#{strg}' '#{$!}' ")
4173
- end
4174
-
4175
- # :category: Table Handling
4176
- def get_cell_text_from_row_with_string(nc_element, table_index, column_index, strg)
4177
- rgx = Regexp.new(strg)
4178
- text = ''
4179
- debug_to_log("strg:'#{strg}', rgx:'#{rgx}', table_index:'#{table_index}', column_index:'#{column_index}'")
4180
- nc_element.tables[table_index].each do |row|
4181
- cell_count = get_cell_count(row)
4182
- if cell_count >= column_index
4183
- #TODO this assumes column 1 is a number column
4184
- # debug_to_log("row:'#{row.cells}'")
4185
- cell_1 = row[1].text
4186
- if cell_1 =~ /\d+/
4187
- row_text = row.text
4188
- if row_text =~ rgx
4189
- text = row[column_index].text
4190
- break
4191
- end
4192
- end
4193
- end
4194
- end
4195
- text
4196
- end
4197
-
4198
- # :category: Table Handling
4199
- def count_rows_with_string(container, table_index, strg)
4200
- hit = 0
4201
- container.tables[table_index].each do |row|
4202
- if get_cell_count(row) >= 1
4203
- # debug_to_log("#{__method__}: #{row.text}")
4204
- #TODO this assumes column 1 is a number column
4205
- if row[1].text =~ /\d+/
4206
- if row.text =~ /#{strg}/i
4207
- hit += 1
4208
- debug_to_log("#{__method__}: #{row.text}")
4209
- end
4210
- end
4211
- end
4212
- end
4213
- debug_to_log("#{__method__}: hit row count: #{hit}")
4214
- hit
4215
- end
4216
-
4217
- # :category: Table Handling
4218
- def fetch_array_for_table_column(nc_element, table_index, column_index)
4219
- ary = []
4220
- nc_element.tables[table_index].each do |row|
4221
- if get_cell_count(row) >= column_index
4222
- #TODO this assumes column 1 is a number column
4223
- if row[1].text =~ /\d+/
4224
- ary << row[column_index].text
4225
- end
4226
- end
4227
- end
4228
- return ary f
4229
- end
4230
-
4231
- # :category: Table Handling
4232
- def fetch_hash_for_table_column(table, column_index, start_row = 2)
4233
- hash = Hash.new
4234
- row_count = 0
4235
- table.each do |row|
4236
- row_count += 1
4237
- if get_cell_count(row) >= column_index
4238
- if row_count >= start_row
4239
- hash[row_count] = row[column_index].text
4240
- end
4241
- end
4242
- end
4243
- hash
4244
- end
4245
-
4246
- # :category: Table Handling
4247
- def get_row_cells_text_as_array(row)
4248
- ary = []
4249
- row.each do |cell|
4250
- ary << cell.text
4251
- end
4252
- ary
4253
- end
4254
-
4255
- # :category: Table Handling
4256
- def count_data_rows(container, data_index, column_index)
4257
- cnt = 0
4258
- # get_objects(container, :tables, true)
4259
- table = container.tables[data_index]
4260
- dump_table_and_rows(table)
4261
- if table
4262
- table.rows.each do |row|
4263
- if get_cell_count(row) >= column_index
4264
- #TODO this assumes column 1 is a number column
4265
- if row[column_index].text =~ /\d+/
4266
- cnt += 1
4267
- end
4268
- end
4269
- end
4270
- end
4271
- sleep_for(2)
4272
- cnt
4273
- end
4274
-
4275
- # :category: GUI
4276
- def drag_and_drop(x1, y1, dx, dy, speed=nil)
4277
- speed = 10 if not speed
4278
- x2 = x1 + dx
4279
- y2 = y1 + dy
4280
- debug_to_log("drag_and_drop: start: [#{x1}, #{y1}] end: [#{x2}, #{y2}]")
4281
-
4282
- @ai.MouseMove(x1, y1, speed)
4283
- @ai.MouseClick("primary", x1, y1)
4284
- sleep_for(0.5)
4285
- @ai.MouseClick("primary", x1, y1)
4286
- sleep_for(0.5)
4287
- @ai.MouseClickDrag("primary", x1, y1, x2, y2, speed)
4288
- end
4289
-
4290
- # :category: GUI
4291
- def drag_and_drop_element(browser, element, dx, dy, speed = nil)
4292
- speed = 10 if not speed
4293
- w1, h1, x1, y1, xc1, yc1, xlr1, ylr1 = get_element_coordinates(browser, element, true)
4294
- msg = "Move #{element} by [#{dx}, #{dy}] from center[#{xc1}, #{yc1}] "
4295
- newxc = xc1 + dx
4296
- newyc = yc1 + dy
4297
- msg << "to center[[#{newxc}, #{newyc}]"
4298
- sleep_for(0.5)
4299
-
4300
- drag_and_drop(xc1, yc1, dx, dy)
4301
-
4302
- sleep_for(1)
4303
- w2, h2, x2, y2, xc2, yc2, xlr2, ylr2 = get_element_coordinates(browser, element, true)
4304
-
4305
- # TODO This adjustment is adhoc and empirical. Needs to be derived more rigorously
4306
- xcerr = xc2 - xc1
4307
- ycerr = yc2 - yc1
4308
-
4309
- debug_to_log("\n" +
4310
- "\t\t xc1: #{xc1}\n" +
4311
- "\t\t yc1: #{yc1}\n" +
4312
- "\t\t xc2: #{xc2}\n" +
4313
- "\t\t yc2: #{yc2}\n" +
4314
- "\t\t xcerr: #{xlrerr}\n" +
4315
- "\t\t ycerr: #{ylrerr}\n" +
4316
- "\t\t x tol: #{@x_tolerance}\n" +
4317
- "\t\t y tol: #{@y_tolerance}\n"
4318
- )
4319
-
4320
- #TODO Add check that window _was_ resized.
4321
- x_ok, x_msg = validate_drag_drop(xcerr, @x_tolerance, newxc, xc2)
4322
- y_ok, y_msg = validate_drag_drop(ycerr, @y_tolerance, newyc, yc2)
4323
- msg = msg + "x: #{x_msg}, y: #{y_msg}"
4324
-
4325
- if x_ok and y_ok
4326
- passed_to_log(msg)
4327
- else
4328
- failed_to_log(msg)
4329
- end
4330
- sleep_for(1)
4331
- rescue
4332
- failed_to_log("Unable to validate drag and drop. #{$!} (#{__LINE__})")
4333
- sleep_for(1)
4334
- end
4335
-
4336
- def get_objects(browser, which, dbg=false)
4337
- cnt = 0
4338
- case which
4339
- when :links
4340
- list = browser.links
4341
- sleep(1)
4342
- when :tables
4343
- list = browser.tables
4344
- when :divs
4345
- list = browser.divs
4346
- when :buttons
4347
- list = browser.buttons
4348
- when :checkboxes
4349
- list = browser.checkboxes
4350
- when :radios
4351
- list = browser.radios
4352
- when :selectlists
4353
- list = browser.selectlists
4354
- when :textfields
4355
- list = browser.textfields
4356
- when :lis
4357
- list = browser.lis
4358
- else
4359
- debug_to_log("Unrecognized dom object '#{which}'")
4360
- end
4361
- if dbg
4362
- list.each do |obj|
4363
- cnt += 1
4364
- debug_to_log("\n============== #{which}:\nindex: #{cnt}\n#{obj}\n#{obj.to_yaml}")
4365
- end
4366
- end
4367
- list
4368
- end
4369
-
4370
- def get_ole(element)
4371
- ole = element.ole_object
4372
- if ole
4373
- passed_to_log("Found ole_object for #{element}.")
4374
- ole
4375
- else
4376
- failed_to_log("Did not find ole_object for #{element}.")
4377
- end
4378
- rescue
4379
- failed_to_log("Unable to find ole_object for #{element}. #{$!}")
4380
- end
4381
-
4382
- def find_all_links_with_exact_href(browser, href)
4383
- links = browser.links
4384
- hash = Hash.new
4385
- idx = 0
4386
- links.each do |l|
4387
- idx += 1
4388
- an_href = href
4389
- my_href = l.href
4390
- if my_href == an_href
4391
- hash[idx] = l
4392
- debug_to_log("#{__method__}:#{idx}\n********\n#{l.to_s}\n\n#{l.to_yaml}")
4393
- end
4394
- end
4395
- hash
4396
- end
4397
-
4398
- def find_link_with_exact_href(browser, href)
4399
- links = browser.links
4400
- link = nil
4401
- index = 0
4402
- links.each do |l|
4403
- index += 1
4404
- an_href = href
4405
- my_href = l.href
4406
- if my_href == an_href
4407
- link = l
4408
- # debug_to_log("#{__method__}:#{__LINE__}\n********\n#{l.to_s}\n\n#{l.to_yaml}")
4409
- break
4410
- end
4411
- end
4412
- link
4413
- end
4414
-
4415
- def find_index_for_object(browser, obj, how, ord, strg)
4416
- obj_sym = (obj.to_s.pluralize).to_sym
4417
- how_str = how.to_s
4418
- ptrn = /#{how}:\s+#{strg}/i
4419
- list = get_objects(browser, obj_sym, true)
4420
- cnt = 0
4421
- idx = 0
4422
- list.each do |nty|
4423
- s = nty.to_s
4424
- # a = nty.to_a
4425
- if s =~ ptrn
4426
- cnt += 1
4427
- if cnt == ord
4428
- break
4429
- end
4430
- end
4431
- idx += 1
4432
- end
4433
- idx
4434
- end
4435
-
4436
- # :category: GUI
4437
- def right_click(element)
4438
- x = element.left_edge_absolute + 2
4439
- y = element.top_edge_absolute + 2
4440
- @ai.MouseClick("secondary", x, y)
4441
- end
4442
-
4443
- # :category: GUI
4444
- def left_click(element)
4445
- x = element.left_edge_absolute + 2
4446
- y = element.top_edge_absolute + 2
4447
- @ai.MouseClick("primary", x, y)
4448
- end
4449
-
4450
- def get_cell_count(row)
4451
- # if @browserAbbrev == 'IE' or $use_firewatir
4452
- row.cells.length
4453
- # else
4454
- # row.cell_count
4455
- # end
4456
- end
4457
-
4458
- # :category: GUI
4459
- def screen_offset(element, browser=nil)
4460
- bx, by, bw, bh = get_browser_coord(browser)
4461
- ex = element.left_edge
4462
- ey = element.top_edge
4463
- [bx + ex, by + ey]
4464
- end
4465
-
4466
- # :category: GUI
4467
- def screen_center(element, browser=nil)
4468
- bx, by, bw, bh = get_browser_coord(browser)
4469
- w, h = element.dimensions.to_a
4470
- cx = bx + w/2
4471
- cy = by + h/2
4472
- [cx, cy]
4473
- end
4474
-
4475
- def screen_lower_right(element, browser=nil)
4476
- bx, by, bw, bh = get_browser_coord(browser)
4477
- w, h = element.dimensions.to_a
4478
- [bx + w, by + h]
4479
- end
4480
-
4481
- #TODO unlikely to work...
4482
- def find_me(where, how, what)
4483
- me = where.element(how, what)
4484
- puts me.inspect
4485
- rescue
4486
- error_to_log("#{where.inspect} doesn't seem to respond to element() #{$!}")
4487
- end
4488
-
4489
- # :category: User Input
4490
- def click_me(element)
4491
- element.click
4492
- rescue
4493
- error_to_log("#{element.inspect} doesn't seem to respond to click() #{$!}")
4494
- end
4495
-
4496
- def filter_bailout_from_rescue(err, msg)
4497
- if msg =~ /bailing out/i
4498
- raise err
4499
- else
4500
- error_to_log(msg)
4501
- end
4502
- end
4503
-
4504
- # :category: Debug
4505
- def get_caller_line
4506
- last_caller = get_callers[0]
4507
- line = last_caller.split(':', 3)[1]
4508
- line
4509
- end
4510
-
4511
- # :category: Debug
4512
- def get_call_list(depth = 9, dbg = false)
4513
- myList = []
4514
- call_list = Kernel.caller
4515
- puts call_list if dbg
4516
- call_list.each_index do |x|
4517
- myCaller = call_list[x].to_s
4518
- break if x > depth or myCaller =~ /:in .run.$/
4519
- myCaller =~ /([\(\)\w_\_\-\.]+\:\d+\:?.*?)$/
4520
- myList << "[#{$1.gsub(/eval/, @myName)}] "
4521
- end
4522
- myList
4523
- end
4524
-
4525
- alias get_callers get_call_list
4526
-
4527
- # :category: Debug
4528
- def get_call_list_new(depth = 9, dbg = false)
4529
- myList = []
4530
- call_list = Kernel.caller
4531
- puts call_list if dbg
4532
- call_list.each_index do |x|
4533
- myCaller = call_list[x].to_s
4534
- break if x > depth or myCaller =~ /:in .run.$/
4535
- if myCaller.include? @myName
4536
- myCaller =~ /([\(\)\w_\_\-\.]+\:\d+\:?.*?)$/
4537
- myList << "[#{$1.gsub(/eval/, @myName)}] "
4538
- break
4539
- end
4540
- end
4541
- if @projName
4542
- call_list.each_index do |x|
4543
- myCaller = call_list[x].to_s
4544
- break if x > depth or myCaller =~ /:in .run.$/
4545
- if myCaller.include? @projName
4546
- myCaller =~ /([\(\)\w_\_\-\.]+\:\d+\:?.*?)$/
4547
- myList << "[#{$1.gsub(/eval/, @projName)}] "
4548
- break
4549
- end
4550
- end
4551
- end
4552
- myList
4553
- end
4554
-
4555
- # :category: Debug
4556
- def get_call_array(depth = 9)
4557
- arr = []
4558
- call_list = Kernel.caller
4559
- call_list.each_index do |x|
4560
- myCaller = call_list[x].to_s
4561
- break if x > depth or myCaller =~ /:in .run.$/
4562
- myCaller =~ /([\(\)\w_\_\-\.]+\:\d+\:?.*?)$/
4563
- arr << $1.gsub(/eval/, @myName)
4564
- end
4565
- arr
4566
- end
4567
-
4568
- # :category: Debug
4569
- def get_debug_list(dbg = false)
4570
- calls = get_call_array(10)
4571
- puts "#{calls.to_yaml}" if dbg
4572
- arr = []
4573
- calls.each_index do |ix|
4574
- if ix > 1 # skip this method and the logging method
4575
- arr << calls[ix]
4576
- end
4577
- end
4578
- puts "#{arr.to_yaml}" if dbg
4579
- if arr.length > 0
4580
- list = 'TRACE:'
4581
- arr.reverse.each { |l| list << "=>#{l}" }
4582
- " [[#{list}]]"
4583
- else
4584
- nil
4585
- end
4586
- end
4587
-
4588
- # :category: Debug
4589
- def dump_all_tables(browser, to_report = false)
4590
- tables = browser.tables
4591
- msg = ''
4592
- tbl_cnt = 0
4593
- tables.each do |tbl|
4594
- tbl_cnt += 1
4595
- row_cnt = 0
4596
- msg <<"\n=================\ntable: #{tbl_cnt}\n=================\n#{tbl}\ntext:\n#{tbl.text}"
4597
- tbl.rows.each do |row|
4598
- row_cnt += 1
4599
- cell_cnt = 0
4600
- msg <<"\n=================\ntable: #{tbl_cnt} row: #{row_cnt}\n#{row.inspect}\n#{row}\ntext:'#{row.text}'"
4601
- row.each do |cell|
4602
- cell_cnt += 1
4603
- msg <<"\n==== cell: #{cell_cnt}\n#{cell.inspect}\n#{row}\ntext: '#{cell.text}'"
4604
- end
4605
- end
4606
- end
4607
- if to_report
4608
- debug_to_report(msg)
4609
- else
4610
- debug_to_log(msg)
4611
- end
4612
- end
4613
-
4614
- # :category: Debug
4615
- def dump_table_and_rows(table, to_report = false)
4616
- msg = "\n=================\ntable\n=================\nn#{table}\n#{table.to_yaml}\nrows:"
4617
- cnt = 0
4618
- table.rows.each do |r|
4619
- cnt += 1
4620
- msg << "\n#{cnt}: #{r.text}"
4621
- end
4622
- msg << "\n=================\n================="
4623
- if to_report
4624
- debug_to_report(msg)
4625
- else
4626
- debug_to_log(msg)
4627
- end
4628
- end
4629
-
4630
- # :category: Debug
4631
- def dump_table_rows_and_cells(tbl)
4632
- msg = ''
4633
- row_cnt = 0
4634
- msg <<"\n=================\ntable: #{tbl.inspect}\n=================\n#{tbl}\ntext:\n#{tbl.text}"
4635
- tbl.rows.each do |row|
4636
- row_cnt += 1
4637
- cell_cnt = 0
4638
- msg <<"\n=================\nrow: #{row_cnt}\n#{row.inspect}\n#{row}\ntext:'#{row.text}'"
4639
- row.each do |cell|
4640
- cell_cnt += 1
4641
- msg <<"\n==== cell: #{cell_cnt}\n#{cell.inspect}\n#{row}\ntext: '#{cell.text}'"
4642
- end
4643
- end
4644
- debug_to_log(msg)
4645
- end
4646
-
4647
- alias dump_table_rows dump_table_rows_and_cells
4648
-
4649
- # :category: Debug
4650
- def dump_array(arr, space_to_underscore = false)
4651
- dump = " #{arr.inspect}\n"
4652
- arr.each_index do |x|
4653
- value = arr[x].to_s
4654
- value.gsub!(/\s/, '_') if space_to_underscore
4655
- dump << " #{x.to_s.rjust(5)}>> '#{arr[x].to_s}'\n"
4656
- end
4657
- dump
4658
- end
4659
-
4660
- # :category: Debug
4661
- def dump_ole_methods(ole)
4662
- rtrn = ''
4663
- ole.ole_methods.each do |m|
4664
- prms = ''
4665
- m.params.each do |p|
4666
- prms << "#{p}, "
4667
- end
4668
- rtrn << "#{m.name}(#{prms.chop.chop})\n"
4669
- end
4670
- rtrn
4671
- end
4672
-
4673
- # :category: Debug
4674
- def dump_ole_get_methods(ole)
4675
- rtrn = ''
4676
- ole.ole_get_methods.each do |m|
4677
- prms = ''
4678
- m.params.each do |p|
4679
- prms << "#{p}, "
4680
- end
4681
- rtrn << "#{m.name}(#{prms.chop.chop})\n"
4682
- end
4683
- rtrn
4684
- end
4685
-
4686
- # :category: Debug
4687
- def dump_ole_help(ole)
4688
- rtrn = ''
4689
- ole.ole_obj_help.each do |m|
4690
- prms = ''
4691
- m.params.each do |p|
4692
- prms << "#{p}, "
4693
- end
4694
- rtrn << "#{m.name}(#{prms.chop.chop})\n"
4695
- end
4696
- rtrn
4697
- end
4698
32
 
4699
- # :category: Debug
4700
- def dump_row_cells(row)
4701
- msg = ''
4702
- cell_cnt = 0
4703
- msg <<"\n=================\nrow: #{row.inspect}\n#{row}\ntext:'#{row.text}'"
4704
- row.each do |cell|
4705
- cell_cnt += 1
4706
- msg <<"\n==== cell: #{cell_cnt}\n#{cell.inspect}\n#{row}\ntext: '#{cell.text}'"
4707
- end
4708
- debug_to_log(msg)
4709
- end
33
+ # def click_me(element)
34
+ # element.click
35
+ # rescue
36
+ # error_to_log("#{element.inspect} doesn't seem to respond to click() #{$!}")
37
+ # end
4710
38
 
4711
- # :category: Debug
4712
- def dump_select_list_options(element)
4713
- msg = "#{element.inspect}"
4714
- options = element.options
4715
- cnt = 1
4716
- options.each do |o|
4717
- msg << "\n\t#{cnt}:\t'#{o}"
4718
- cnt += 1
4719
- end
4720
- debug_to_log(msg)
4721
- end
4722
39
 
4723
40
  end