giri Vuppula
giri Vuppula

Reputation: 11

Sharing State and data between cucumber steps in Ruby automation

I am creating an automation framework using Ruby, Selenium webdriver and cucumber (Gherkin), so looking to share the data between steps like Scenario Context in specflow (c#) and cucumber (java)?

Any other suggestions will appreciated

Upvotes: 1

Views: 477

Answers (1)

diabolist
diabolist

Reputation: 4099

Following has lots of ruby pseudocode

There are several ways to share state between steps.

  1. Identify something with a name and then look it up using the name
Given a user Fred
When Fred does Foo

Given 'a user Fred'
  User.create(name: Fred)
end

When `Fred does Foo` do
  fred = User.find_by_name('Fred')
  foo_with(fred)
end

  1. Identify something by type and then look it up using last.
Given a user
When the user does foo

Given 'a user'
  User.create
end

When 'the user does foo' do
  user = User.last
  foo_with(user)
end
  1. Use a global
Given a user Fred
When Fred does foo

Given 'a user Fred' do
  @fred = User.create(name: Fred)
end

When 'Fred does foo' do
  foo_with(@fred)
end

Using a global is the easiest, and the most dangerous. If you apply great discipline then it can work really well. This is what I use exclusively now.

  • never reassign a global
  • keep number of globals relatively low
  • don't use globals in helper methods (instead pass them in as params)
  • don't have complex step defs (instead make a step def a call to a helper method)

Upvotes: 0

Related Questions