Monday, October 3, 2011

Some Very Small Cucumber Tips

For those who have been watching my transition from primarily being a manual tester to this generations concept of automated testing (I did a bunch of shell scripting in the 90's and worked with Tcl/Tk as an automation framework for some time. This was before the rage of record and playback tools took over the testing world). Now my world revolves around Cucumber, which uses Ruby to complete the plumbing (at least in my environment, I realize Cucumber is language independent :) ).

Over the past few months, I've been tweaking with it and discovered it can do some cool things, but most important, I think it's important that people realize what Cucumber isn't. Many people will tell you that Cucumber is programming put into plain English (or fill in your favorite language). It's not. In fact without the underlying language plumbing (again, take your pick), the English syntax does absolutely nothing. You have to define every statement that you use. Additionally, every statement has to have coherent code underneath it that ties in with each other statement. So when you see something like this:

    Given I am on the home page
    And I click "Sign Up" within ".login"
    When I fill in "Username" with "<username>"
    And I fill in "Password" with "<password>"
    And I fill in "Confirm password" with "<email>"
    And I fill in "Email" with "<email>"
    And I select "<time_zone>" from "Time Zone"
    And I select "<i_am>" from "I am"
    And I select "<year_of_birth>" from "Year of Birth"
    And I select "<country>" from "Country"
    And I click "Create Account"
    Then I should see "1 error prohibited this user from being saved" within ".errorExplanation"

You have to realize that every one of those statements has an underlying set of Ruby statements that make it coherent. Oh, and for those wondering, what's that stuff in the "<>"? That's syntax you can use with Scenario Outlines, a cool little technique to make tests easier to code if they will be run multiple times with the exact same parameters. The way that you take advantage of it is to make an Examples table:

Examples:
|username|password|email|time_zone|i_am|year_of_birth|Country|
|testname1|mypass12|mytest12@gmail.com|Pacific|male|1979|Canada|


I realize this is probably "blinding flash of the obvious" to some people, but it's little things like this that make me smile and find different ways to use Cucumber that I hadn't immediately considered.

Another neat things that was shown to me was to create a file called selector.rb. By creating this file, you can gather together steps that are frequently run and make one "plain english step". I'm not entirely sure why it's called selector.rb other than to give the user a chance to take their common calls to html elements and simplify their naming. While the ability to do that is cool, what I really like is the ability to chain serial commands like the following:


Given /^I log in to facebook with good credentials$/ do
  And  %Q|I fill in "email" with "myusername@email.com" within "#login_form"|
  And  %Q|I fill in "pass" with "aP@ssw0rd" within "#login_form"|
  And  %Q|I click "Log In" within "#login_form"|


The beauty of the above statement is that I only need to make a line as follows:

Given I log into facebook with good credentials

Note, this isn't a free ride. If you put a bunch of lines into a single line statement. It can be maddening to figure out exactly which line is causing the problem, because the error message will relate to the entire grouping. For that reason, try not to overload your selector.rb file with lots of multi-line statements. It makes for elegant and short looking tests, but the system sill has to parse all of the substitutions and run them. If anything goes wrong in the block, the entire block fails, and so does the rest of the test.

So there ya' go, some master of the obvious stuff, but it's make my testing a lot easier and in some ways a lot more fun :).

No comments: