jarib-celerity 0.0.6.11 → 0.0.6.12

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.
data/lib/celerity.rb CHANGED
@@ -66,6 +66,7 @@ require "celerity/elements/text_field.rb"
66
66
  require "celerity/util"
67
67
  require "celerity/default_viewer"
68
68
  require "celerity/listener"
69
+ require "celerity/ignoring_web_connection"
69
70
  require "celerity/browser"
70
71
  require "celerity/watir_compatibility"
71
72
 
@@ -32,9 +32,10 @@ module Celerity
32
32
  # @see Celerity::Container for an introduction to the main API.
33
33
  #
34
34
  # @option opts :log_level [Symbol] (:warning) @see log_level=
35
- # @option opts :browser [:firefox, :internet_explorer] (:internet_explorer) Set the BrowserVersion used by HtmlUnit. Defaults to Internet Explorer.
35
+ # @option opts :browser [:internet_explorer, :firefox, :firefox3] (:firefox) Set the BrowserVersion used by HtmlUnit. Defaults to Firefox 2.
36
36
  # @option opts :css [Boolean] (false) Enable CSS. Disabled by default.
37
- # @option opts :secure_ssl [Boolean] (true) Disable secure SSL. Enabled by default.
37
+ # @option opts :secure_ssl [Boolean] (true) Enable/disable secure SSL. Enabled by default.
38
+ # @option opts :javascript_enabled [Boolean] (true) Enable/disable JavaScript evaluation. Enabled by default.
38
39
  # @option opts :resynchronize [Boolean] (false) Use HtmlUnit::NicelyResynchronizingAjaxController to resynchronize Ajax calls.
39
40
  # @option opts :javascript_exceptions [Boolean] (false) Raise exceptions on script errors. Disabled by default.
40
41
  # @option opts :status_code_exceptions [Boolean] (false) Raise exceptions on failing status codes (404 etc.). Disabled by default.
@@ -42,6 +43,7 @@ module Celerity
42
43
  # @option opts :charset [String] ("UTF-8") Specify the charset that webclient will use for requests, and those where texts are getting gibberished, like Browser#html.
43
44
  # @option opts :proxy [String] (nil) Proxy server to use, in address:port format.
44
45
  # @option opts :user_agent [String] Override the User-Agent set by the :browser option
46
+ # @option opts :ignore_pattern [Regexp] See Browser#ignore_pattern=
45
47
  #
46
48
  # @return [Celerity::Browser] An instance of the browser.
47
49
  #
@@ -56,7 +58,7 @@ module Celerity
56
58
  unless (render_types = [:html, :xml, nil]).include?(opts[:render])
57
59
  raise ArgumentError, "expected one of #{render_types.inspect} for key :render"
58
60
  end
59
-
61
+
60
62
  opts = opts.dup # we'll delete from opts, so dup to avoid side effects
61
63
  @options = opts.dup # keep the unmodified version around as well
62
64
 
@@ -64,7 +66,7 @@ module Celerity
64
66
  @charset = opts.delete(:charset) || "UTF-8"
65
67
  self.log_level = opts.delete(:log_level) || :warning
66
68
 
67
- @last_url, @page = nil
69
+ @page = nil
68
70
  @error_checkers = []
69
71
  @browser = self # for Container#browser
70
72
 
@@ -272,13 +274,27 @@ module Celerity
272
274
  end
273
275
 
274
276
  #
275
- # Goto the last url - HtmlUnit doesn't have a 'back' functionality, so we only have 1 history item :)
276
- # @return [String, nil] The url of the resulting page, or nil if none was stored.
277
+ # Goto back one history item
278
+ # @return [String] The url of the resulting page.
277
279
  #
278
280
 
279
281
  def back
280
- # TODO: this is naive, need capability from HtmlUnit
281
- goto(@last_url) if @last_url
282
+ @webclient.getCurrentWindow.getHistory.back
283
+ refresh_page_from_window
284
+
285
+ url
286
+ end
287
+
288
+ #
289
+ # Go forward one history item
290
+ # @return [String] The url of the resulting page.
291
+ #
292
+
293
+ def forward
294
+ @webclient.getCurrentWindow.getHistory.forward
295
+ refresh_page_from_window
296
+
297
+ url
282
298
  end
283
299
 
284
300
  #
@@ -349,7 +365,7 @@ module Celerity
349
365
  max_age = opts.delete(:max_age) || (Time.now + 60*60*24) # not sure if this is correct
350
366
  secure = opts.delete(:secure) || false
351
367
 
352
- raise "unknown option: #{opts.inspect}" unless opts.empty?
368
+ raise(ArgumentError, "unknown option: #{opts.inspect}") unless opts.empty?
353
369
 
354
370
  cookie = Cookie.new(domain, name, value, path, max_age, secure)
355
371
  @webclient.getCookieManager.addCookie(cookie)
@@ -361,13 +377,15 @@ module Celerity
361
377
  # @param [String] domain
362
378
  # @param [String] name
363
379
  #
380
+ # @raise [CookieNotFoundError] if the cookie doesn't exist
381
+ #
364
382
 
365
383
  def remove_cookie(domain, name)
366
384
  cm = @webclient.getCookieManager
367
385
  cookie = cm.getCookies.find { |c| c.getDomain == domain && c.getName == name }
368
386
 
369
387
  if cookie.nil?
370
- raise "no cookie with domain #{domain.inspect} and name #{name.inspect}"
388
+ raise CookieNotFoundError, "no cookie with domain #{domain.inspect} and name #{name.inspect}"
371
389
  end
372
390
 
373
391
  cm.removeCookie(cookie)
@@ -598,6 +616,21 @@ module Celerity
598
616
  level
599
617
  end
600
618
 
619
+ #
620
+ # If a request is made to an URL that matches the pattern set here, Celerity
621
+ # will ignore the request and return an empty page with content type "text/html" instead.
622
+ #
623
+ # This is useful to block unwanted requests (like ads/banners).
624
+ #
625
+
626
+ def ignore_pattern=(regexp)
627
+ unless regexp.kind_of?(Regexp)
628
+ raise TypeError, "expected Regexp, got #{regexp.inspect}:#{regexp.class}"
629
+ end
630
+
631
+ Celerity::IgnoringWebConnection.new(@webclient, regexp)
632
+ end
633
+
601
634
  #
602
635
  # Checks if we have a page currently loaded.
603
636
  # @return [true, false]
@@ -664,6 +697,20 @@ module Celerity
664
697
  !@webclient.useInsecureSSL
665
698
  end
666
699
 
700
+ #
701
+ # Turn on/off JavaScript execution
702
+ #
703
+ # @param [Bool]
704
+ #
705
+
706
+ def javascript_enabled=(bool)
707
+ @webclient.setJavaScriptEnabled(bool)
708
+ end
709
+
710
+ def javascript_enabled
711
+ @webclient.isJavaScriptEnabled
712
+ end
713
+
667
714
  #
668
715
  # Sets the current page object for the browser
669
716
  #
@@ -672,11 +719,10 @@ module Celerity
672
719
  #
673
720
 
674
721
  def page=(value)
675
- @last_url = url() if exist?
676
722
  @page = value
677
723
 
678
724
  if @page.respond_to?("getDocumentElement")
679
- @object = @page.getDocumentElement
725
+ @object = @page.getDocumentElement || @object
680
726
  elsif @page.is_a? HtmlUnit::UnexpectedPage
681
727
  raise UnexpectedPageException, @page.getWebResponse.getContentType
682
728
  end
@@ -690,7 +736,7 @@ module Celerity
690
736
  #
691
737
  # Check that we have a @page object.
692
738
  #
693
- # @raise [Celerity::Exception::UnknownObjectException] if no page is loaded.
739
+ # @raise [UnknownObjectException] if no page is loaded.
694
740
  # @api private
695
741
  #
696
742
 
@@ -727,32 +773,35 @@ module Celerity
727
773
  def setup_webclient(opts)
728
774
  browser = (opts.delete(:browser) || :firefox).to_sym
729
775
 
730
- case browser
731
- when :firefox, :ff, :ff2
732
- browser_version = ::HtmlUnit::BrowserVersion::FIREFOX_2
733
- when :firefox3, :ff3
734
- browser_version = ::HtmlUnit::BrowserVersion::FIREFOX_3
735
- when :internet_explorer, :ie
736
- browser_version = ::HtmlUnit::BrowserVersion::INTERNET_EXPLORER_7
737
- else
738
- raise ArgumentError, "unknown browser: #{browser.inspect}"
739
- end
776
+ browser_version = case browser
777
+ when :firefox, :ff, :ff2
778
+ ::HtmlUnit::BrowserVersion::FIREFOX_2
779
+ when :firefox3, :ff3
780
+ ::HtmlUnit::BrowserVersion::FIREFOX_3
781
+ when :internet_explorer, :ie
782
+ ::HtmlUnit::BrowserVersion::INTERNET_EXPLORER_7
783
+ else
784
+ raise ArgumentError, "unknown browser: #{browser.inspect}"
785
+ end
740
786
 
741
787
  if ua = opts.delete(:user_agent)
742
788
  browser_version.setUserAgent(ua)
743
789
  end
744
790
 
745
- if proxy = opts.delete(:proxy)
746
- phost, pport = proxy.split(":")
747
- @webclient = ::HtmlUnit::WebClient.new(browser_version, phost, pport.to_i)
748
- else
749
- @webclient = ::HtmlUnit::WebClient.new(browser_version)
750
- end
791
+ @webclient = if proxy = opts.delete(:proxy)
792
+ phost, pport = proxy.split(":")
793
+ ::HtmlUnit::WebClient.new(browser_version, phost, pport.to_i)
794
+ else
795
+ ::HtmlUnit::WebClient.new(browser_version)
796
+ end
751
797
 
752
798
  self.javascript_exceptions = false unless opts.delete(:javascript_exceptions)
753
799
  self.status_code_exceptions = false unless opts.delete(:status_code_exceptions)
754
800
  self.css = false unless opts.delete(:css)
801
+ self.javascript_enabled = opts.delete(:javascript_enabled) != false
755
802
  self.secure_ssl = opts.delete(:secure_ssl) == false
803
+ self.ignore_pattern = opts.delete(:ignore_pattern) if opts[:ignore_pattern]
804
+
756
805
  @webclient.setAjaxController(::HtmlUnit::NicelyResynchronizingAjaxController.new) if opts.delete(:resynchronize)
757
806
  end
758
807
 
@@ -118,7 +118,7 @@ module Celerity
118
118
 
119
119
  def get_by_idents(meth, idents)
120
120
  with_nullpointer_retry do
121
- @object.getAllHtmlChildElements.send(meth) do |e|
121
+ all_elements.send(meth) do |e|
122
122
  next unless @tags.include?(e.getTagName)
123
123
  idents.any? { |id| element_matches_ident?(e, id) }
124
124
  end
@@ -142,13 +142,20 @@ module Celerity
142
142
  def elements_by_tag_names(tags = @tags)
143
143
  with_nullpointer_retry do
144
144
  # HtmlUnit's getHtmlElementsByTagNames won't get elements in the correct
145
- # order (making :index fail), so we're using getAllHtmlChildElements instead.
146
- @object.getAllHtmlChildElements.select do |elem|
147
- tags.include?(elem.getTagName)
148
- end
145
+ # order (making :index fail), so we're looping through all elements instead.
146
+ all_elements.select { |elem| tags.include?(elem.getTagName) }
149
147
  end
150
148
  end
151
149
 
150
+ def all_elements
151
+ unless @object
152
+ raise %{internal error in #{self.class}: @container=#{@container.inspect} @element_class=#{@element_class.inspect}
153
+ Please report this failure and the code/HTML that caused it at http://github.com/jarib/celerity/issues}
154
+ end
155
+
156
+ @object.getAllHtmlChildElements
157
+ end
158
+
152
159
  # HtmlUnit throws NPEs sometimes when we're locating elements
153
160
  # Retry seems to work fine.
154
161
  def with_nullpointer_retry(max_retries = 3)
@@ -3,7 +3,9 @@ module Celerity
3
3
  #
4
4
  # Input: Button
5
5
  #
6
- # Class representing button elements
6
+ # Class representing button elements.
7
+ #
8
+ # This class covers both <button> and <input type="submit|reset|image|button" /> elements.
7
9
  #
8
10
 
9
11
  class Button < InputElement
@@ -1,6 +1,7 @@
1
1
  module Celerity
2
2
  class Meta < Element
3
- ATTRIBUTES = [:name, :id, :'http-equiv', :content, :scheme] | HTML_401_TRANSITIONAL[:i18n]
3
+ ATTRIBUTES = [:name, :id, :'http-equiv', :content, :scheme] | HTML_401_TRANSITIONAL[:i18n]
4
+ DEFAULT_HOW = :id
4
5
  TAGS = [ Identifier.new('meta') ]
5
6
  end
6
7
  end
@@ -8,7 +8,7 @@ module Celerity
8
8
  include ClickableElement
9
9
  include DisabledElement
10
10
 
11
- TAGS = [ Identifier.new('option')]
11
+ TAGS = [ Identifier.new('option') ]
12
12
  ATTRIBUTES = BASE_ATTRIBUTES | [:selected, :disabled, :label, :value]
13
13
  DEFAULT_HOW = :text
14
14
 
@@ -73,5 +73,11 @@ module Celerity
73
73
 
74
74
  class UnexpectedPageException < CelerityException; end
75
75
 
76
+ #
77
+ # This exception is thrown if an unexpected content type is returned by the server.
78
+ #
79
+
80
+ class CookieNotFoundError < CelerityException; end
81
+
76
82
  end # Exception
77
83
  end # Celerity
@@ -0,0 +1,15 @@
1
+ module Celerity
2
+ class IgnoringWebConnection < HtmlUnit::Util::FalsifyingWebConnection
3
+
4
+ def initialize(web_client, pattern)
5
+ super(web_client)
6
+ @pattern = pattern
7
+ end
8
+
9
+ def getResponse(request_settings)
10
+ return super unless request_settings.getUrl.toString =~ @pattern
11
+ createWebResponse(request_settings, "", "text/html")
12
+ end
13
+
14
+ end
15
+ end
@@ -3,7 +3,7 @@ module Celerity #:nodoc:
3
3
  MAJOR = 0
4
4
  MINOR = 0
5
5
  TINY = 6
6
- PATCH = 11 # Set to nil for official release
6
+ PATCH = 12 # Set to nil for official release
7
7
 
8
8
  STRING = [MAJOR, MINOR, TINY, PATCH].compact.join('.')
9
9
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jarib-celerity
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6.11
4
+ version: 0.0.6.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jari Bakken
@@ -11,7 +11,7 @@ autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
13
 
14
- date: 2009-06-26 00:00:00 -07:00
14
+ date: 2009-07-28 00:00:00 -07:00
15
15
  default_executable:
16
16
  dependencies:
17
17
  - !ruby/object:Gem::Dependency
@@ -37,7 +37,6 @@ extra_rdoc_files:
37
37
  files:
38
38
  - History.txt
39
39
  - lib
40
- - lib/celerity
41
40
  - lib/celerity/browser.rb
42
41
  - lib/celerity/clickable_element.rb
43
42
  - lib/celerity/collections.rb
@@ -47,7 +46,6 @@ files:
47
46
  - lib/celerity/element.rb
48
47
  - lib/celerity/element_collection.rb
49
48
  - lib/celerity/element_locator.rb
50
- - lib/celerity/elements
51
49
  - lib/celerity/elements/button.rb
52
50
  - lib/celerity/elements/file_field.rb
53
51
  - lib/celerity/elements/form.rb
@@ -66,7 +64,16 @@ files:
66
64
  - lib/celerity/elements/table_row.rb
67
65
  - lib/celerity/elements/text_field.rb
68
66
  - lib/celerity/exception.rb
69
- - lib/celerity/htmlunit
67
+ - lib/celerity/htmlunit.rb
68
+ - lib/celerity/identifier.rb
69
+ - lib/celerity/ignoring_web_connection.rb
70
+ - lib/celerity/input_element.rb
71
+ - lib/celerity/listener.rb
72
+ - lib/celerity/short_inspect.rb
73
+ - lib/celerity/util.rb
74
+ - lib/celerity/version.rb
75
+ - lib/celerity/watir_compatibility.rb
76
+ - lib/celerity.rb
70
77
  - lib/celerity/htmlunit/commons-codec-1.3.jar
71
78
  - lib/celerity/htmlunit/commons-collections-3.2.1.jar
72
79
  - lib/celerity/htmlunit/commons-httpclient-3.1.jar
@@ -76,23 +83,13 @@ files:
76
83
  - lib/celerity/htmlunit/cssparser-0.9.5.jar
77
84
  - lib/celerity/htmlunit/htmlunit-2.6-SNAPSHOT.jar
78
85
  - lib/celerity/htmlunit/htmlunit-core-js-2.6-SNAPSHOT.jar
79
- - lib/celerity/htmlunit/nekohtml-1.9.13-20090507.082850-2.jar
86
+ - lib/celerity/htmlunit/nekohtml-1.9.13-20090714.153721-7.jar
80
87
  - lib/celerity/htmlunit/sac-1.3.jar
81
88
  - lib/celerity/htmlunit/serializer-2.7.1.jar
82
89
  - lib/celerity/htmlunit/xalan-2.7.1.jar
83
90
  - lib/celerity/htmlunit/xercesImpl-2.8.1.jar
84
91
  - lib/celerity/htmlunit/xml-apis-1.3.04.jar
85
- - lib/celerity/htmlunit.rb
86
- - lib/celerity/identifier.rb
87
- - lib/celerity/input_element.rb
88
- - lib/celerity/listener.rb
89
- - lib/celerity/resources
90
92
  - lib/celerity/resources/no_viewer.png
91
- - lib/celerity/short_inspect.rb
92
- - lib/celerity/util.rb
93
- - lib/celerity/version.rb
94
- - lib/celerity/watir_compatibility.rb
95
- - lib/celerity.rb
96
93
  - License.txt
97
94
  - Rakefile
98
95
  - README.txt
@@ -101,6 +98,7 @@ files:
101
98
  - tasks/rdoc.rake
102
99
  has_rdoc: false
103
100
  homepage: http://celerity.rubyforge.org
101
+ licenses:
104
102
  post_install_message:
105
103
  rdoc_options:
106
104
  - --main
@@ -122,7 +120,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
122
120
  requirements: []
123
121
 
124
122
  rubyforge_project: celerity
125
- rubygems_version: 1.2.0
123
+ rubygems_version: 1.3.5
126
124
  signing_key:
127
125
  specification_version: 3
128
126
  summary: Celerity is a JRuby library for easy and fast functional test automation for web applications