watir 1.6.5 → 1.6.6.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (186) hide show
  1. data/CHANGES +461 -326
  2. data/VERSION +1 -0
  3. data/bin/watir-console +6 -6
  4. data/lib/changes.rb +3 -3
  5. data/lib/license.rb +38 -38
  6. data/lib/readme.rb +140 -140
  7. data/lib/watir/WindowHelper.rb +49 -49
  8. data/lib/watir/camel_case.rb +66 -66
  9. data/lib/watir/clickJSDialog.rb +19 -19
  10. data/lib/watir/close_all.rb +37 -37
  11. data/lib/watir/collections.rb +392 -344
  12. data/lib/watir/container.rb +857 -815
  13. data/lib/watir/contrib/enabled_popup.rb +20 -20
  14. data/lib/watir/contrib/ie-new-process.rb +27 -27
  15. data/lib/watir/contrib/page_checker.rb +29 -29
  16. data/lib/watir/cookiemanager.rb +55 -55
  17. data/lib/watir/core.rb +28 -0
  18. data/lib/watir/core_ext.rb +17 -17
  19. data/lib/watir/datahandler.rb +107 -107
  20. data/lib/watir/dialog.rb +46 -46
  21. data/lib/watir/element.rb +351 -343
  22. data/lib/watir/element_collections.rb +97 -97
  23. data/lib/watir/form.rb +170 -170
  24. data/lib/watir/frame.rb +59 -59
  25. data/lib/watir/html_element.rb +22 -0
  26. data/lib/watir/ie-class.rb +1006 -1009
  27. data/lib/watir/ie-process.rb +39 -39
  28. data/lib/watir/ie.rb +70 -133
  29. data/lib/watir/image.rb +130 -130
  30. data/lib/watir/input_elements.rb +614 -572
  31. data/lib/watir/irb-history.rb +30 -30
  32. data/lib/watir/link.rb +64 -64
  33. data/lib/watir/locator.rb +200 -176
  34. data/lib/watir/logger.rb +19 -19
  35. data/lib/watir/modal_dialog.rb +122 -122
  36. data/lib/watir/module.rb +37 -0
  37. data/lib/watir/non_control_elements.rb +145 -145
  38. data/lib/watir/page-container.rb +116 -107
  39. data/lib/watir/popup.rb +29 -29
  40. data/lib/watir/process.rb +19 -19
  41. data/lib/watir/screen_capture.rb +115 -115
  42. data/lib/watir/setFileDialog.rb +16 -16
  43. data/lib/watir/table.rb +395 -362
  44. data/lib/watir/watir_simple.rb +475 -475
  45. data/lib/watir/win32.rb +35 -35
  46. data/lib/watir/win32ole.rb +14 -14
  47. data/lib/watir/winClicker.rb +496 -496
  48. data/rakefile.rb +70 -0
  49. data/unittests/all_tests.rb +10 -10
  50. data/unittests/buttons_xpath_test.rb +69 -69
  51. data/unittests/checkbox_test.rb +179 -179
  52. data/unittests/checkbox_xpath_test.rb +107 -107
  53. data/unittests/click_no_wait_test.rb +21 -0
  54. data/unittests/core_tests.rb +17 -17
  55. data/unittests/css_test.rb +42 -35
  56. data/unittests/defer_test.rb +46 -46
  57. data/unittests/dialog_test.rb +77 -77
  58. data/unittests/div2_xpath_test.rb +21 -21
  59. data/unittests/div_test.rb +188 -188
  60. data/unittests/div_xpath_test.rb +96 -96
  61. data/unittests/element_test.rb +49 -0
  62. data/unittests/errorchecker_test.rb +32 -22
  63. data/unittests/filefield_test.rb +39 -39
  64. data/unittests/filefield_xpath_test.rb +33 -33
  65. data/unittests/form_test.rb +280 -280
  66. data/unittests/form_xpath_test.rb +252 -252
  67. data/unittests/frame_test.rb +155 -155
  68. data/unittests/google_form_test.rb +15 -15
  69. data/unittests/html/JavascriptClick.html +39 -39
  70. data/unittests/html/blankpage.html +11 -11
  71. data/unittests/html/buttons1.html +40 -40
  72. data/unittests/html/checkboxes1.html +89 -89
  73. data/unittests/html/click_no_wait.html +14 -0
  74. data/unittests/html/complex_table.html +36 -36
  75. data/unittests/html/cssTest.html +42 -42
  76. data/unittests/html/depot_store.html +59 -59
  77. data/unittests/html/div.html +93 -93
  78. data/unittests/html/div_xml.html +20 -20
  79. data/unittests/html/fileupload.html +45 -45
  80. data/unittests/html/formTest1.html +38 -38
  81. data/unittests/html/forms2.html +44 -44
  82. data/unittests/html/forms3.html +131 -131
  83. data/unittests/html/forms4.html +27 -27
  84. data/unittests/html/frame_buttons.html +3 -3
  85. data/unittests/html/frame_links.html +3 -3
  86. data/unittests/html/frame_multi.html +4 -4
  87. data/unittests/html/google_india.html +119 -119
  88. data/unittests/html/iframeTest.html +12 -12
  89. data/unittests/html/iframeTest1.html +6 -6
  90. data/unittests/html/iframeTest2.html +5 -5
  91. data/unittests/html/images1.html +66 -66
  92. data/unittests/html/javascriptevents.html +35 -35
  93. data/unittests/html/link_pass.html +10 -10
  94. data/unittests/html/links1.html +38 -38
  95. data/unittests/html/links2.html +10 -10
  96. data/unittests/html/links_multi.html +14 -14
  97. data/unittests/html/list_matters.html +720 -720
  98. data/unittests/html/lists.html +17 -17
  99. data/unittests/html/map_test.html +31 -31
  100. data/unittests/html/modal_dialog.html +10 -10
  101. data/unittests/html/modal_dialog_launcher.html +11 -11
  102. data/unittests/html/nestedFrames.html +6 -6
  103. data/unittests/html/new_browser.html +16 -16
  104. data/unittests/html/pass.html +12 -12
  105. data/unittests/html/popups1.html +59 -59
  106. data/unittests/html/pre.html +27 -27
  107. data/unittests/html/radioButtons1.html +71 -71
  108. data/unittests/html/select_tealeaf.html +54 -54
  109. data/unittests/html/selectboxes1.html +52 -52
  110. data/unittests/html/simple_table.html +26 -26
  111. data/unittests/html/simple_table_buttons.html +104 -104
  112. data/unittests/html/simple_table_columns.html +76 -76
  113. data/unittests/html/table1.html +181 -181
  114. data/unittests/html/tableCell_using_xpath.html +19 -19
  115. data/unittests/html/table_and_tablerow_to_a.html +174 -0
  116. data/unittests/html/textarea.html +30 -30
  117. data/unittests/html/textfields1.html +88 -88
  118. data/unittests/html/textsearch.html +44 -44
  119. data/unittests/html/wallofcheckboxes.html +1003 -1003
  120. data/unittests/html/xpath_nbsp.html +12 -12
  121. data/unittests/ie_exists_test.rb +33 -33
  122. data/unittests/ie_mock.rb +94 -94
  123. data/unittests/ie_test.rb +51 -51
  124. data/unittests/images_test.rb +157 -157
  125. data/unittests/images_xpath_test.rb +91 -91
  126. data/unittests/links_multi_test.rb +48 -48
  127. data/unittests/links_test.rb +175 -175
  128. data/unittests/links_xpath_test.rb +39 -39
  129. data/unittests/lists_test.rb +32 -32
  130. data/unittests/map_test.rb +98 -98
  131. data/unittests/minmax_test.rb +31 -31
  132. data/unittests/navigate_test.rb +39 -39
  133. data/unittests/nbsp_xpath_test.rb +16 -16
  134. data/unittests/non_core_tests.rb +12 -12
  135. data/unittests/other/WindowLogonExample.rb +27 -27
  136. data/unittests/other/WindowLogonExtra.rb +6 -6
  137. data/unittests/other/all_tests_concurrent.rb +57 -57
  138. data/unittests/other/jscriptExtraAlert.rb +6 -6
  139. data/unittests/other/jscriptExtraConfirmCancel.rb +6 -6
  140. data/unittests/other/jscriptExtraConfirmOk.rb +6 -6
  141. data/unittests/other/jscriptPushButton.rb +6 -6
  142. data/unittests/other/jscript_test.rb +63 -63
  143. data/unittests/other/navigate_exception_test.rb +24 -24
  144. data/unittests/other/rexml_unit_test.rb +27 -27
  145. data/unittests/other/screen_capture_test.rb +54 -54
  146. data/unittests/other/testcase_method_order_test.rb +35 -35
  147. data/unittests/other/testcase_verify_test.rb +24 -24
  148. data/unittests/other/wait_until_test.rb +99 -99
  149. data/unittests/pagecontainstext_test.rb +69 -69
  150. data/unittests/parent_child_test.rb +43 -43
  151. data/unittests/perf_test.rb +20 -20
  152. data/unittests/popups_test.rb +43 -43
  153. data/unittests/pre_test.rb +53 -53
  154. data/unittests/radios_test.rb +212 -212
  155. data/unittests/radios_xpath_test.rb +101 -101
  156. data/unittests/security_setting_test.rb +23 -23
  157. data/unittests/selectbox_test.rb +148 -148
  158. data/unittests/selectbox_xpath_test.rb +113 -113
  159. data/unittests/setup.rb +87 -77
  160. data/unittests/speed_settings_test.rb +67 -67
  161. data/unittests/table_and_tablerow_to_a_test.rb +117 -0
  162. data/unittests/table_cell_using_xpath_test.rb +35 -35
  163. data/unittests/table_test.rb +376 -376
  164. data/unittests/table_xpath_test.rb +110 -110
  165. data/unittests/test_tests.rb +9 -9
  166. data/unittests/textarea_test.rb +92 -92
  167. data/unittests/textarea_xpath_test.rb +78 -78
  168. data/unittests/textfield_for_ch_char_test.rb +31 -31
  169. data/unittests/textfields_test.rb +218 -218
  170. data/unittests/textfields_xpath_test.rb +111 -111
  171. data/unittests/window_tests.rb +10 -10
  172. data/unittests/windows/attach_to_existing_window_test.rb +53 -53
  173. data/unittests/windows/attach_to_new_window_test.rb +83 -83
  174. data/unittests/windows/close_window_test.rb +20 -20
  175. data/unittests/windows/frame_links_test.rb +25 -25
  176. data/unittests/windows/ie-each_test.rb +47 -47
  177. data/unittests/windows/iedialog_test.rb +54 -54
  178. data/unittests/windows/js_events_test.rb +42 -55
  179. data/unittests/windows/modal_dialog_test.rb +128 -128
  180. data/unittests/windows/new_test.rb +57 -57
  181. data/unittests/windows/open_close_test.rb +18 -18
  182. data/unittests/windows/send_keys_test.rb +33 -33
  183. data/unittests/xpath_tests.rb +10 -10
  184. metadata +85 -31
  185. data/lib/watir/utils.rb +0 -20
  186. data/lib/watir/version.rb +0 -5
@@ -1,21 +1,21 @@
1
- # http://www.vbcity.com/forums/topic.asp?tid=108859
2
- require 'watir/ie'
3
- module Watir
4
- module PageContainer
5
- include Win32
6
- def enabled_popup(timeout=4)
7
- # Use handle of our parent window to see if we have any currently
8
- # enabled popup.
9
- hwnd_modal = 0
10
- Waiter.wait_until(timeout) do
11
- hwnd_modal, arr = GetWindow.call(hwnd, GW_ENABLEDPOPUP)
12
- hwnd_modal > 0
13
- end
14
- # use hwnd() method to find the IE or Container hwnd (overriden by IE)
15
- if hwnd_modal == hwnd() || 0 == hwnd_modal
16
- hwnd_modal = nil
17
- end
18
- hwnd_modal
19
- end
20
- end
1
+ # http://www.vbcity.com/forums/topic.asp?tid=108859
2
+ require 'watir/ie'
3
+ module Watir
4
+ module PageContainer
5
+ include Win32
6
+ def enabled_popup(timeout=4)
7
+ # Use handle of our parent window to see if we have any currently
8
+ # enabled popup.
9
+ hwnd_modal = 0
10
+ Waiter.wait_until(timeout) do
11
+ hwnd_modal, arr = GetWindow.call(hwnd, GW_ENABLEDPOPUP)
12
+ hwnd_modal > 0
13
+ end
14
+ # use hwnd() method to find the IE or Container hwnd (overriden by IE)
15
+ if hwnd_modal == hwnd() || 0 == hwnd_modal
16
+ hwnd_modal = nil
17
+ end
18
+ hwnd_modal
19
+ end
20
+ end
21
21
  end
@@ -1,27 +1,27 @@
1
- # based on http://svn.instiki.org/instiki/trunk/test/watir/e2e.rb
2
- # and http://rubyforge.org/pipermail/wtr-general/2005-November/004108.html
3
-
4
- require 'watir/ie-process'
5
-
6
- class IEProcess < Watir::IE::Process
7
- def stop
8
- right_to_terminate_process = 1
9
- handle = Win32API.new('kernel32.dll', 'OpenProcess', 'lil', 'l').
10
- call(right_to_terminate_process, 0, @process_id)
11
- Win32API.new('kernel32.dll', 'TerminateProcess', 'll', 'l').call(handle, 0)
12
- end
13
-
14
- end
15
-
16
- module Watir
17
- class IE
18
- def process_id
19
- @process_id ||= IEProcess.process_id_from_hwnd @ie.hwnd
20
- end
21
- attr_writer :process_id
22
- def kill
23
- iep = IEProcess.new process_id
24
- iep.stop
25
- end
26
- end
27
- end
1
+ # based on http://svn.instiki.org/instiki/trunk/test/watir/e2e.rb
2
+ # and http://rubyforge.org/pipermail/wtr-general/2005-November/004108.html
3
+
4
+ require 'watir/ie-process'
5
+
6
+ class IEProcess < Watir::IE::Process
7
+ def stop
8
+ right_to_terminate_process = 1
9
+ handle = Win32API.new('kernel32.dll', 'OpenProcess', 'lil', 'l').
10
+ call(right_to_terminate_process, 0, @process_id)
11
+ Win32API.new('kernel32.dll', 'TerminateProcess', 'll', 'l').call(handle, 0)
12
+ end
13
+
14
+ end
15
+
16
+ module Watir
17
+ class IE
18
+ def process_id
19
+ @process_id ||= IEProcess.process_id_from_hwnd @ie.hwnd
20
+ end
21
+ attr_writer :process_id
22
+ def kill
23
+ iep = IEProcess.new process_id
24
+ iep.stop
25
+ end
26
+ end
27
+ end
@@ -1,29 +1,29 @@
1
- # This module includes checkers which are run on every page load
2
- #
3
- # In order to use this module, add a require to one of your test scripts:
4
- # require 'watir/contrib/page_checker'
5
- # To add checkers, call the ie.add_checker method
6
- #
7
- # ie.add_checker(PageCheckers::NAVIGATION_CHECKER)
8
- #
9
- # Checkers are Ruby proc objects which are called within Watir::IE and passed
10
- # the current instance of ie.
11
-
12
- module PageCheckers
13
-
14
- # This checker iterates through the current document including any frames
15
- # and checks for http errors, 404, 500 etc
16
- NAVIGATION_CHECKER = lambda do |ie|
17
- if ie.document.frames.length > 1
18
- 1.upto ie.document.frames.length do |i|
19
- begin
20
- ie.frame(:index, i).check_for_http_error
21
- rescue Watir::Exception::UnknownFrameException
22
- # frame can be already destroyed
23
- end
24
- end
25
- else
26
- ie.check_for_http_error
27
- end
28
- end
29
- end
1
+ # This module includes checkers which are run on every page load
2
+ #
3
+ # In order to use this module, add a require to one of your test scripts:
4
+ # require 'watir/contrib/page_checker'
5
+ # To add checkers, call the ie.add_checker method
6
+ #
7
+ # ie.add_checker(PageCheckers::NAVIGATION_CHECKER)
8
+ #
9
+ # Checkers are Ruby proc objects which are called within Watir::IE and passed
10
+ # the current instance of ie.
11
+
12
+ module PageCheckers
13
+
14
+ # This checker iterates through the current document including any frames
15
+ # and checks for http errors, 404, 500 etc
16
+ NAVIGATION_CHECKER = lambda do |ie|
17
+ if ie.document.frames.length > 1
18
+ 1.upto ie.document.frames.length do |i|
19
+ begin
20
+ ie.frame(:index, i).check_for_http_error
21
+ rescue Watir::Exception::UnknownFrameException
22
+ # frame can be already destroyed
23
+ end
24
+ end
25
+ else
26
+ ie.check_for_http_error
27
+ end
28
+ end
29
+ end
@@ -1,56 +1,56 @@
1
- # should be moved to watir/contrib
2
- # use of modules here is ugly
3
-
4
- require 'find'
5
- module Watir
6
- module CookieManager
7
-
8
- class Dir
9
- def Dir.visit(dir = '.', files_first = false, &block)
10
- if files_first
11
- paths = []
12
- Find.find(dir) { |path| paths << path }
13
- paths.reverse_each {|path| yield path}
14
- else
15
- Find.find(dir, &block)
16
- end
17
- end
18
- # simulates unix rm -rf command
19
- def Dir.rm_rf(dir)
20
- Dir.visit(dir, true) do |path|
21
- if FileTest.directory?(path)
22
- begin
23
- Dir.unlink(path)
24
- rescue # Security Exception for Content.IE
25
- end
26
- else
27
- begin
28
- File.unlink(path)
29
- rescue #Security exception index.dat etc.
30
- end
31
- end
32
- end
33
- end
34
- end
35
-
36
- class WatirHelper
37
- #taken from shlObj.h used in win32 SHGetSpecialFolderLocation
38
- #define CSIDL_INTERNET_CACHE 0x0020
39
- #define CSIDL_COOKIES 0x0021
40
- #define CSIDL_HISTORY 0x0022
41
- COOKIES = 0x0021
42
- INTERNET_CACHE = 0x0020
43
-
44
- def WatirHelper.getSpecialFolderLocation(specFolderName)
45
- shell = WIN32OLE.new('Shell.Application')
46
- folder = shell.Namespace(specFolderName)
47
- folderItem = folder.Self
48
- folderPath = folderItem.Path
49
- end
50
- def WatirHelper.deleteSpecialFolderContents(specFolderName)
51
- Dir.rm_rf(self.getSpecialFolderLocation(specFolderName))
52
- end
53
-
54
- end
55
- end
1
+ # should be moved to watir/contrib
2
+ # use of modules here is ugly
3
+
4
+ require 'find'
5
+ module Watir
6
+ module CookieManager
7
+
8
+ class Dir
9
+ def Dir.visit(dir = '.', files_first = false, &block)
10
+ if files_first
11
+ paths = []
12
+ Find.find(dir) { |path| paths << path }
13
+ paths.reverse_each {|path| yield path}
14
+ else
15
+ Find.find(dir, &block)
16
+ end
17
+ end
18
+ # simulates unix rm -rf command
19
+ def Dir.rm_rf(dir)
20
+ Dir.visit(dir, true) do |path|
21
+ if FileTest.directory?(path)
22
+ begin
23
+ Dir.unlink(path)
24
+ rescue # Security Exception for Content.IE
25
+ end
26
+ else
27
+ begin
28
+ File.unlink(path)
29
+ rescue #Security exception index.dat etc.
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+
36
+ class WatirHelper
37
+ #taken from shlObj.h used in win32 SHGetSpecialFolderLocation
38
+ #define CSIDL_INTERNET_CACHE 0x0020
39
+ #define CSIDL_COOKIES 0x0021
40
+ #define CSIDL_HISTORY 0x0022
41
+ COOKIES = 0x0021
42
+ INTERNET_CACHE = 0x0020
43
+
44
+ def WatirHelper.getSpecialFolderLocation(specFolderName)
45
+ shell = WIN32OLE.new('Shell.Application')
46
+ folder = shell.Namespace(specFolderName)
47
+ folderItem = folder.Self
48
+ folderPath = folderItem.Path
49
+ end
50
+ def WatirHelper.deleteSpecialFolderContents(specFolderName)
51
+ Dir.rm_rf(self.getSpecialFolderLocation(specFolderName))
52
+ end
53
+
54
+ end
55
+ end
56
56
  end
@@ -0,0 +1,28 @@
1
+ # these require statements are needed for Watir
2
+ # to work with minimum functionality
3
+ #
4
+ # this is needed for #click_no_wait to perform faster
5
+ require 'watir/win32ole'
6
+
7
+ require 'logger'
8
+ require 'watir/exceptions'
9
+ require 'watir/matches'
10
+
11
+ require 'watir/core_ext'
12
+ require 'watir/logger'
13
+ require 'watir/container'
14
+ require 'watir/locator'
15
+ require 'watir/page-container'
16
+ require 'watir/ie-class'
17
+ require 'watir/element'
18
+ require 'watir/element_collections'
19
+ require 'watir/form'
20
+ require 'watir/non_control_elements'
21
+ require 'watir/input_elements'
22
+ require 'watir/table'
23
+ require 'watir/image'
24
+ require 'watir/link'
25
+ require 'watir/html_element'
26
+
27
+ require 'watir/waiter'
28
+ require 'watir/module'
@@ -1,18 +1,18 @@
1
- class String
2
-
3
- #
4
- # "Watir::Span" => "Span"
5
- #
6
-
7
- def demodulize
8
- gsub(/^.*::/, '')
9
- end
10
-
11
- #
12
- # "FooBar" => "foo_bar"
13
- #
14
-
15
- def underscore
16
- gsub(/\B[A-Z][^A-Z]/, '_\&').downcase.gsub(' ', '_')
17
- end
1
+ class String
2
+
3
+ #
4
+ # "Watir::Span" => "Span"
5
+ #
6
+
7
+ def demodulize
8
+ gsub(/^.*::/, '')
9
+ end
10
+
11
+ #
12
+ # "FooBar" => "foo_bar"
13
+ #
14
+
15
+ def underscore
16
+ gsub(/\B[A-Z][^A-Z]/, '_\&').downcase.gsub(' ', '_')
17
+ end
18
18
  end
@@ -1,107 +1,107 @@
1
- require 'watir/win32ole'
2
-
3
- class String
4
-
5
- def pred
6
- self[0..-2] << (self[-1]-1).chr
7
- end
8
-
9
- end
10
-
11
- class DataHandler
12
-
13
- # This allows you to put data in a csv file or an xls file and access it in your scripts for
14
- # a data driven approach.
15
- #
16
- # You can supply a file with extension csv or xls and it will just work
17
- #
18
- # e.g.
19
- # require 'datahandler'
20
- # d = DataHandler.new("data.csv")
21
- # loginId = d.data[0].LoginId --> line 1 of the csv file and column LoginId
22
- # pin = d.data[0].Pin --> line 1 of the cvs file and column Pin
23
- #
24
- # b = Datahandler.new("data.xls")
25
- # loginId = b.data[0].LoginId --> line 1 of workbook 1 of the xls file with column LoginId
26
- #
27
- # Remember that line 1 of the data is referenced by using 0 - and this excludes the header line.
28
- #
29
- def initialize(datafile)
30
-
31
- # if the data files are in the same dir as the one you execute from this works
32
- directory = `dir`
33
- directory.match(/Directory of(.+)$/)
34
- working_directory = $1.to_s.gsub(/\\/,"/").gsub(/^\s/,"")
35
- file = "#{working_directory.chomp}\\#{datafile}"
36
-
37
- # otherwise it will take the location you specify
38
- if File.exists?(file)
39
- @datafile = file
40
- else
41
- @datafile = datafile
42
- end
43
-
44
- @excel = WIN32OLE::new('excel.Application')
45
- @workbook = @excel.Workbooks.Open(@datafile)
46
- @worksheet = @workbook.Worksheets(1)
47
- @excel['Visible'] = false
48
-
49
- end
50
-
51
- def csv_data
52
- csv_data = File.readlines(@datafile)
53
- header = csv_data[0].split(",")
54
- header.each{|z| z.chomp!}
55
- data = Struct.new("Data", *header)
56
- csv_data.shift
57
- data_array = []
58
- csv_data.each do |line|
59
- line.each{|z| line.chomp!}
60
- data_array << data.new(*line.split(","))
61
- end
62
- return data_array
63
- end
64
-
65
- def number_columns
66
- column = last_column = "a"
67
- while @worksheet.Range("a1:#{column}1")['Value']
68
- if !@worksheet.Range("#{column}1")['Value']
69
- last_column = column.pred
70
- break
71
- end
72
- column.succ!
73
- end
74
- last_column
75
- end
76
-
77
- def xls_data
78
- line = "1"
79
- data = []
80
- while @worksheet.Range("a#{line}")['Value']
81
- data << @worksheet.Range("a#{line}:#{number_columns}#{line}")['Value']
82
- line.succ!
83
- end
84
-
85
- header = data[0].flatten
86
- dataObject = Struct.new("DataObject", *header)
87
- data.shift
88
- data_array = []
89
- data.each do |line|
90
- data_array << dataObject.new(*line.flatten)
91
- end
92
- @excel.Quit
93
- @excel = nil
94
- GC.start
95
- data_array
96
- end
97
-
98
- def data
99
- extension = @datafile.split(".")[-1]
100
- if extension == "xls"
101
- xls_data
102
- elsif extension == "csv"
103
- csv_data
104
- end
105
- end
106
-
107
- end
1
+ require 'watir/win32ole'
2
+
3
+ class String
4
+
5
+ def pred
6
+ self[0..-2] << (self[-1]-1).chr
7
+ end
8
+
9
+ end
10
+
11
+ class DataHandler
12
+
13
+ # This allows you to put data in a csv file or an xls file and access it in your scripts for
14
+ # a data driven approach.
15
+ #
16
+ # You can supply a file with extension csv or xls and it will just work
17
+ #
18
+ # e.g.
19
+ # require 'datahandler'
20
+ # d = DataHandler.new("data.csv")
21
+ # loginId = d.data[0].LoginId --> line 1 of the csv file and column LoginId
22
+ # pin = d.data[0].Pin --> line 1 of the cvs file and column Pin
23
+ #
24
+ # b = Datahandler.new("data.xls")
25
+ # loginId = b.data[0].LoginId --> line 1 of workbook 1 of the xls file with column LoginId
26
+ #
27
+ # Remember that line 1 of the data is referenced by using 0 - and this excludes the header line.
28
+ #
29
+ def initialize(datafile)
30
+
31
+ # if the data files are in the same dir as the one you execute from this works
32
+ directory = `dir`
33
+ directory.match(/Directory of(.+)$/)
34
+ working_directory = $1.to_s.gsub(/\\/,"/").gsub(/^\s/,"")
35
+ file = "#{working_directory.chomp}\\#{datafile}"
36
+
37
+ # otherwise it will take the location you specify
38
+ if File.exists?(file)
39
+ @datafile = file
40
+ else
41
+ @datafile = datafile
42
+ end
43
+
44
+ @excel = WIN32OLE::new('excel.Application')
45
+ @workbook = @excel.Workbooks.Open(@datafile)
46
+ @worksheet = @workbook.Worksheets(1)
47
+ @excel['Visible'] = false
48
+
49
+ end
50
+
51
+ def csv_data
52
+ csv_data = File.readlines(@datafile)
53
+ header = csv_data[0].split(",")
54
+ header.each{|z| z.chomp!}
55
+ data = Struct.new("Data", *header)
56
+ csv_data.shift
57
+ data_array = []
58
+ csv_data.each do |line|
59
+ line.each{|z| line.chomp!}
60
+ data_array << data.new(*line.split(","))
61
+ end
62
+ return data_array
63
+ end
64
+
65
+ def number_columns
66
+ column = last_column = "a"
67
+ while @worksheet.Range("a1:#{column}1")['Value']
68
+ if !@worksheet.Range("#{column}1")['Value']
69
+ last_column = column.pred
70
+ break
71
+ end
72
+ column.succ!
73
+ end
74
+ last_column
75
+ end
76
+
77
+ def xls_data
78
+ line = "1"
79
+ data = []
80
+ while @worksheet.Range("a#{line}")['Value']
81
+ data << @worksheet.Range("a#{line}:#{number_columns}#{line}")['Value']
82
+ line.succ!
83
+ end
84
+
85
+ header = data[0].flatten
86
+ dataObject = Struct.new("DataObject", *header)
87
+ data.shift
88
+ data_array = []
89
+ data.each do |line|
90
+ data_array << dataObject.new(*line.flatten)
91
+ end
92
+ @excel.Quit
93
+ @excel = nil
94
+ GC.start
95
+ data_array
96
+ end
97
+
98
+ def data
99
+ extension = @datafile.split(".")[-1]
100
+ if extension == "xls"
101
+ xls_data
102
+ elsif extension == "csv"
103
+ csv_data
104
+ end
105
+ end
106
+
107
+ end