akephalos2 2.0.7-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. data/MIT_LICENSE +20 -0
  2. data/README.md +330 -0
  3. data/bin/akephalos +102 -0
  4. data/lib/akephalos/capybara.rb +347 -0
  5. data/lib/akephalos/client/cookies.rb +73 -0
  6. data/lib/akephalos/client/filter.rb +120 -0
  7. data/lib/akephalos/client.rb +192 -0
  8. data/lib/akephalos/configuration.rb +49 -0
  9. data/lib/akephalos/console.rb +32 -0
  10. data/lib/akephalos/cucumber.rb +6 -0
  11. data/lib/akephalos/htmlunit/ext/confirm_handler.rb +18 -0
  12. data/lib/akephalos/htmlunit/ext/http_method.rb +30 -0
  13. data/lib/akephalos/htmlunit.rb +31 -0
  14. data/lib/akephalos/node.rb +192 -0
  15. data/lib/akephalos/page.rb +118 -0
  16. data/lib/akephalos/remote_client.rb +93 -0
  17. data/lib/akephalos/server.rb +79 -0
  18. data/lib/akephalos/version.rb +3 -0
  19. data/lib/akephalos.rb +19 -0
  20. data/vendor/html-unit/apache-mime4j-0.6.jar +0 -0
  21. data/vendor/html-unit/commons-codec-1.4.jar +0 -0
  22. data/vendor/html-unit/commons-collections-3.2.1.jar +0 -0
  23. data/vendor/html-unit/commons-io-2.0.1.jar +0 -0
  24. data/vendor/html-unit/commons-lang3-3.0.1.jar +0 -0
  25. data/vendor/html-unit/commons-logging-1.1.1.jar +0 -0
  26. data/vendor/html-unit/cssparser-0.9.6-20110829.205617-3.jar +0 -0
  27. data/vendor/html-unit/htmlunit-2.10-SNAPSHOT.jar +0 -0
  28. data/vendor/html-unit/htmlunit-core-js-2.9.jar +0 -0
  29. data/vendor/html-unit/httpclient-4.1.2.jar +0 -0
  30. data/vendor/html-unit/httpcore-4.1.2.jar +0 -0
  31. data/vendor/html-unit/httpmime-4.1.2.jar +0 -0
  32. data/vendor/html-unit/nekohtml-1.9.15.jar +0 -0
  33. data/vendor/html-unit/sac-1.3.jar +0 -0
  34. data/vendor/html-unit/serializer-2.7.1.jar +0 -0
  35. data/vendor/html-unit/xalan-2.7.1.jar +0 -0
  36. data/vendor/html-unit/xercesImpl-2.9.1.jar +0 -0
  37. data/vendor/html-unit/xml-apis-1.3.04.jar +0 -0
  38. metadata +127 -0
data/MIT_LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Bernerd Schaefer
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,330 @@
1
+ # Important Notice
2
+
3
+ This repo has rewritten its history and as such is not compatible with the main Akephalos repo.
4
+
5
+ You can get the unaltered – before history rewrite – pristine copy at: [https://github.com/Nerian/akephalos](https://github.com/Nerian/akephalos)
6
+
7
+ Further development will be done here:
8
+ [https://github.com/Nerian/akephalos2](https://github.com/Nerian/akephalos2)
9
+
10
+ The reason why its history was rewrote was to remove .jar vendor files that were making its size huge.
11
+
12
+
13
+ # Akephalos
14
+
15
+ Akephalos is a full-stack headless browser for integration testing with
16
+ [Capybara](https://github.com/jnicklas/capybara). It is built on top of [HtmlUnit](http://htmlunit.sourceforge.net),
17
+ a GUI-less browser for the Java platform, but can be run on both JRuby and
18
+ MRI with no need for JRuby to be installed on the system.
19
+
20
+
21
+ ## Installation
22
+
23
+ ``` ruby
24
+ gem install akephalos2
25
+ ```
26
+
27
+ Or
28
+
29
+ ``` ruby
30
+ gem 'akephalos2', :require => 'akephalos'
31
+ ```
32
+
33
+ Or (for the current master branch)
34
+
35
+ ``` ruby
36
+ gem 'akephalos', :git => 'git://github.com/Nerian/akephalos2.git', :submodules => true
37
+ ```
38
+
39
+ # Questions, bugs, etc:
40
+
41
+ We use GitHub issues:
42
+
43
+ [https://github.com/Nerian/akephalos2/issues](https://github.com/Nerian/akephalos2/issues)
44
+
45
+ # Development
46
+
47
+ <a href='http://travis-ci.org/#!/Nerian/akephalos2'>
48
+ <img src="https://secure.travis-ci.org/Nerian/akephalos2.png?branch=master&amp;.png"/>
49
+ </a>
50
+
51
+ ``` bash
52
+ git clone --recursive https://github.com/Nerian/akephalos2
53
+ ```
54
+
55
+ The HTMLUnit files are located at [https://github.com/Nerian/html-unit-vendor](https://github.com/Nerian/html-unit-vendor) and are automatically downloaded as a submodule.
56
+
57
+ Also, we have a .rvmrc file already cooked:
58
+
59
+ ``` bash
60
+ cp .rvmrc.example .rvmrc
61
+ ```
62
+
63
+ ## Setup
64
+
65
+ Configuring akephalos is as simple as requiring it and setting Capybara's
66
+ javascript driver:
67
+
68
+ ``` ruby
69
+ require 'akephalos'
70
+ Capybara.javascript_driver = :akephalos
71
+ ```
72
+
73
+ ## Basic Usage
74
+
75
+ Akephalos provides a driver for Capybara, so using Akephalos is no
76
+ different than using Selenium or Rack::Test. For a full usage guide, check
77
+ out Capybara's DSL [documentation](http://github.com/jnicklas/capybara). It
78
+ makes no assumptions about the testing framework being used, and works with
79
+ RSpec, Cucumber, and Test::Unit.
80
+
81
+ Here's some sample RSpec code:
82
+
83
+ ``` ruby
84
+ describe "Home Page" do
85
+ before { visit "/" }
86
+
87
+ context "searching" do
88
+
89
+ before do
90
+ fill_in "Search", :with => "akephalos"
91
+ click_button "Go"
92
+ end
93
+
94
+ it "returns results" { page.should have_css("#results") }
95
+
96
+ it "includes the search term" { page.should have_content("akephalos") }
97
+ end
98
+
99
+ end
100
+ ```
101
+
102
+ Capybara allows you to perform your action on a context, for example inside a div or a frame. With Akephalos you can select the frame either by id or by index.
103
+
104
+ ``` html
105
+ <body>
106
+ <p id="test">Test</p>
107
+ <iframe id="first" src="/one_text"></iframe>
108
+ <p id="test2">Test2</p>
109
+ <iframe class="second" src="/two_text"></iframe>
110
+ <p id="test3">Test3</p>
111
+ <iframe id="third" src="/three_text"></iframe>
112
+ </body>
113
+ ```
114
+
115
+ You can operate within the context of iframe `test2` with any of these calls:
116
+
117
+ ``` ruby
118
+ # By ID
119
+ within_frame("test2") do
120
+ ....
121
+ end
122
+
123
+ # By index
124
+ within_frame(1) do
125
+ ....
126
+ end
127
+ ```
128
+
129
+ ## Configuration
130
+
131
+ There are now a few configuration options available through Capybara's new
132
+ `register_driver` API.
133
+
134
+ ### Configuring the max memory that Java Virtual Machine can use
135
+
136
+ The max memory that the JVM is going to use can be set using an environment variable in your spec_helper or .bashrc file.
137
+
138
+ ``` ruby
139
+ ENV['akephalos_jvm_max_memory']
140
+ ```
141
+
142
+
143
+ The default value is 128 MB.
144
+
145
+ If you use akephalos's bin the parameter `-m [memory]` sets the max memory for the JVM.
146
+
147
+ ``` bash
148
+ $ akephalos -m 670
149
+ ```
150
+
151
+
152
+ ### Using a different browser
153
+
154
+ HtmlUnit supports a few browser implementations, and you can choose which
155
+ browser you would like to use through Akephalos. By default, Akephalos uses
156
+ Firefox 3.6.
157
+
158
+ ``` ruby
159
+ Capybara.register_driver :akephalos do |app|
160
+ # available options:
161
+ # :ie6, :ie7, :ie8, :firefox_3_6
162
+ Capybara::Driver::Akephalos.new(app, :browser => :ie8)
163
+ end
164
+ ```
165
+
166
+
167
+ ### Using a Proxy Server
168
+
169
+ ``` ruby
170
+ Capybara.register_driver :akephalos do |app|
171
+ Capybara::Driver::Akephalos.new(app, :http_proxy => 'myproxy.com', :http_proxy_port => 8080)
172
+ end
173
+ ```
174
+
175
+
176
+ ### Ignoring javascript errors
177
+
178
+ By default HtmlUnit (and Akephalos) will raise an exception when an error
179
+ is encountered in javascript files. This is generally desirable, except
180
+ that certain libraries aren't supported by HtmlUnit. If possible, it's
181
+ best to keep the default behaviour, and use Filters (see below) to mock
182
+ offending libraries. If needed, however, you can configure Akephalos to
183
+ ignore javascript errors.
184
+
185
+ ``` ruby
186
+ Capybara.register_driver :akephalos do |app|
187
+ Capybara::Driver::Akephalos.new(app, :validate_scripts => false)
188
+ end
189
+ ```
190
+
191
+
192
+ ### Setting the HtmlUnit log level
193
+
194
+ By default it uses the 'fatal' level. You can change that like this:
195
+
196
+ ``` ruby
197
+ Capybara.register_driver :akephalos do |app|
198
+ # available options
199
+ # "trace", "debug", "info", "warn", "error", or "fatal"
200
+ Capybara::Driver::Akephalos.new(app, :htmlunit_log_level => 'fatal')
201
+ end
202
+ ```
203
+
204
+
205
+ ### Running Akephalos with Spork
206
+
207
+ ``` ruby
208
+ Spork.prefork do
209
+ ...
210
+ Akephalos::RemoteClient.manager
211
+ end
212
+
213
+ Spork.each_run do
214
+ ...
215
+ Thread.current['DRb'] = { 'server' => DRb::DRbServer.new }
216
+ end
217
+ ```
218
+
219
+
220
+ More info at : [sporking-with-akephalos](http://spacevatican.org/2011/7/3/sporking-with-akephalos)
221
+
222
+ ### Filters
223
+
224
+ Akephalos allows you to filter requests originating from the browser and return mock responses. This will let you easily filter requests for external resources when running your tests, such as Facebook's API and Google Analytics.
225
+
226
+ Configuring filters in Akephalos should be familiar to anyone who has used FakeWeb or a similar library. The simplest filter requires only an HTTP method (:get, :post, :put, :delete, :any) and a string or regex to match against.
227
+
228
+ ``` ruby
229
+ Akephalos.filter(:get, "http://www.google.com")
230
+ Akephalos.filter(:any, %r{^http://(api\.)?twitter\.com/.*$})
231
+ ```
232
+
233
+
234
+ By default, all filtered requests will return an empty body with a 200 status code. You can change this by passing additional options to your filter call.
235
+
236
+ ``` ruby
237
+ Akephalos.filter(:get, "http://google.com/missing",
238
+ :status => 404, :body => "... <h1>Not Found</h1> ...")
239
+
240
+ Akephalos.filter(:post, "http://my-api.com/resource.xml",
241
+ :status => 201, :headers => {
242
+ "Content-Type" => "application/xml",
243
+ "Location" => "http://my-api.com/resources/1.xml" },
244
+ :body => {:id => 100}.to_xml)
245
+ ```
246
+
247
+
248
+ And that's really all there is to it! It should be fairly trivial to set up filters for the external resources you need to fake. For reference, however, here's what we ended up using for our external sources.
249
+
250
+ #### Example: Google Maps
251
+
252
+ Google Analytics code is passively applied based on HTML comments, so simply returning an empty response body is enough to disable it without errors.
253
+
254
+ ``` ruby
255
+ Akephalos.filter(:get, "http://www.google-analytics.com/ga.js",
256
+ :headers => {"Content-Type" => "application/javascript"})
257
+ ```
258
+
259
+
260
+ Google Maps requires the most extensive amount of API definitions of the three, but these few lines cover everything we've encountered so far.
261
+
262
+ ``` ruby
263
+ Akephalos.filter(:get, "http://maps.google.com/maps/api/js?sensor=false",
264
+ :headers => {"Content-Type" => "application/javascript"},
265
+ :body => "window.google = {
266
+ maps: {
267
+ LatLng: function(){},
268
+ Map: function(){},
269
+ Marker: function(){},
270
+ MapTypeId: {ROADMAP:1}
271
+ }
272
+ };")
273
+ ```
274
+
275
+
276
+ #### Example: Facebook Connect
277
+
278
+ Facebook Connect
279
+
280
+ When you enable Facebook Connect on your page, the FeatureLoader is requested, and then additional resources are loaded when you call FB_RequireFeatures. We can therefore return an empty function from our filter to disable all Facebook Connect code.
281
+
282
+ ``` ruby
283
+ Akephalos.filter(:get,
284
+ "http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php",
285
+ :headers => {"Content-Type" => "application/javascript"},
286
+ :body => "window.FB_RequireFeatures = function() {};")
287
+ ```
288
+
289
+
290
+ ### Akephalos' Interactive mode
291
+
292
+ #### bin/akephalos
293
+
294
+ The bundled akephalos binary provides a command line interface to a few useful features.
295
+
296
+ #### akephalos --interactive
297
+
298
+ Running Akephalos in interactive mode gives you an IRB context for interacting with your site just as you would in your tests:
299
+
300
+ ``` ruby
301
+ $ akephalos --interactive
302
+ -> Capybara.app_host # => "http://localhost:3000"
303
+ -> page.visit "/"
304
+ -> page.fill_in "Search", :with => "akephalos"
305
+ -> page.click_button "Go"
306
+ -> page.has_css?("#search_results") # => true
307
+ ```
308
+
309
+
310
+ #### akephalos --use-htmlunit-snapshot
311
+
312
+ This will instruct Akephalos to use the latest development snapshot of HtmlUnit as found on it's Cruise Control server. HtmlUnit and its dependencies will be unpacked into vendor/htmlunit in the current working directory.
313
+
314
+ This is what the output looks like:
315
+
316
+ ``` ruby
317
+ $ akephalos --use-htmlunit-snapshot
318
+
319
+ Downloading latest snapshot... done
320
+ Extracting dependencies... done
321
+ ========================================
322
+ The latest HtmlUnit snapshot has been extracted to vendor/htmlunit!
323
+ Once HtmlUnit has been extracted, Akephalos will automatically detect
324
+ the vendored version and use it instead of the bundled version.
325
+ ```
326
+
327
+
328
+ #### akephalos --server <socket_file>
329
+
330
+ Akephalos uses this command internally to start a JRuby DRb server using the provided socket file.
data/bin/akephalos ADDED
@@ -0,0 +1,102 @@
1
+ #!/usr/bin/env ruby
2
+ # vim:set filetype=ruby:
3
+
4
+ require "pathname"
5
+ require "optparse"
6
+ require 'rubygems'
7
+
8
+ options = { :interactive => false, :default_jvm_max_memory => '128' }
9
+
10
+ parser = OptionParser.new do |opts|
11
+ opts.banner = "Usage: akephalos [--interactive, --use-htmlunit-snapshot, --memory [number]] | [--server] <port>"
12
+ opts.on("-s", "--server", "Run in server mode (default)")
13
+ opts.on("-i", "--interactive", "Run in interactive mode") { options[:interactive] = true }
14
+ opts.on("--use-htmlunit-snapshot", "Use the snapshot of htmlunit") { options[:use_htmlunit_snapshot] = true }
15
+ opts.on("-m", "--memory [number]", "Max memory for the Java Virtual Machine, defaults to #{options[:default_jvm_max_memory]}
16
+ or env variable $akephalos_jvm_max_memory") do |memory|
17
+ options[:akephalos_jvm_max_memory] = memory.to_s
18
+ end
19
+
20
+ if options[:akephalos_jvm_max_memory].nil?
21
+ if ENV['akephalos_jvm_max_memory'].nil?
22
+ options[:akephalos_jvm_max_memory] = options[:default_jvm_max_memory]
23
+ else
24
+ options[:akephalos_jvm_max_memory] = ENV['akephalos_jvm_max_memory']
25
+ end
26
+ end
27
+
28
+ puts "Using #{options[:akephalos_jvm_max_memory]} MB for the JVM"
29
+
30
+ opts.on_tail("-h", "--help", "Show this message") { puts opts; exit }
31
+ end
32
+ parser.parse!
33
+
34
+ root = Pathname(__FILE__).expand_path.dirname.parent
35
+ lib = root + 'lib'
36
+ src = root + 'vendor'
37
+
38
+ case
39
+ when options[:use_htmlunit_snapshot]
40
+ require "fileutils"
41
+
42
+ FileUtils.mkdir_p("vendor/html-unit")
43
+ Dir["vendor/html-unit/*.jar"].each { |jar| File.unlink(jar) }
44
+
45
+ Dir.chdir("vendor") do
46
+ $stdout.print "Downloading latest snapshot... "
47
+ $stdout.flush
48
+ %x[curl -O http://build.canoo.com/htmlunit/artifacts/htmlunit-2.10-SNAPSHOT-with-dependencies.zip]
49
+ puts "done"
50
+
51
+ $stdout.print "Extracting dependencies... "
52
+ $stdout.flush
53
+ %x[unzip -j -d html-unit htmlunit-2.10-SNAPSHOT-with-dependencies.zip htmlunit-2.10-SNAPSHOT/lib htmlunit-2.10-SNAPSHOT/lib/* &> /dev/null]
54
+ puts "done"
55
+
56
+ File.unlink "htmlunit-2.10-SNAPSHOT-with-dependencies.zip"
57
+ end
58
+
59
+ $stdout.puts "="*40
60
+ $stdout.puts "The latest HtmlUnit snapshot has been extracted to vendor/html-unit!"
61
+ when options[:interactive]
62
+ $LOAD_PATH.unshift('vendor', lib, src)
63
+ require 'akephalos'
64
+ require 'akephalos/console'
65
+ Akephalos::Console.start
66
+ else
67
+ unless port = ARGV[0]
68
+ puts parser.help
69
+ exit
70
+ end
71
+
72
+ if RUBY_PLATFORM == "java"
73
+ $LOAD_PATH.unshift("vendor", lib, src)
74
+ require 'akephalos/server'
75
+ Akephalos::Server.start!(port)
76
+ else
77
+ require 'jruby-jars'
78
+
79
+ jruby = JRubyJars.core_jar_path
80
+ jruby_stdlib = JRubyJars.stdlib_jar_path
81
+
82
+ java_args = [
83
+ "-Xmx#{options[:akephalos_jvm_max_memory]}M",
84
+ "-cp", [JRubyJars.core_jar_path, JRubyJars.stdlib_jar_path].join(File::PATH_SEPARATOR),
85
+ "org.jruby.Main",
86
+ "-X+O"
87
+ ]
88
+ ruby_args = [
89
+ "-Ku",
90
+ "-I", ["vendor", lib, src].join(File::PATH_SEPARATOR),
91
+ "-r", "akephalos/server",
92
+ "-e", "Akephalos::Server.start!(#{port.inspect})"
93
+ ]
94
+
95
+ # Bundler sets ENV["RUBYOPT"] to automatically load bundler/setup.rb, but
96
+ # since the akephalos server doesn't have any gem dependencies and is
97
+ # always executed with the same context, we clear RUBYOPT before running
98
+ # exec.
99
+ ENV["RUBYOPT"] = "--1.9"
100
+ exec("java", *(java_args + ruby_args))
101
+ end
102
+ end