09 January 2013

Drying the Cucumber - Calling Steps from Within Steps

After adding quite a few Cucumber tests to my current automation framework, I wanted to get it a bit more clean.  There's a lot of code, some of which could be reused.

First I looked at this as a Ruby problem and tried to create classes within the Cucumber Step Definition files and do some inheritance.

However, I found that Cucumber has it's own way of handling this.  You can call a step from within a step.  Similar to a method call from a different class, you can do something like this:

Given /^a user who is at the Profile Edit screen$/ do
    step "user logs in"
    .....
end

That code:
step "user logs in"
will grab the code driving that step. In my case it could be a login.rb file that has
Given /^user logs in$/ do
    @browser = Watir::Browser.new browser_type
    @browser.goto "http://www......"
    ....
end

Doing this helps dry up tests and keep the code clean.

No comments:

Post a Comment