awetestlib 0.1.28 → 0.1.29pre1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (82) hide show
  1. data/.gitattributes +22 -22
  2. data/.gitignore +69 -69
  3. data/.yardopts +7 -7
  4. data/README.md +108 -108
  5. data/awetestlib.gemspec +57 -57
  6. data/awetestlib.windows.gemspec +41 -41
  7. data/awetestlib_notes.txt +4 -4
  8. data/awetestlib_osx.gemspec +47 -48
  9. data/bin/awetestlib +99 -99
  10. data/bin/awetestlib-android-setup.rb +26 -26
  11. data/bin/awetestlib-cucumber-setup.rb +28 -28
  12. data/bin/awetestlib-driver-setup.rb +21 -21
  13. data/bin/awetestlib-helpers.rb +41 -41
  14. data/bin/awetestlib-mobile-app-setup.rb +31 -31
  15. data/bin/awetestlib-netbeans-setup.rb +58 -58
  16. data/bin/awetestlib-regression-setup.rb +15 -15
  17. data/bin/awetestlib-rubymine-setup.rb +39 -39
  18. data/drivers/chromedriver.exe +0 -0
  19. data/ext/Rakefile +1 -1
  20. data/ext/mkrf_conf.rb +27 -27
  21. data/lib/awetestlib/html_report.rb +142 -142
  22. data/lib/awetestlib/logging.rb +366 -366
  23. data/lib/awetestlib/regression/browser.rb +1380 -1375
  24. data/lib/awetestlib/regression/drag_and_drop.rb +421 -420
  25. data/lib/awetestlib/regression/find.rb +345 -345
  26. data/lib/awetestlib/regression/legacy.rb +1187 -1187
  27. data/lib/awetestlib/regression/page_data.rb +191 -191
  28. data/lib/awetestlib/regression/runner.rb +307 -307
  29. data/lib/awetestlib/regression/tables.rb +627 -619
  30. data/lib/awetestlib/regression/user_input.rb +576 -576
  31. data/lib/awetestlib/regression/utilities.rb +1046 -988
  32. data/lib/awetestlib/regression/validations.rb +1093 -1074
  33. data/lib/awetestlib/regression/waits.rb +470 -470
  34. data/lib/awetestlib/runner.rb +18 -18
  35. data/lib/awetestlib.rb +41 -41
  36. data/lib/patches/README +2 -2
  37. data/lib/patches/firewatir.rb +106 -106
  38. data/lib/patches/watir.rb +175 -175
  39. data/lib/version.rb +2 -2
  40. data/license.txt +13 -13
  41. data/netbeans_setup.md +29 -29
  42. data/rdoc_test.bat +1 -1
  43. data/rubymine_setup.md +23 -23
  44. data/setup_samples/sample_android/features/dk.mejer.hansen.control.FlyingColorsPlayerAidActivity.apk +0 -0
  45. data/setup_samples/sample_android/features/sample_android.feature +5 -5
  46. data/setup_samples/sample_android/features/step_definitions/calabash_steps.rb +1 -1
  47. data/setup_samples/sample_android/features/support/app_installation_hooks.rb +36 -36
  48. data/setup_samples/sample_android/features/support/app_life_cycle_hooks.rb +14 -14
  49. data/setup_samples/sample_android/features/test_servers/8ba795a0288381ae346b67867b586881_0.3.2.apk +0 -0
  50. data/setup_samples/sample_cucumber/features/step_definitions/predefined_steps.rb +76 -76
  51. data/setup_samples/sample_cucumber/features/yahoo_mail.feature +11 -11
  52. data/setup_samples/sample_mobile_app/features/my_first.feature +14 -14
  53. data/setup_samples/sample_mobile_app/features/step_definitions/predefined_webview_steps.rb +80 -80
  54. data/setup_samples/sample_mobile_app/features/support/env.rb +2 -2
  55. data/setup_samples/sample_netbeans/demo.rb +86 -86
  56. data/setup_samples/sample_netbeans/nbproject/configs/Demo.properties +2 -2
  57. data/setup_samples/sample_netbeans/nbproject/private/config.properties +1 -1
  58. data/setup_samples/sample_netbeans/nbproject/private/configs/Demo.properties +2 -2
  59. data/setup_samples/sample_netbeans/nbproject/private/private.properties +2 -2
  60. data/setup_samples/sample_netbeans/nbproject/project.properties +5 -5
  61. data/setup_samples/sample_netbeans/nbproject/project.xml +13 -13
  62. data/setup_samples/sample_rubymine/.idea/encodings.xml +5 -5
  63. data/setup_samples/sample_rubymine/.idea/misc.xml +5 -5
  64. data/setup_samples/sample_rubymine/.idea/modules.xml +9 -9
  65. data/setup_samples/sample_rubymine/.idea/sample_rubymine.iml +9 -9
  66. data/setup_samples/sample_rubymine/.idea/scopes/scope_settings.xml +4 -4
  67. data/setup_samples/sample_rubymine/.idea/vcs.xml +7 -7
  68. data/setup_samples/sample_rubymine/.idea/workspace.xml +213 -213
  69. data/setup_samples/sample_rubymine/demo.rb +86 -86
  70. data/test/create_zoho.rb +66 -66
  71. data/test/create_zoho_account1.rb +68 -68
  72. data/test/create_zoho_account2.rb +72 -72
  73. data/test/demo.rb +87 -87
  74. data/test/google_search1.rb +16 -16
  75. data/test/google_search2.rb +19 -19
  76. data/test/login_1.rb +37 -37
  77. data/test/login_1a.rb +37 -37
  78. data/test/login_2.rb +32 -32
  79. data/test/zoho_exercise.rb +21 -21
  80. data/test/zoho_util.rb +487 -487
  81. data/tmp/placeholder.html +71 -71
  82. metadata +18 -49
@@ -1,1375 +1,1380 @@
1
- module Awetestlib
2
- # Awetest DSL for browser based testing.
3
- module Regression
4
- # Methods to manage browser windows: open, close, attach, verify health, and clean up.
5
- module Browser
6
-
7
- #def run #DO WE NEED? Use this method to tell user they need to create a run method?
8
- ## Not here in any case.
9
- # setup
10
- # set_script_variables
11
- # run_test
12
- #rescue
13
- # fatal_to_log("(#{__LINE__}) #{$!}")
14
- # browser.close
15
- # raise
16
- #end
17
-
18
- # @!group Browser
19
-
20
- # @note webdriver specific - still work in progress
21
- def go_to_wd_url(browser, url)
22
-
23
- Watir::Browser.class_eval do
24
- def goto(uri)
25
- uri = "http://#{uri}" unless uri =~ URI.regexp
26
- @driver.navigate.to uri
27
- run_checkers
28
- end
29
- end
30
- browser.goto(url)
31
-
32
- #in basic_auth1 edit:
33
- #a = Thread.new {
34
- # goto_wd_url(browser, @myURL)
35
- # }
36
-
37
- end
38
-
39
- alias goto_wd_url go_to_wd_url
40
-
41
- # Open a browser based on the command line parameters that identify the browser and
42
- # version to use for the test.
43
- # @example
44
- # browser = open_browser('www.google.com')
45
- # @param [String, Regexp] url When provided, the browser will go to this url.
46
- # @return [Watir::Browser]
47
- def open_browser(url = nil)
48
- message_to_report("Opening browser: #{@targetBrowser.name}")
49
- case @targetBrowser.abbrev
50
- when 'IE'
51
- @myBrowser = open_ie
52
- if @myBrowser.class.to_s == "Watir::IE"
53
- @myHwnd = @myBrowser.hwnd
54
- end
55
- when 'FF'
56
- @myBrowser = open_ff_for_version
57
- when 'S'
58
- aBrowser = Watir::Browser.new :safari
59
- @myBrowser = aBrowser
60
- when 'C', 'GC'
61
- @myBrowser = open_chrome
62
- else
63
- raise "Unsupported browser: #{@targetBrowser.name}"
64
- end
65
- if url
66
- go_to_url(@myBrowser, url)
67
- end
68
- @myBrowser
69
- end
70
-
71
- # Open IE (Internet Explorer) browser instance.
72
- # If global variable $watir_script is set to true in the first line of the script,
73
- # classic Watir will be used to drive the browser,
74
- # otherwise Watir Webdriver will be used.
75
- # @return [Watir::Browser]
76
- def open_ie
77
- if $watir_script
78
- browser = Watir::IE.new
79
- else
80
- browser = Watir::Browser.new :ie
81
- end
82
- browser
83
- end
84
-
85
- # Open FF (Firefox) browser instance.
86
- # @param [Fixnum] version A number designating the version of the browser to be opened.
87
- # @return [Watir::Browser, Firewatir::Browser]
88
- # Returns Firewatir::Browser if target browser is Firefox version less than 4.0
89
- def open_ff_for_version(version = @targetVersion)
90
- browser = Watir::Browser.new :firefox
91
- end
92
-
93
- # Open FF (Firefox) browser instance under FireWatir.
94
- # @return [Firewatir::Browser]
95
- def open_ff
96
- # Watir::Browser.default = 'firefox'
97
- browser = Watir::Browser.new :firefox
98
- end
99
-
100
- # Open GC (Google Chrome) browser instance.
101
- # @return [Watir::Browser] Browser is Google Chrome.
102
- def open_chrome
103
- browser = Watir::Browser.new(:chrome)
104
- end
105
-
106
- # Instruct browser to navigate to a specific URL
107
- # @param [Watir::Browser] browser A reference to the browser window or container element to be tested.
108
- # @param [String] url When provided, the browser will go to this url.
109
- # and the instance variable @myURL will be set to this value.
110
- # @return [Boolean] True when navigation to url succeeds.
111
- def go_to_url(browser, url = nil)
112
- if url
113
- @myURL = url
114
- end
115
- message_to_report("URL: #{@myURL}")
116
- browser.goto(@myURL)
117
- true
118
- rescue
119
- fatal_to_log("Unable to navigate to '#{@myURL}': '#{$!}'")
120
- end
121
-
122
- # Return a reference to a browser window. Used to attach a browser window to a variable
123
- # which can then be passed to methods that require a *browser* parameter.
124
- # @example
125
- # mainwindow = open_browser('www.google.com')
126
- # click(mainwindow, :button, :id, 'an id string') # click a button that opens another browser window
127
- # popup = attach_browser(mainwindow, :url, '[url of new window]') #*or*
128
- # popup = attach_browser(mainwindow, :title, '[title of new window]')
129
- # @todo Update to work with webdriver for IE.
130
- # @param [Watir::Browser] browser A reference to the current browser window.
131
- # @param [Symbol] how The element attribute used to identify the window: *:title* or :url.
132
- # @param [String|Regexp] what A string or a regular expression to be found in the *how* attribute that uniquely identifies the element.
133
- # @param [String] desc Contains a message or description intended to appear in the log and/or report output
134
- # @return [Watir::Browser] Internet Explorer
135
- def attach_browser(browser, how, what, desc = '')
136
- debug_to_log("Attaching browser window :#{how}=>'#{what}' #{desc}")
137
- uri_decoded_pattern = ::URI.encode(what.to_s.gsub('(?-mix:', '').gsub(')', ''))
138
-
139
- if $watir_script
140
- tmpbrowser = Watir::IE.attach(how, what)
141
- browser.visible = true
142
- if tmpbrowser
143
- tmpbrowser.visible = true
144
- tmpbrowser.speed = :fast
145
- else
146
- raise "Browser window :#{how}=>'#{what}' has at least one doc not in completed ready state."
147
- end
148
- else
149
- browser.driver.switch_to.window(browser.driver.window_handles[0])
150
- browser.window(how, /#{uri_decoded_pattern}/).use
151
- tmpbrowser = browser
152
- end
153
-
154
- # case @browserAbbrev
155
- # when 'IE'
156
- # tmpbrowser = Watir::IE.attach(how, what)
157
- # browser.visible = true
158
- # if tmpbrowser
159
- # tmpbrowser.visible = true
160
- # tmpbrowser.speed = :fast
161
- # else
162
- # raise "Browser window :#{how}=>'#{what}' has at least one doc not in completed ready state."
163
- # end
164
- # when 'FF'
165
- # #TODO: This may be dependent on Firefox version if webdriver doesn't support 3.6.17 and below
166
- # browser.driver.switch_to.window(browser.driver.window_handles[0])
167
- # browser.window(how, /#{uri_decoded_pattern}/).use
168
- # tmpbrowser = browser
169
- # when 'S'
170
- # Watir::Safari.attach(how, what)
171
- # tmpbrowser = browser
172
- # when 'C', 'GC'
173
- # browser.window(how, /#{uri_decoded_pattern}/).use
174
- # tmpbrowser = browser
175
- # end
176
-
177
-
178
- debug_to_log("#{__method__}: tmpbrowser:#{tmpbrowser.inspect}")
179
- tmpbrowser
180
- end
181
-
182
-
183
- # Returns a reference to a new browser window. Used to attach a new browser window to a variable
184
- # which can then be passed to methods that require a *browser* parameter. Calls attach_browser().
185
- # @example
186
- # mainwindow = open_browser('www.google.com')
187
- # click(mainwindow, :button, :id, 'an id string') # click a button that opens another browser window
188
- # popup = attach_popup(mainwindow, :url, '[url of new window]') *or*
189
- # popup = attach_popup(mainwindow, :title, '[title of new window]')
190
- # @param [Watir::Browser] browser A reference to the current browser window.
191
- # @param [Symbol] how The element attribute used to identify the window: *:title* or :url.
192
- # @param [String, Regexp] what A string or a regular expression to be found in the *how* attribute that uniquely identifies the element.
193
- # @param [String] desc Contains a message or description intended to appear in the log and/or report output
194
- # @return [Watir::Browser] The new browser window.
195
- def attach_popup(browser, how, what, desc = '')
196
- msg = "Attach popup :#{how}=>'#{what}'. #{desc}"
197
- popup = attach_browser(browser, how, what, desc)
198
- sleep_for(1)
199
- debug_to_log("#{popup.inspect}")
200
- if is_browser?(popup)
201
- title = popup.title
202
- passed_to_log("#{msg} title='#{title}'")
203
- return popup
204
- else
205
- failed_to_log(msg)
206
- end
207
- rescue
208
- failed_to_log("Unable to attach popup :#{how}=>'#{what}'. #{desc} '#{$!}' (#{__LINE__})")
209
- end
210
-
211
- # Locate and close instances of IE browsers
212
- def find_other_browsers
213
- cnt = 0
214
- if @targetBrowser.abbrev == 'IE'
215
- Watir::IE.each do |ie|
216
- debug_to_log("#{ie.inspect}")
217
- ie.close()
218
- cnt = cnt + 1
219
- end
220
- end
221
- debug_to_log("Found #{cnt} IE browser(s).")
222
- return cnt
223
- rescue
224
- error_to_log("#{$!} (#{__LINE__})\n#{Kernel.caller.to_yaml}", __LINE__)
225
- return 0
226
- end
227
-
228
- # @!endgroup Browser
229
-
230
- # @!group Login
231
-
232
- # Simple login method
233
- # @param [Watir::Browser] browser A reference to the browser window or container element to be tested.
234
- # @param [String] user Login ID for the user.
235
- # @param [String] password Password for the user.
236
- # @return [Boolean] True if login succeeds.
237
- def login(browser, user, password)
238
- #TODO: Needs to be more flexible about finding login id and password textfields
239
- #TODO: Parameterize url and remove references to environment
240
- myURL = @myAppEnv.url
241
- runenv = @myAppEnv.nodename
242
- message_tolog("URL: #{myURL}") if @myAppEnv
243
- message_tolog("Beginning login: User: #{user} Environment: #{@myAppEnv.nodename}") if @myAppEnv
244
- if validate(browser, @myName, __LINE__)
245
- browser.goto(@myAppEnv.url)
246
- if validate(browser, @myName)
247
- set_textfield_by_name(browser, 'loginId', user)
248
- set_textfield_by_name(browser, 'password', password)
249
- click_button_by_value(browser, 'Login')
250
- if validate(browser, @myName)
251
- passed_to_log("Login successful.")
252
- true
253
- end
254
- else
255
- failed_to_log("Unable to login to application: '#{$!}'")
256
- #screen_capture( "#{@myRoot}/screens/#{myName}_#{@runid}_#{__LINE__.to_s}_#{Time.new.to_f.to_s}.jpg")
257
- end
258
- end
259
- rescue
260
- failed_to_log("Unable to login to application: '#{$!}'")
261
- end
262
-
263
- # Logon to webpage using Basic Authorization type of logon. Uses AutoIt
264
- # @param [Watir::Browser] browser A reference to the browser window or container element to be tested.
265
- # @param [String] user Login ID for the user.
266
- # @param [String] password Password for the user.
267
- # @param [String] url The URL to log on to.
268
- # @param [Boolean] bypass_validate When set to true, the call to validate(),
269
- # which checks the health of the browser, is skipped..
270
- def basic_auth(browser, user, password, url, bypass_validate = false)
271
- mark_testlevel("Basic Authorization Login", 0)
272
-
273
- message_to_report ("Login: #{user}")
274
- message_to_report ("URL: #{url}")
275
- message_to_report ("Password: #{password}")
276
-
277
- @login_title = "Connect to"
278
-
279
- a = Thread.new {
280
- browser.goto(url)
281
- }
282
-
283
- sleep_for(2)
284
- message_to_log("#{@login_title}...")
285
-
286
- if (@ai.WinWait(@login_title, "", 90) > 0)
287
- win_title = @ai.WinGetTitle(@login_title)
288
- debug_to_log("Basic Auth Login window appeared: '#{win_title}'")
289
- @ai.WinActivate(@login_title)
290
- @ai.ControlSend(@login_title, '', "[CLASS:Edit; INSTANCE:2]", '!u')
291
- @ai.ControlSend(@login_title, '', "[CLASS:Edit; INSTANCE:2]", user, 1)
292
- @ai.ControlSend(@login_title, '', "[CLASS:Edit; INSTANCE:3]", password.gsub(/!/, '{!}'), 1)
293
- @ai.ControlClick(@login_title, "", '[CLASS:Button; INSTANCE:1]')
294
- else
295
- debug_to_log("Basic Auth Login window did not appear.")
296
- end
297
- a.join
298
-
299
- validate(browser, @myName) unless bypass_validate
300
-
301
- message_to_report("URL: [#{browser.url}] User: [#{user}]")
302
-
303
- end
304
-
305
- # Provide an authorization token or passcode in a specified text field element identified by its *:id* attribute.
306
- # @param [Watir::Browser] browser A reference to the browser window or container element to be tested.
307
- # @param [String] role Usually a user role designation ('Administrator', 'User', etc.)
308
- # @param [String] token Authentification token required by logon process.
309
- # @param [String/Regexp] id Value of the *:id* attribute of the text field that will receive the *token*.
310
- def token_auth(browser, role, token, id = 'token_pass')
311
- set_textfield_by_id(browser, id, token)
312
- click_button_by_value(browser, 'Continue')
313
- if validate_text(browser, 'The requested page requires authentication\.\s*Please enter your Passcode below', nil, true)
314
- bail_out(browser, __LINE__, "Token authorization failed on '#{token}'")
315
- end
316
- end
317
-
318
- # @!endgroup Logon
319
-
320
- # @!group Error Handling
321
-
322
- # Exit more or less gracefully from script when errors are too severe to continue.
323
- # Normally not called in a test script or project library.
324
- # @param [Watir::Browser] browser A reference to the browser window or container element to be tested.
325
- # @param [Fixnum] lnbr Line number in calling script.
326
- # @param [String] desc Contains a message or description intended to appear in the log and/or report output
327
- #
328
- def bail_out(browser, lnbr, desc)
329
- ts = Time.new
330
- msg = "Bailing out at util line #{lnbr} #{ts} " + desc
331
- puts "#{msg}"
332
- fatal_to_log(msg, lnbr)
333
- debug_to_log(dump_caller(lnbr))
334
- if is_browser?(browser)
335
- if @browserAbbrev == 'IE'
336
- hwnd = browser.hwnd
337
- kill_browser(hwnd, lnbr, browser)
338
- raise(RuntimeError, msg, caller)
339
- elsif @browserAbbrev == 'FF'
340
- debug_to_log("#{browser.inspect}")
341
- debug_to_log("#{browser.to_s}")
342
- raise(RuntimeError, msg, caller)
343
- end
344
- end
345
- @status = 'bailout'
346
- raise(RuntimeError, msg, caller)
347
- end
348
-
349
-
350
- # Check for the presence of IE browser instances.
351
- # @return [Fixnum] The number of IE browser instances encountered.
352
- def check_for_other_browsers
353
- cnt1 = find_other_browsers
354
- cnt2 = Watir::Process.count 'iexplore.exe'
355
- debug_to_log("check_for_other_browsers: cnt1: #{cnt1} cnt2: #{cnt2}")
356
- rescue
357
- error_to_log("#{$!} (#{__LINE__})\n#{Kernel.caller.to_yaml}")
358
- end
359
-
360
- # Check for the presence of IE browser instances and close all that are found.
361
- def check_for_and_clear_other_browsers
362
- if @targetBrowser.abbrev == 'IE'
363
- debug_to_log("#{__method__}:")
364
- cnt1 = find_other_browsers
365
- cnt2 = Watir::IE.process_count
366
- debug_to_log("#{__method__}: cnt1: #{cnt1} cnt2: #{cnt2}")
367
- begin
368
- Watir::IE.each do |ie|
369
- pid = Watir::IE::Process.process_id_from_hwnd(ie.hwnd)
370
- debug_to_log("#{__method__}: Killing browser process: hwnd #{ie.hwnd} pid #{pid} title '#{ie.title}' (#{__LINE__})")
371
- do_taskkill(INFO, pid)
372
- sleep_for(10)
373
- end
374
- #Watir::IE.close_all()
375
- rescue
376
- debug_to_log("#{__method__}: #{$!} (#{__LINE__})")
377
- end
378
- sleep(3)
379
- cnt1 = find_other_browsers
380
- cnt2 = Watir::IE.process_count
381
- if cnt1 > 0 or cnt2 > 0
382
- debug_to_log("#{__method__}:cnt1: #{cnt1} cnt2: #{cnt2}")
383
- begin
384
- Watir::IE.each do |ie|
385
- pid = Watir::IE::Process.process_id_from_hwnd(ie.hwnd)
386
- debug_to_log("#{__method__}: Killing browser process: hwnd #{ie.hwnd} pid #{pid} title '#{ie.title}' (#{__LINE__})")
387
- do_taskkill(INFO, pid)
388
- sleep_for(10)
389
- end
390
- #Watir::IE.close_all()
391
- rescue
392
- debug_to_log("#{__method__}:#{$!} (#{__LINE__})")
393
- end
394
- end
395
- end
396
- rescue
397
- error_to_log("#{__method__}: #{$!} (#{__LINE__})\n#{Kernel.caller.to_yaml}")
398
- end
399
-
400
- # Force browser instance to close, one way or the other.
401
- # Instance is identified by *hwnd*, the Windows OS handle for the process.
402
- # @param [String] hwnd The value for the window handle for the browser process.
403
- # @param [Fixnum] lnbr Line number in calling script.
404
- # @param [Watir::Browser] browser A reference to the browser window or container element to be closed.
405
- def kill_browser(hwnd, lnbr, browser = nil, doflag = false)
406
- # TODO Firefox
407
- logit = false
408
- if @browserAbbrev == 'FF'
409
- if is_browser?(browser) # and browser.url.length > 1
410
- logit = true
411
- here = __LINE__
412
- url = browser.url
413
- #capture_screen(browser, Time.new.to_f) if @screenCaptureOn
414
- browser.close if url.length > 0
415
- @status = 'killbrowser'
416
- fatal_to_log("Kill browser called from line #{lnbr}")
417
- end
418
- elsif hwnd
419
- pid = Watir::IE::Process.process_id_from_hwnd(hwnd)
420
- if pid and pid > 0 and pid < 538976288
421
- if browser.exists?
422
- here = __LINE__
423
- logit = true
424
- url = browser.url
425
- #capture_screen(browser, Time.new.to_f) if @screenCaptureOn
426
- browser.close
427
- sleep(2)
428
- if browser.exists?
429
- do_taskkill(FATAL, pid)
430
- end
431
- @status = 'killbrowser'
432
- end
433
- end
434
- if logit
435
- debug_to_log("#{@browserName} window hwnd #{hwnd} pid #{pid} #{url} (#{here})")
436
- fatal_to_log("Kill browser called from line #{lnbr}")
437
- end
438
- end
439
- end
440
-
441
- # @!endgroup Error Handling
442
-
443
- # @!group Browser
444
-
445
- # Use enabled_popup and winclicker to determine if there is an active modal popup.
446
- # Useful only when no wait action has been invoked.
447
- # @param [Watir::Browser] browser A reference to the browser window or container element to be tested.
448
- # @param [String] button The visible name of the button to be clicked to close the popup.
449
- # @return [String] containing the window handle of the closed modal popup.
450
- def modal_exists?(browser, button = nil)
451
- rtrn = nil
452
- if @browserAbbrev == 'IE'
453
- Timeout::timeout(2) do
454
- begin
455
- if browser.enabled_popup
456
- hwnd = browser.enabled_popup(5)
457
- debug_to_log("Modal popup with handle #{hwnd} found. (#{__LINE__})")
458
- wc = WinClicker.new
459
- wc.makeWindowActive(hwnd)
460
- rtrn = wc.getWindowTitle(hwnd)
461
- if button
462
- wc.clickWindowsButton_hWnd(hwnd, button)
463
- end
464
- wc = nil
465
- end
466
- rescue Timeout::Error
467
- debug_to_log("No Modal popup found. (#{__LINE__})")
468
- return rtrn
469
- end
470
- return rtrn
471
- end
472
- rtrn
473
- else
474
- rtrn
475
- end
476
- end
477
-
478
- # Close a browser window identified by its title.
479
- # Uses AutoIt. Windows only.
480
- # @param [Watir::Browser] browser A reference to the browser window or container element to be tested.
481
- # @param [String] title The title of the window to be closed. Matched from beginning of string.
482
- # @param [String] desc Contains a message or description intended to appear in the log and/or report output
483
- # @param [String] text (optional) The text of the window to be closed. Matched from beginning of string.
484
- def close_window_by_title(browser, title, desc = '', text = '')
485
- msg = "Window '#{title}':"
486
- if @ai.WinWait(title, text, WAIT) > 0
487
- passed_to_log("#{msg} appeared. #{desc}")
488
- myHandle = @ai.WinGetHandle(title, text)
489
- full_text = @ai.WinGetText(title)
490
- debug_to_log("#{msg} hwnd: #{myHandle.inspect}")
491
- debug_to_log("#{msg} title: '#{title}' text: '#{full_text}'")
492
- if @ai.WinClose(title, text) > 0
493
- passed_to_log("#{msg} closed successfully. #{desc}")
494
- else
495
- failed_to_log("#{msg} close failed. (#{__LINE__}) #{desc}")
496
- end
497
- else
498
- failed_to_log("#{msg} did not appear after #{WAIT} seconds. (#{__LINE__}) #{desc}")
499
- end
500
- rescue
501
- failed_to_log("#{msg}: Unable to close: '#{$!}'. (#{__LINE__}) #{desc}")
502
- end
503
-
504
- # Closes main browser session. Misnamed. Usually used at end of script to shut down browser.
505
- def logout(browser, where = @myName, lnbr = __LINE__)
506
- #TODO Firewatir 1.6.5 does not implement .exists for FireWatir::Firefox class
507
- debug_to_log("Logging out in #{where} at line #{lnbr}.", lnbr, true)
508
- debug_to_log("#{__method__}: browser: #{browser.inspect} (#{__LINE__})")
509
-
510
- if ['FF', 'S'].include?(@browserAbbrev) || browser.exists?
511
- case @browserAbbrev
512
- when 'FF'
513
- if is_browser?(browser)
514
- url = browser.url
515
- title = browser.title
516
- debug_to_log("#{__method__}: Firefox browser url: [#{url}]")
517
- debug_to_log("#{__method__}: Firefox browser title: [#{title}]")
518
- debug_to_log("#{__method__}: Closing browser: #{where} (#{lnbr})")
519
- if url and url.length > 1
520
- browser.close
521
- else
522
- browser = FireWatir::Firefox.attach(:title, title)
523
- browser.close
524
- end
525
-
526
- end
527
- when 'IE'
528
- hwnd = browser.hwnd
529
- pid = Watir::IE::Process.process_id_from_hwnd(hwnd)
530
- debug_to_log("#{__method__}: Closing browser: hwnd #{hwnd} pid #{pid} #{where} (#{lnbr}) (#{__LINE__})")
531
- browser.close
532
- if browser.exists? and pid > 0 and pid < 538976288 # value of uninitialized memory location
533
- debug_to_log("Retry close browser: hwnd #{hwnd} pid #{pid} #{where} #{lnbr} (#{__LINE__})")
534
- browser.close
535
- end
536
- if browser.exists? and pid > 0 and pid < 538976288 # value of uninitialized memory location
537
- kill_browser(browser.hwnd, __LINE__, browser)
538
- end
539
- when 'S'
540
- if is_browser?(browser)
541
- url = browser.url
542
- title = browser.title
543
- debug_to_log("Safari browser url: [#{url}]")
544
- debug_to_log("Safari browser title: [#{title}]")
545
- debug_to_log("Closing browser: #{where} (#{lnbr})")
546
- close_modal_s # to close any leftover modal dialogs
547
- browser.close
548
- end
549
- when 'C', 'GC'
550
- if is_browser?(browser)
551
- url = browser.url
552
- title = browser.title
553
- debug_to_log("Chrome browser url: [#{url}]")
554
- debug_to_log("Chrome browser title: [#{title}]")
555
- debug_to_log("Closing browser: #{where} (#{lnbr})")
556
- if url and url.length > 1
557
- browser.close
558
- #else
559
- #browser = FireWatir::Firefox.attach(:title, title)
560
- #browser.close
561
- end
562
-
563
- end
564
- else
565
- raise "Unsupported browser: '#{@browserAbbrev}'"
566
- end
567
- end
568
- # rescue => e
569
- # if not e.is_a?(Vapir::WindowGoneException)
570
- # raise e
571
- # end
572
- end
573
-
574
- # Close a browser popup window. Does not apply to modal popups.
575
- # @param [Watir::Browser] popup Reference to the popup to be closed
576
- def close_new_window_popup(popup)
577
- if is_browser?(popup)
578
- url = popup.url
579
- debug_to_log("Closing popup '#{url}' ")
580
- popup.close
581
-
582
- end
583
- end
584
-
585
- # Close an HTML panel or division by clicking a link within it identified by the *:text* value of the link.
586
- # @param [Watir::Browser] browser A reference to the browser window or container element to be tested.
587
- # @param [Watir::Browser] panel Reference to the panel (usually a div element) to be closed
588
- def close_panel_by_text(browser, panel, what = 'Close')
589
- if validate(browser, @myName, __LINE__)
590
- if @browserAbbrev == 'IE'
591
- panel.link(:text, what).click!
592
- elsif $USE_FIREWATIR
593
- begin
594
- panel.link(:text, what).click
595
- rescue => e
596
- unless rescue_me(e, __method__, rescue_me_command(:link, :id, what, :click), "#{panel.class}")
597
- raise e
598
- end
599
- end
600
- else
601
- panel.link(:text, what).click(:wait => false)
602
- end
603
- sleep_for(1)
604
- if validate(browser, @myName, __LINE__)
605
- passed_to_log("Panel '#{what}' (by :text) closed.")
606
- true
607
- end
608
- else
609
- failed_to_log("Panel '#{strg}' (by :text) still open.")
610
- end
611
- rescue
612
- failed_to_log("Click on '#{strg}'(by :text) failed: '#{$!}' (#{__LINE__})")
613
- end
614
-
615
- #def close_modal_ie(title, button = "OK", text = '', side = 'primary', wait = WAIT, desc = '', quiet = false)
616
- # #TODO needs simplifying and debug code cleaned up
617
- # title = translate_popup_title(title)
618
- # msg = "'#{title}'"
619
- # msg << " with text '#{text}'" if text.length > 0
620
- # msg << " (#{desc})" if desc.length > 0
621
- # @ai.Opt("WinSearchChildren", 1) # Match any substring in the title
622
- # if @ai.WinWait(title, text, wait) > 0
623
- # myHandle = @ai.WinGetHandle(title, text)
624
- # full_text = @ai.WinGetText(title)
625
- # #debug_to_report("Found popup handle:'#{myHandle}', title:'#{title}', text:'#{full_text}'")
626
- # if myHandle.length > 0
627
- # debug_to_log("hwnd: #{myHandle.inspect}")
628
- # passed_to_log("#{msg} appeared.") unless quiet
629
- # sleep_for(0.5)
630
- # @ai.WinActivate(title, text)
631
- # if @ai.WinActive(title, text) # > 0 #Hack to prevent fail when windows session locked
632
- # debug_to_log("#{msg} activated.")
633
- # if @ai.ControlFocus(title, text, button) # > 0
634
- # controlHandle = @ai.ControlGetHandle(title, '', "[CLASS:Button; TEXT:#{button}]")
635
- # if not controlHandle
636
- # button = "&#{button}"
637
- # controlHandle = @ai.ControlGetHandle(title, '', "[CLASS:Button; TEXT:#{button}]")
638
- # end
639
- # debug_to_log("Handle for button '#{button}': [#{controlHandle}]")
640
- # debug_to_log("#{msg} focus gained.")
641
- # # sleep_for(2)
642
- # if @ai.ControlClick(title, text, button, side) # > 0
643
- # # if @ai.ControlClick(title, text, "[Handle:#{controlHandle}]", side) > 0
644
- # # debug_to_log("#{msg} #{side} click on 'Handle:#{controlHandle}'." )
645
- # debug_to_log("#{msg} #{side} click on '#{button}' successful.")
646
- # sleep_for(1)
647
- # if @ai.WinExists(title, text) > 0
648
- # debug_to_log("#{msg} close popup failed on click '#{button}'. Trying WinClose. (#{__LINE__})")
649
- # @ai.WinClose(title, text)
650
- # if @ai.WinExists(title, text) > 0
651
- # debug_to_log("#{msg} close popup failed with WinClose('#{title}','#{text}'). (#{__LINE__})")
652
- # @ai.WinKill(title, text)
653
- # if @ai.WinExists(title, text) > 0
654
- # debug_to_log("#{msg} close popup failed with WinKill('#{title}','#{text}'). (#{__LINE__})")
655
- # else
656
- # debug_to_log("#{msg} closed successfully with WinKill('#{title}','#{text}').")
657
- # end
658
- # else
659
- # debug_to_log("#{msg} closed successfully with WinClose('#{title}','#{text}').")
660
- # end
661
- # else
662
- # passed_to_log("#{msg} closed successfully.") unless quiet
663
- # end
664
- # else
665
- # failed_to_log("#{msg} #{side} click on '#{button}' failed. (#{__LINE__})")
666
- # end
667
- # else
668
- # failed_to_log("#{msg} Unable to gain focus on button (#{__LINE__})")
669
- # end
670
- # else
671
- # failed_to_log("#{msg} Unable to activate (#{__LINE__})")
672
- # end
673
- # else
674
- # failed_to_log("#{msg} did not appear after #{wait} seconds. (#{__LINE__})")
675
- # end
676
- # else
677
- # failed_to_log("#{msg} did not appear after #{wait} seconds. (#{__LINE__})")
678
- # end
679
- #rescue
680
- # failed_to_log("Close popup title=#{title} failed: '#{$!}' (#{__LINE__})")
681
- #end
682
-
683
- # Close an browser window (popup) by clicking a link within it identified by the *:text* value of the link.
684
- # @param [Watir::Browser] popup A reference to the browser window or container element to be closed.
685
- # @param [String] what Uniquely identify the *:link* element within the popup by the value in its *:text* attribute.
686
- # @param [String] desc Contains a message or description intended to appear in the log and/or report output
687
- def close_popup_by_text(popup, what = 'Close', desc = '')
688
- count = 0
689
- url = popup.url
690
- if validate(popup, @myName, __LINE__)
691
- count = string_count_in_string(popup.text, what)
692
- if count > 0
693
- begin
694
- popup.link(:text, what).click
695
- rescue => e
696
- unless rescue_me(e, __method__, rescue_me_command(:link, :text, what, :click), "#{popup.class}")
697
- raise e
698
- end
699
- end
700
- passed_to_log("Popup #{url} closed by clicking link with text '#{what}'. #{desc}")
701
- true
702
- else
703
- failed_to_log("Link :text=>'#{what}' for popup #{url} not found. #{desc}")
704
- end
705
- end
706
- rescue
707
- failed_to_log("Close popup #{url} with click link :text+>'#{what}' failed: '#{$!}' (#{__LINE__})")
708
- debug_to_log("#{strg} appears #{count} times in popup.text.")
709
- raise
710
- end
711
-
712
- # Close a modal dialog.
713
- # @param [Watir::Browser] browser A reference to the browser window or container element to be tested.
714
- # @param [String] title The title of the window to be closed. Matched from beginning of string.
715
- # @param [String] button The display name of the button to be clicked.
716
- # @param [String] text The text of the window to be closed. Matched from beginning of string.
717
- # @param [String] side A string identifying which mouse button to click.
718
- # @param [Fixnum] wait Number of seconds to wait for the popup to be seen.
719
- def close_modal(browser, title="", button="OK", text='', side = 'primary', wait = WAIT)
720
- case @targetBrowser.abbrev
721
- when 'IE'
722
- close_modal_ie(browser, title, button, text, side, wait)
723
- when 'FF'
724
- close_modal_ff(browser, title, button, text, side)
725
- when 'S'
726
- close_modal_s
727
- when 'C', 'GC'
728
- close_modal_c(browser, title)
729
- end
730
- end
731
-
732
- # TODO: Logging
733
- # Close a Chrome modal popup by :url.
734
- def close_modal_c(browser, url)
735
- browser.window(:url, url).close
736
- end
737
-
738
- # TODO: Logging
739
- # Close a Safari modal popup by closing the frontmost Safari dialog. MacOS only
740
- def close_modal_s
741
- # simply closes the frontmost Safari dialog
742
- Appscript.app("Safari").activate; Appscript.app("System Events").processes["Safari"].key_code(52)
743
- end
744
-
745
- # Close an IE modal popup by its title.
746
- # @param [Watir::Browser] browser A reference to the browser window or container element to be tested.
747
- # @param [String] title The title of the window to be closed. Matched from beginning of string.
748
- # @param [String] button The display name of the button to be clicked.
749
- # @param [String] text The text of the window to be closed. Matched from beginning of string.
750
- # @param [String] side A string identifying which mouse button to click.
751
- # @param [Fixnum] wait Number of seconds to wait for the popup to be seen.
752
- # @param [String] desc Contains a message or description intended to appear in the log and/or report output
753
- # @param [Boolean] quiet If true, fewer messages and pass/fail validations are logged.
754
- def close_modal_ie(browser, title="", button="OK", text='', side = 'primary', wait = WAIT, desc = '', quiet = false)
755
- #TODO needs simplifying, incorporating text verification, and debug code cleaned up
756
- title = translate_popup_title(title)
757
- msg = "Modal window (popup) '#{title}'"
758
- if @ai.WinWait(title, text, wait)
759
- myHandle = @ai.WinGetHandle(title, text)
760
- if myHandle.length > 0
761
- debug_to_log("hwnd: #{myHandle.inspect}")
762
- passed_to_log("#{msg} appeared.") unless quiet
763
- window_handle = "[HANDLE:#{myHandle}]"
764
- sleep_for(0.5)
765
- @ai.WinActivate(window_handle)
766
- if @ai.WinActive(window_handle)
767
- debug_to_log("#{msg} activated.")
768
- controlHandle = @ai.ControlGetHandle(title, '', "[CLASS:Button; TEXT:#{button}]")
769
- if not controlHandle.length > 0
770
- button = "&#{button}"
771
- controlHandle = @ai.ControlGetHandle(title, '', "[CLASS:Button; TEXT:#{button}]")
772
- end
773
- debug_to_log("Handle for button '#{button}': [#{controlHandle}]")
774
- debug_to_log("#{msg} focus gained.")
775
- if @ai.ControlClick(title, '', "[CLASS:Button; TEXT:#{button}]")
776
- passed_to_log("#{msg} #{side} click on '[CLASS:Button; TEXT:#{button}]' successful.")
777
- sleep_for(0.5)
778
- if @ai.WinExists(window_handle)
779
- debug_to_log("#{msg} close popup failed on click '#{button}'. Trying WinClose. (#{__LINE__})")
780
- @ai.WinClose(title, text)
781
- if @ai.WinExists(window_handle)
782
- debug_to_log("#{msg} close popup failed with WinClose(#{window_handle}). (#{__LINE__})")
783
- @ai.WinKill(window_handle)
784
- if @ai.WinExists(window_handle)
785
- debug_to_log("#{msg} close popup failed with WinKill(#{window_handle}). (#{__LINE__})")
786
- else
787
- debug_to_log("#{msg} closed successfully with WinKill(#{window_handle}).")
788
- end
789
- else
790
- debug_to_log("#{msg} closed successfully with WinClose(#{window_handle}).")
791
- end
792
- else
793
- passed_to_log("#{msg} closed successfully.")
794
- end
795
- else
796
- failed_to_log("#{msg} #{side} click on '[CLASS:Button; TEXT:#{button}]' failed. (#{window_handle}) (#{__LINE__})")
797
- end
798
- else
799
- failed_to_log("#{msg} Unable to activate (#{window_handle}) (#{__LINE__})")
800
- end
801
- else
802
- failed_to_log("#{msg} did not appear after #{wait} seconds. (#{window_handle}) (#{__LINE__})")
803
- end
804
- else
805
- failed_to_log("#{msg} did not appear after #{wait} seconds.(#{window_handle}) (#{__LINE__})")
806
- end
807
- rescue
808
- failed_to_log("Close popup title=#{title} failed: '#{$!}' (#{__LINE__})")
809
- end
810
-
811
- #alias close_popup_validate_text close_modal_ie
812
- #alias close_popup close_modal_ie
813
-
814
- # private :close_modal_ie
815
-
816
- # Close an IE modal popup by its title. Calls close_modal_ie.
817
- # @deprecated Use close_modal.
818
- # @param [String] title The title of the window to be closed. Matched from beginning of string.
819
- # @param [String] button The display name of the button to be clicked.
820
- # @param [String] text The text of the window to be closed. Matched from beginning of string.
821
- # @param [String] side A string identifying which mouse button to click.
822
- # @param [Fixnum] wait Number of seconds to wait for the popup to be seen.
823
- # @param [String] desc Contains a message or description intended to appear in the log and/or report output
824
- # @param [Boolean] quiet If true, fewer messages and pass/fail validations are logged.
825
- def close_popup(title = '', button = 'OK', text = '', side = 'primary',
826
- wait = WAIT, desc = '', quiet = false)
827
- debug_to_log("#{__method__} begin")
828
- close_modal_ie(@myBrowser, title, button, text, side, wait, desc, quiet)
829
- end
830
-
831
- alias close_popup_validate_text close_popup
832
-
833
- # Close a Firefox modal popup by its title.
834
- # @param [Watir::Browser] browser A reference to the browser window or container element to be tested.
835
- # @param [String] title The title of the window to be closed. Matched from beginning of string.
836
- # @param [String] button The display name of the button to be clicked.
837
- # @param [String] text The text of the window to be closed. Matched from beginning of string.
838
- # @param [String] side A string identifying which mouse button to click.
839
- # @return [Boolean] True if the modal is successfully closed.
840
- def close_modal_ff(browser, title="", button=nil, text='', side='')
841
- title = translate_popup_title(title)
842
- msg = "Modal dialog (popup): title=#{title} button='#{button}' text='#{text}' side='#{side}':"
843
- modal = browser.modal_dialog(:timeout => WAIT)
844
- if modal.exists?
845
- modal_text = modal.text
846
- if text.length > 0
847
- if modal_text =~ /#{text}/
848
- passed_to_log("#{msg} appeared with match on '#{text}'.")
849
- else
850
- failed_to_log("#{msg} appeared but did not match '#{text}' ('#{modal_text}).")
851
- end
852
- else
853
- passed_to_log("#{msg} appeared.")
854
- end
855
- if button
856
- modal.click_button(button)
857
- else
858
- modal.close
859
- end
860
- if modal.exists?
861
- failed_to_log("#{msg} close failed. (#{__LINE__})")
862
- else
863
- passed_to_log("#{msg} closed successfully.")
864
- true
865
- end
866
- else
867
- failed_to_log("#{msg} did not appear after #{WAIT} seconds. (#{__LINE__})")
868
- end
869
- rescue
870
- failed_to_log("#{msg} Unable to validate modal popup: '#{$!}'. (#{__LINE__})")
871
- end
872
-
873
- # Wait for a modal popup to appear and then close it.
874
- # Used when modal popup in response to browser action is intermittent or unpredictable.
875
- # @param [String] title The title of the window to be closed. Matched from beginning of string.
876
- # @param [String] text The text of the window to be closed. Matched from beginning of string.
877
- # @param [String] button The display name of the button to be clicked.
878
- # @param [String] side A string identifying which mouse button to click.
879
- # @param [Fixnum] wait Number of seconds to wait for the popup to be seen.
880
- # @param [String] desc Contains a message or description intended to appear in the log and/or report output
881
- # @return [Boolean] True if the modal is successfully closed.
882
- def handle_popup(title, text = '', button= 'OK', side = 'primary', wait = WAIT, desc = '')
883
- title = translate_popup_title(title)
884
- msg = "'#{title}'"
885
- if text.length > 0
886
- msg << " with text '#{text}'"
887
- end
888
- @ai.Opt("WinSearchChildren", 1) # match title from start, forcing default
889
-
890
- if button and button.length > 0
891
- if button =~ /ok|yes/i
892
- id = '1'
893
- else
894
- id = '2'
895
- end
896
- else
897
- id = ''
898
- end
899
-
900
- if @ai.WinWait(title, '', wait) > 0
901
- myHandle = @ai.WinGetHandle(title, '')
902
- window_handle = "[HANDLE:#{myHandle}]"
903
- full_text = @ai.WinGetText(window_handle)
904
- debug_to_log("Found popup handle:'#{myHandle}', title:'#{title}', text:'#{full_text}'")
905
-
906
- controlHandle = @ai.ControlGetHandle(window_handle, '', "[CLASS:Button; TEXT:#{button}]")
907
- if not controlHandle
908
- # button = "&#{button}"
909
- controlHandle = @ai.ControlGetHandle(window_handle, '', "[CLASS:Button; TEXT:&#{button}]")
910
- end
911
-
912
- if text.length > 0
913
- if full_text =~ /#{text}/
914
- passed_to_log("Found popup handle:'#{myHandle}', title:'#{title}', text includes '#{text}'. #{desc}")
915
- else
916
- failed_to_log("Found popup handle:'#{myHandle}', title:'#{title}', text does not include '#{text}'. Closing it. #{desc}")
917
- end
918
- end
919
-
920
- @ai.WinActivate(window_handle, '')
921
- @ai.ControlClick(window_handle, '', id, side)
922
- if @ai.WinExists(title, '') > 0
923
- debug_to_log("#{msg} @ai.ControlClick on '#{button}' (ID:#{id}) with handle '#{window_handle}' failed to close window. Trying title.")
924
- @ai.ControlClick(title, '', id, side)
925
- if @ai.WinExists(title, '') > 0
926
- debug_to_report("#{msg} @ai.ControlClick on '#{button}' (ID:#{id}) with title '#{title}' failed to close window. Forcing closed.")
927
- @ai.WinClose(title, '')
928
- if @ai.WinExists(title, '') > 0
929
- debug_to_report("#{msg} @ai.WinClose on title '#{title}' failed to close window. Killing window.")
930
- @ai.WinKill(title, '')
931
- if @ai.WinExists(title, '') > 0
932
- failed_to_log("#{msg} @ai.WinKill on title '#{title}' failed to close window")
933
- else
934
- passed_to_log("Killed: popup handle:'#{myHandle}', title:'#{title}'. #{desc}")
935
- true
936
- end
937
- else
938
- passed_to_log("Forced closed: popup handle:'#{myHandle}', title:'#{title}'. #{desc}")
939
- true
940
- end
941
- else
942
- passed_to_log("Closed on '#{button}': popup handle:'#{myHandle}', title:'#{title}'. #{desc}")
943
- true
944
- end
945
- else
946
- passed_to_log("Closed on '#{button}': popup handle:'#{myHandle}', title:'#{title}'. #{desc}")
947
- true
948
- end
949
-
950
- else
951
- failed_to_log("#{msg} did not appear after #{wait} seconds. #{desc} (#{__LINE__})")
952
- end
953
- rescue
954
- failed_to_log("Unable to handle popup #{msg}: '#{$!}' #{desc} (#{__LINE__})")
955
-
956
- end
957
-
958
- # Return a reference to an IE browser window based on one of its attributes.
959
- # @param [Watir::Browser] browser A reference to the browser window or container element to be tested.
960
- # @param [Symbol] how The element attribute used to identify the window: :url, :title, or :hwnd.
961
- # @param [String, Regexp] what A string or a regular expression to be found in the *how* attribute that uniquely identifies the element.
962
- # @param [String] desc Contains a message or description intended to appear in the log and/or report output
963
- # @return [Watir::IE] A reference to the popup.
964
- def find_popup(browser, how, what, desc = '')
965
- msg = "Find popup :#{how}=>'#{what}'. #{desc}"
966
- popup = Watir::IE.find(how, what) # TODO: too browser specific
967
- sleep_for(1)
968
- debug_to_log("#{popup.inspect}")
969
- if is_browser?(popup)
970
- # title = popup.title
971
- passed_to_log(msg)
972
- return popup
973
- else
974
- failed_to_log(msg)
975
- end
976
- rescue
977
- failed_to_log("Unable to find popup :#{how}=>'#{what}'. #{desc} '#{$!}' (#{__LINE__})")
978
- end
979
-
980
- # Confirm that the object passed in *browser* is actually a Browser object.
981
- # @param [Watir::Browser] browser A reference to the window or container element to be tested.
982
- def is_browser?(browser)
983
- myClass = browser.class.to_s
984
- case @targetBrowser.abbrev
985
- when 'IE'
986
- myClass =~ /Watir::IE|Watir::Browser/i
987
- when 'FF'
988
- myClass =~ /Watir::Browser/i
989
- when 'S'
990
- myClass =~ /Watir::Browser/i
991
- when 'C'
992
- myClass =~ /Watir::Browser/i
993
- end
994
- end
995
-
996
- alias is_browser is_browser?
997
-
998
- # Translate window title supplied in *title* to a title appropriate for the targeted browser and version
999
- # actually being run.
1000
- # Used primarily for handling of modal popups and dialogs.
1001
- # This allows cross-browser compatibility for handling modal popups and other windows accessed by titlt.
1002
- # @param [String] title The title of the window to be closed.
1003
- def translate_popup_title(title)
1004
- new_title = title
1005
- case @browserAbbrev
1006
- when 'IE'
1007
- if @browserVersion
1008
- case @browserVersion
1009
- when '8.0'
1010
- case title
1011
- when "Microsoft Internet Explorer"
1012
- new_title = "Message from webpage"
1013
- when "The page at"
1014
- new_title = "Message from webpage"
1015
- end
1016
- when '7.0'
1017
- case title
1018
- when "Message from webpage"
1019
- new_title = "Microsoft Internet Explorer"
1020
- when "The page at"
1021
- new_title = "Windows Internet Explorer"
1022
- end
1023
- when '6.0'
1024
- case title
1025
- when "Message from webpage"
1026
- new_title = "Microsoft Internet Explorer"
1027
- when "The page at"
1028
- new_title = "Microsoft Internet Explorer"
1029
- end
1030
- else
1031
- case title
1032
- when "Microsoft Internet Explorer"
1033
- new_title = "Message from webpage"
1034
- when "The page at"
1035
- new_title = "Message from webpage"
1036
- end
1037
- end
1038
- else
1039
- case title
1040
- when "Microsoft Internet Explorer"
1041
- new_title = "Message from webpage"
1042
- when "The page at"
1043
- new_title = "Message from webpage"
1044
- end
1045
- end
1046
- when 'FF'
1047
- case title
1048
- when 'File Download'
1049
- new_title = 'Opening'
1050
- when "Microsoft Internet Explorer"
1051
- new_title = 'The page at'
1052
- when "Message from webpage"
1053
- new_title = 'The page at'
1054
- end
1055
- when 'C'
1056
- case title
1057
- when 'File Download'
1058
- new_title = 'Save As'
1059
- when "Microsoft Internet Explorer"
1060
- new_title = 'The page at'
1061
- when "Message from webpage"
1062
- new_title = 'The page at'
1063
- end
1064
- end
1065
- new_title
1066
- end
1067
-
1068
- # Identify the exact version of the Browser currently being executed.
1069
- # @todo Bring up to date with newer browser versions
1070
- # @param [Watir::Browser] browser A reference to the browser window or container element to be tested.
1071
- def get_browser_version(browser)
1072
- debug_to_log("starting get_browser_version")
1073
- case @targetBrowser.abbrev
1074
- when 'IE'
1075
- @browserAbbrev = 'IE'
1076
- @browserName = 'Internet Explorer'
1077
- if $watir_script
1078
- @browserAppInfo = browser.document.invoke('parentWindow').navigator.appVersion
1079
- else
1080
- @browserAppInfo = browser.execute_script("return navigator.userAgent;")
1081
- end
1082
- @browserAppInfo =~ /MSIE\s(.*?);/
1083
- @browserVersion = $1
1084
- when 'FF'
1085
- @browserAbbrev = 'FF'
1086
- @browserName = 'Firefox'
1087
- @browserVersion = '6.01' #TODO: get actual version from browser
1088
- @browserAppInfo = browser.execute_script("return navigator.userAgent;")
1089
- debug_to_log("#{@browserName}, @browserAppInfo: (#{@browserAppInfo})")
1090
- when 'S'
1091
- @browserAbbrev = 'S'
1092
- @browserName = 'Safari'
1093
- @browserVersion = '5.0.4' #TODO: get actual version from browser itself
1094
- @browserAppInfo = browser.execute_script("return navigator.userAgent;")
1095
- debug_to_log("#{@browserName}, @browserAppInfo: (#{@browserAppInfo})")
1096
- when 'C'
1097
- @browserAbbrev = 'C'
1098
- @browserName = 'Chrome'
1099
- @browserVersion = '11.0' #TODO: get actual version from browser
1100
- @browserAppInfo = browser.execute_script("return navigator.userAgent;")
1101
- debug_to_log("#{@browserName}, @browserAppInfo: (#{@browserAppInfo})")
1102
- end
1103
- rescue
1104
- debug_to_log("Unable to determine #{@browserAbbrev} browser version: '#{$!}' (#{__LINE__})")
1105
- ensure
1106
- message_to_log("Browser: [#{@browserAbbrev} #{@browserVersion}]")
1107
- end
1108
-
1109
- protected :get_browser_version
1110
-
1111
- #def filter_bailout_from_rescue(err, msg)
1112
- # if msg =~ /bailing out/i
1113
- # raise err
1114
- # else
1115
- # error_to_log(msg)
1116
- # end
1117
- #end
1118
-
1119
- # @!group Browser
1120
-
1121
- # Open and attach a browser popup window where the link to open it and its title contain the same string.
1122
- # @deprecated
1123
- def open_popup_through_link_title(browser, title, pattern, name)
1124
- click_title(browser, title)
1125
- #TODO need some kind of wait for process here
1126
- sleep_for 2
1127
- attach_popup_by_url(browser, pattern, name)
1128
- rescue
1129
- failed_to_log("Unable to open popup '#{name}': '#{$!}' (#{__LINE__})")
1130
- end
1131
-
1132
- # @!group Error Handling
1133
-
1134
- # Verifies health of the browser. Looks for common http and system errors that are unrecoverable and
1135
- # attempts to gracefully bail out of the script.
1136
- # Calls rescue_me() when trying to capture the text to filter out known false errors
1137
- # and handle container elements that don't respond to the .text method.
1138
- # @param [Watir::Browser] browser A reference to the browser window or container element to be tested.
1139
- # @param [String] file_name The file name of the executing script.
1140
- # @param [Fixnum] lnbr Contains a message or description intended to appear in the log and/or report output
1141
- # @param [Boolean] dbg If set to true additional debug messages are written to the log.
1142
- #
1143
- # @return [Boolean] True if no error conditions have been encountered.
1144
- def validate(browser, file_name = @myName, lnbr = "#{__LINE__}", dbg = false)
1145
- debug_to_log("#{__method__} begin") if dbg
1146
- msg = ''
1147
- myOK = true
1148
- if not browser
1149
- msg = "#{file_name}----browser is nil object. (#{lnbr})"
1150
- myOK = false
1151
- elsif not browser.class.to_s =~ /Watir/
1152
- msg = "#{file_name}----not a Watir object. (#{lnbr})"
1153
- debug_to_log(browser.inspect)
1154
- myOK = false
1155
-
1156
- else
1157
- if browser.respond_to?(:url)
1158
- if not browser.url == @currentURL
1159
- @currentURL = browser.url
1160
- debug_to_log("Current URL: [#{@currentURL}]")
1161
- # mark_testlevel( "Current URL: [#{@currentURL}]", 1 )
1162
- end
1163
- end
1164
-
1165
- if @capture_js_errors
1166
- if browser.respond_to?(:status)
1167
- if browser.status.downcase =~ /errors? on page/ and
1168
- not browser.status.downcase.include?('Waiting for')
1169
- capture_js_error(browser)
1170
- end
1171
- end
1172
- end
1173
-
1174
- begin
1175
- browser_text = browser.text.downcase
1176
- rescue => e
1177
- unless rescue_me(e, __method__, "browser.text.downcase", "#{browser.class}", browser)
1178
- debug_to_log("browser.text.downcase in #{__method__} #{browser.class}")
1179
- debug_to_log("#{get_callers}")
1180
- raise e
1181
- else
1182
- return true
1183
- end
1184
- end
1185
-
1186
- if browser_text
1187
- if browser_text.match(/unrecognized error condition has occurred/i)
1188
- msg = "#{file_name}----Unrecognized Exception occurred. (#{lnbr})"
1189
- myOK = false
1190
-
1191
- elsif browser_text.match(/cannot find server or dns error/i)
1192
- msg = "#{file_name}----Cannot find server error or DNS error. (#{lnbr})"
1193
- myOK = false
1194
-
1195
- elsif browser_text.match(/the rpc server is unavailable/i)
1196
- msg = "#{file_name}----RPC server unavailable. (#{lnbr})"
1197
- myOK = false
1198
-
1199
- elsif browser_text.match(/404 not found/i) or
1200
- browser_text.match(/the page you were looking for does\s*n[o']t exist/i)
1201
- msg = "#{file_name}----RFC 2068 HTTP/1.1: 404 URI Not Found. (#{lnbr})"
1202
- myOK = false
1203
-
1204
- elsif browser_text.match(/we're sorry, but something went wrong/i) or
1205
- browser_text.match(/http status 500/i)
1206
- msg = "#{file_name}----RFC 2068 HTTP/1.1: 500 Internal Server Error. (#{lnbr})"
1207
- myOK = false
1208
-
1209
- elsif browser_text.match(/internet explorer cannot display the webpage/i)
1210
- msg = "#{file_name}----Probably RFC 2068 HTTP/1.1: 500 Internal Server Error. (#{lnbr})"
1211
- myOK = false
1212
-
1213
- elsif browser_text.match(/503.*service unavailable/i)
1214
- msg = "#{file_name}----RFC 2068 HTTP/1.1: 503 Service Unavailable. (#{lnbr})"
1215
- myOK = false
1216
-
1217
- elsif browser_text.match(/java.lang.NullPointerException/i)
1218
- msg = "#{file_name}----java.lang.NullPointerException. (#{lnbr})"
1219
- myOK = false
1220
-
1221
- elsif browser_text.match(/due to unscheduled maintenance/i)
1222
- msg = "#{file_name}----Due to unscheduled maintenance. (#{lnbr})"
1223
- myOK = false
1224
-
1225
- elsif browser_text.match(/network\s+error\s*(.+)$/i)
1226
- $1.chomp!
1227
- msg = "#{file_name}----Network Error #{$1}. (#{lnbr})"
1228
- myOK = false
1229
-
1230
- elsif browser_text.match(/warning: page has expired/i)
1231
- msg = "#{file_name}----Page using information from form has expired. Not automatically resubmitted. (#{lnbr})"
1232
- myOK = false
1233
-
1234
- elsif browser_text.match(/no backend server available/i)
1235
- msg = "#{file_name}----Cannot Reach Server (#{lnbr})"
1236
- myOK = false
1237
-
1238
- elsif browser_text.match(/sign on\s+.+\s+unsuccessful/i)
1239
- msg = "#{file_name}----Invalid Id or Password (#{lnbr})"
1240
- myOK = false
1241
-
1242
- elsif browser_text.match(/you are not authorized/i)
1243
- msg = "#{file_name}----Not authorized to view this page. (#{lnbr})"
1244
- myOK = false
1245
-
1246
- elsif browser_text.match(/too many incorrect login attempts have been made/i)
1247
- msg = "#{file_name}----Invalid Id or Password. Too many tries. (#{lnbr})"
1248
- myOK = false
1249
-
1250
- elsif browser_text.match(/system error\.\s+an error has occurred/i)
1251
- msg = "#{file_name}----System Error. An error has occurred. Please try again or call the Help Line for assistance. (#{lnbr})"
1252
- myOK = false
1253
-
1254
- elsif browser_text.match(/Internal Server failure,\s+NSAPI plugin/i)
1255
- msg = "#{file_name}----Internal Server failure, NSAPI plugin. (#{lnbr})"
1256
- myOK = false
1257
-
1258
- elsif browser_text.match(/Error Page/i)
1259
- msg = "#{file_name}----Error Page. (#{lnbr})"
1260
- myOK = false
1261
-
1262
- elsif browser_text.match(/The website cannot display the page/i)
1263
- msg = "#{file_name}----HTTP 500. (#{lnbr})"
1264
- myOK = false
1265
-
1266
- # elsif browser_text.match(/Insufficient Data/i)
1267
- # msg = "#{file_name}----Insufficient Data. (#{lnbr})"
1268
- # myOK = false
1269
-
1270
- elsif browser_text.match(/The timeout period elapsed/i)
1271
- msg = "#{file_name}----Time out period elapsed or server not responding. (#{lnbr})"
1272
- myOK = false
1273
-
1274
- elsif browser_text.match(/Unexpected\s+errors*\s+occur+ed\.\s+(?:-+)\s+(.+)/i)
1275
- msg = "#{file_name}----Unexpected errors occurred. #{$2.slice(0, 120)} (#{lnbr})"
1276
- if not browser_text.match(/close the window and try again/i)
1277
- myOK = false
1278
- else
1279
- debug_to_log("#{msg}")
1280
- end
1281
-
1282
- elsif browser_text.match(/Server Error in (.+) Application\.\s+(?:-+)\s+(.+)/i)
1283
- msg = "#{file_name}----Server Error in #{1} Application. #{$2.slice(0, 100)} (#{lnbr})"
1284
- myOK = false
1285
-
1286
- elsif browser_text.match(/Server Error in (.+) Application\./i)
1287
- msg = "#{file_name}----Server Error in #{1} Application. '#{browser_text.slice(0, 250)}...' (#{lnbr})"
1288
- myOK = false
1289
-
1290
- elsif browser_text.match(/An error has occur+ed\. Please contact support/i)
1291
- msg = "#{file_name}----An error has occurred. Please contact support (#{lnbr})"
1292
- myOK = false
1293
-
1294
- end
1295
- else
1296
- debug_to_log("browser.text returned nil")
1297
- end
1298
- end
1299
-
1300
- if not myOK
1301
- msg << " (#{browser.url})"
1302
- puts msg
1303
- debug_to_log(browser.inspect)
1304
- debug_to_log(browser.text)
1305
- fatal_to_log(msg, lnbr)
1306
- raise(RuntimeError, msg, caller)
1307
- else
1308
- debug_to_log("#{__method__} returning OK") if dbg
1309
- return myOK
1310
- end
1311
-
1312
- rescue
1313
- errmsg = $!
1314
- if errmsg and errmsg.match(msg)
1315
- errmsg = ''
1316
- end
1317
- bail_out(browser, lnbr, "#{msg} #{errmsg}")
1318
- end
1319
-
1320
- alias validate_browser validate
1321
-
1322
- # @!endgroup Error Handling
1323
-
1324
- # @!group Backward compatible usages
1325
-
1326
- # Returns a reference to a browser window using the window's *:url* attribute. Calls attach_browser().
1327
- # @example
1328
- # mainwindow = open_browser('www.google.com')
1329
- # click(mainwindow, :button, :id, 'an id string') # click a button that opens another browser window
1330
- # popup = attach_browser_by_url(mainwindow, '[url of new window]')
1331
- # @param [Watir::Browser] browser A reference to the current browser window.
1332
- # @param [String, Regexp] what The value in the targeted attribute that uniquely identifies the new window
1333
- # @param [String] desc Contains a message or description intended to appear in the log and/or report output
1334
- # @return [Watir::Browser]
1335
- def attach_browser_by_url(browser, what, desc = '')
1336
- attach_browser(browser, :url, what, desc)
1337
- end
1338
-
1339
- alias attach_browser_with_url attach_browser_by_url
1340
-
1341
- # Returns a reference to a new browser window identified by its *:title* attribute. Used to attach a new browser window to a variable
1342
- # which can then be passed to methods that require a *browser* parameter. Calls attach_browser().
1343
- # @param (see #attach_browser_by_url)
1344
- def attach_popup_by_title(browser, what, desc = '')
1345
- attach_popup(browser, :title, what, desc)
1346
- end
1347
-
1348
- # Returns a reference to a new browser window identified by its *:url* attribute. Used to attach a new browser window to a variable
1349
- # which can then be passed to methods that require a *browser* parameter. Calls attach_browser().
1350
- # @param (see #attach_browser_by_url)
1351
- def attach_popup_by_url(browser, what, desc = '')
1352
- attach_popup(browser, :url, what, desc)
1353
- end
1354
-
1355
- alias get_popup_with_url attach_popup_by_url
1356
- alias attach_popup_with_url attach_popup_by_url
1357
- alias attach_iepopup attach_popup_by_url
1358
-
1359
- # Close a popup browser window (non-modal) by clicking on a link with :title *what*.
1360
- # This method does not check to make sure the popup is actually closed.
1361
- # @param [Watir::Browser] popup A reference to the current popup browser window.
1362
- # @param [String, Regexp] what The value in the targeted attribute that uniquely identifies the new window
1363
- # @param [String] desc Contains a message or description intended to appear in the log and/or report output
1364
- # @return [Boolean] True if the click is successful.
1365
- def close_popup_by_button_title(popup, what, desc = '')
1366
- click(popup, :link, :title, what, desc)
1367
- end
1368
-
1369
- # @!endgroup Backward
1370
-
1371
-
1372
- end
1373
- end
1374
- end
1375
-
1
+ module Awetestlib
2
+ # Awetest DSL for browser based testing.
3
+ module Regression
4
+ # Methods to manage browser windows: open, close, attach, verify health, and clean up.
5
+ module Browser
6
+
7
+ #def run #DO WE NEED? Use this method to tell user they need to create a run method?
8
+ ## Not here in any case.
9
+ # setup
10
+ # set_script_variables
11
+ # run_test
12
+ #rescue
13
+ # fatal_to_log("(#{__LINE__}) #{$!}")
14
+ # browser.close
15
+ # raise
16
+ #end
17
+
18
+ # @!group Browser
19
+
20
+ # @note webdriver specific - still work in progress
21
+ def go_to_wd_url(browser, url)
22
+
23
+ Watir::Browser.class_eval do
24
+ def goto(uri)
25
+ uri = "http://#{uri}" unless uri =~ URI.regexp
26
+ @driver.navigate.to uri
27
+ run_checkers
28
+ end
29
+ end
30
+ browser.goto(url)
31
+
32
+ #in basic_auth1 edit:
33
+ #a = Thread.new {
34
+ # goto_wd_url(browser, @myURL)
35
+ # }
36
+
37
+ end
38
+
39
+ alias goto_wd_url go_to_wd_url
40
+
41
+ # Open a browser based on the command line parameters that identify the browser and
42
+ # version to use for the test.
43
+ # @example
44
+ # browser = open_browser('www.google.com')
45
+ # @param [String, Regexp] url When provided, the browser will go to this url.
46
+ # @return [Watir::Browser]
47
+ def open_browser(url = nil)
48
+ message_to_report("Opening browser: #{@targetBrowser.name}")
49
+ case @targetBrowser.abbrev
50
+ when 'IE'
51
+ @myBrowser = open_ie
52
+ if @myBrowser.class.to_s == "Watir::IE"
53
+ @myHwnd = @myBrowser.hwnd
54
+ end
55
+ when 'FF'
56
+ @myBrowser = open_ff_for_version
57
+ when 'S'
58
+ aBrowser = Watir::Browser.new :safari
59
+ @myBrowser = aBrowser
60
+ when 'C', 'GC'
61
+ @myBrowser = open_chrome
62
+ else
63
+ raise "Unsupported browser: #{@targetBrowser.name}"
64
+ end
65
+ if url
66
+ go_to_url(@myBrowser, url)
67
+ end
68
+ @myBrowser
69
+ end
70
+
71
+ # Open IE (Internet Explorer) browser instance.
72
+ # If global variable $watir_script is set to true in the first line of the script,
73
+ # classic Watir will be used to drive the browser,
74
+ # otherwise Watir Webdriver will be used.
75
+ # @return [Watir::Browser]
76
+ def open_ie
77
+ if $watir_script
78
+ browser = Watir::IE.new
79
+ else
80
+ browser = Watir::Browser.new :ie
81
+ end
82
+ browser
83
+ end
84
+
85
+ # Open FF (Firefox) browser instance.
86
+ # @param [Fixnum] version A number designating the version of the browser to be opened.
87
+ # @return [Watir::Browser, Firewatir::Browser]
88
+ # Returns Firewatir::Browser if target browser is Firefox version less than 4.0
89
+ def open_ff_for_version(version = @targetVersion)
90
+ browser = Watir::Browser.new :firefox
91
+ end
92
+
93
+ # Open FF (Firefox) browser instance under FireWatir.
94
+ # @return [Firewatir::Browser]
95
+ def open_ff
96
+ # Watir::Browser.default = 'firefox'
97
+ browser = Watir::Browser.new :firefox
98
+ end
99
+
100
+ # Open GC (Google Chrome) browser instance.
101
+ # @return [Watir::Browser] Browser is Google Chrome.
102
+ def open_chrome
103
+ browser = Watir::Browser.new(:chrome)
104
+ end
105
+
106
+ # Instruct browser to navigate to a specific URL
107
+ # @param [Watir::Browser] browser A reference to the browser window or container element to be tested.
108
+ # @param [String] url When provided, the browser will go to this url.
109
+ # and the instance variable @myURL will be set to this value.
110
+ # @return [Boolean] True when navigation to url succeeds.
111
+ def go_to_url(browser, url = nil)
112
+ if url
113
+ @myURL = url
114
+ end
115
+ message_to_report("URL: #{@myURL}")
116
+ browser.goto(@myURL)
117
+ true
118
+ rescue
119
+ fatal_to_log("Unable to navigate to '#{@myURL}': '#{$!}'")
120
+ end
121
+
122
+ # Return a reference to a browser window. Used to attach a browser window to a variable
123
+ # which can then be passed to methods that require a *browser* parameter.
124
+ # @example
125
+ # mainwindow = open_browser('www.google.com')
126
+ # click(mainwindow, :button, :id, 'an id string') # click a button that opens another browser window
127
+ # popup = attach_browser(mainwindow, :url, '[url of new window]') #*or*
128
+ # popup = attach_browser(mainwindow, :title, '[title of new window]')
129
+ # @todo Update to work with webdriver for IE.
130
+ # @param [Watir::Browser] browser A reference to the current browser window.
131
+ # @param [Symbol] how The element attribute used to identify the window: *:title* or :url.
132
+ # @param [String|Regexp] what A string or a regular expression to be found in the *how* attribute that uniquely identifies the element.
133
+ # @param [String] desc Contains a message or description intended to appear in the log and/or report output
134
+ # @return [Watir::Browser] Internet Explorer
135
+ def attach_browser(browser, how, what, desc = '')
136
+ debug_to_log("Attaching browser window :#{how}=>'#{what}' #{desc}")
137
+ uri_decoded_pattern = ::URI.encode(what.to_s.gsub('(?-mix:', '').gsub(')', ''))
138
+
139
+ if $watir_script
140
+ tmpbrowser = Watir::IE.attach(how, what)
141
+ browser.visible = true
142
+ if tmpbrowser
143
+ tmpbrowser.visible = true
144
+ tmpbrowser.speed = :fast
145
+ else
146
+ raise "Browser window :#{how}=>'#{what}' has at least one doc not in completed ready state."
147
+ end
148
+ else
149
+ browser.driver.switch_to.window(browser.driver.window_handles[0])
150
+ browser.window(how, /#{uri_decoded_pattern}/).use
151
+ tmpbrowser = browser
152
+ end
153
+
154
+ # case @browserAbbrev
155
+ # when 'IE'
156
+ # tmpbrowser = Watir::IE.attach(how, what)
157
+ # browser.visible = true
158
+ # if tmpbrowser
159
+ # tmpbrowser.visible = true
160
+ # tmpbrowser.speed = :fast
161
+ # else
162
+ # raise "Browser window :#{how}=>'#{what}' has at least one doc not in completed ready state."
163
+ # end
164
+ # when 'FF'
165
+ # #TODO: This may be dependent on Firefox version if webdriver doesn't support 3.6.17 and below
166
+ # browser.driver.switch_to.window(browser.driver.window_handles[0])
167
+ # browser.window(how, /#{uri_decoded_pattern}/).use
168
+ # tmpbrowser = browser
169
+ # when 'S'
170
+ # Watir::Safari.attach(how, what)
171
+ # tmpbrowser = browser
172
+ # when 'C', 'GC'
173
+ # browser.window(how, /#{uri_decoded_pattern}/).use
174
+ # tmpbrowser = browser
175
+ # end
176
+
177
+
178
+ debug_to_log("#{__method__}: tmpbrowser:#{tmpbrowser.inspect}")
179
+ tmpbrowser
180
+ end
181
+
182
+
183
+ # Returns a reference to a new browser window. Used to attach a new browser window to a variable
184
+ # which can then be passed to methods that require a *browser* parameter. Calls attach_browser().
185
+ # @example
186
+ # mainwindow = open_browser('www.google.com')
187
+ # click(mainwindow, :button, :id, 'an id string') # click a button that opens another browser window
188
+ # popup = attach_popup(mainwindow, :url, '[url of new window]') *or*
189
+ # popup = attach_popup(mainwindow, :title, '[title of new window]')
190
+ # @param [Watir::Browser] browser A reference to the current browser window.
191
+ # @param [Symbol] how The element attribute used to identify the window: *:title* or :url.
192
+ # @param [String, Regexp] what A string or a regular expression to be found in the *how* attribute that uniquely identifies the element.
193
+ # @param [String] desc Contains a message or description intended to appear in the log and/or report output
194
+ # @return [Watir::Browser] The new browser window.
195
+ def attach_popup(browser, how, what, desc = '')
196
+ msg = "Attach popup :#{how}=>'#{what}'. #{desc}"
197
+ popup = attach_browser(browser, how, what, desc)
198
+ sleep_for(1)
199
+ debug_to_log("#{popup.inspect}")
200
+ if is_browser?(popup)
201
+ title = popup.title
202
+ passed_to_log("#{msg} title='#{title}'")
203
+ return popup
204
+ else
205
+ failed_to_log(msg)
206
+ end
207
+ rescue
208
+ failed_to_log("Unable to attach popup :#{how}=>'#{what}'. #{desc} '#{$!}' (#{__LINE__})")
209
+ end
210
+
211
+ # Locate and close instances of IE browsers
212
+ def find_other_browsers
213
+ cnt = 0
214
+ if @targetBrowser.abbrev == 'IE'
215
+ Watir::IE.each do |ie|
216
+ debug_to_log("#{ie.inspect}")
217
+ ie.close()
218
+ cnt = cnt + 1
219
+ end
220
+ end
221
+ debug_to_log("Found #{cnt} IE browser(s).")
222
+ return cnt
223
+ rescue
224
+ error_to_log("#{$!} (#{__LINE__})\n#{Kernel.caller.to_yaml}", __LINE__)
225
+ return 0
226
+ end
227
+
228
+ # @!endgroup Browser
229
+
230
+ # @!group Login
231
+
232
+ # Simple login method
233
+ # @param [Watir::Browser] browser A reference to the browser window or container element to be tested.
234
+ # @param [String] user Login ID for the user.
235
+ # @param [String] password Password for the user.
236
+ # @return [Boolean] True if login succeeds.
237
+ def login(browser, user, password)
238
+ #TODO: Needs to be more flexible about finding login id and password textfields
239
+ #TODO: Parameterize url and remove references to environment
240
+ myURL = @myAppEnv.url
241
+ runenv = @myAppEnv.nodename
242
+ message_tolog("URL: #{myURL}") if @myAppEnv
243
+ message_tolog("Beginning login: User: #{user} Environment: #{@myAppEnv.nodename}") if @myAppEnv
244
+ if validate(browser, @myName, __LINE__)
245
+ browser.goto(@myAppEnv.url)
246
+ if validate(browser, @myName)
247
+ set_textfield_by_name(browser, 'loginId', user)
248
+ set_textfield_by_name(browser, 'password', password)
249
+ click_button_by_value(browser, 'Login')
250
+ if validate(browser, @myName)
251
+ passed_to_log("Login successful.")
252
+ true
253
+ end
254
+ else
255
+ failed_to_log("Unable to login to application: '#{$!}'")
256
+ #screen_capture( "#{@myRoot}/screens/#{myName}_#{@runid}_#{__LINE__.to_s}_#{Time.new.to_f.to_s}.jpg")
257
+ end
258
+ end
259
+ rescue
260
+ failed_to_log("Unable to login to application: '#{$!}'")
261
+ end
262
+
263
+ # Logon to webpage using Basic Authorization type of logon. Uses AutoIt
264
+ # @param [Watir::Browser] browser A reference to the browser window or container element to be tested.
265
+ # @param [String] user Login ID for the user.
266
+ # @param [String] password Password for the user.
267
+ # @param [String] url The URL to log on to.
268
+ # @param [Boolean] bypass_validate When set to true, the call to validate(),
269
+ # which checks the health of the browser, is skipped..
270
+ def basic_auth(browser, user, password, url, bypass_validate = false)
271
+ mark_testlevel("Basic Authorization Login", 0)
272
+
273
+ message_to_report ("Login: #{user}")
274
+ message_to_report ("URL: #{url}")
275
+ message_to_report ("Password: #{password}")
276
+
277
+ @login_title = "Connect to"
278
+
279
+ a = Thread.new {
280
+ browser.goto(url)
281
+ }
282
+
283
+ sleep_for(2)
284
+ message_to_log("#{@login_title}...")
285
+
286
+ if (@ai.WinWait(@login_title, "", 90) > 0)
287
+ win_title = @ai.WinGetTitle(@login_title)
288
+ debug_to_log("Basic Auth Login window appeared: '#{win_title}'")
289
+ @ai.WinActivate(@login_title)
290
+ @ai.ControlSend(@login_title, '', "[CLASS:Edit; INSTANCE:2]", '!u')
291
+ @ai.ControlSend(@login_title, '', "[CLASS:Edit; INSTANCE:2]", user, 1)
292
+ @ai.ControlSend(@login_title, '', "[CLASS:Edit; INSTANCE:3]", password.gsub(/!/, '{!}'), 1)
293
+ @ai.ControlClick(@login_title, "", '[CLASS:Button; INSTANCE:1]')
294
+ else
295
+ debug_to_log("Basic Auth Login window did not appear.")
296
+ end
297
+ a.join
298
+
299
+ validate(browser, @myName) unless bypass_validate
300
+
301
+ message_to_report("URL: [#{browser.url}] User: [#{user}]")
302
+
303
+ end
304
+
305
+ # Provide an authorization token or passcode in a specified text field element identified by its *:id* attribute.
306
+ # @param [Watir::Browser] browser A reference to the browser window or container element to be tested.
307
+ # @param [String] role Usually a user role designation ('Administrator', 'User', etc.)
308
+ # @param [String] token Authentification token required by logon process.
309
+ # @param [String/Regexp] id Value of the *:id* attribute of the text field that will receive the *token*.
310
+ def token_auth(browser, role, token, id = 'token_pass')
311
+ set_textfield_by_id(browser, id, token)
312
+ click_button_by_value(browser, 'Continue')
313
+ if validate_text(browser, 'The requested page requires authentication\.\s*Please enter your Passcode below', nil, true)
314
+ bail_out(browser, __LINE__, "Token authorization failed on '#{token}'")
315
+ end
316
+ end
317
+
318
+ # @!endgroup Logon
319
+
320
+ # @!group Error Handling
321
+
322
+ # Exit more or less gracefully from script when errors are too severe to continue.
323
+ # Normally not called in a test script or project library.
324
+ # @param [Watir::Browser] browser A reference to the browser window or container element to be tested.
325
+ # @param [Fixnum] lnbr Line number in calling script.
326
+ # @param [String] desc Contains a message or description intended to appear in the log and/or report output
327
+ #
328
+ def bail_out(browser, lnbr, desc)
329
+ ts = Time.new
330
+ msg = "Bailing out at util line #{lnbr} #{ts} " + desc
331
+ puts "#{msg}"
332
+ fatal_to_log(msg, lnbr)
333
+ debug_to_log(dump_caller(lnbr))
334
+ if is_browser?(browser)
335
+ if @browserAbbrev == 'IE'
336
+ hwnd = browser.hwnd
337
+ kill_browser(hwnd, lnbr, browser)
338
+ raise(RuntimeError, msg, caller)
339
+ elsif @browserAbbrev == 'FF'
340
+ debug_to_log("#{browser.inspect}")
341
+ debug_to_log("#{browser.to_s}")
342
+ raise(RuntimeError, msg, caller)
343
+ end
344
+ end
345
+ @status = 'bailout'
346
+ raise(RuntimeError, msg, caller)
347
+ end
348
+
349
+
350
+ # Check for the presence of IE browser instances.
351
+ # @return [Fixnum] The number of IE browser instances encountered.
352
+ def check_for_other_browsers
353
+ cnt1 = find_other_browsers
354
+ cnt2 = Watir::Process.count 'iexplore.exe'
355
+ debug_to_log("check_for_other_browsers: cnt1: #{cnt1} cnt2: #{cnt2}")
356
+ rescue
357
+ error_to_log("#{$!} (#{__LINE__})\n#{Kernel.caller.to_yaml}")
358
+ end
359
+
360
+ # Check for the presence of IE browser instances and close all that are found.
361
+ def check_for_and_clear_other_browsers
362
+ if @targetBrowser.abbrev == 'IE'
363
+ debug_to_log("#{__method__}:")
364
+ cnt1 = find_other_browsers
365
+ cnt2 = Watir::IE.process_count
366
+ debug_to_log("#{__method__}: cnt1: #{cnt1} cnt2: #{cnt2}")
367
+ begin
368
+ Watir::IE.each do |ie|
369
+ pid = Watir::IE::Process.process_id_from_hwnd(ie.hwnd)
370
+ debug_to_log("#{__method__}: Killing browser process: hwnd #{ie.hwnd} pid #{pid} title '#{ie.title}' (#{__LINE__})")
371
+ do_taskkill(INFO, pid)
372
+ sleep_for(10)
373
+ end
374
+ #Watir::IE.close_all()
375
+ rescue
376
+ debug_to_log("#{__method__}: #{$!} (#{__LINE__})")
377
+ end
378
+ sleep(3)
379
+ cnt1 = find_other_browsers
380
+ cnt2 = Watir::IE.process_count
381
+ if cnt1 > 0 or cnt2 > 0
382
+ debug_to_log("#{__method__}:cnt1: #{cnt1} cnt2: #{cnt2}")
383
+ begin
384
+ Watir::IE.each do |ie|
385
+ pid = Watir::IE::Process.process_id_from_hwnd(ie.hwnd)
386
+ debug_to_log("#{__method__}: Killing browser process: hwnd #{ie.hwnd} pid #{pid} title '#{ie.title}' (#{__LINE__})")
387
+ do_taskkill(INFO, pid)
388
+ sleep_for(10)
389
+ end
390
+ #Watir::IE.close_all()
391
+ rescue
392
+ debug_to_log("#{__method__}:#{$!} (#{__LINE__})")
393
+ end
394
+ end
395
+ end
396
+ rescue
397
+ error_to_log("#{__method__}: #{$!} (#{__LINE__})\n#{Kernel.caller.to_yaml}")
398
+ end
399
+
400
+ # Force browser instance to close, one way or the other.
401
+ # Instance is identified by *hwnd*, the Windows OS handle for the process.
402
+ # @param [String] hwnd The value for the window handle for the browser process.
403
+ # @param [Fixnum] lnbr Line number in calling script.
404
+ # @param [Watir::Browser] browser A reference to the browser window or container element to be closed.
405
+ def kill_browser(hwnd, lnbr, browser = nil, doflag = false)
406
+ # TODO Firefox
407
+ logit = false
408
+ if @browserAbbrev == 'FF'
409
+ if is_browser?(browser) # and browser.url.length > 1
410
+ logit = true
411
+ here = __LINE__
412
+ url = browser.url
413
+ #capture_screen(browser, Time.new.to_f) if @screenCaptureOn
414
+ browser.close if url.length > 0
415
+ @status = 'killbrowser'
416
+ fatal_to_log("Kill browser called from line #{lnbr}")
417
+ end
418
+ elsif hwnd
419
+ pid = Watir::IE::Process.process_id_from_hwnd(hwnd)
420
+ if pid and pid > 0 and pid < 538976288
421
+ if browser.exists?
422
+ here = __LINE__
423
+ logit = true
424
+ url = browser.url
425
+ #capture_screen(browser, Time.new.to_f) if @screenCaptureOn
426
+ browser.close
427
+ sleep(2)
428
+ if browser.exists?
429
+ do_taskkill(FATAL, pid)
430
+ end
431
+ @status = 'killbrowser'
432
+ end
433
+ end
434
+ if logit
435
+ debug_to_log("#{@browserName} window hwnd #{hwnd} pid #{pid} #{url} (#{here})")
436
+ fatal_to_log("Kill browser called from line #{lnbr}")
437
+ end
438
+ end
439
+ end
440
+
441
+ # @!endgroup Error Handling
442
+
443
+ # @!group Browser
444
+
445
+ # Use enabled_popup and winclicker to determine if there is an active modal popup.
446
+ # Useful only when no wait action has been invoked.
447
+ # @param [Watir::Browser] browser A reference to the browser window or container element to be tested.
448
+ # @param [String] button The visible name of the button to be clicked to close the popup.
449
+ # @return [String] containing the window handle of the closed modal popup.
450
+ def modal_exists?(browser, button = nil)
451
+ rtrn = nil
452
+ if @browserAbbrev == 'IE'
453
+ Timeout::timeout(2) do
454
+ begin
455
+ if browser.enabled_popup
456
+ hwnd = browser.enabled_popup(5)
457
+ debug_to_log("Modal popup with handle #{hwnd} found. (#{__LINE__})")
458
+ wc = WinClicker.new
459
+ wc.makeWindowActive(hwnd)
460
+ rtrn = wc.getWindowTitle(hwnd)
461
+ if button
462
+ wc.clickWindowsButton_hWnd(hwnd, button)
463
+ end
464
+ wc = nil
465
+ end
466
+ rescue Timeout::Error
467
+ debug_to_log("No Modal popup found. (#{__LINE__})")
468
+ return rtrn
469
+ end
470
+ return rtrn
471
+ end
472
+ rtrn
473
+ else
474
+ rtrn
475
+ end
476
+ end
477
+
478
+ # Close a browser window identified by its title.
479
+ # Uses AutoIt. Windows only.
480
+ # @param [Watir::Browser] browser A reference to the browser window or container element to be tested.
481
+ # @param [String] title The title of the window to be closed. Matched from beginning of string.
482
+ # @param [String] desc Contains a message or description intended to appear in the log and/or report output
483
+ # @param [String] text (optional) The text of the window to be closed. Matched from beginning of string.
484
+ def close_window_by_title(browser, title, desc = '', text = '')
485
+ msg = "Window '#{title}':"
486
+ if @ai.WinWait(title, text, WAIT) > 0
487
+ passed_to_log("#{msg} appeared. #{desc}")
488
+ myHandle = @ai.WinGetHandle(title, text)
489
+ full_text = @ai.WinGetText(title)
490
+ debug_to_log("#{msg} hwnd: #{myHandle.inspect}")
491
+ debug_to_log("#{msg} title: '#{title}' text: '#{full_text}'")
492
+ if @ai.WinClose(title, text) > 0
493
+ passed_to_log("#{msg} closed successfully. #{desc}")
494
+ else
495
+ failed_to_log("#{msg} close failed. (#{__LINE__}) #{desc}")
496
+ end
497
+ else
498
+ failed_to_log("#{msg} did not appear after #{WAIT} seconds. (#{__LINE__}) #{desc}")
499
+ end
500
+ rescue
501
+ failed_to_log("#{msg}: Unable to close: '#{$!}'. (#{__LINE__}) #{desc}")
502
+ end
503
+
504
+ # Closes main browser session. Misnamed. Usually used at end of script to shut down browser.
505
+ def close_browser(browser, where = @myName, lnbr = __LINE__)
506
+ #TODO Firewatir 1.6.5 does not implement .exists for FireWatir::Firefox class
507
+ debug_to_log("Logging out in #{where} at line #{lnbr}.", lnbr, true)
508
+ debug_to_log("#{__method__}: browser: #{browser.inspect} (#{__LINE__})")
509
+
510
+ url = browser.url
511
+ title = browser.title
512
+
513
+ if ['FF', 'S'].include?(@browserAbbrev) || browser.exists?
514
+ case @browserAbbrev
515
+ when 'FF'
516
+ if is_browser?(browser)
517
+ debug_to_log("#{__method__}: Firefox browser url: [#{url}]")
518
+ debug_to_log("#{__method__}: Firefox browser title: [#{title}]")
519
+ debug_to_log("#{__method__}: Closing browser: #{where} (#{lnbr})")
520
+ if url and url.length > 1
521
+ browser.close
522
+ else
523
+ browser = FireWatir::Firefox.attach(:title, title)
524
+ browser.close
525
+ end
526
+
527
+ end
528
+ when 'IE'
529
+ debug_to_log("#{__method__}: Internet Explorer browser url: [#{url}]")
530
+ debug_to_log("#{__method__}: Internet Explorer browser title: [#{title}]")
531
+ debug_to_log("#{__method__}: Closing browser: #{where} (#{lnbr})")
532
+ if $watir_script
533
+ hwnd = browser.hwnd
534
+ pid = Watir::IE::Process.process_id_from_hwnd(hwnd)
535
+ debug_to_log("#{__method__}: Closing browser: hwnd #{hwnd} pid #{pid} #{where} (#{lnbr}) (#{__LINE__})")
536
+ browser.close
537
+ if browser.exists? and pid > 0 and pid < 538976288 # value of uninitialized memory location
538
+ debug_to_log("Retry close browser: hwnd #{hwnd} pid #{pid} #{where} #{lnbr} (#{__LINE__})")
539
+ browser.close
540
+ end
541
+ if browser.exists? and pid > 0 and pid < 538976288 # value of uninitialized memory location
542
+ kill_browser(browser.hwnd, __LINE__, browser)
543
+ end
544
+ else
545
+ browser.close
546
+ end
547
+ when 'S'
548
+ if is_browser?(browser)
549
+ url = browser.url
550
+ title = browser.title
551
+ debug_to_log("Safari browser url: [#{url}]")
552
+ debug_to_log("Safari browser title: [#{title}]")
553
+ debug_to_log("Closing browser: #{where} (#{lnbr})")
554
+ close_modal_s # to close any leftover modal dialogs
555
+ browser.close
556
+ end
557
+ when 'C', 'GC'
558
+ if is_browser?(browser)
559
+ url = browser.url
560
+ title = browser.title
561
+ debug_to_log("Chrome browser url: [#{url}]")
562
+ debug_to_log("Chrome browser title: [#{title}]")
563
+ debug_to_log("Closing browser: #{where} (#{lnbr})")
564
+ if url and url.length > 1
565
+ browser.close
566
+ end
567
+
568
+ end
569
+ else
570
+ raise "Unsupported browser: '#{@browserAbbrev}'"
571
+ end
572
+ end
573
+ rescue
574
+ failed_to_log(unable_to)
575
+ end
576
+
577
+ alias logout close_browser
578
+
579
+ # Close a browser popup window. Does not apply to modal popups.
580
+ # @param [Watir::Browser] popup Reference to the popup to be closed
581
+ def close_new_window_popup(popup)
582
+ if is_browser?(popup)
583
+ url = popup.url
584
+ debug_to_log("Closing popup '#{url}' ")
585
+ popup.close
586
+
587
+ end
588
+ end
589
+
590
+ # Close an HTML panel or division by clicking a link within it identified by the *:text* value of the link.
591
+ # @param [Watir::Browser] browser A reference to the browser window or container element to be tested.
592
+ # @param [Watir::Browser] panel Reference to the panel (usually a div element) to be closed
593
+ def close_panel_by_text(browser, panel, what = 'Close')
594
+ if validate(browser, @myName, __LINE__)
595
+ if @browserAbbrev == 'IE'
596
+ panel.link(:text, what).click!
597
+ elsif $USE_FIREWATIR
598
+ begin
599
+ panel.link(:text, what).click
600
+ rescue => e
601
+ unless rescue_me(e, __method__, rescue_me_command(:link, :id, what, :click), "#{panel.class}")
602
+ raise e
603
+ end
604
+ end
605
+ else
606
+ panel.link(:text, what).click(:wait => false)
607
+ end
608
+ sleep_for(1)
609
+ if validate(browser, @myName, __LINE__)
610
+ passed_to_log("Panel '#{what}' (by :text) closed.")
611
+ true
612
+ end
613
+ else
614
+ failed_to_log("Panel '#{strg}' (by :text) still open.")
615
+ end
616
+ rescue
617
+ failed_to_log("Click on '#{strg}'(by :text) failed: '#{$!}' (#{__LINE__})")
618
+ end
619
+
620
+ #def close_modal_ie(title, button = "OK", text = '', side = 'primary', wait = WAIT, desc = '', quiet = false)
621
+ # #TODO needs simplifying and debug code cleaned up
622
+ # title = translate_popup_title(title)
623
+ # msg = "'#{title}'"
624
+ # msg << " with text '#{text}'" if text.length > 0
625
+ # msg << " (#{desc})" if desc.length > 0
626
+ # @ai.Opt("WinSearchChildren", 1) # Match any substring in the title
627
+ # if @ai.WinWait(title, text, wait) > 0
628
+ # myHandle = @ai.WinGetHandle(title, text)
629
+ # full_text = @ai.WinGetText(title)
630
+ # #debug_to_report("Found popup handle:'#{myHandle}', title:'#{title}', text:'#{full_text}'")
631
+ # if myHandle.length > 0
632
+ # debug_to_log("hwnd: #{myHandle.inspect}")
633
+ # passed_to_log("#{msg} appeared.") unless quiet
634
+ # sleep_for(0.5)
635
+ # @ai.WinActivate(title, text)
636
+ # if @ai.WinActive(title, text) # > 0 #Hack to prevent fail when windows session locked
637
+ # debug_to_log("#{msg} activated.")
638
+ # if @ai.ControlFocus(title, text, button) # > 0
639
+ # controlHandle = @ai.ControlGetHandle(title, '', "[CLASS:Button; TEXT:#{button}]")
640
+ # if not controlHandle
641
+ # button = "&#{button}"
642
+ # controlHandle = @ai.ControlGetHandle(title, '', "[CLASS:Button; TEXT:#{button}]")
643
+ # end
644
+ # debug_to_log("Handle for button '#{button}': [#{controlHandle}]")
645
+ # debug_to_log("#{msg} focus gained.")
646
+ # # sleep_for(2)
647
+ # if @ai.ControlClick(title, text, button, side) # > 0
648
+ # # if @ai.ControlClick(title, text, "[Handle:#{controlHandle}]", side) > 0
649
+ # # debug_to_log("#{msg} #{side} click on 'Handle:#{controlHandle}'." )
650
+ # debug_to_log("#{msg} #{side} click on '#{button}' successful.")
651
+ # sleep_for(1)
652
+ # if @ai.WinExists(title, text) > 0
653
+ # debug_to_log("#{msg} close popup failed on click '#{button}'. Trying WinClose. (#{__LINE__})")
654
+ # @ai.WinClose(title, text)
655
+ # if @ai.WinExists(title, text) > 0
656
+ # debug_to_log("#{msg} close popup failed with WinClose('#{title}','#{text}'). (#{__LINE__})")
657
+ # @ai.WinKill(title, text)
658
+ # if @ai.WinExists(title, text) > 0
659
+ # debug_to_log("#{msg} close popup failed with WinKill('#{title}','#{text}'). (#{__LINE__})")
660
+ # else
661
+ # debug_to_log("#{msg} closed successfully with WinKill('#{title}','#{text}').")
662
+ # end
663
+ # else
664
+ # debug_to_log("#{msg} closed successfully with WinClose('#{title}','#{text}').")
665
+ # end
666
+ # else
667
+ # passed_to_log("#{msg} closed successfully.") unless quiet
668
+ # end
669
+ # else
670
+ # failed_to_log("#{msg} #{side} click on '#{button}' failed. (#{__LINE__})")
671
+ # end
672
+ # else
673
+ # failed_to_log("#{msg} Unable to gain focus on button (#{__LINE__})")
674
+ # end
675
+ # else
676
+ # failed_to_log("#{msg} Unable to activate (#{__LINE__})")
677
+ # end
678
+ # else
679
+ # failed_to_log("#{msg} did not appear after #{wait} seconds. (#{__LINE__})")
680
+ # end
681
+ # else
682
+ # failed_to_log("#{msg} did not appear after #{wait} seconds. (#{__LINE__})")
683
+ # end
684
+ #rescue
685
+ # failed_to_log("Close popup title=#{title} failed: '#{$!}' (#{__LINE__})")
686
+ #end
687
+
688
+ # Close an browser window (popup) by clicking a link within it identified by the *:text* value of the link.
689
+ # @param [Watir::Browser] popup A reference to the browser window or container element to be closed.
690
+ # @param [String] what Uniquely identify the *:link* element within the popup by the value in its *:text* attribute.
691
+ # @param [String] desc Contains a message or description intended to appear in the log and/or report output
692
+ def close_popup_by_text(popup, what = 'Close', desc = '')
693
+ count = 0
694
+ url = popup.url
695
+ if validate(popup, @myName, __LINE__)
696
+ count = string_count_in_string(popup.text, what)
697
+ if count > 0
698
+ begin
699
+ popup.link(:text, what).click
700
+ rescue => e
701
+ unless rescue_me(e, __method__, rescue_me_command(:link, :text, what, :click), "#{popup.class}")
702
+ raise e
703
+ end
704
+ end
705
+ passed_to_log("Popup #{url} closed by clicking link with text '#{what}'. #{desc}")
706
+ true
707
+ else
708
+ failed_to_log("Link :text=>'#{what}' for popup #{url} not found. #{desc}")
709
+ end
710
+ end
711
+ rescue
712
+ failed_to_log("Close popup #{url} with click link :text+>'#{what}' failed: '#{$!}' (#{__LINE__})")
713
+ debug_to_log("#{strg} appears #{count} times in popup.text.")
714
+ raise
715
+ end
716
+
717
+ # Close a modal dialog.
718
+ # @param [Watir::Browser] browser A reference to the browser window or container element to be tested.
719
+ # @param [String] title The title of the window to be closed. Matched from beginning of string.
720
+ # @param [String] button The display name of the button to be clicked.
721
+ # @param [String] text The text of the window to be closed. Matched from beginning of string.
722
+ # @param [String] side A string identifying which mouse button to click.
723
+ # @param [Fixnum] wait Number of seconds to wait for the popup to be seen.
724
+ def close_modal(browser, title="", button="OK", text='', side = 'primary', wait = WAIT)
725
+ case @targetBrowser.abbrev
726
+ when 'IE'
727
+ close_modal_ie(browser, title, button, text, side, wait)
728
+ when 'FF'
729
+ close_modal_ff(browser, title, button, text, side)
730
+ when 'S'
731
+ close_modal_s
732
+ when 'C', 'GC'
733
+ close_modal_c(browser, title)
734
+ end
735
+ end
736
+
737
+ # TODO: Logging
738
+ # Close a Chrome modal popup by :url.
739
+ def close_modal_c(browser, url)
740
+ browser.window(:url, url).close
741
+ end
742
+
743
+ # TODO: Logging
744
+ # Close a Safari modal popup by closing the frontmost Safari dialog. MacOS only
745
+ def close_modal_s
746
+ # simply closes the frontmost Safari dialog
747
+ Appscript.app("Safari").activate; Appscript.app("System Events").processes["Safari"].key_code(52)
748
+ end
749
+
750
+ # Close an IE modal popup by its title.
751
+ # @param [Watir::Browser] browser A reference to the browser window or container element to be tested.
752
+ # @param [String] title The title of the window to be closed. Matched from beginning of string.
753
+ # @param [String] button The display name of the button to be clicked.
754
+ # @param [String] text The text of the window to be closed. Matched from beginning of string.
755
+ # @param [String] side A string identifying which mouse button to click.
756
+ # @param [Fixnum] wait Number of seconds to wait for the popup to be seen.
757
+ # @param [String] desc Contains a message or description intended to appear in the log and/or report output
758
+ # @param [Boolean] quiet If true, fewer messages and pass/fail validations are logged.
759
+ def close_modal_ie(browser, title="", button="OK", text='', side = 'primary', wait = WAIT, desc = '', quiet = false)
760
+ #TODO needs simplifying, incorporating text verification, and debug code cleaned up
761
+ title = translate_popup_title(title)
762
+ msg = "Modal window (popup) '#{title}'"
763
+ if @ai.WinWait(title, text, wait)
764
+ myHandle = @ai.WinGetHandle(title, text)
765
+ if myHandle.length > 0
766
+ debug_to_log("hwnd: #{myHandle.inspect}")
767
+ passed_to_log("#{msg} appeared.") unless quiet
768
+ window_handle = "[HANDLE:#{myHandle}]"
769
+ sleep_for(0.5)
770
+ @ai.WinActivate(window_handle)
771
+ if @ai.WinActive(window_handle)
772
+ debug_to_log("#{msg} activated.")
773
+ controlHandle = @ai.ControlGetHandle(title, '', "[CLASS:Button; TEXT:#{button}]")
774
+ if not controlHandle.length > 0
775
+ button = "&#{button}"
776
+ controlHandle = @ai.ControlGetHandle(title, '', "[CLASS:Button; TEXT:#{button}]")
777
+ end
778
+ debug_to_log("Handle for button '#{button}': [#{controlHandle}]")
779
+ debug_to_log("#{msg} focus gained.")
780
+ if @ai.ControlClick(title, '', "[CLASS:Button; TEXT:#{button}]")
781
+ passed_to_log("#{msg} #{side} click on '[CLASS:Button; TEXT:#{button}]' successful.")
782
+ sleep_for(0.5)
783
+ if @ai.WinExists(window_handle)
784
+ debug_to_log("#{msg} close popup failed on click '#{button}'. Trying WinClose. (#{__LINE__})")
785
+ @ai.WinClose(title, text)
786
+ if @ai.WinExists(window_handle)
787
+ debug_to_log("#{msg} close popup failed with WinClose(#{window_handle}). (#{__LINE__})")
788
+ @ai.WinKill(window_handle)
789
+ if @ai.WinExists(window_handle)
790
+ debug_to_log("#{msg} close popup failed with WinKill(#{window_handle}). (#{__LINE__})")
791
+ else
792
+ debug_to_log("#{msg} closed successfully with WinKill(#{window_handle}).")
793
+ end
794
+ else
795
+ debug_to_log("#{msg} closed successfully with WinClose(#{window_handle}).")
796
+ end
797
+ else
798
+ passed_to_log("#{msg} closed successfully.")
799
+ end
800
+ else
801
+ failed_to_log("#{msg} #{side} click on '[CLASS:Button; TEXT:#{button}]' failed. (#{window_handle}) (#{__LINE__})")
802
+ end
803
+ else
804
+ failed_to_log("#{msg} Unable to activate (#{window_handle}) (#{__LINE__})")
805
+ end
806
+ else
807
+ failed_to_log("#{msg} did not appear after #{wait} seconds. (#{window_handle}) (#{__LINE__})")
808
+ end
809
+ else
810
+ failed_to_log("#{msg} did not appear after #{wait} seconds.(#{window_handle}) (#{__LINE__})")
811
+ end
812
+ rescue
813
+ failed_to_log("Close popup title=#{title} failed: '#{$!}' (#{__LINE__})")
814
+ end
815
+
816
+ #alias close_popup_validate_text close_modal_ie
817
+ #alias close_popup close_modal_ie
818
+
819
+ # private :close_modal_ie
820
+
821
+ # Close an IE modal popup by its title. Calls close_modal_ie.
822
+ # @deprecated Use close_modal.
823
+ # @param [String] title The title of the window to be closed. Matched from beginning of string.
824
+ # @param [String] button The display name of the button to be clicked.
825
+ # @param [String] text The text of the window to be closed. Matched from beginning of string.
826
+ # @param [String] side A string identifying which mouse button to click.
827
+ # @param [Fixnum] wait Number of seconds to wait for the popup to be seen.
828
+ # @param [String] desc Contains a message or description intended to appear in the log and/or report output
829
+ # @param [Boolean] quiet If true, fewer messages and pass/fail validations are logged.
830
+ def close_popup(title = '', button = 'OK', text = '', side = 'primary',
831
+ wait = WAIT, desc = '', quiet = false)
832
+ debug_to_log("#{__method__} begin")
833
+ close_modal_ie(@myBrowser, title, button, text, side, wait, desc, quiet)
834
+ end
835
+
836
+ alias close_popup_validate_text close_popup
837
+
838
+ # Close a Firefox modal popup by its title.
839
+ # @param [Watir::Browser] browser A reference to the browser window or container element to be tested.
840
+ # @param [String] title The title of the window to be closed. Matched from beginning of string.
841
+ # @param [String] button The display name of the button to be clicked.
842
+ # @param [String] text The text of the window to be closed. Matched from beginning of string.
843
+ # @param [String] side A string identifying which mouse button to click.
844
+ # @return [Boolean] True if the modal is successfully closed.
845
+ def close_modal_ff(browser, title="", button=nil, text='', side='')
846
+ title = translate_popup_title(title)
847
+ msg = "Modal dialog (popup): title=#{title} button='#{button}' text='#{text}' side='#{side}':"
848
+ modal = browser.modal_dialog(:timeout => WAIT)
849
+ if modal.exists?
850
+ modal_text = modal.text
851
+ if text.length > 0
852
+ if modal_text =~ /#{text}/
853
+ passed_to_log("#{msg} appeared with match on '#{text}'.")
854
+ else
855
+ failed_to_log("#{msg} appeared but did not match '#{text}' ('#{modal_text}).")
856
+ end
857
+ else
858
+ passed_to_log("#{msg} appeared.")
859
+ end
860
+ if button
861
+ modal.click_button(button)
862
+ else
863
+ modal.close
864
+ end
865
+ if modal.exists?
866
+ failed_to_log("#{msg} close failed. (#{__LINE__})")
867
+ else
868
+ passed_to_log("#{msg} closed successfully.")
869
+ true
870
+ end
871
+ else
872
+ failed_to_log("#{msg} did not appear after #{WAIT} seconds. (#{__LINE__})")
873
+ end
874
+ rescue
875
+ failed_to_log("#{msg} Unable to validate modal popup: '#{$!}'. (#{__LINE__})")
876
+ end
877
+
878
+ # Wait for a modal popup to appear and then close it.
879
+ # Used when modal popup in response to browser action is intermittent or unpredictable.
880
+ # @param [String] title The title of the window to be closed. Matched from beginning of string.
881
+ # @param [String] text The text of the window to be closed. Matched from beginning of string.
882
+ # @param [String] button The display name of the button to be clicked.
883
+ # @param [String] side A string identifying which mouse button to click.
884
+ # @param [Fixnum] wait Number of seconds to wait for the popup to be seen.
885
+ # @param [String] desc Contains a message or description intended to appear in the log and/or report output
886
+ # @return [Boolean] True if the modal is successfully closed.
887
+ def handle_popup(title, text = '', button= 'OK', side = 'primary', wait = WAIT, desc = '')
888
+ title = translate_popup_title(title)
889
+ msg = "'#{title}'"
890
+ if text.length > 0
891
+ msg << " with text '#{text}'"
892
+ end
893
+ @ai.Opt("WinSearchChildren", 1) # match title from start, forcing default
894
+
895
+ if button and button.length > 0
896
+ if button =~ /ok|yes/i
897
+ id = '1'
898
+ else
899
+ id = '2'
900
+ end
901
+ else
902
+ id = ''
903
+ end
904
+
905
+ if @ai.WinWait(title, '', wait) > 0
906
+ myHandle = @ai.WinGetHandle(title, '')
907
+ window_handle = "[HANDLE:#{myHandle}]"
908
+ full_text = @ai.WinGetText(window_handle)
909
+ debug_to_log("Found popup handle:'#{myHandle}', title:'#{title}', text:'#{full_text}'")
910
+
911
+ controlHandle = @ai.ControlGetHandle(window_handle, '', "[CLASS:Button; TEXT:#{button}]")
912
+ if not controlHandle
913
+ # button = "&#{button}"
914
+ controlHandle = @ai.ControlGetHandle(window_handle, '', "[CLASS:Button; TEXT:&#{button}]")
915
+ end
916
+
917
+ if text.length > 0
918
+ if full_text =~ /#{text}/
919
+ passed_to_log("Found popup handle:'#{myHandle}', title:'#{title}', text includes '#{text}'. #{desc}")
920
+ else
921
+ failed_to_log("Found popup handle:'#{myHandle}', title:'#{title}', text does not include '#{text}'. Closing it. #{desc}")
922
+ end
923
+ end
924
+
925
+ @ai.WinActivate(window_handle, '')
926
+ @ai.ControlClick(window_handle, '', id, side)
927
+ if @ai.WinExists(title, '') > 0
928
+ debug_to_log("#{msg} @ai.ControlClick on '#{button}' (ID:#{id}) with handle '#{window_handle}' failed to close window. Trying title.")
929
+ @ai.ControlClick(title, '', id, side)
930
+ if @ai.WinExists(title, '') > 0
931
+ debug_to_report("#{msg} @ai.ControlClick on '#{button}' (ID:#{id}) with title '#{title}' failed to close window. Forcing closed.")
932
+ @ai.WinClose(title, '')
933
+ if @ai.WinExists(title, '') > 0
934
+ debug_to_report("#{msg} @ai.WinClose on title '#{title}' failed to close window. Killing window.")
935
+ @ai.WinKill(title, '')
936
+ if @ai.WinExists(title, '') > 0
937
+ failed_to_log("#{msg} @ai.WinKill on title '#{title}' failed to close window")
938
+ else
939
+ passed_to_log("Killed: popup handle:'#{myHandle}', title:'#{title}'. #{desc}")
940
+ true
941
+ end
942
+ else
943
+ passed_to_log("Forced closed: popup handle:'#{myHandle}', title:'#{title}'. #{desc}")
944
+ true
945
+ end
946
+ else
947
+ passed_to_log("Closed on '#{button}': popup handle:'#{myHandle}', title:'#{title}'. #{desc}")
948
+ true
949
+ end
950
+ else
951
+ passed_to_log("Closed on '#{button}': popup handle:'#{myHandle}', title:'#{title}'. #{desc}")
952
+ true
953
+ end
954
+
955
+ else
956
+ failed_to_log("#{msg} did not appear after #{wait} seconds. #{desc} (#{__LINE__})")
957
+ end
958
+ rescue
959
+ failed_to_log("Unable to handle popup #{msg}: '#{$!}' #{desc} (#{__LINE__})")
960
+
961
+ end
962
+
963
+ # Return a reference to an IE browser window based on one of its attributes.
964
+ # @param [Watir::Browser] browser A reference to the browser window or container element to be tested.
965
+ # @param [Symbol] how The element attribute used to identify the window: :url, :title, or :hwnd.
966
+ # @param [String, Regexp] what A string or a regular expression to be found in the *how* attribute that uniquely identifies the element.
967
+ # @param [String] desc Contains a message or description intended to appear in the log and/or report output
968
+ # @return [Watir::IE] A reference to the popup.
969
+ def find_popup(browser, how, what, desc = '')
970
+ msg = "Find popup :#{how}=>'#{what}'. #{desc}"
971
+ popup = Watir::IE.find(how, what) # TODO: too browser specific
972
+ sleep_for(1)
973
+ debug_to_log("#{popup.inspect}")
974
+ if is_browser?(popup)
975
+ # title = popup.title
976
+ passed_to_log(msg)
977
+ return popup
978
+ else
979
+ failed_to_log(msg)
980
+ end
981
+ rescue
982
+ failed_to_log("Unable to find popup :#{how}=>'#{what}'. #{desc} '#{$!}' (#{__LINE__})")
983
+ end
984
+
985
+ # Confirm that the object passed in *browser* is actually a Browser object.
986
+ # @param [Watir::Browser] browser A reference to the window or container element to be tested.
987
+ def is_browser?(browser)
988
+ myClass = browser.class.to_s
989
+ case @targetBrowser.abbrev
990
+ when 'IE'
991
+ myClass =~ /Watir::IE|Watir::Browser/i
992
+ when 'FF'
993
+ myClass =~ /Watir::Browser/i
994
+ when 'S'
995
+ myClass =~ /Watir::Browser/i
996
+ when 'C'
997
+ myClass =~ /Watir::Browser/i
998
+ end
999
+ end
1000
+
1001
+ alias is_browser is_browser?
1002
+
1003
+ # Translate window title supplied in *title* to a title appropriate for the targeted browser and version
1004
+ # actually being run.
1005
+ # Used primarily for handling of modal popups and dialogs.
1006
+ # This allows cross-browser compatibility for handling modal popups and other windows accessed by titlt.
1007
+ # @param [String] title The title of the window to be closed.
1008
+ def translate_popup_title(title)
1009
+ new_title = title
1010
+ case @browserAbbrev
1011
+ when 'IE'
1012
+ if @browserVersion
1013
+ case @browserVersion
1014
+ when '8.0'
1015
+ case title
1016
+ when "Microsoft Internet Explorer"
1017
+ new_title = "Message from webpage"
1018
+ when "The page at"
1019
+ new_title = "Message from webpage"
1020
+ end
1021
+ when '7.0'
1022
+ case title
1023
+ when "Message from webpage"
1024
+ new_title = "Microsoft Internet Explorer"
1025
+ when "The page at"
1026
+ new_title = "Windows Internet Explorer"
1027
+ end
1028
+ when '6.0'
1029
+ case title
1030
+ when "Message from webpage"
1031
+ new_title = "Microsoft Internet Explorer"
1032
+ when "The page at"
1033
+ new_title = "Microsoft Internet Explorer"
1034
+ end
1035
+ else
1036
+ case title
1037
+ when "Microsoft Internet Explorer"
1038
+ new_title = "Message from webpage"
1039
+ when "The page at"
1040
+ new_title = "Message from webpage"
1041
+ end
1042
+ end
1043
+ else
1044
+ case title
1045
+ when "Microsoft Internet Explorer"
1046
+ new_title = "Message from webpage"
1047
+ when "The page at"
1048
+ new_title = "Message from webpage"
1049
+ end
1050
+ end
1051
+ when 'FF'
1052
+ case title
1053
+ when 'File Download'
1054
+ new_title = 'Opening'
1055
+ when "Microsoft Internet Explorer"
1056
+ new_title = 'The page at'
1057
+ when "Message from webpage"
1058
+ new_title = 'The page at'
1059
+ end
1060
+ when 'C'
1061
+ case title
1062
+ when 'File Download'
1063
+ new_title = 'Save As'
1064
+ when "Microsoft Internet Explorer"
1065
+ new_title = 'The page at'
1066
+ when "Message from webpage"
1067
+ new_title = 'The page at'
1068
+ end
1069
+ end
1070
+ new_title
1071
+ end
1072
+
1073
+ # Identify the exact version of the Browser currently being executed.
1074
+ # @todo Bring up to date with newer browser versions
1075
+ # @param [Watir::Browser] browser A reference to the browser window or container element to be tested.
1076
+ def get_browser_version(browser)
1077
+ debug_to_log("starting get_browser_version")
1078
+ case @targetBrowser.abbrev
1079
+ when 'IE'
1080
+ @browserAbbrev = 'IE'
1081
+ @browserName = 'Internet Explorer'
1082
+ if $watir_script
1083
+ @browserAppInfo = browser.document.invoke('parentWindow').navigator.appVersion
1084
+ else
1085
+ @browserAppInfo = browser.execute_script("return navigator.userAgent;")
1086
+ end
1087
+ @browserAppInfo =~ /MSIE\s(.*?);/
1088
+ @browserVersion = $1
1089
+ when 'FF'
1090
+ @browserAbbrev = 'FF'
1091
+ @browserName = 'Firefox'
1092
+ @browserVersion = '6.01' #TODO: get actual version from browser
1093
+ @browserAppInfo = browser.execute_script("return navigator.userAgent;")
1094
+ debug_to_log("#{@browserName}, @browserAppInfo: (#{@browserAppInfo})")
1095
+ when 'S'
1096
+ @browserAbbrev = 'S'
1097
+ @browserName = 'Safari'
1098
+ @browserVersion = '5.0.4' #TODO: get actual version from browser itself
1099
+ @browserAppInfo = browser.execute_script("return navigator.userAgent;")
1100
+ debug_to_log("#{@browserName}, @browserAppInfo: (#{@browserAppInfo})")
1101
+ when 'C'
1102
+ @browserAbbrev = 'C'
1103
+ @browserName = 'Chrome'
1104
+ @browserVersion = '11.0' #TODO: get actual version from browser
1105
+ @browserAppInfo = browser.execute_script("return navigator.userAgent;")
1106
+ debug_to_log("#{@browserName}, @browserAppInfo: (#{@browserAppInfo})")
1107
+ end
1108
+ rescue
1109
+ debug_to_log("Unable to determine #{@browserAbbrev} browser version: '#{$!}' (#{__LINE__})")
1110
+ ensure
1111
+ message_to_log("Browser: [#{@browserAbbrev} #{@browserVersion}]")
1112
+ end
1113
+
1114
+ protected :get_browser_version
1115
+
1116
+ #def filter_bailout_from_rescue(err, msg)
1117
+ # if msg =~ /bailing out/i
1118
+ # raise err
1119
+ # else
1120
+ # error_to_log(msg)
1121
+ # end
1122
+ #end
1123
+
1124
+ # @!group Browser
1125
+
1126
+ # Open and attach a browser popup window where the link to open it and its title contain the same string.
1127
+ # @deprecated
1128
+ def open_popup_through_link_title(browser, title, pattern, name)
1129
+ click_title(browser, title)
1130
+ #TODO need some kind of wait for process here
1131
+ sleep_for 2
1132
+ attach_popup_by_url(browser, pattern, name)
1133
+ rescue
1134
+ failed_to_log("Unable to open popup '#{name}': '#{$!}' (#{__LINE__})")
1135
+ end
1136
+
1137
+ # @!group Error Handling
1138
+
1139
+ # Verifies health of the browser. Looks for common http and system errors that are unrecoverable and
1140
+ # attempts to gracefully bail out of the script.
1141
+ # Calls rescue_me() when trying to capture the text to filter out known false errors
1142
+ # and handle container elements that don't respond to the .text method.
1143
+ # @param [Watir::Browser] browser A reference to the browser window or container element to be tested.
1144
+ # @param [String] file_name The file name of the executing script.
1145
+ # @param [Fixnum] lnbr Contains a message or description intended to appear in the log and/or report output
1146
+ # @param [Boolean] dbg If set to true additional debug messages are written to the log.
1147
+ #
1148
+ # @return [Boolean] True if no error conditions have been encountered.
1149
+ def validate(browser, file_name = @myName, lnbr = "#{__LINE__}", dbg = false)
1150
+ debug_to_log("#{__method__} begin") if dbg
1151
+ msg = ''
1152
+ myOK = true
1153
+ if not browser
1154
+ msg = "#{file_name}----browser is nil object. (#{lnbr})"
1155
+ myOK = false
1156
+ elsif not browser.class.to_s =~ /Watir/
1157
+ msg = "#{file_name}----not a Watir object. (#{lnbr})"
1158
+ debug_to_log(browser.inspect)
1159
+ myOK = false
1160
+
1161
+ else
1162
+ if browser.respond_to?(:url)
1163
+ if not browser.url == @currentURL
1164
+ @currentURL = browser.url
1165
+ debug_to_log("Current URL: [#{@currentURL}]")
1166
+ # mark_testlevel( "Current URL: [#{@currentURL}]", 1 )
1167
+ end
1168
+ end
1169
+
1170
+ if @capture_js_errors
1171
+ if browser.respond_to?(:status)
1172
+ if browser.status.downcase =~ /errors? on page/ and
1173
+ not browser.status.downcase.include?('Waiting for')
1174
+ capture_js_error(browser)
1175
+ end
1176
+ end
1177
+ end
1178
+
1179
+ begin
1180
+ browser_text = browser.text.downcase
1181
+ rescue => e
1182
+ unless rescue_me(e, __method__, "browser.text.downcase", "#{browser.class}", browser)
1183
+ debug_to_log("browser.text.downcase in #{__method__} #{browser.class}")
1184
+ debug_to_log("#{get_callers}")
1185
+ raise e
1186
+ else
1187
+ return true
1188
+ end
1189
+ end
1190
+
1191
+ if browser_text
1192
+ if browser_text.match(/unrecognized error condition has occurred/i)
1193
+ msg = "#{file_name}----Unrecognized Exception occurred. (#{lnbr})"
1194
+ myOK = false
1195
+
1196
+ elsif browser_text.match(/cannot find server or dns error/i)
1197
+ msg = "#{file_name}----Cannot find server error or DNS error. (#{lnbr})"
1198
+ myOK = false
1199
+
1200
+ elsif browser_text.match(/the rpc server is unavailable/i)
1201
+ msg = "#{file_name}----RPC server unavailable. (#{lnbr})"
1202
+ myOK = false
1203
+
1204
+ elsif browser_text.match(/404 not found/i) or
1205
+ browser_text.match(/the page you were looking for does\s*n[o']t exist/i)
1206
+ msg = "#{file_name}----RFC 2068 HTTP/1.1: 404 URI Not Found. (#{lnbr})"
1207
+ myOK = false
1208
+
1209
+ elsif browser_text.match(/we're sorry, but something went wrong/i) or
1210
+ browser_text.match(/http status 500/i)
1211
+ msg = "#{file_name}----RFC 2068 HTTP/1.1: 500 Internal Server Error. (#{lnbr})"
1212
+ myOK = false
1213
+
1214
+ elsif browser_text.match(/internet explorer cannot display the webpage/i)
1215
+ msg = "#{file_name}----Probably RFC 2068 HTTP/1.1: 500 Internal Server Error. (#{lnbr})"
1216
+ myOK = false
1217
+
1218
+ elsif browser_text.match(/503.*service unavailable/i)
1219
+ msg = "#{file_name}----RFC 2068 HTTP/1.1: 503 Service Unavailable. (#{lnbr})"
1220
+ myOK = false
1221
+
1222
+ elsif browser_text.match(/java.lang.NullPointerException/i)
1223
+ msg = "#{file_name}----java.lang.NullPointerException. (#{lnbr})"
1224
+ myOK = false
1225
+
1226
+ elsif browser_text.match(/due to unscheduled maintenance/i)
1227
+ msg = "#{file_name}----Due to unscheduled maintenance. (#{lnbr})"
1228
+ myOK = false
1229
+
1230
+ elsif browser_text.match(/network\s+error\s*(.+)$/i)
1231
+ $1.chomp!
1232
+ msg = "#{file_name}----Network Error #{$1}. (#{lnbr})"
1233
+ myOK = false
1234
+
1235
+ elsif browser_text.match(/warning: page has expired/i)
1236
+ msg = "#{file_name}----Page using information from form has expired. Not automatically resubmitted. (#{lnbr})"
1237
+ myOK = false
1238
+
1239
+ elsif browser_text.match(/no backend server available/i)
1240
+ msg = "#{file_name}----Cannot Reach Server (#{lnbr})"
1241
+ myOK = false
1242
+
1243
+ elsif browser_text.match(/sign on\s+.+\s+unsuccessful/i)
1244
+ msg = "#{file_name}----Invalid Id or Password (#{lnbr})"
1245
+ myOK = false
1246
+
1247
+ elsif browser_text.match(/you are not authorized/i)
1248
+ msg = "#{file_name}----Not authorized to view this page. (#{lnbr})"
1249
+ myOK = false
1250
+
1251
+ elsif browser_text.match(/too many incorrect login attempts have been made/i)
1252
+ msg = "#{file_name}----Invalid Id or Password. Too many tries. (#{lnbr})"
1253
+ myOK = false
1254
+
1255
+ elsif browser_text.match(/system error\.\s+an error has occurred/i)
1256
+ msg = "#{file_name}----System Error. An error has occurred. Please try again or call the Help Line for assistance. (#{lnbr})"
1257
+ myOK = false
1258
+
1259
+ elsif browser_text.match(/Internal Server failure,\s+NSAPI plugin/i)
1260
+ msg = "#{file_name}----Internal Server failure, NSAPI plugin. (#{lnbr})"
1261
+ myOK = false
1262
+
1263
+ elsif browser_text.match(/Error Page/i)
1264
+ msg = "#{file_name}----Error Page. (#{lnbr})"
1265
+ myOK = false
1266
+
1267
+ elsif browser_text.match(/The website cannot display the page/i)
1268
+ msg = "#{file_name}----HTTP 500. (#{lnbr})"
1269
+ myOK = false
1270
+
1271
+ # elsif browser_text.match(/Insufficient Data/i)
1272
+ # msg = "#{file_name}----Insufficient Data. (#{lnbr})"
1273
+ # myOK = false
1274
+
1275
+ elsif browser_text.match(/The timeout period elapsed/i)
1276
+ msg = "#{file_name}----Time out period elapsed or server not responding. (#{lnbr})"
1277
+ myOK = false
1278
+
1279
+ elsif browser_text.match(/Unexpected\s+errors*\s+occur+ed\.\s+(?:-+)\s+(.+)/i)
1280
+ msg = "#{file_name}----Unexpected errors occurred. #{$2.slice(0, 120)} (#{lnbr})"
1281
+ if not browser_text.match(/close the window and try again/i)
1282
+ myOK = false
1283
+ else
1284
+ debug_to_log("#{msg}")
1285
+ end
1286
+
1287
+ elsif browser_text.match(/Server Error in (.+) Application\.\s+(?:-+)\s+(.+)/i)
1288
+ msg = "#{file_name}----Server Error in #{1} Application. #{$2.slice(0, 100)} (#{lnbr})"
1289
+ myOK = false
1290
+
1291
+ elsif browser_text.match(/Server Error in (.+) Application\./i)
1292
+ msg = "#{file_name}----Server Error in #{1} Application. '#{browser_text.slice(0, 250)}...' (#{lnbr})"
1293
+ myOK = false
1294
+
1295
+ elsif browser_text.match(/An error has occur+ed\. Please contact support/i)
1296
+ msg = "#{file_name}----An error has occurred. Please contact support (#{lnbr})"
1297
+ myOK = false
1298
+
1299
+ end
1300
+ else
1301
+ debug_to_log("browser.text returned nil")
1302
+ end
1303
+ end
1304
+
1305
+ if not myOK
1306
+ msg << " (#{browser.url})"
1307
+ puts msg
1308
+ debug_to_log(browser.inspect)
1309
+ debug_to_log(browser.text)
1310
+ fatal_to_log(msg, lnbr)
1311
+ raise(RuntimeError, msg, caller)
1312
+ else
1313
+ debug_to_log("#{__method__} returning OK") if dbg
1314
+ return myOK
1315
+ end
1316
+
1317
+ rescue
1318
+ errmsg = $!
1319
+ if errmsg and errmsg.match(msg)
1320
+ errmsg = ''
1321
+ end
1322
+ bail_out(browser, lnbr, "#{msg} #{errmsg}")
1323
+ end
1324
+
1325
+ alias validate_browser validate
1326
+
1327
+ # @!endgroup Error Handling
1328
+
1329
+ # @!group Backward compatible usages
1330
+
1331
+ # Returns a reference to a browser window using the window's *:url* attribute. Calls attach_browser().
1332
+ # @example
1333
+ # mainwindow = open_browser('www.google.com')
1334
+ # click(mainwindow, :button, :id, 'an id string') # click a button that opens another browser window
1335
+ # popup = attach_browser_by_url(mainwindow, '[url of new window]')
1336
+ # @param [Watir::Browser] browser A reference to the current browser window.
1337
+ # @param [String, Regexp] what The value in the targeted attribute that uniquely identifies the new window
1338
+ # @param [String] desc Contains a message or description intended to appear in the log and/or report output
1339
+ # @return [Watir::Browser]
1340
+ def attach_browser_by_url(browser, what, desc = '')
1341
+ attach_browser(browser, :url, what, desc)
1342
+ end
1343
+
1344
+ alias attach_browser_with_url attach_browser_by_url
1345
+
1346
+ # Returns a reference to a new browser window identified by its *:title* attribute. Used to attach a new browser window to a variable
1347
+ # which can then be passed to methods that require a *browser* parameter. Calls attach_browser().
1348
+ # @param (see #attach_browser_by_url)
1349
+ def attach_popup_by_title(browser, what, desc = '')
1350
+ attach_popup(browser, :title, what, desc)
1351
+ end
1352
+
1353
+ # Returns a reference to a new browser window identified by its *:url* attribute. Used to attach a new browser window to a variable
1354
+ # which can then be passed to methods that require a *browser* parameter. Calls attach_browser().
1355
+ # @param (see #attach_browser_by_url)
1356
+ def attach_popup_by_url(browser, what, desc = '')
1357
+ attach_popup(browser, :url, what, desc)
1358
+ end
1359
+
1360
+ alias get_popup_with_url attach_popup_by_url
1361
+ alias attach_popup_with_url attach_popup_by_url
1362
+ alias attach_iepopup attach_popup_by_url
1363
+
1364
+ # Close a popup browser window (non-modal) by clicking on a link with :title *what*.
1365
+ # This method does not check to make sure the popup is actually closed.
1366
+ # @param [Watir::Browser] popup A reference to the current popup browser window.
1367
+ # @param [String, Regexp] what The value in the targeted attribute that uniquely identifies the new window
1368
+ # @param [String] desc Contains a message or description intended to appear in the log and/or report output
1369
+ # @return [Boolean] True if the click is successful.
1370
+ def close_popup_by_button_title(popup, what, desc = '')
1371
+ click(popup, :link, :title, what, desc)
1372
+ end
1373
+
1374
+ # @!endgroup Backward
1375
+
1376
+
1377
+ end
1378
+ end
1379
+ end
1380
+