Skip to content

Testing with Cucumber

jejacks0n edited this page Oct 10, 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. I really like cucumber, and think it has changed how I do development, so here's some info that'll help you test any additions you make to the the editor itself, and pages loaded into Mercury. I'll be building this out more and more in the future.

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 of 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

You'll also notice that I'm using "within the toolbar" on several of those steps as well. Here's the selector for that as well, which just needs to go into your selectors.

when 'the toolbar'
  '.mercury-toolbar-container'

If you're looking for more selectors and steps, you can checkout what's already in the project -- many of the steps, selector helpers, and content helpers will get you to a pretty good starting point.