Sharing data between steps in Behave BDD

Tutorial​​ 8​​ –​​ Sharing data between steps​​ in Behave BDD​​ 

Welcome to the 8th article in Behave BDD series!

What you will Learn :

  • Overview of sharing data between steps

  • ‘context’​​ variable

  • Practical Example#1

  • Practical Example#2

  • Add a scenario in example#2

Overview of sharing data between steps

In the previous tutorial, we saw how​​ to pass parameters​​ to​​ steps. Now we will see how to pass parameters​​ between​​ steps.​​ This is required because we need to share information between ‘steps’ frequently. By ‘steps’ we mean the functions in the step definition file.

Now, Python keeps variables alive only in the function they are defined.​​ We​​ cannot access these variables outside of the function they are defined in. However, there might be a need to send data from first function to the second function. So how can we achieve this?​​ Behave provides us a feature to accomplish that.

Please note that it is not a good practice to define global variables. The reason being, global variables are only useful in the file they are defined. These cannot be used outside of the file.​​ During​​ testing, we will be having​​ lots of​​ step definitions​​ and we might need to share the same information all over the place.​​ Global var will​​ not​​ be of any help here.​​ 

For example, lets suppose we have the browser open and the instance of the browser is stored in a variable. Our tests will be in different files​​ and all these​​ the files​​ should be able to​​ access the​​ browser.​​ This is not possible using global var.

Look at the below example:​​ 

  • A random email id is being generated in the first step​​ (Given)

  • We want to login using the same email id in next step (When).​​ But how do we know what the email is?

  • Finally we want to verify the same email id in next step (Then)

So we need some way to store the value of email id that got generated in the first step and use that value in the subsequent steps.​​ 

‘context’​​ variable

The ‘context’ variable is an instance of a class that is used to store contextual data during test run. The syntax is:
context.<variable name> = <value>

Example:

context.rand_email​​ = ‘TestUser1@email.com’

Now we can use this variable in our selenium code (pseudo code below):

during run-time this would have the value TestUser1@email.com​​ 

email_username_field.send_keys(context.rand_email)

 ​​​​ 

  • Important note:​​ context.rand_email​​ representing email address is only valid for a particular scenario in a feature​​ file. So we cannot share​​ the context variables​​ between other​​ scenario(s). We will see this in a practical example soon.

Practical Example#1

Let us consider the same feature

Let us write the step defintion​​ for the ‘Given’ step. Notice step#8. Here we are creating a varibale that we can use in other steps​​ 

Let us run this much, as you see below, the value of variable gets printed

Let us implement ‘When’ step now that will use the same variable

Let us run, see that value gets printed

Let us implement ‘Then’ step now that will use the same variable

Let us run, see that value gets printed

So this is how we can share the data between steps.

Practical Example#2

Let us look at another example

Below is step def file

Lets run it, notice the o/p

Add a scenario in example#2

Let us now add one more scenario to our above example. This is a test case where we are re-requesting for a refund

Let us write step def for the ‘When’ step and let us use the same variable, see below

Now when we run the feature, we get an error. The reason being, the variable that was used in first scenario is not valid for another scenario

Similarly let us add the ‘Then’ step

Now when we run the feature, we get​​ the same​​ error.​​ The summary also says that 1 scenario passed and 1 failed

So this is how we share the data between steps.

Thank you​​ for reading!

Share On

Share on facebook
Share on twitter
Share on linkedin
Share on whatsapp
Share on tumblr
Share on email

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top

Lifetime Membership Club

LIFETIME MEMBERSHIP BIG SALE - ALL LIVE COURES JUST - 7000 RS/ 99 USD
Attend All Live courses in just 7000 rs / $99 - offer ends 10th Nov 2024