This fork's new home is at https://github.com/Nerian/akephalos2
Akephalos is a full-stack headless browser for integration testing with Capybara. It is built on top of HtmlUnit, a GUI-less browser for the Java platform, but can be run on both JRuby and MRI with no need for JRuby to be installed on the system.
gem install akephalos
Or
gem 'akephalos', :git => 'git://github.com/Nerian/akephalos.git'
Configuring akephalos is as simple as requiring it and setting Capybara's javascript driver:
require 'akephalos'
Capybara.javascript_driver = :akephalos
Akephalos provides a driver for Capybara, so using Akephalos is no different than using Selenium or Rack::Test. For a full usage guide, check out Capybara's DSL documentation. It makes no assumptions about the testing framework being used, and works with RSpec, Cucumber, and Test::Unit.
Here's some sample RSpec code:
describe "Home Page" do
before { visit "/" }
context "searching" do
before do
fill_in "Search", :with => "akephalos"
click_button "Go"
end
it "returns results" { page.should have_css("#results") }
it "includes the search term" { page.should have_content("akephalos") }
end
end
There are now a few configuration options available through Capybara's new
register_driver
API.
The max memory that the JVM is going to use can be set using an environment variable in your spec_helper or .bashrc file.
ENV['akephalos_jvm_max_memory']
The default value is 256 MB.
If you use akephalos's bin the parameter -m [memory]
sets the max memory for the JVM.
$ akephalos -m 670
HtmlUnit supports a few browser implementations, and you can choose which browser you would like to use through Akephalos. By default, Akephalos uses Firefox 3.6.
Capybara.register_driver :akephalos do |app|
# available options:
# :ie6, :ie7, :ie8, :firefox_3, :firefox_3_6
Capybara::Driver::Akephalos.new(app, :browser => :ie8)
end
Capybara.register_driver :akephalos do |app|
Capybara::Driver::Akephalos.new(app, :http_proxy => 'myproxy.com', :http_proxy_port => 8080)
end
By default HtmlUnit (and Akephalos) will raise an exception when an error is encountered in javascript files. This is generally desirable, except that certain libraries aren't supported by HtmlUnit. If possible, it's best to keep the default behaviour, and use Filters (see below) to mock offending libraries. If needed, however, you can configure Akephalos to ignore javascript errors.
Capybara.register_driver :akephalos do |app|
Capybara::Driver::Akephalos.new(app, :validate_scripts => false)
end
By default it uses the 'fatal' level. You can change that like this:
Capybara.register_driver :akephalos do |app|
# available options
# "trace", "debug", "info", "warn", "error", or "fatal"
Capybara::Driver::Akephalos.new(app, :htmlunit_log_level => 'fatal')
end
Spork.prefork do
...
Akephalos::RemoteClient.manager
end
Spork.each_run do
...
Thread.current['DRb'] = { 'server' => DRb::DRbServer.new }
end
More info at : sporking-with-akephalos
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.
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.
Akephalos.filter(:get, "http://www.google.com")
Akephalos.filter(:any, %r{^http://(api\.)?twitter\.com/.*$})
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.
Akephalos.filter(:get, "http://google.com/missing",
:status => 404, :body => "... <h1>Not Found</h1> ...")
Akephalos.filter(:post, "http://my-api.com/resource.xml",
:status => 201, :headers => {
"Content-Type" => "application/xml",
"Location" => "http://my-api.com/resources/1.xml" },
:body => {:id => 100}.to_xml)
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.
Google Analytics code is passively applied based on HTML comments, so simply returning an empty response body is enough to disable it without errors.
Akephalos.filter(:get, "http://www.google-analytics.com/ga.js",
:headers => {"Content-Type" => "application/javascript"})
Google Maps requires the most extensive amount of API definitions of the three, but these few lines cover everything we've encountered so far.
Akephalos.filter(:get, "http://maps.google.com/maps/api/js?sensor=false",
:headers => {"Content-Type" => "application/javascript"},
:body => "window.google = {
maps: {
LatLng: function(){},
Map: function(){},
Marker: function(){},
MapTypeId: {ROADMAP:1}
}
};")
Facebook Connect
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.
Akephalos.filter(:get,
"http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php",
:headers => {"Content-Type" => "application/javascript"},
:body => "window.FB_RequireFeatures = function() {};")
The bundled akephalos binary provides a command line interface to a few useful features.
Running Akephalos in interactive mode gives you an IRB context for interacting with your site just as you would in your tests:
$ akephalos --interactive
-> Capybara.app_host # => "http://localhost:3000"
-> page.visit "/"
-> page.fill_in "Search", :with => "akephalos"
-> page.click_button "Go"
-> page.has_css?("#search_results") # => true
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.
This is what the output looks like:
$ akephalos --use-htmlunit-snapshot
Downloading latest snapshot... done
Extracting dependencies... done
========================================
The latest HtmlUnit snapshot has been extracted to vendor/htmlunit!
Once HtmlUnit has been extracted, Akephalos will automatically detect
the vendored version and use it instead of the bundled version.
Akephalos uses this command internally to start a JRuby DRb server using the provided socket file.
- API Documentation
- Source code and issues are hosted on github.