Skip to content

Testing with Cucumber

jejacks0n edited this page Oct 8, 2011 · 5 revisions

This was brought up and I thought it deserved some attention. It's not a particularly easy thing to do, but it's relatively simple to provide examples for.

Feature:
  As a content editor type person
  In order to manage content
  I should be able to edit content using the Mercury toolbar

  @javascript
  Scenario: A user can expect to see the editor load
    When I go to the root page
    Then I should see "Save" within the toolbar
    And I should see "Preview" within the toolbar
    And I should see "Bold" within the toolbar
    And I should see "Editable region" in the content frame

This is a pretty simple scenario, and is simply to make sure the editor's loading properly. You could throw this in your project to ensure that Mercury continues to work should you upgrade the gem or something.

The last step in the scenario makes some assertions about the content that's loaded into the iframe. This is also useful because there could be something that keeps this from happening (eg. a wild JavaScript error).

Here's the step that I use to make that a little less painful, which just scopes any existing steps to the context to the main Mercury content frame. It can go into a custom_web_steps.rb file or something similar, and can be used on all/most other steps by simply putting "in the content frame" at the end of them.

# Scope step for the mercury content frame
When /^(.*) in the content frame$/ do |step|
  page.driver.within_frame('mercury_iframe') { When step }
end