Saturday, January 20, 2018

How to pass the test data from feature file?

In the example scenario 'Validate login page title' we have taken, we have not passed the test data to step definitions from feature file. The step definitions are hard coded with the test data, the browser type, url and page title as shown in the below screenshot. Hard coding never been a good programming practice.


What if we want to run the same test for different browser and application(url)? We need to write another test and different step definitions. This increases code duplication.

To avoid this Cucumber supports us to pass test data like browser name, application url, page title and etc. from feature file (test steps). If we can write a generic logic in step definitions which can work based on the test data passed from test steps, aren't we good to go? Yes, we are.

So to pass the test data from any step, keep the parameter in double quotes as shown in the below screenshot. In our example, we are going to pass browser, application url and title.


If we observe, after adding double quotes the steps get highlighted in yellow color again. The reason is, when we change at least single character in the existing step it will lose mapping with it's step definition and becomes a new step. We need to write the steps definitions in the same we did previously. You may delete the previous step definitions now.


We have implemented new step definitions, one thing to be noted here is Regular Expression. Whatever the parameters we put in double quotes in the steps in feature file will be converted as a regular expression which means that value is dynamic.

And the parameters will appear in blue color after implementing step definitions as shown in the below screenshot.


Now execute runner class and see the results. The test should pass as below.


There is no problem if the values (test data) to be passed are fixed. But if  we want to execute same test for another application, we need change the test data in the double quotes. What if I want to run the same test for another 100 applications? Doesn't the tester need to change the test data 100 times? . Changing the test data every time is time consuming, require more efforts and not good practice in automation.

So to overcome this problem, where we can run same test for 'n' number of test data sets, Cucumber has feature called 'Scenario Outline'. We will learn this in the next part.

Please comment your suggestions and queries on this topic. Thanks for reading!!

#seleniumbabu #HappyLearning #HappyTesting

1 comment:

  1. The information which you have provided is very good. It is very useful who is looking for
    selenium Online Training Bangalore

    ReplyDelete

Katalon 08: Integration of HP ALM with Katalon Studio

In this post, this post we discuss about integration of  HP ALM with Katalon Studio. If we can integrate HP ALM with Katalon we can update ...