Skip to content
This repository was archived by the owner on Nov 16, 2018. It is now read-only.

Calling Steps from Step Definitions

joahking edited this page Aug 13, 2010 · 23 revisions

It is possible to call steps from Step Definitions:

# ruby
Given /^the user (.*) exists$/ do |name|
  # ...
end

Given /^I log in as (.*)$/ do |name|
  # ...
end

Given /^(.*) is logged in$/ do |name|
  Given "the user #{name} exists"
  Given "I log in as #{name}"
end

This is practical if you have several common steps that you want to perform in several scenarios. This allows you to do this in a Scenario:

# feature
Scenario: View last incidents
  Given Linda is logged in # This will in fact invoke 2 step definitions
  When I go to the incident page

Instead of having a lot of repetition:

# feature
Scenario: View last incidents
  Given the user Linda exists
  And I log in as Linda
  When I go to the incident page

Calling with tables and strings

This is only possible in 0.1.99 and higher

Sometimes you want to call a step that has been designed to take a table as input:
(See Using tabular data in features for details about tables).

# ruby
Given /^an expense report for (.*) with the following posts:$/ do |date, posts_table|
  # The posts_table variable is an instance of Cucumber::Ast::Table
end

This can easily be called from a plain text step like this:

# feature
Given an expense report for Jan 2009 with the following posts:
  | account | description | amount |
  | INT-100 | Taxi        |    114 |
  | CUC-101 | Peeler      |     22 |

But what if you want to call this from a step definition? There are a couple of ways to do this:

# ruby
Given /A simple expense report/ do
  Given "an expense report for Jan 2009 with the following posts:", table(%{
    | account | description | amount |
    | INT-100 | Taxi        |    114 |
    | CUC-101 | Peeler      |     22 |
  })
end

Or, if you prefer a more programmatic approach:

# ruby
Given /A simple expense report/ do
  Given "an expense report for Jan 2009 with the following posts:", Cucumber::Ast::Table.new([
    %w{ account description amount },
    %w{ INT-100 Taxi        114    },
    %w{ CUC-101 Peeler      22     }
  ])
end

You can also simply receive a table and pass it down, here the spanish step is passing its table to the english one:

  1. ruby
    Entonces /^debo ver los links:$/ do |links_table|
    Then “I should see the links:”, links_table
    end
  1. this one really implements the deal
    Then /^I should see the links:$/ do |links_table|
    links_table.hashes.each do |hash|
    Then “I should see the link #{hash[‘link’]}”
    end
    end

Clone this wiki locally