Mechanical Cuke
mechanical-cuke re-implements the Cucumber step definitions provided by cucumber-rails’ web_steps.rb in Mechanize. This allows Cucumber features to be written for any site by running the tests over HTTP.
mechanical-cuke also provides a driver for Capybara with enough support to run the cucumber-rails web_steps.rb. This allows you use Mechanize in conjunction with Capybara other drivers (handy if you need to do some Javascript testing).
Rational
Webrat and Capybara both can drive tests over HTML. However, Webrat’s Mechanize support is limited (doesn’t work with multipart forms, can’t submit empty form fields, no support for basic auth). Capybara drives tests over HTTP using browsers, excellent for testing Javascript, but slow for test that only involve HTML.
Usage
To use the standalone version of Mechanical Cuke add:
require 'mechanical-cuke'
to features/support/env.rb
To use Mechanical Cuke with Capybara add:
require 'mechanical-cuke/capybara'
Capybara.default_driver = :mechanical_cuke
In features/support/paths.rb you will need to make sure you are returning the full URL. The simplest way to do this is to add ‘URL’ + before the case statement in the path_to function e.g.:
def path_to(page_name)
case page_name
becomes
def path_to(page_name)
'http://localhost:3000' +
case page_name
Steps Provided by the Standalone Version
Given am on path
When I go to path
GET the page at path and sets it to the current page for follow steps.
When I press "button"
Press button with label “button”. If the button is in a form the form is submitted.
When I follow "link"
Click the link with text “link”.
When I fill in "field" with "value"
When I fill in "value" for "field"
Fill in the text field, password field, or text area field with value. field can be the id or name of the field, or the text within the label for the field.
When I fill in the following:
Takes a table of fields and values of the form:
When I fill in the following:
| Account Number | 5002 |
| Expiry date | 2009-11-01 |
| Note | Nice guy |
When I select "value" from "field"
Selects the option with value from select list field. field can be the id or name of the field, or the text within the label for the field.
When I check "field"
When I uncheck "field"
Check or uncheck the checkbox “field”.
When I choose "field"
Checks radio button field. field can be the id or the text within the label for the field; the field name would be ambiguous.
When I attach the file “path” to “field”
Attach the file in path to the file field field.
Then I should see "text"
Then I should see /regexp/
The body of the page should contain text or regexp.
Then I should not see "text"
Then I should not see /regexp/
The body of the page should not contain text or regexp.
Then the "field" field should contain "value"
Then the "field" field should not contain "value"
The value field should be (or not be) value.
Then the "_field_" checkbox should be checked
Then the "_field_" checkbox not should be checked
The checkbox field should be (or not be) checked.
Then I should be on _path_
The path/uri of the current page should be path.
Then show me the page
Save the current page and open in the default browser.
Basic Auth
You can provide basic auth credentials by calling:
basic_auth(username,password)
in your step definitions before visiting a protected page.
Todo
Add scopes to allow steps to function within an element e.g:
When I press "Submit" within "#login_form"
Make Rspec support actually work.
Make Capybara driver complete.
Author
Spike Ilacqua