Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preserve variable in cucumber?

I want to access variables in difference Given/Then/When clauses. How to preserve variables so that they are accessible everywhere?

Given(#something) do
  foo = 123 # I want to preserve foo
end

Then(#something) do
  # how to access foo at this point??? 
end
like image 384
OneZero Avatar asked Sep 23 '13 14:09

OneZero


People also ask

How do I set environment variables in cucumber?

Click on the Environment Variables field. Enter the environment variable and its value into the dialog.

What is scenario context in cucumber?

Scenario Context class holds the test data information explicitly. It helps you store values in a key-value pair between the steps. Moreover, it helps in organizing step definitions better rather than using private variables in step definition classes.


1 Answers

To share variables across step definitions, you need to use instance or global variables.

Instance variables can be used when you need to share data across step definitions but only for the one test (ie the variables are cleared after each scenario). Instance variables start with a @.

Given(#something) do
  @foo = 123
end

Then(#something) do
  p @foo
  #=> 123
end

If you want to share a variable across all scenarios, you could use a global variable, which start with a $.

Given(#something) do
  $foo = 123
end

Then(#something) do
  p $foo
  #=> 123
end

Note: It is usually recommended not to share variables between steps/scenarios as it creates coupling.

like image 124
Justin Ko Avatar answered Oct 23 '22 09:10

Justin Ko