2011-10-27 23:34:14 +01:00
|
|
|
# Poltergeist - A PhantomJS driver for Capybara #
|
|
|
|
|
2012-01-03 23:52:23 +00:00
|
|
|
Version: 0.2.0
|
2011-10-27 23:34:14 +01:00
|
|
|
|
|
|
|
Poltergeist is a driver for [Capybara](https://github.com/jnicklas/capybara). It allows you to
|
|
|
|
run your Capybara tests on a headless [WebKit](http://webkit.org) browser,
|
|
|
|
provided by [PhantomJS](http://www.phantomjs.org/).
|
|
|
|
|
|
|
|
## Installation ##
|
|
|
|
|
|
|
|
Add `poltergeist` to your Gemfile, and add in your test setup add:
|
|
|
|
|
|
|
|
require 'capybara/poltergeist'
|
|
|
|
Capybara.javascript_driver = :poltergeist
|
|
|
|
|
|
|
|
Currently PhantomJS is not 'truly headless', so to run it on a continuous integration
|
|
|
|
server you will need to use [Xvfb](http://en.wikipedia.org/wiki/Xvfb). You can either use the
|
|
|
|
[headless gem](https://github.com/leonid-shevtsov/headless) for this,
|
|
|
|
or make sure that Xvfb is running and the `DISPLAY` environment variable is set.
|
|
|
|
|
2012-01-03 20:04:37 +00:00
|
|
|
## Installing PhantomJS ##
|
|
|
|
|
|
|
|
You need PhantomJS 1.4.1+, built against Qt 4.8, on your system.
|
|
|
|
|
|
|
|
### Mac users ##
|
|
|
|
|
|
|
|
By far the easiest, most reliable thing to do is to [install the
|
|
|
|
pre-built static binary](http://code.google.com/p/phantomjs/downloads/detail?name=phantomjs-1.4.1-macosx-static-x86.zip&can=2&q=).
|
|
|
|
Try this first.
|
|
|
|
|
|
|
|
### Linux users, or if the pre-built Mac binary doesn't work ###
|
|
|
|
|
|
|
|
You need to build PhantomJS manually. Unfortunately, this not
|
|
|
|
currently straightforward, for two reasons:
|
|
|
|
|
|
|
|
1. Using Poltergeist with PhantomJS built against Qt 4.7 causes
|
|
|
|
segfaults in WebKit's Javascript engine. Fortunately, this problem
|
|
|
|
doesn't occur under the recently released Qt 4.8. But if you don't
|
|
|
|
have Qt 4.8 on your system (check with `qmake --version`), you'll
|
|
|
|
need to build it.
|
|
|
|
|
|
|
|
2. A change in the version of WebKit bundled with Qt 4.8 means that in order
|
|
|
|
to be able to attach files to file `<input>` elements, we must apply
|
|
|
|
a patch to the Qt source tree that PhantomJS is built against.
|
|
|
|
|
|
|
|
So, you basically have two options:
|
|
|
|
|
|
|
|
1. **If you have Qt 4.8 on your system, and don't need to use file
|
|
|
|
inputs**, [follow the standard PhantomJS build instructions](http://code.google.com/p/phantomjs/wiki/BuildInstructions).
|
|
|
|
|
2012-01-06 21:23:43 +01:00
|
|
|
2. **Otherwise**, [download the PhantomJS tarball](http://code.google.com/p/phantomjs/downloads/detail?name=phantomjs-1.4.1-source.tar.gz&can=2&q=)
|
|
|
|
and run either `deploy/build-linux.sh --qt-4.8` or `cd deploy; ./build-mac.sh`.
|
2012-01-03 20:04:37 +00:00
|
|
|
The script will
|
|
|
|
download Qt, apply some patches, build it, and then build PhantomJS
|
|
|
|
against the patched build of Qt. It takes quite a while, around 30
|
|
|
|
minutes on a modern computer with two hyperthreaded cores. Afterwards,
|
|
|
|
you should copy the `bin/phantomjs` binary into your `PATH`.
|
|
|
|
|
|
|
|
PhantomJS 1.5 plans to bundle a stripped-down version of Qt, which will
|
|
|
|
reduce the build time a bit (although most of the time is spent building
|
|
|
|
WebKit) and make it easier to apply patches. When it is possible to make
|
|
|
|
static builds for Linux, those may be provided too, so most users will
|
|
|
|
avoid having to build it themselves.
|
|
|
|
|
2011-10-27 23:34:14 +01:00
|
|
|
## What's supported? ##
|
|
|
|
|
|
|
|
Poltergeist supports basically everything that is supported by the stock Selenium driver,
|
|
|
|
including Javascript, drag-and-drop, etc.
|
|
|
|
|
2011-10-31 22:23:52 +00:00
|
|
|
There are some additional features:
|
|
|
|
|
|
|
|
### Taking screenshots ###
|
|
|
|
|
|
|
|
You can grab screenshots of the page at any point by calling
|
2011-10-27 23:34:14 +01:00
|
|
|
`page.driver.render('/path/to/file.png')` (this works the same way as the PhantomJS
|
|
|
|
render feature, so you can specify other extensions like `.pdf`, `.gif`, etc.)
|
|
|
|
|
2011-10-31 23:04:02 +00:00
|
|
|
By default, only the viewport will be rendered (the part of the page that is in view). To render
|
|
|
|
the entire page, use `page.driver.render('/path/to/file.png', :full => true)`.
|
|
|
|
|
2011-10-31 22:23:52 +00:00
|
|
|
### Resizing the window ###
|
|
|
|
|
|
|
|
Sometimes the window size is important to how things are rendered. Poltergeist sets the window
|
|
|
|
size to 1024x768 by default, but you can set this yourself with `page.driver.resize(width, height)`.
|
|
|
|
|
2011-10-27 23:34:14 +01:00
|
|
|
## Customization ##
|
|
|
|
|
|
|
|
You can customize the way that Capybara sets up Poltegeist via the following code in your
|
|
|
|
test setup:
|
|
|
|
|
|
|
|
Capybara.register_driver :poltergeist do |app|
|
|
|
|
Capybara::Poltergeist::Driver.new(app, options)
|
|
|
|
end
|
|
|
|
|
|
|
|
`options` is a hash of options. The following options are supported:
|
|
|
|
|
|
|
|
* `:phantomjs` (String) - A custom path to the phantomjs executable
|
|
|
|
* `:debug` (Boolean) - When true, debug output is logged to `STDERR`
|
|
|
|
* `:logger` (Object responding to `puts`) - When present, debug output is written to this object
|
2012-01-13 13:10:30 +00:00
|
|
|
* `:timeout` (Numeric) - The number of seconds we'll wait for a response
|
|
|
|
when communicating with PhantomJS. `nil` means wait forever. Default
|
2012-01-13 14:42:34 +00:00
|
|
|
is 30.
|
2011-10-27 23:34:14 +01:00
|
|
|
|
|
|
|
## Bugs ##
|
|
|
|
|
|
|
|
Please file bug reports on Github and include example code to reproduce the problem wherever
|
2012-01-03 20:04:37 +00:00
|
|
|
possible. (Tests are even better.) Please also provide the output with
|
|
|
|
`:debug` turned on, and screenshots if you think it's relevant.
|
2011-10-27 23:34:14 +01:00
|
|
|
|
|
|
|
## Why not use [capybara-webkit](https://github.com/thoughtbot/capybara-webkit)? ##
|
|
|
|
|
|
|
|
If capybara-webkit works for you, then by all means carry on using it.
|
|
|
|
|
|
|
|
However, I have had some trouble with it, and Poltergeist basically started
|
|
|
|
as an experiment to see whether a PhantomJS driver was possible. (It turned out it
|
2012-01-03 20:04:37 +00:00
|
|
|
was, but only thanks to some new features since the 1.3 release.)
|
2011-10-27 23:34:14 +01:00
|
|
|
|
|
|
|
In the long term, I think having a PhantomJS driver makes sense, because that allows
|
|
|
|
PhantomJS to concentrate on being an awesome headless browser, while the capybara driver
|
|
|
|
(Poltergeist) is able to be the minimal amount of glue code necessary to drive the
|
|
|
|
browser.
|
|
|
|
|
2012-01-03 20:04:37 +00:00
|
|
|
I also find it more pleasant to hack in CoffeeScript than C++,
|
|
|
|
particularly as my C++ experience only goes as far as trying to make
|
|
|
|
PhantomJS/Qt/WebKit work with Poltergeist :)
|
|
|
|
|
|
|
|
## Hacking ##
|
|
|
|
|
|
|
|
Contributions are very welcome and I will happily give commit access to
|
|
|
|
anyone who does a few good pull requests.
|
|
|
|
|
|
|
|
To get setup, run `bundle install`. You can run the full test suite with
|
|
|
|
`rspec spec/` or `rake`.
|
|
|
|
|
|
|
|
I previously set up the repository on [Travis CI](http://travis-ci.org/)
|
|
|
|
but unfortunately given they need a custom-built Qt+PhantomJS in order
|
|
|
|
to pass, it can't be used for now. When static Linux PhantomJS builds
|
|
|
|
are working this can be revisited.
|
|
|
|
|
|
|
|
While PhantomJS is capable of compiling and running CoffeeScript code
|
|
|
|
directly, I prefer to compile the code myself and distribute that (it
|
|
|
|
makes debugging easier). Running `rake autocompile` will watch the
|
|
|
|
`.coffee` files for changes, and compile them into
|
|
|
|
`lib/capybara/client/compiled`.
|
|
|
|
|
2012-01-11 22:16:35 +00:00
|
|
|
## Changes ##
|
|
|
|
|
|
|
|
### 0.3 (unreleased) ###
|
|
|
|
|
|
|
|
* There was a bad bug to do with clicking elements in a page where the
|
|
|
|
page is smaller than the window. The incorrect position would be
|
|
|
|
calculated, and so the click would happen in the wrong place. This is
|
|
|
|
fixed. [Issue #8]
|
|
|
|
|
2012-01-13 13:10:30 +00:00
|
|
|
* Poltergeist didn't work in conjunction with the Thin web server,
|
|
|
|
because that server uses Event Machine, and Poltergeist was assuming
|
|
|
|
that it was the only thing in the process using EventMachine.
|
|
|
|
|
|
|
|
To solve this, EventMachine usage has been completely removed, which
|
|
|
|
has the welcome side-effect of being more efficient because we
|
|
|
|
no longer have the overhead of running a mostly-idle event loop.
|
|
|
|
|
|
|
|
[Issue #6]
|
|
|
|
|
|
|
|
* Added the `:timeout` option to configure the timeout when talking to
|
|
|
|
PhantomJS.
|
|
|
|
|
2012-01-11 22:16:35 +00:00
|
|
|
### 0.2 ###
|
|
|
|
|
|
|
|
* First version considered 'ready', hopefully fewer problems.
|
|
|
|
|
|
|
|
### 0.1 ###
|
|
|
|
|
|
|
|
* First version, various problems.
|
|
|
|
|
2011-10-27 23:34:14 +01:00
|
|
|
## License ##
|
|
|
|
|
|
|
|
Copyright (c) 2011 Jonathan Leighton
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
a copy of this software and associated documentation files (the
|
|
|
|
"Software"), to deal in the Software without restriction, including
|
|
|
|
without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be
|
|
|
|
included in all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
|
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
|
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|