Terminus

Terminus is an experimental Capybara driver implemented in client-side JavaScript. It lets you script your application in any browser on any device, without needing browser plugins. This allows several types of testing to be automated:

  • Cross-browser testing

  • Multi-browser interaction e.g. messaging apps

  • Run tests on remote machines, phones, iPads etc.

It is also a remote scripting tool, giving you a REPL that lets you run JavaScript in any number of browsers at once.

Usage

Terminus is a Capybara driver. For the most part, you will not use it directly: you will use the Capybara API and it will send instructions to Terminus for execution. To set Terminus as your driver:

require 'capybara'
require 'terminus'

Capybara.current_driver = :terminus

Terminus does require some extra setup before you can use it to control your app. First up, you need to start the Terminus server on the machine where your application will be running:

$ terminus

This starts the server on port 7004. Now open a browser at 127.0.0.1:7004/. (I recommend using the IP address of the Terminus host; Chrome has bugs that can stop WebSockets working if you use the hostname.) This is the ‘holding page’. A browser is said to be ‘docked’ while it is visiting this page, meaning it is ready and waiting to run some tests for you.

To let Terminus control your app’s pages, you need to include this just before the closing body tag when in testing mode:

<!-- For example if you're using Rails -->

<% if Rails.env.test? %>
  <%= Terminus.driver_script '127.0.0.1' %>
<% end %>

If the browser you’re using is on a different machine to the Terminus server, replace 127.0.0.1 with the Terminus machine’s IP as seen by the browser. For example if I’m running the browser in VirtualBox and the Terminus server on the host OS, then I set the IP to 10.0.2.2. You could also use request.host to get the right IP automatically if you’re running your app and your Terminus server on the same machine.

Finally, in your tests you need to make sure there’s a docked browser and select it. In a ‘before’ block, run the following:

Terminus.ensure_docked_browser
Terminus.browser = :docked

After each test is finished, you need to return the browser to the holding page to make it ready to accept new work. In an ‘after’ block:

Terminus.return_to_dock

This returns all currently connected browsers to the holding page.

Notes / to-do

  • Support IE, which has no built-in XPath engine for querying the DOM. I’m working on Pathology (see github.com/jcoglan/pathology) to try and fix this but it’s currently not fast enough.

  • Allow Terminus.browser= to select browsers by name and version so we can control multiple browsers at once.

  • It’s slow, especially at filling out forms. Use it to sanity-check your cross-browser code and JavaScript, not to test your whole app.

  • It can be a little brittle, and occasionally there seem to be race conditions when running the Capybara specs.

License

(The MIT License)

Copyright © 2010 James Coglan

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.