watirgrid 0.0.7 → 0.0.8.pre

Sign up to get free protection for your applications and to get access to all the features.
data/lib/provider.rb CHANGED
@@ -1,242 +1,228 @@
1
- #!/usr/bin/env ruby
2
- # provider.rb
3
- # Rinda Ring Provider
4
-
5
- require 'rubygems'
6
- require 'rinda/ring'
7
- require 'rinda/tuplespace'
8
- require 'logger'
9
- require 'drb/acl'
10
- require 'uuid'
11
-
12
- begin
13
- require 'watir'
14
- rescue LoadError
15
- end
16
-
17
- begin
18
- require 'firewatir'
19
- rescue LoadError
20
-
21
- begin
22
- require 'safariwatir'
23
- rescue LoadError
24
- end
25
-
26
- end
27
-
28
- module Watir
29
-
30
- ##
31
- # Extend Watir with a Provider class
32
- # to determine which browser type is supported by the
33
- # remote DRb process. This returns the DRb front object.
34
- class Provider
35
-
36
- include DRbUndumped # all objects will be proxied, not copied
37
-
38
- attr_reader :browser
39
-
40
- def initialize(browser = nil)
41
- browser = (browser || 'tmp').downcase.to_sym
42
- case browser
43
- when :safari
44
- @browser = Watir::Safari
45
- when :firefox
46
- @browser = FireWatir::Firefox
47
- when :ie
48
- @browser = Watir::IE
49
- else
50
- @browser = find_supported_browser
51
- end
52
- end
53
-
54
- def find_supported_browser
55
- if Watir::IE then return Watir::IE end
56
- if FireWatir::Firefox then return FireWatir::Firefox end
57
- if Watir::Safari then return Watir::Safari end
58
- end
59
-
60
- def new_browser
61
- if @browser.nil?
62
- find_supported_browser.new
63
- else
64
- @browser.new
65
- end
66
- end
67
-
68
- ##
69
- # Get a list of running browsers (optionally specified by browser)
70
- # 'iexplore','firefox','firefox-bin','chrome','safari','opera'
71
- def get_running_browsers(browser=nil)
72
- browsers = browser || \
73
- ['iexplore','firefox','firefox-bin','chrome','safari','opera']
74
- case Config::CONFIG['arch']
75
- when /mswin/
76
- %x[tasklist].split(/\s+/).collect { |x| x[/\w+/]} \
77
- & browsers.collect { |x| x.downcase }
78
- when /linux|darwin/
79
- %x[ps -A | grep -v ruby].split(/\/|\s+/).collect { |x| x.downcase} \
80
- & browsers
81
- end
82
- end
83
-
84
- def get_running_processes
85
- %x[ps -A | grep -v ruby].split(/\/|\s+/).collect.uniq
86
- end
87
-
88
- ##
89
- # Kill any browser running
90
- def kill_all_browsers
91
- case Config::CONFIG['arch']
92
- when /mswin/
93
- browsers = ['iexplore.exe', 'firefox.exe', 'chrome.exe']
94
- browsers.each { |browser| %x[taskkill /F /IM #{browser}] }
95
- when /linux/
96
- browsers = ['firefox', 'chrome', 'opera']
97
- browsers.each { |browser| %x[killall -r #{browser}] }
98
- when /darwin/
99
- browsers = ['firefox-bin', 'Chrome', 'Safari']
100
- browsers.each { |browser| %x[killall -m #{browser}] }
101
- end
102
- end
103
-
104
- ##
105
- # Kill all browsers specified by browser name
106
- # Windows: 'iexplore.exe', 'firefox.exe', 'chrome.exe'
107
- # Linux: 'firefox', 'chrome', 'opera'
108
- # OSX: 'firefox-bin', 'Chrome', 'Safari'
109
- def kill_browser(browser)
110
- case Config::CONFIG['arch']
111
- when /mswin/
112
- %x[taskkill /F /IM #{browser}]
113
- when /linux/
114
- %x[killall -r #{browser}]
115
- when /darwin/
116
- %x[killall -m #{browser}]
117
- end
118
- end
119
-
120
- ##
121
- # Start firefox (with an optional bin path) using the -jssh extension
122
- def start_firefox_jssh(path=nil)
123
- case Config::CONFIG['arch']
124
- when /mswin/
125
- bin = path || "C:/Program Files/Mozilla Firefox/firefox.exe"
126
- when /linux/
127
- bin = path || "/usr/bin/firefox"
128
- when /darwin/
129
- bin = path || "/Applications/Firefox.app/Contents/MacOS/firefox-bin"
130
- end
131
- # fork off and die!
132
- Thread.new {system(bin, "about:blank", "-jssh")}
133
- end
134
-
135
- ##
136
- # Get the logged-in user
137
- def get_logged_in_user
138
- %x[whoami].chomp
139
- end
140
-
141
- ##
142
- # Grep for a process (Linux/OSX-with-port only)
143
- def process_grep(pattern)
144
- %x[pgrep -l #{pattern}].split(/\n/)
145
- end
146
-
147
- def renew_provider
148
- self.class.superclass
149
- end
150
-
151
- end
152
-
153
- end
154
-
155
- class Provider
156
-
157
- attr_accessor :drb_server_uri, :ring_server_uri
158
-
159
- def initialize(params = {})
160
- @drb_server_host = params[:drb_server_host] || external_interface
161
- @drb_server_port = params[:drb_server_port] || 0
162
- @ring_server_host = params[:ring_server_host] || external_interface
163
- @ring_server_port = params[:ring_server_port] || Rinda::Ring_PORT
164
-
165
- @renewer = params[:renewer] || Rinda::SimpleRenewer.new
166
- @browser_type = params[:browser_type] || nil
167
-
168
- logfile = params[:logfile] || STDOUT
169
- @log = Logger.new(logfile, 'daily')
170
- @log.level = params[:loglevel] || Logger::INFO
171
- @log.datetime_format = "%Y-%m-%d %H:%M:%S "
172
-
173
- end
174
-
175
- ##
176
- # Start providing watir objects on the ring server
177
- def start
178
- # create a DRb 'front' object
179
- watir_provider = Watir::Provider.new(@browser_type)
180
- architecture = Config::CONFIG['arch']
181
- hostname = ENV['SERVER_NAME'] || %x{hostname}.strip
182
-
183
- # setup the security--remember to call before DRb.start_service()
184
- DRb.install_acl(ACL.new(@acls))
185
-
186
- # start the DRb Server
187
- drb_server = DRb.start_service(
188
- "druby://#{@drb_server_host}:#{@drb_server_port}")
189
-
190
- # obtain DRb Server uri
191
- @drb_server_uri = drb_server.uri
192
- @log.info("DRb server started on : #{@drb_server_uri}")
193
-
194
- # create a service tuple
195
- @tuple = [
196
- :WatirGrid,
197
- :WatirProvider,
198
- watir_provider,
199
- 'A watir provider',
200
- hostname,
201
- architecture,
202
- @browser_type,
203
- UUID.new.generate
204
- ]
205
-
206
- # locate the Rinda Ring Server via a UDP broadcast
207
- @log.debug("Attempting to find ring server on : druby://#{@ring_server_host}:#{@ring_server_port}")
208
- ring_server = Rinda::RingFinger.new(@ring_server_host, @ring_server_port)
209
- ring_server = ring_server.lookup_ring_any
210
- @log.info("Ring server found on : druby://#{@ring_server_host}:#{@ring_server_port}")
211
-
212
- # advertise this service on the primary remote tuple space
213
- ring_server.write(@tuple, @renewer)
214
-
215
- # log DRb server uri
216
- @log.info("New tuple registered : druby://#{@ring_server_host}:#{@ring_server_port}")
217
-
218
- # wait for explicit stop via ctrl-c
219
- DRb.thread.join if __FILE__ == $0
220
- end
221
-
222
- ##
223
- # Stop the provider by shutting down the DRb service
224
- def stop
225
- DRb.stop_service
226
- @log.info("DRb server stopped on : #{@drb_server_uri}")
227
- end
228
-
229
- private
230
-
231
- ##
232
- # Get the external facing interface for this server
233
- def external_interface
234
- begin
235
- UDPSocket.open {|s| s.connect('ping.watirgrid.com', 1); s.addr.last }
236
- rescue
237
- '127.0.0.1'
238
- end
239
- end
240
-
241
- end
242
-
1
+ #!/usr/bin/env ruby
2
+ # provider.rb
3
+ # Rinda Ring Provider
4
+
5
+ require 'rubygems'
6
+ require 'rinda/ring'
7
+ require 'rinda/tuplespace'
8
+ require 'logger'
9
+ require 'drb/acl'
10
+ require 'uuid'
11
+
12
+ module Watir
13
+
14
+ ##
15
+ # Extend Watir with a Provider class
16
+ # to determine which browser type is supported by the
17
+ # remote DRb process. This returns the DRb front object.
18
+ class Provider
19
+
20
+ include DRbUndumped # all objects will be proxied, not copied
21
+
22
+ attr_reader :browser
23
+
24
+ def initialize(browser = nil)
25
+ browser = (browser || 'tmp').downcase.to_sym
26
+ case browser
27
+ when :safari
28
+ require 'safariwatir'
29
+ @browser = Watir::Safari
30
+ when :firefox
31
+ require 'firewatir'
32
+ @browser = FireWatir::Firefox
33
+ when :ie
34
+ require 'watir'
35
+ @browser = Watir::IE
36
+ when :webdriver
37
+ require 'watir-webdriver'
38
+ @browser = Watir::Browser
39
+ else
40
+ @browser = find_supported_browser
41
+ end
42
+ end
43
+
44
+ #~ def find_supported_browser
45
+ #~ if Watir::IE then return Watir::IE end
46
+ #~ if FireWatir::Firefox then return FireWatir::Firefox end
47
+ #~ if Watir::Safari then return Watir::Safari end
48
+ #~ end
49
+
50
+ def new_browser(webdriver_browser_type = nil)
51
+ @browser.new(webdriver_browser_type)
52
+ end
53
+
54
+ ##
55
+ # Get a list of running browsers (optionally specified by browser)
56
+ # 'iexplore','firefox','firefox-bin','chrome','safari','opera'
57
+ def get_running_browsers(browser=nil)
58
+ browsers = browser || \
59
+ ['iexplore','firefox','firefox-bin','chrome','safari','opera']
60
+ case Config::CONFIG['arch']
61
+ when /mswin/
62
+ %x[tasklist].split(/\s+/).collect { |x| x[/\w+/]} \
63
+ & browsers.collect { |x| x.downcase }
64
+ when /linux|darwin/
65
+ %x[ps -A | grep -v ruby].split(/\/|\s+/).collect { |x| x.downcase} \
66
+ & browsers
67
+ end
68
+ end
69
+
70
+ def get_running_processes
71
+ %x[ps -A | grep -v ruby].split(/\/|\s+/).collect.uniq
72
+ end
73
+
74
+ ##
75
+ # Kill any browser running
76
+ def kill_all_browsers
77
+ case Config::CONFIG['arch']
78
+ when /mswin/
79
+ browsers = ['iexplore.exe', 'firefox.exe', 'chrome.exe']
80
+ browsers.each { |browser| %x[taskkill /F /IM #{browser}] }
81
+ when /linux/
82
+ browsers = ['firefox', 'chrome', 'opera']
83
+ browsers.each { |browser| %x[killall -r #{browser}] }
84
+ when /darwin/
85
+ browsers = ['firefox-bin', 'Chrome', 'Safari']
86
+ browsers.each { |browser| %x[killall -m #{browser}] }
87
+ end
88
+ end
89
+
90
+ ##
91
+ # Kill all browsers specified by browser name
92
+ # Windows: 'iexplore.exe', 'firefox.exe', 'chrome.exe'
93
+ # Linux: 'firefox', 'chrome', 'opera'
94
+ # OSX: 'firefox-bin', 'Chrome', 'Safari'
95
+ def kill_browser(browser)
96
+ case Config::CONFIG['arch']
97
+ when /mswin/
98
+ %x[taskkill /F /IM #{browser}]
99
+ when /linux/
100
+ %x[killall -r #{browser}]
101
+ when /darwin/
102
+ %x[killall -m #{browser}]
103
+ end
104
+ end
105
+
106
+ ##
107
+ # Start firefox (with an optional bin path) using the -jssh extension
108
+ def start_firefox_jssh(path=nil)
109
+ case Config::CONFIG['arch']
110
+ when /mswin/
111
+ bin = path || "C:/Program Files/Mozilla Firefox/firefox.exe"
112
+ when /linux/
113
+ bin = path || "/usr/bin/firefox"
114
+ when /darwin/
115
+ bin = path || "/Applications/Firefox.app/Contents/MacOS/firefox-bin"
116
+ end
117
+ # fork off and die!
118
+ Thread.new {system(bin, "about:blank", "-jssh")}
119
+ end
120
+
121
+ ##
122
+ # Get the logged-in user
123
+ def get_logged_in_user
124
+ %x[whoami].chomp
125
+ end
126
+
127
+ ##
128
+ # Grep for a process (Linux/OSX-with-port only)
129
+ def process_grep(pattern)
130
+ %x[pgrep -l #{pattern}].split(/\n/)
131
+ end
132
+
133
+ def renew_provider
134
+ self.class.superclass
135
+ end
136
+
137
+ end
138
+
139
+ end
140
+
141
+ class Provider
142
+
143
+ attr_accessor :drb_server_uri, :ring_server_uri
144
+
145
+ def initialize(params = {})
146
+ @drb_server_host = params[:drb_server_host] || external_interface
147
+ @drb_server_port = params[:drb_server_port] || 0
148
+ @ring_server_host = params[:ring_server_host] || external_interface
149
+ @ring_server_port = params[:ring_server_port] || Rinda::Ring_PORT
150
+
151
+ @renewer = params[:renewer] || Rinda::SimpleRenewer.new
152
+ @browser_type = params[:browser_type] || nil
153
+
154
+ logfile = params[:logfile] || STDOUT
155
+ @log = Logger.new(logfile, 'daily')
156
+ @log.level = params[:loglevel] || Logger::INFO
157
+ @log.datetime_format = "%Y-%m-%d %H:%M:%S "
158
+
159
+ end
160
+
161
+ ##
162
+ # Start providing watir objects on the ring server
163
+ def start
164
+ # create a DRb 'front' object
165
+ watir_provider = Watir::Provider.new(@browser_type)
166
+ architecture = Config::CONFIG['arch']
167
+ hostname = ENV['SERVER_NAME'] || %x{hostname}.strip
168
+
169
+ # setup the security--remember to call before DRb.start_service()
170
+ DRb.install_acl(ACL.new(@acls))
171
+
172
+ # start the DRb Server
173
+ drb_server = DRb.start_service(
174
+ "druby://#{@drb_server_host}:#{@drb_server_port}")
175
+
176
+ # obtain DRb Server uri
177
+ @drb_server_uri = drb_server.uri
178
+ @log.info("DRb server started on : #{@drb_server_uri}")
179
+
180
+ # create a service tuple
181
+ @tuple = [
182
+ :WatirGrid,
183
+ :WatirProvider,
184
+ watir_provider,
185
+ 'A watir provider',
186
+ hostname,
187
+ architecture,
188
+ @browser_type,
189
+ UUID.new.generate
190
+ ]
191
+
192
+ # locate the Rinda Ring Server via a UDP broadcast
193
+ @log.debug("Attempting to find ring server on : druby://#{@ring_server_host}:#{@ring_server_port}")
194
+ ring_server = Rinda::RingFinger.new(@ring_server_host, @ring_server_port)
195
+ ring_server = ring_server.lookup_ring_any
196
+ @log.info("Ring server found on : druby://#{@ring_server_host}:#{@ring_server_port}")
197
+
198
+ # advertise this service on the primary remote tuple space
199
+ ring_server.write(@tuple, @renewer)
200
+
201
+ # log DRb server uri
202
+ @log.info("New tuple registered : druby://#{@ring_server_host}:#{@ring_server_port}")
203
+
204
+ # wait for explicit stop via ctrl-c
205
+ DRb.thread.join if __FILE__ == $0
206
+ end
207
+
208
+ ##
209
+ # Stop the provider by shutting down the DRb service
210
+ def stop
211
+ DRb.stop_service
212
+ @log.info("DRb server stopped on : #{@drb_server_uri}")
213
+ end
214
+
215
+ private
216
+
217
+ ##
218
+ # Get the external facing interface for this server
219
+ def external_interface
220
+ begin
221
+ UDPSocket.open {|s| s.connect('ping.watirgrid.com', 1); s.addr.last }
222
+ rescue
223
+ '127.0.0.1'
224
+ end
225
+ end
226
+
227
+ end
228
+