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,108 +1,117 @@
1
- module Watir
2
- # A PageContainer contains an HTML Document. In other words, it is a
3
- # what JavaScript calls a Window.
4
- module PageContainer
5
- include Watir::Exception
6
-
7
- # This method checks the currently displayed page for http errors, 404, 500 etc
8
- # It gets called internally by the wait method, so a user does not need to call it explicitly
9
-
10
- def check_for_http_error
11
- # check for IE7
12
- n = self.document.invoke('parentWindow').navigator.appVersion
13
- m=/MSIE\s(.*?);/.match( n )
14
- if m and m[1] =='7.0'
15
- if m = /HTTP (\d\d\d.*)/.match( self.title )
16
- raise NavigationException, m[1]
17
- end
18
- else
19
- # assume its IE6
20
- url = self.document.location.href
21
- if /shdoclc.dll/.match(url)
22
- m = /id=IEText.*?>(.*?)</i.match(self.html)
23
- raise NavigationException, m[1] if m
24
- end
25
- end
26
- false
27
- end
28
-
29
- # The HTML Page
30
- def page
31
- document.documentelement
32
- end
33
- private :page
34
-
35
- # The HTML of the current page
36
- def html
37
- page.outerhtml
38
- end
39
-
40
- # The url of the page object.
41
- def url
42
- page.document.location.href
43
- end
44
-
45
- # The text of the current page
46
- def text
47
- page.innertext.strip
48
- end
49
-
50
- def eval_in_spawned_process(command)
51
- command.strip!
52
- load_path_code = _code_that_copies_readonly_array($LOAD_PATH, '$LOAD_PATH')
53
- ruby_code = "require 'watir/ie'; "
54
- # ruby_code = "$HIDE_IE = #{$HIDE_IE};" # This prevents attaching to a window from setting it visible. However modal dialogs cannot be attached to when not visible.
55
- ruby_code << "pc = #{attach_command}; " # pc = page container
56
- # IDEA: consider changing this to not use instance_eval (it makes the code hard to understand)
57
- ruby_code << "pc.instance_eval(#{command.inspect})"
58
- exec_string = "start rubyw -e #{(load_path_code + '; ' + ruby_code).inspect}"
59
- system(exec_string)
60
- end
61
-
62
- def set_container container
63
- @container = container
64
- @page_container = self
65
- end
66
-
67
- # This method is used to display the available html frames that Internet Explorer currently has loaded.
68
- # This method is usually only used for debugging test scripts.
69
- def show_frames
70
- if allFrames = document.frames
71
- count = allFrames.length
72
- puts "there are #{count} frames"
73
- for i in 0..count-1 do
74
- begin
75
- fname = allFrames.item(i).name.to_s
76
- puts "frame index: #{i + 1} name: #{fname}"
77
- rescue => e
78
- if e.to_s.match(/Access is denied/)
79
- puts "frame index: #{i + 1} Access Denied, see http://wiki.openqa.org/display/WTR/FAQ#access-denied"
80
- end
81
- end
82
- end
83
- else
84
- puts "no frames"
85
- end
86
- end
87
-
88
- # Search the current page for specified text or regexp.
89
- # Returns the index if the specified text was found.
90
- # Returns matchdata object if the specified regexp was found.
91
- #
92
- # *Deprecated*
93
- # Instead use
94
- # IE#text.include? target
95
- # or
96
- # IE#text.match target
97
- def contains_text(target)
98
- if target.kind_of? Regexp
99
- self.text.match(target)
100
- elsif target.kind_of? String
101
- self.text.index(target)
102
- else
103
- raise ArgumentError, "Argument #{target} should be a string or regexp."
104
- end
105
- end
106
-
107
- end # module
1
+ module Watir
2
+ # A PageContainer contains an HTML Document. In other words, it is a
3
+ # what JavaScript calls a Window.
4
+ module PageContainer
5
+ include Watir::Exception
6
+
7
+ # This method checks the currently displayed page for http errors, 404, 500 etc
8
+ # It gets called internally by the wait method, so a user does not need to call it explicitly
9
+
10
+ def check_for_http_error
11
+ # check for IE7
12
+ n = self.document.invoke('parentWindow').navigator.appVersion
13
+ m=/MSIE\s(.*?);/.match( n )
14
+ if m and m[1] =='7.0'
15
+ if m = /HTTP (\d\d\d.*)/.match( self.title )
16
+ raise NavigationException, m[1]
17
+ end
18
+ else
19
+ # assume its IE6
20
+ url = self.document.location.href
21
+ if /shdoclc.dll/.match(url)
22
+ m = /id=IEText.*?>(.*?)</i.match(self.html)
23
+ raise NavigationException, m[1] if m
24
+ end
25
+ end
26
+ false
27
+ end
28
+
29
+ # The HTML Page
30
+ def page
31
+ document.documentelement
32
+ end
33
+
34
+ private :page
35
+
36
+ # The HTML of the current page
37
+ def html
38
+ page.outerhtml
39
+ end
40
+
41
+ # The url of the page object.
42
+ def url
43
+ page.document.location.href
44
+ end
45
+
46
+ # The text of the current page
47
+ def text
48
+ page.innertext.strip
49
+ end
50
+
51
+ def click_no_wait(element)
52
+ ruby_code = "require 'rubygems';" <<
53
+ "require '#{File.expand_path(File.dirname(__FILE__))}/core';" <<
54
+ "#{element}.click!"
55
+ system(spawned_click_no_wait_command(ruby_code))
56
+ end
57
+
58
+ def spawned_click_no_wait_command(command)
59
+ unless $DEBUG
60
+ "start rubyw -e #{command.inspect}"
61
+ else
62
+ puts "#click_no_wait command:"
63
+ command = "ruby -e #{command.inspect}"
64
+ puts command
65
+ command
66
+ end
67
+ end
68
+
69
+ private :spawned_click_no_wait_command
70
+
71
+ def set_container container
72
+ @container = container
73
+ @page_container = self
74
+ end
75
+
76
+ # This method is used to display the available html frames that Internet Explorer currently has loaded.
77
+ # This method is usually only used for debugging test scripts.
78
+ def show_frames
79
+ if allFrames = document.frames
80
+ count = allFrames.length
81
+ puts "there are #{count} frames"
82
+ for i in 0..count-1 do
83
+ begin
84
+ fname = allFrames.item(i).name.to_s
85
+ puts "frame index: #{i + 1} name: #{fname}"
86
+ rescue => e
87
+ if e.to_s.match(/Access is denied/)
88
+ puts "frame index: #{i + 1} Access Denied, see http://wiki.openqa.org/display/WTR/FAQ#access-denied"
89
+ end
90
+ end
91
+ end
92
+ else
93
+ puts "no frames"
94
+ end
95
+ end
96
+
97
+ # Search the current page for specified text or regexp.
98
+ # Returns the index if the specified text was found.
99
+ # Returns matchdata object if the specified regexp was found.
100
+ #
101
+ # *Deprecated*
102
+ # Instead use
103
+ # IE#text.include? target
104
+ # or
105
+ # IE#text.match target
106
+ def contains_text(target)
107
+ if target.kind_of? Regexp
108
+ self.text.match(target)
109
+ elsif target.kind_of? String
110
+ self.text.index(target)
111
+ else
112
+ raise ArgumentError, "Argument #{target} should be a string or regexp."
113
+ end
114
+ end
115
+
116
+ end # module
108
117
  end
@@ -1,30 +1,30 @@
1
- module Watir
2
- # POPUP object
3
- class PopUp
4
- def initialize(container)
5
- @container = container
6
- @page_container = container.page_container
7
- end
8
-
9
- def button(caption)
10
- return JSButton.new(@container.getIE.hwnd, caption)
11
- end
12
- end
13
-
14
- class JSButton
15
- def initialize(hWnd, caption)
16
- @hWnd = hWnd
17
- @caption = caption
18
- end
19
-
20
- def startClicker(waitTime=3)
21
- clicker = WinClicker.new
22
- clicker.clickJSDialog_Thread
23
- # clickerThread = Thread.new(@caption) {
24
- # sleep waitTime
25
- # puts "After the wait time in startClicker"
26
- # clickWindowsButton_hwnd(hwnd, buttonCaption)
27
- #}
28
- end
29
- end
1
+ module Watir
2
+ # POPUP object
3
+ class PopUp
4
+ def initialize(container)
5
+ @container = container
6
+ @page_container = container.page_container
7
+ end
8
+
9
+ def button(caption)
10
+ return JSButton.new(@container.getIE.hwnd, caption)
11
+ end
12
+ end
13
+
14
+ class JSButton
15
+ def initialize(hWnd, caption)
16
+ @hWnd = hWnd
17
+ @caption = caption
18
+ end
19
+
20
+ def startClicker(waitTime=3)
21
+ clicker = WinClicker.new
22
+ clicker.clickJSDialog_Thread
23
+ # clickerThread = Thread.new(@caption) {
24
+ # sleep waitTime
25
+ # puts "After the wait time in startClicker"
26
+ # clickWindowsButton_hwnd(hwnd, buttonCaption)
27
+ #}
28
+ end
29
+ end
30
30
  end
@@ -1,20 +1,20 @@
1
- module Watir
2
- module Process
3
-
4
- # Returns the number of windows processes running with the specified name.
5
- def self.count name
6
- mgmt = WIN32OLE.connect('winmgmts:\\\\.')
7
- processes = mgmt.InstancesOf('win32_process')
8
- processes.extend Enumerable
9
- processes.select{|x| x.name == name}.length
10
- end
11
-
12
- end
13
-
14
- class IE
15
- # Returns the number of IEXPLORE processes currently running.
16
- def self.process_count
17
- Watir::Process.count 'iexplore.exe'
18
- end
19
- end
1
+ module Watir
2
+ module Process
3
+
4
+ # Returns the number of windows processes running with the specified name.
5
+ def self.count name
6
+ mgmt = WIN32OLE.connect('winmgmts:\\\\.')
7
+ processes = mgmt.InstancesOf('win32_process')
8
+ processes.extend Enumerable
9
+ processes.select{|x| x.name == name}.length
10
+ end
11
+
12
+ end
13
+
14
+ class IE
15
+ # Returns the number of IEXPLORE processes currently running.
16
+ def self.process_count
17
+ Watir::Process.count 'iexplore.exe'
18
+ end
19
+ end
20
20
  end
@@ -1,115 +1,115 @@
1
-
2
- require 'Win32API'
3
-
4
- module Watir
5
- module ScreenCapture
6
-
7
- KEYEVENTF_KEYUP = 0x2
8
- SW_HIDE = 0
9
- SW_SHOW = 5
10
- SW_SHOWNORMAL = 1
11
- VK_CONTROL = 0x11
12
- VK_F4 = 0x73
13
- VK_MENU = 0x12
14
- VK_RETURN = 0x0D
15
- VK_SHIFT = 0x10
16
- VK_SNAPSHOT = 0x2C
17
- VK_TAB = 0x09
18
- GMEM_MOVEABLE = 0x0002
19
- CF_TEXT = 1
20
-
21
- # this method saves the current window or whole screen as either a bitmap or a jpeg
22
- # It uses paint to save the file, so will barf if a duplicate filename is selected, or the path doesnt exist etc
23
- # * filename - string - the name of the file to save. If its not fully qualified the current directory is used
24
- # * active_window - boolean - if true, the whole screen is captured, if false, just the active window is captured
25
- # * save_as_bmp - boolean - if true saves the file as a bitmap, saves it as a jpeg otherwise
26
- def screen_capture(filename , active_window_only=false, save_as_bmp=false)
27
-
28
-
29
- keybd_event = Win32API.new("user32", "keybd_event", ['I','I','L','L'], 'V')
30
- vkKeyScan = Win32API.new("user32", "VkKeyScan", ['I'], 'I')
31
- winExec = Win32API.new("kernel32", "WinExec", ['P','L'], 'L')
32
- openClipboard = Win32API.new('user32', 'OpenClipboard', ['L'], 'I')
33
- setClipboardData = Win32API.new('user32', 'SetClipboardData', ['I', 'I'], 'I')
34
- closeClipboard = Win32API.new('user32', 'CloseClipboard', [], 'I')
35
- globalAlloc = Win32API.new('kernel32', 'GlobalAlloc', ['I', 'I'], 'I')
36
- globalLock = Win32API.new('kernel32', 'GlobalLock', ['I'], 'I')
37
- globalUnlock = Win32API.new('kernel32', 'GlobalUnlock', ['I'], 'I')
38
- memcpy = Win32API.new('msvcrt', 'memcpy', ['I', 'P', 'I'], 'I')
39
-
40
-
41
- filename = Dir.getwd.tr('/','\\') + '\\' + filename unless filename.index('\\')
42
-
43
- if active_window_only ==false
44
- keybd_event.Call(VK_SNAPSHOT,0,0,0) # Print Screen
45
- else
46
- keybd_event.Call(VK_SNAPSHOT,1,0,0) # Alt+Print Screen
47
- end
48
-
49
- winExec.Call('mspaint.exe', SW_SHOW)
50
- sleep(1)
51
-
52
- # Ctrl + V : Paste
53
- keybd_event.Call(VK_CONTROL, 1, 0, 0)
54
- keybd_event.Call(vkKeyScan.Call(?V), 1, 0, 0)
55
- keybd_event.Call(vkKeyScan.Call(?V), 1, KEYEVENTF_KEYUP, 0)
56
- keybd_event.Call(VK_CONTROL, 1, KEYEVENTF_KEYUP, 0)
57
-
58
-
59
- # Alt F + A : Save As
60
- keybd_event.Call(VK_MENU, 1, 0, 0)
61
- keybd_event.Call(vkKeyScan.Call(?F), 1, 0, 0)
62
- keybd_event.Call(vkKeyScan.Call(?F), 1, KEYEVENTF_KEYUP, 0)
63
- keybd_event.Call(VK_MENU, 1, KEYEVENTF_KEYUP, 0)
64
- keybd_event.Call(vkKeyScan.Call(?A), 1, 0, 0)
65
- keybd_event.Call(vkKeyScan.Call(?A), 1, KEYEVENTF_KEYUP, 0)
66
- sleep(1)
67
-
68
- # copy filename to clipboard
69
- hmem = globalAlloc.Call(GMEM_MOVEABLE, filename.length+1)
70
- mem = globalLock.Call(hmem)
71
- memcpy.Call(mem, filename, filename.length+1)
72
- globalUnlock.Call(hmem)
73
- openClipboard.Call(0)
74
- setClipboardData.Call(CF_TEXT, hmem)
75
- closeClipboard.Call
76
- sleep(1)
77
-
78
- # Ctrl + V : Paste
79
- keybd_event.Call(VK_CONTROL, 1, 0, 0)
80
- keybd_event.Call(vkKeyScan.Call(?V), 1, 0, 0)
81
- keybd_event.Call(vkKeyScan.Call(?V), 1, KEYEVENTF_KEYUP, 0)
82
- keybd_event.Call(VK_CONTROL, 1, KEYEVENTF_KEYUP, 0)
83
-
84
- if save_as_bmp == false
85
- # goto the combo box
86
- keybd_event.Call(VK_TAB, 1, 0, 0)
87
- keybd_event.Call(VK_TAB, 1, KEYEVENTF_KEYUP, 0)
88
- sleep(0.5)
89
-
90
- # select the first entry with J
91
- keybd_event.Call(vkKeyScan.Call(?J), 1, 0, 0)
92
- keybd_event.Call(vkKeyScan.Call(?J), 1, KEYEVENTF_KEYUP, 0)
93
- sleep(0.5)
94
- end
95
-
96
- # Enter key
97
- keybd_event.Call(VK_RETURN, 1, 0, 0)
98
- keybd_event.Call(VK_RETURN, 1, KEYEVENTF_KEYUP, 0)
99
- sleep(1)
100
-
101
- # Alt + F4 : Exit
102
- keybd_event.Call(VK_MENU, 1, 0, 0)
103
- keybd_event.Call(VK_F4, 1, 0, 0)
104
- keybd_event.Call(VK_F4, 1, KEYEVENTF_KEYUP, 0)
105
- keybd_event.Call(VK_MENU, 1, KEYEVENTF_KEYUP, 0)
106
- sleep(1)
107
-
108
- end
109
- end
110
-
111
- end
112
-
113
-
114
- #screenCapture( "f.bmp", false , true)
115
-
1
+
2
+ require 'Win32API'
3
+
4
+ module Watir
5
+ module ScreenCapture
6
+
7
+ KEYEVENTF_KEYUP = 0x2
8
+ SW_HIDE = 0
9
+ SW_SHOW = 5
10
+ SW_SHOWNORMAL = 1
11
+ VK_CONTROL = 0x11
12
+ VK_F4 = 0x73
13
+ VK_MENU = 0x12
14
+ VK_RETURN = 0x0D
15
+ VK_SHIFT = 0x10
16
+ VK_SNAPSHOT = 0x2C
17
+ VK_TAB = 0x09
18
+ GMEM_MOVEABLE = 0x0002
19
+ CF_TEXT = 1
20
+
21
+ # this method saves the current window or whole screen as either a bitmap or a jpeg
22
+ # It uses paint to save the file, so will barf if a duplicate filename is selected, or the path doesnt exist etc
23
+ # * filename - string - the name of the file to save. If its not fully qualified the current directory is used
24
+ # * active_window - boolean - if true, the whole screen is captured, if false, just the active window is captured
25
+ # * save_as_bmp - boolean - if true saves the file as a bitmap, saves it as a jpeg otherwise
26
+ def screen_capture(filename , active_window_only=false, save_as_bmp=false)
27
+
28
+
29
+ keybd_event = Win32API.new("user32", "keybd_event", ['I','I','L','L'], 'V')
30
+ vkKeyScan = Win32API.new("user32", "VkKeyScan", ['I'], 'I')
31
+ winExec = Win32API.new("kernel32", "WinExec", ['P','L'], 'L')
32
+ openClipboard = Win32API.new('user32', 'OpenClipboard', ['L'], 'I')
33
+ setClipboardData = Win32API.new('user32', 'SetClipboardData', ['I', 'I'], 'I')
34
+ closeClipboard = Win32API.new('user32', 'CloseClipboard', [], 'I')
35
+ globalAlloc = Win32API.new('kernel32', 'GlobalAlloc', ['I', 'I'], 'I')
36
+ globalLock = Win32API.new('kernel32', 'GlobalLock', ['I'], 'I')
37
+ globalUnlock = Win32API.new('kernel32', 'GlobalUnlock', ['I'], 'I')
38
+ memcpy = Win32API.new('msvcrt', 'memcpy', ['I', 'P', 'I'], 'I')
39
+
40
+
41
+ filename = Dir.getwd.tr('/','\\') + '\\' + filename unless filename.index('\\')
42
+
43
+ if active_window_only ==false
44
+ keybd_event.Call(VK_SNAPSHOT,0,0,0) # Print Screen
45
+ else
46
+ keybd_event.Call(VK_SNAPSHOT,1,0,0) # Alt+Print Screen
47
+ end
48
+
49
+ winExec.Call('mspaint.exe', SW_SHOW)
50
+ sleep(1)
51
+
52
+ # Ctrl + V : Paste
53
+ keybd_event.Call(VK_CONTROL, 1, 0, 0)
54
+ keybd_event.Call(vkKeyScan.Call(?V), 1, 0, 0)
55
+ keybd_event.Call(vkKeyScan.Call(?V), 1, KEYEVENTF_KEYUP, 0)
56
+ keybd_event.Call(VK_CONTROL, 1, KEYEVENTF_KEYUP, 0)
57
+
58
+
59
+ # Alt F + A : Save As
60
+ keybd_event.Call(VK_MENU, 1, 0, 0)
61
+ keybd_event.Call(vkKeyScan.Call(?F), 1, 0, 0)
62
+ keybd_event.Call(vkKeyScan.Call(?F), 1, KEYEVENTF_KEYUP, 0)
63
+ keybd_event.Call(VK_MENU, 1, KEYEVENTF_KEYUP, 0)
64
+ keybd_event.Call(vkKeyScan.Call(?A), 1, 0, 0)
65
+ keybd_event.Call(vkKeyScan.Call(?A), 1, KEYEVENTF_KEYUP, 0)
66
+ sleep(1)
67
+
68
+ # copy filename to clipboard
69
+ hmem = globalAlloc.Call(GMEM_MOVEABLE, filename.length+1)
70
+ mem = globalLock.Call(hmem)
71
+ memcpy.Call(mem, filename, filename.length+1)
72
+ globalUnlock.Call(hmem)
73
+ openClipboard.Call(0)
74
+ setClipboardData.Call(CF_TEXT, hmem)
75
+ closeClipboard.Call
76
+ sleep(1)
77
+
78
+ # Ctrl + V : Paste
79
+ keybd_event.Call(VK_CONTROL, 1, 0, 0)
80
+ keybd_event.Call(vkKeyScan.Call(?V), 1, 0, 0)
81
+ keybd_event.Call(vkKeyScan.Call(?V), 1, KEYEVENTF_KEYUP, 0)
82
+ keybd_event.Call(VK_CONTROL, 1, KEYEVENTF_KEYUP, 0)
83
+
84
+ if save_as_bmp == false
85
+ # goto the combo box
86
+ keybd_event.Call(VK_TAB, 1, 0, 0)
87
+ keybd_event.Call(VK_TAB, 1, KEYEVENTF_KEYUP, 0)
88
+ sleep(0.5)
89
+
90
+ # select the first entry with J
91
+ keybd_event.Call(vkKeyScan.Call(?J), 1, 0, 0)
92
+ keybd_event.Call(vkKeyScan.Call(?J), 1, KEYEVENTF_KEYUP, 0)
93
+ sleep(0.5)
94
+ end
95
+
96
+ # Enter key
97
+ keybd_event.Call(VK_RETURN, 1, 0, 0)
98
+ keybd_event.Call(VK_RETURN, 1, KEYEVENTF_KEYUP, 0)
99
+ sleep(1)
100
+
101
+ # Alt + F4 : Exit
102
+ keybd_event.Call(VK_MENU, 1, 0, 0)
103
+ keybd_event.Call(VK_F4, 1, 0, 0)
104
+ keybd_event.Call(VK_F4, 1, KEYEVENTF_KEYUP, 0)
105
+ keybd_event.Call(VK_MENU, 1, KEYEVENTF_KEYUP, 0)
106
+ sleep(1)
107
+
108
+ end
109
+ end
110
+
111
+ end
112
+
113
+
114
+ #screenCapture( "f.bmp", false , true)
115
+