Search code examples
cucumbercucumber-jvmgherkin

How to implement different data for cucumber scenarios based on environment


I have an issue with executing the cucumber-jvm scenarios in different environments. Data that is incorporated in the feature files for scenarios belongs to one environment. To execute the scenarios in different environemnts, I need to update the data in the features files as per the environment to be executed.

for example, in the following scenario, i have the search criteria included in the feature file. search criteria is valid for lets say QA env.

Scenario: search user with valid criteria

Given user navigated to login page
And clicked search link
When searched by providing search criteria
|fname1  |lname1  |address1|address2|city1|state1|58884|
Then verify the results displayed 

it works fine in QA env. But to execute the same scenario in other environments (UAT,stage..), i need to modify search criteria in feature files as per the data in those environments.

I'm thinking about maintaing the data for scenarios in properties file for different environments and read it based on the execution environment.

if data is in properties file, scenario will look like below. Instead of the search criteria, I will give propertyName:

Scenario: search user with valid criteria

Given user navigated to login page
And clicked search link
When searched by providing search criteria
|validSearchCriteria|
Then verify the results displayed 

Is there any other way I could maintain the data for scenarios for all environments and use it as per the environment the scenario is getting executed? please let me know.

Thanks


Solution

  • You can do this in two ways

    1. Push the programming up, so that you pass in the search criteria by the way you run cucumber

    2. Push the programming down, so that your step definition uses the environment to decide where to get the valid search criteria from

    Both of these involve writing a more abstract feature that does not specify the details of the search criteria. So you should end up with a feature that is like

    Scenario: Search with valid criteria
      When I search with valid criteria
      Then I get valid results
    

    I would implement this using the second method and write the step definitions as follows:

    When "I search with valid criteria" do
      search_with_valid_criteria
    end
    
    module SearchStepHelper
      def search_with_valid_criteria
        criteria = retrieve_criteria
        search_with criteria
      end
    
      def retrieve_criteria
        # get the environment you are working in
        # use the environment to retrieve the search criteria
        # return the criteria
      end
    end
    World SearchStepHelper
    

    Notice that all I have done is change the place where you do the work, from the feature, to a helper method in a module.

    This means that as you are doing your programming in a proper programming language (rather than in the features) you can do whatever you want to get the correct criteria.