jarib-celerity 0.0.5.5 → 0.0.5.6

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.
@@ -5,22 +5,38 @@ module Celerity
5
5
  attr_accessor :page, :object, :charset
6
6
  attr_reader :webclient, :viewer
7
7
 
8
+ #
8
9
  # Initialize a browser and goto the given URL
9
- # @param uri The URL to go to.
10
- # @return Instance of Celerity::Browser.
10
+ #
11
+ # @param [String] uri The URL to go to.
12
+ # @return [Celerity::Browser] instance.
13
+ #
14
+
11
15
  def self.start(uri)
12
16
  browser = new
13
17
  browser.goto(uri)
14
18
  browser
15
19
  end
16
20
 
21
+ #
17
22
  # Not implemented. Use ClickableElement#click_and_attach instead.
23
+ #
24
+
18
25
  def self.attach(*args)
19
26
  raise NotImplementedError, "use ClickableElement#click_and_attach instead"
20
27
  end
21
28
 
29
+ def inspect
30
+ vars = (instance_variables - %w[@webclient @browser @object])
31
+ vars = vars.map { |var| "#{var}=#{instance_variable_get(var).inspect}" }.join(" ")
32
+ '#<%s:0x%s %s>' % [self.class.name, self.hash.to_s(16), vars]
33
+ end
34
+
35
+ #
22
36
  # Creates a browser object.
23
37
  #
38
+ # @see Celerity::Container for an introduction to the main API.
39
+ #
24
40
  # @option opts :log_level [Symbol] (:warning) @see log_level=
25
41
  # @option opts :browser [:firefox, :internet_explorer] (:internet_explorer) Set the BrowserVersion used by HtmlUnit. Defaults to Internet Explorer.
26
42
  # @option opts :css [Boolean] (false) Enable CSS. Disabled by default.
@@ -33,8 +49,10 @@ module Celerity
33
49
  # @option opts :proxy [String] (nil) Proxy server to use, in address:port format.
34
50
  #
35
51
  # @return [Celerity::Browser] An instance of the browser.
36
- # @see Celerity::Container for a small introduction to the API.
52
+ #
37
53
  # @api public
54
+ #
55
+
38
56
  def initialize(opts = {})
39
57
  unless opts.is_a?(Hash)
40
58
  raise TypeError, "wrong argument type #{opts.class}, expected Hash"
@@ -58,10 +76,13 @@ module Celerity
58
76
  find_viewer
59
77
  end
60
78
 
79
+ #
61
80
  # Goto the given URL
62
81
  #
63
82
  # @param [String] uri The url.
64
83
  # @return [String] The url.
84
+ #
85
+
65
86
  def goto(uri)
66
87
  uri = "http://#{uri}" unless uri =~ %r{://}
67
88
 
@@ -73,37 +94,71 @@ module Celerity
73
94
  url()
74
95
  end
75
96
 
97
+ #
98
+ # Set the credentials used for basic HTTP authentication. (Celerity only)
99
+ #
100
+ # Example:
101
+ # browser.credentials = "username:password"
102
+ #
103
+ # @param [String] A string with username / password, separated by a colon
104
+ #
105
+
106
+ def credentials=(string)
107
+ user, pass = string.split(":")
108
+ dcp = HtmlUnit::DefaultCredentialsProvider.new
109
+ dcp.addCredentials(user, pass)
110
+ @webclient.setCredentialsProvider(dcp)
111
+ end
112
+
113
+ #
76
114
  # Unsets the current page / closes all windows
115
+ #
116
+
77
117
  def close
78
118
  @page = nil
79
119
  @webclient.closeAllWindows
80
120
  end
81
121
 
122
+ #
82
123
  # @return [String] the URL of the current page
124
+ #
125
+
83
126
  def url
84
127
  assert_exists
85
128
  # will be renamed getUrl => getRequestUrl
86
129
  @page.getWebResponse.getUrl.toString
87
130
  end
88
131
 
132
+ #
89
133
  # @return [String] the title of the current page
134
+ #
135
+
90
136
  def title
91
137
  @page ? @page.getTitleText : ''
92
138
  end
93
139
 
140
+ #
94
141
  # @return [String] the HTML content of the current page
142
+ #
143
+
95
144
  def html
96
145
  @page ? @page.getWebResponse.getContentAsString : ''
97
146
  end
98
-
147
+
148
+ #
99
149
  # @return [String] the XML representation of the DOM
150
+ #
151
+
100
152
  def xml
101
153
  return '' unless @page
102
154
  return @page.asXml if @page.respond_to?(:asXml)
103
155
  return text # fallback to text (for exampel for "plain/text" pages)
104
156
  end
105
157
 
158
+ #
106
159
  # @return [String] a text representation of the current page
160
+ #
161
+
107
162
  def text
108
163
  return '' unless @page
109
164
 
@@ -116,33 +171,45 @@ module Celerity
116
171
  # Celerity::Util.normalize_text(string)
117
172
  string
118
173
  end
119
-
174
+
175
+ #
120
176
  # @return [Hash] response headers as a hash
177
+ #
178
+
121
179
  def response_headers
122
180
  return {} unless @page
123
181
 
124
182
  Hash[*@page.getWebResponse.getResponseHeaders.map { |obj| [obj.name, obj.value] }.flatten]
125
183
  end
126
-
184
+
185
+ #
127
186
  # @return [String] content-type as in 'text/html'
187
+ #
188
+
128
189
  def content_type
129
190
  return '' unless @page
130
191
 
131
192
  @page.getWebResponse.getContentType
132
193
  end
133
-
194
+
195
+ #
134
196
  # @return [IO, nil] page contents as an IO, returns nil if no page is loaded.
197
+ #
198
+
135
199
  def io
136
200
  return nil unless @page
137
201
 
138
202
  @page.getWebResponse.getContentAsStream.to_io
139
203
  end
140
204
 
205
+ #
141
206
  # Check if the current page contains the given text.
142
207
  #
143
208
  # @param [String, Regexp] expected_text The text to look for.
209
+ # @return [Numeric, nil] The index of the matched text, or nil if it isn't found.
144
210
  # @raise [TypeError]
145
- # @return [Numeric, nil] The index of the matched text, or nil if it doesn't match.
211
+ #
212
+
146
213
  def contains_text(expected_text)
147
214
  return nil unless exist?
148
215
  super
@@ -167,36 +234,54 @@ module Celerity
167
234
  objects.map { |o| element_from_dom_node(o) }.compact
168
235
  end
169
236
 
170
- # @return [HtmlUnit::HtmlHtml] the underlying HtmlUnit object.
237
+ #
238
+ # @return [HtmlUnit::HtmlHtml] the underlying HtmlUnit document.
239
+ #
240
+
171
241
  def document
172
242
  @object
173
243
  end
174
244
 
245
+ #
175
246
  # Goto the last url - HtmlUnit doesn't have a 'back' functionality, so we only have 1 history item :)
176
247
  # @return [String, nil] The url of the resulting page, or nil if none was stored.
248
+ #
249
+
177
250
  def back
178
251
  # TODO: this is naive, need capability from HtmlUnit
179
252
  goto(@last_url) if @last_url
180
253
  end
181
254
 
255
+ #
182
256
  # Wait for javascript jobs to finish
257
+ #
258
+
183
259
  def wait
184
260
  assert_exists
185
261
  @page.getEnclosingWindow.getJobManager.waitForAllJobsToFinish(10000)
186
262
  end
187
263
 
264
+ #
188
265
  # Refresh the current page
266
+ #
267
+
189
268
  def refresh
190
269
  assert_exists
191
270
  self.page = @page.refresh
192
271
  end
193
272
 
273
+ #
194
274
  # Clears all cookies. (Celerity only)
275
+ #
276
+
195
277
  def clear_cookies
196
278
  @webclient.getCookieManager.clearCookies
197
279
  end
198
280
 
281
+ #
199
282
  # Get the cookies for this session. (Celerity only)
283
+ #
284
+
200
285
  def cookies
201
286
  result = {}
202
287
 
@@ -208,8 +293,11 @@ module Celerity
208
293
  result
209
294
  end
210
295
 
296
+ #
211
297
  # Execute the given JavaScript on the current page. (Celerity only)
212
298
  # @return [Object] The resulting Object
299
+ #
300
+
213
301
  def execute_script(source)
214
302
  assert_exists
215
303
  @page.executeJavaScript(source.to_s).getJavaScriptResult
@@ -231,11 +319,14 @@ module Celerity
231
319
  end
232
320
  end
233
321
 
322
+ #
234
323
  # Wait until the given block evaluates to true (Celerity only)
235
324
  #
236
325
  # @param [Fixnum] timeout Number of seconds to wait before timing out (default: 30).
237
326
  # @yieldparam [Celerity::Browser] browser The browser instance.
238
327
  # @see Celerity::Browser#resynchronized
328
+ #
329
+
239
330
  def wait_until(timeout = 30, &block)
240
331
  Timeout.timeout(timeout) do
241
332
  until yield(self)
@@ -245,11 +336,14 @@ module Celerity
245
336
  end
246
337
  end
247
338
 
339
+ #
248
340
  # Wait while the given block evaluates to true (Celerity only)
249
341
  #
250
342
  # @param [Fixnum] timeout Number of seconds to wait before timing out (default: 30).
251
343
  # @yieldparam [Celerity::Browser] browser The browser instance.
252
344
  # @see Celerity::Browser#resynchronized
345
+ #
346
+
253
347
  def wait_while(timeout = 30, &block)
254
348
  Timeout.timeout(timeout) do
255
349
  while yield(self)
@@ -259,11 +353,77 @@ module Celerity
259
353
  end
260
354
  end
261
355
 
356
+ #
357
+ # Allows you to temporarily switch to HtmlUnit's NicelyResynchronizingAjaxController to resynchronize ajax calls.
358
+ #
359
+ # @browser.resynchronized do |b|
360
+ # b.link(:id, 'load_fancy_ajax_stuff').click
361
+ # end
362
+ #
363
+ # @yieldparam [Celerity::Browser] browser The current browser object.
364
+ # @see Celerity::Browser#new for options on how to always use this.
365
+ #
366
+
367
+ def resynchronized(&block)
368
+ old_controller = @webclient.ajaxController
369
+ @webclient.setAjaxController(::HtmlUnit::NicelyResynchronizingAjaxController.new)
370
+
371
+ yield(self)
372
+
373
+ @webclient.setAjaxController(old_controller)
374
+ end
375
+
376
+ #
377
+ # Start or stop HtmlUnit's DebuggingWebConnection. (Celerity only)
378
+ # The output will go to /tmp/«name»
379
+ #
380
+ # @param [Boolean] bool start or stop
381
+ # @param [String] name required if bool is true
382
+ #
383
+
384
+ def debug_web_connection(bool, name = nil)
385
+ if bool
386
+ raise "no name given" unless name
387
+ @old_webconnection = @webclient.getWebConnection
388
+ dwc = HtmlUnit::Util::DebuggingWebConnection.new(@old_webconnection, name)
389
+ @webclient.setWebConnection(dwc)
390
+ $stderr.puts "debug-webconnection on"
391
+ else
392
+ @webclient.setWebConnection(@old_webconnection) if @old_webconnection
393
+ $stderr.puts "debug-webconnection off"
394
+ end
395
+ end
396
+
397
+ #
398
+ # Add a listener block for one of the available types. (Celerity only)
399
+ # Types map to HtmlUnit interfaces like this:
400
+ #
401
+ # :status => StatusHandler
402
+ # :alert => AlertHandler ( window.alert() )
403
+ # :web_window_event => WebWindowListener
404
+ # :html_parser => HTMLParserListener
405
+ # :incorrectness => IncorrectnessListener
406
+ # :confirm => ConfirmHandler ( window.confirm() )
407
+ # :prompt => PromptHandler ( window.prompt() )
408
+ #
409
+ #
410
+ # @param [Symbol] type One of the above symbols.
411
+ # @param [Proc] block A block to be executed for events of this type.
412
+ #
413
+
414
+ def add_listener(type, &block)
415
+ @listener ||= Celerity::Listener.new(@webclient)
416
+ @listener.add_listener(type, &block)
417
+ end
418
+
419
+ #
262
420
  # Add a 'checker' proc that will be run on every page load
263
421
  #
264
422
  # @param [Proc] checker The proc to be run (can also be given as a block)
265
423
  # @yieldparam [Celerity::Browser] browser The current browser object.
266
424
  # @raise [ArgumentError] if no Proc or block was given.
425
+ #
426
+
267
427
  def add_checker(checker = nil, &block)
268
428
  if block_given?
269
429
  @error_checkers << block
@@ -274,76 +434,52 @@ module Celerity
274
434
  end
275
435
  end
276
436
 
437
+ #
277
438
  # Remove the given checker from the list of checkers
278
439
  # @param [Proc] checker The Proc to disable.
440
+ #
441
+
279
442
  def disable_checker(checker)
280
443
  @error_checkers.delete(checker)
281
444
  end
282
-
445
+
446
+ #
283
447
  # :finest, :finer, :fine, :config, :info, :warning, :severe, or :off, :all
448
+ #
284
449
  # @return [Symbol] the current log level
450
+ #
451
+
285
452
  def log_level
286
453
  java.util.logging.Logger.getLogger('com.gargoylesoftware.htmlunit').level.to_s.downcase.to_sym
287
454
  end
288
455
 
456
+ #
289
457
  # Set Java log level (default is :warning)
290
458
  #
291
459
  # @param [Symbol] level :finest, :finer, :fine, :config, :info, :warning, :severe, or :off, :all
460
+ #
461
+
292
462
  def log_level=(level)
293
463
  java.util.logging.Logger.getLogger('com.gargoylesoftware.htmlunit').level = java.util.logging.Level.const_get(level.to_s.upcase)
294
464
  end
295
465
 
466
+ #
296
467
  # Checks if we have a page currently loaded.
297
468
  # @return [true, false]
469
+ #
470
+
298
471
  def exist?
299
472
  !!@page
300
473
  end
301
474
  alias_method :exists?, :exist?
302
475
 
303
- # Allows you to temporarily switch to HtmlUnit's NicelyResynchronizingAjaxController to resynchronize ajax calls.
304
- #
305
- # @browser.resynchronized do |b|
306
- # b.link(:id, 'load_fancy_ajax_stuff').click
307
- # end
308
- #
309
- # @yieldparam [Celerity::Browser] browser The current browser object.
310
- # @see Celerity::Browser#new for options on how to always use this.
311
- def resynchronized(&block)
312
- old_controller = @webclient.ajaxController
313
- @webclient.setAjaxController(::HtmlUnit::NicelyResynchronizingAjaxController.new)
314
-
315
- yield(self)
316
-
317
- @webclient.setAjaxController(old_controller)
318
- end
319
-
320
- #--
321
- # TODO: could be private?
322
- #++
323
476
  #
324
- # Check that we have a @page object.
477
+ # Sets the current page object for the browser
325
478
  #
326
- # @raise [Celerity::Exception::UnknownObjectException] if no page is loaded.
479
+ # @param [HtmlUnit::HtmlPage] value The page to set.
327
480
  # @api private
328
- def assert_exists
329
- raise UnknownObjectException, "no page loaded" unless exist?
330
- end
331
-
332
- #--
333
- # TODO: could be private?
334
- #++
335
- # Runs the all the checker procs added by +add_checker+
336
481
  #
337
- # @see add_checker
338
- # @api private
339
- def run_error_checks
340
- @error_checkers.each { |e| e[self] }
341
- end
342
482
 
343
- # Set the current page object for the browser
344
- #
345
- # @param [HtmlUnit::HtmlPage] value The page to set.
346
- # @api private
347
483
  def page=(value)
348
484
  @last_url = url() if exist?
349
485
  @page = value
@@ -360,47 +496,35 @@ module Celerity
360
496
  value
361
497
  end
362
498
 
363
- # Start or stop HtmlUnit's DebuggingWebConnection.
364
- # The output will go to /tmp/«name»
365
- #
366
- # @param [Boolean] bool start or stop
367
- # @param [String] name required if bool is true
368
- def debug_web_connection(bool, name = nil)
369
- if bool
370
- raise "no name given" unless name
371
- @old_webconnection = @webclient.getWebConnection
372
- dwc = HtmlUnit::Util::DebuggingWebConnection.new(@old_webconnection, name)
373
- @webclient.setWebConnection(dwc)
374
- $stderr.puts "debug-webconnection on"
375
- else
376
- @webclient.setWebConnection(@old_webconnection) if @old_webconnection
377
- $stderr.puts "debug-webconnection off"
378
- end
379
- end
380
-
381
- # Add a listener block for one of the available types.
382
- # Types map to HtmlUnit interfaces like this:
383
- #
384
- # :status => StatusHandler
385
- # :alert => AlertHandler ( window.alert() )
386
- # :web_window_event => WebWindowListener
387
- # :html_parser => HTMLParserListener
388
- # :incorrectness => IncorrectnessListener
389
- # :confirm => ConfirmHandler ( window.confirm() )
390
- # :prompt => PromptHandler ( window.prompt() )
391
499
  #
500
+ # Check that we have a @page object.
392
501
  #
393
- # @param [Symbol] type One of the above symbols.
394
- # @param [Proc] block A block to be executed for events of this type.
395
- def add_listener(type, &block)
396
- @listener ||= Celerity::Listener.new(@webclient)
397
- @listener.add_listener(type, &block)
502
+ # @raise [Celerity::Exception::UnknownObjectException] if no page is loaded.
503
+ # @api private
504
+ #
505
+
506
+ def assert_exists
507
+ raise UnknownObjectException, "no page loaded" unless exist?
398
508
  end
399
509
 
400
510
  private
401
511
 
512
+ #
513
+ # Runs the all the checker procs added by +add_checker+
514
+ #
515
+ # @see add_checker
516
+ # @api private
517
+ #
518
+
519
+ def run_error_checks
520
+ @error_checkers.each { |e| e[self] }
521
+ end
522
+
523
+ #
402
524
  # Configure the webclient according to the options given to #new.
403
525
  # @see initialize
526
+ #
527
+
404
528
  def setup_webclient(opts)
405
529
  browser = case opts.delete(:browser)
406
530
  when :firefox then ::HtmlUnit::BrowserVersion::FIREFOX_2
@@ -421,9 +545,12 @@ module Celerity
421
545
  @webclient.setAjaxController(::HtmlUnit::NicelyResynchronizingAjaxController.new) if opts.delete(:resynchronize)
422
546
  end
423
547
 
548
+ #
424
549
  # This *should* be unneccessary, but sometimes the page we get from the
425
550
  # window is different (ie. a different object) from our current @page
426
551
  # (Used by #wait_while and #wait_until)
552
+ #
553
+
427
554
  def refresh_page_from_window
428
555
  new_page = @page.getEnclosingWindow.getEnclosedPage
429
556
 
@@ -433,17 +560,23 @@ module Celerity
433
560
  Log.debug "unneccessary refresh"
434
561
  end
435
562
  end
436
-
563
+
564
+ #
437
565
  # Render the current page on the viewer.
438
566
  # @api private
567
+ #
568
+
439
569
  def render
440
570
  @viewer.render_html(self.send(@render_type), url)
441
571
  rescue DRb::DRbConnError, Errno::ECONNREFUSED => e
442
572
  @viewer = DefaultViewer
443
573
  end
444
574
 
575
+ #
445
576
  # Check if we have a viewer available on druby://127.0.0.1:6429
446
577
  # @api private
578
+ #
579
+
447
580
  def find_viewer
448
581
  viewer = DRbObject.new_with_uri("druby://127.0.0.1:6429")
449
582
  if viewer.respond_to?(:render_html)
@@ -455,6 +588,10 @@ module Celerity
455
588
  @viewer = DefaultViewer
456
589
  end
457
590
 
591
+ #
592
+ # Convert the given HtmlUnit object to a Celerity object
593
+ #
594
+
458
595
  def element_from_dom_node(obj)
459
596
  if element_class = Celerity::Util.htmlunit2celerity(obj.class)
460
597
  element_class.new(self, :object, obj)