0

My requirement is to load parameters from an external resource in cucumber

For an example:

Scenario Outline: eating   
    Given there are <start> cucumbers   
    When I eat <eat> cucumbers   
    Then I should have <left> cucumbers

  Examples:
    | start | eat | left |
    |  12   |  5  |  7   |
    |  20   |  5  |  15  |

In above example I like to move example data in any external source like excel or JSON and I also want the same behavior to use single scenario with multiple iteration. Currently If I move my data to external source then I need to duplicate the scenario n number of times and using @before I read the data from external source which looks weird approach to me

Before hook we can read data from excel that I already done. the problem is as the example data is out of feature it become scenario not scenario outline which run the scenario only one time. If I put loop in step definition function then if any one data example fails it failed the entire scenario which make it unreliable

I have found one similar post regarding same but that doesn't provide any clarification and the answer doesn't provide any JSON structure , document etc regarding same

Integrating external data source with Cucumber feature file

Do we have any provision for same in cucumber or can we override any existing functionality to achieve same

I would like to know if someone already done it already, if yes please share with us.

Shubham Jain
  • 16,610
  • 15
  • 78
  • 125
  • @Grasshopper any suggestions please – Shubham Jain Jul 03 '19 at 08:49
  • @mpkorstanje any suggestions please – Shubham Jain Jul 03 '19 at 08:49
  • I did it using the 2 approaches, reading the json as part of step def (make sure to wrap the validation with try...catch) and populating the examples table based on json data using another feature file (like populate_feature_file_name.feature). Let me know if you are interested in any of these options. – supputuri Jul 03 '19 at 12:45
  • @supputuri thanks for your reply ..... It will be great if you can share both approaches so further on any individual opt as per their requirement.. in my case I like to know the first approach - reading the json as part of step def (make sure to wrap the validation with try...catch) – Shubham Jain Jul 03 '19 at 13:15
  • @ShubhamJain You can have a look at [this](http://grasshopper.tech/418/). It is pretty tightly coupled with maven lifecycle. For u to decide about the pros and cons of using this. – Grasshopper Jul 03 '19 at 17:31
  • Thanks for sharing @Grasshopper ... I will implement it and soon will let you know about it .. :) – Shubham Jain Jul 03 '19 at 17:37
  • @ShubhamJain I implemented the 2 solutions in Ruby, have to translate that to Java for you. Will post it once it ready. – supputuri Jul 04 '19 at 04:13

1 Answers1

0

Here is the rough draft of the solution that will give you an idea, you can refine this. Basically you have to call this method separately that will update the feature file and you will have the examples table updated with the json data.

String jsonPath = "C:\\xxx\\sample.json";
String featurePath = "C:\\xxx\\sample.feature";
String tempFilePath = "C:\\xxx\\sample.temp";
// copy all the steps till example from orinal file to temp

File fin = new File(featurePath);
FileInputStream fis = new FileInputStream(fin);
BufferedReader in = new BufferedReader(new InputStreamReader(fis));

FileWriter fstream = new FileWriter(tempFilePath, true);
BufferedWriter out = new BufferedWriter(fstream);

String aLine = null;
while ((aLine = in.readLine()) != null) {
    //Process each line and add output to Dest.txt file
    out.write(aLine);
    out.newLine();
    if (aLine.contains("Examples:")) {
        break;              
    }
}

// open the json
ObjectMapper objectMapper = new ObjectMapper();
try {
    JsonNode root = objectMapper.readTree(new File(jsonPath));
    system.out.println(root); 
    // for each object in root
    out.write("|start|eat|left|");
    out.newLine();
    for (JsonNode row : root) {
        String start = row.get("start").toString();
        String eat = row.get("eat").toString();
        String left = row.get("left").toString();
        // add these deatils to the examples table
        out.write("|" + start + "|" + eat + "|" + left + "|" );
        out.newLine();
    }
} catch (JsonProcessingException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
in.close();
out.close();
fin.delete();
new File(tempFilePath).renameTo(fin);
supputuri
  • 13,644
  • 2
  • 21
  • 39
  • thanks, Are you writing the example data in feature file at runtime ? – Shubham Jain Jul 05 '19 at 09:05
  • Thanks for your efforts but my requirement is totally different... I want my example data in excel and drive cucumber scenario outline from excel not from cucumber feature file – Shubham Jain Jul 05 '19 at 12:34
  • you mean reading the example data from excel or writing the example data to excel? – supputuri Jul 05 '19 at 12:35
  • I will move my example table from feature to excel, no writing required, I just want cucumber to execute them as before. means map the example table from excel not from feature, it require an override in cucumber function I guess – Shubham Jain Jul 05 '19 at 12:40
  • Yeah, this should be your starting point should be https://github.com/cucumber/cucumber-jvm/blob/master/core/src/main/java/cucumber/runtime/Runtime.java – supputuri Jul 05 '19 at 13:07