Tutorial 18 – Pass user data to step definition (Behave BDD)
Welcome to the 18th article in Behave BDD series!
What you will Learn :
Pass user data from command line to step definition
Pass user data from command line to step definition
Let us consider the below feature file. We have added a tagname to first scenario
A portion of the step definition file is as seen below. Make sure to import the pdb module so that we can add the breakpoint
In the previous tutorial, we had seen how to pass the string from feature file to step definition.
In this tutorial, we will see how to pass the user data from command line to step definition.
Let us first add a breakpoint, see line#8 below
Next, open the terminal and execute the feature file by using the tagname
As expected, we enter the (Pdb) prompt
To double check, let us execute ‘l’ command to ensure that breakpoint is correctly set, see below. Make sure that the pointer arrow is pointing towards pdb.set_trace() as seen below
Next execute dir(context) command. As you can see, we have a property ‘config’
Next execute dir(context.config) . As you can see, we have a property ‘userdata’
Next execute context.config.userdata. Notice that right now it returns an empty directory
Exit the (Pdb) prompt by executing exit
Next, add line#8 in the step def file. The variable ‘fname’ will catch the value of key ‘firstname’ that we will pass through command line
The way we pass userdata from command line is -D key/value pair
Let us execute the below command with userdata in the form of key=value pair:
behave --no-capture -t SmokeW2A -D firstname=w2a
Ensure that pointer is pointing towards breakpoint, see below
Next execute context.config.userdata
Notice above that this time the directory is not empty. We can thus use this data in our step def file.
Let us comment line#10 and add line#9 to print the value of key, see below
Execute behave --no-capture -t SmokeW2A -D firstname=w2a
Notice the o/p. We are able to print the value. Thus we are successful in passing the userdata from command line to step def file
Similarly we can keep adding userdata:
behave --no-capture -t abc -D firstname=w2a -D env=test -D browser=chrome
Thank you for reading!