What object is in play for instance variables (i.e. What is self) in cucumber definitions?

I don't understand how to use Cucumber in Ruby, especially with regard to instance variables.

In the context of my immediate example, in the Before before section, a hooks.rb

variable is assigned @browser

.

@browser = Watir::Browser.new @browser_selected.to_sym

(where @browser_selected

usually "chrome")

Step definitions use bbrowser. As a simple example:@browser.send_keys(:tab)

I don't understand which object contains @browser as an attribute. How does this make sense in this context? I know that the code I'm puzzled with is always in a block and I understand that every such block is used (via the "Given / When / Then" message it is attached to) to be preprocessed in some mysterious way ...

In light of this mystery, there is the definition of instance variables. How to find out the amount of instance variables in such blocks?

+3


source to share


1 answer


self

in the steps cucumber and hooks are just a Ruby object, the "world" that is used in all scenarios. The block in each step definition is executed in the context of a world with a help the_world.instance_eval

or something similar, which means that when each block is running self

, it is a world. So the object that all these instance variables belong to is the same object, the world. The scope of all these instance variables is the entire script.

Therefore, it is important to use instance variables sparingly in Cucumber steps, and also make it clear that you are using them in step names (i.e. clearly state the step names that they refer to some state). These steps explicitly refer to the thing that gets stored behind the scenes (i.e. refers to the same instance variable):

Given there is a thing
When I frob the thing
Then the thing should be frobbed

      



This is normal and normal. But it would be awful if I When I frob the thing

predicted the expected results of the assertion and hide them in the instance variables as well, and Then the thing should be frobbed

use those instance variables in my assertions. Then the thing should be frobbed

will not work unless When I frob the thing

preceded by it, which makes it less reusable, and this limitation will not be obvious to others who write functions, and they will be frustrated. (Don't be my former colleague.)

Back to the World: Cucumber creates a new world for each script and throws it at the end, so the script instance variables do not affect the next script. In an ordinary cucumber, the world is just an example Object

. In cucumber rails, this is an instance Cucumber::Rails::World

( which is fun to read ). Besides the methods built into the world in cucumber rails, the world gets its methods by extending modules (as in the_world.extend SomeModule

). As with instance variables, all methods from all modules that are globally stuck on the same object (world), so you sometimes have to worry about name conflicts.

+5


source







All Articles