Passing parameters to Steps in behave

Tutorial​​ 7​​ –​​ Passing parameters to Steps in behave​​ 

Welcome to the 7th article in Behave BDD series!

What you will Learn :

  • A note on ‘And’ keyword

  • Parameters

  • Passing parameters to steps

  • unicode and int​​ 

A note on ‘And’ Keyword

Before we proceed, a small note on ‘And’ keyword.​​ 

‘And’ represents the preceding keyword (‘Given’, ‘When’, ‘Then’). The feature on the left hand side repeats the Given/Then keywords (although the feature is written​​ perfectly​​ fine). However, it is recommented to use the ‘And’ keyword if you would like to repeat the previous keyword (see right hand side​​ that looks for expressive)

Parameters

To understand this concept, look at the below figure.​​ Look at the similarity of​​ 3 ‘Given’ steps. They are almost the same except that they are on different pages (landing/products/pricing).​​ So the ‘Given’ statements are doing the same thing except that they are referring to different pages.​​ The question that arises now is,​​ do​​ we​​ want to write 3​​ different​​ step definitions​​ for each of these 3 ‘Given’ steps​​ OR do​​ we​​ want to write a​​ single​​ step definition that specifies: go to landing page or go to products page or go to pricing page?​​ 

Similarly, in the below example, it would​​ not​​ make any sense to write 2 separate step definitions​​ to add items to cart

So, in essence, we need to pass values to functions(steps)​​ in the step definition file.​​ Functions may take some kind of input that they will take actions on.​​ The best practice is to put the parameters in quotes in the feature file, examples below

Passing parameters to steps

Let us now see some practical examples. Create a feature file having below steps

Copy the description of any step

We will now create​​ a step definiton file with only one @when method for all the 3 steps.​​ Paste the above description between the double quotes as seen below​​ 

Now, we will replace​​ “previous” with a variable {item}. You can have any variable name. We then pass this variable in the search_for_orders function. Finally we print this variable​​ ​​ 

Save the file and run the feature file.​​ 

Notice below that the print statement gets executed 3​​ times and prints the value of respective variables (although we have written only one step def function)

Let us add one more step in our feature file and save it

Run the feature file, notice that the additional step too gets executed​​ 

Let us add 2 more lines in our feature file, see below

Add the step def​​ 

Run the feature file, the function gets executed for both the parameters, see below

Let us add 1 more line in our feature file, see below

Although right now we have a step with only “success”, later we can have a similar step with “error” or “failure”. So we can parametrize this step. So let us add the step def, see below

Run the feature file

unicode and​​ int

Let us now see how to convert a unicode or string type to integer type. To understand this, add the below scenario

Next add a step definition. Make sure that the description is within single quotes this time and the parameter is within double quotes as seen below

Next, let us add the function and try to print the ‘type’ of parameter that we pass, see line 19​​ 

Run the feature file. Notice below that it is of type string

Let us now add below 2 lines in our step def. In line#21 we are using the ‘int’ function to convert string to an integer

Save and run the feature file. Notice that this time the type is integer

So this is how we can convert the types when we pass parameters to 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 1st Nov 2024