How to use external data file (for example a json file) for scenario outline to iterate over a scenario in CucumberJS
Asked
Active
Viewed 310 times
-2
-
2Hi, welcome to SO. Can you please provide some more details and some code example of what you tried? – Chelmy88 Aug 22 '19 at 15:03
1 Answers
0
You need use a custom code to inject the data in Examples
of Scenario Outline
. NoraUi automation Framework use this technic.
Example of scenario:
@loginLogout
Feature: loginLogout (Scenario that login and logout with any good user.)
Scenario Outline: Scenario that login and logout with any good user.
Given I check that 'user' '<user>' is not empty
Given I check that 'user' '<password>' is not empty
Given 'BAKERY_HOME' is opened
Then The BAKERY home page is displayed
When I log in to BAKERY as '<user>' '<password>'
Then The administrator part of the BAKERY portal is displayed?
|key|expected|actual|
|profile|admin|<profile>|
Then The referencer part of the BAKERY portal is displayed?
|key|expected|actual|
|profile|referencer|<profile>|
And I wait 3 seconds
When I log out of BAKERY
Then The BAKERY logout page is displayed
And I go back to 'BAKERY_HOME'
Examples:
#DATA
|id|user|password|profile|
|1|foo|123456|admin|
|2|bar|123456|referencer|
#END
Exemple of injector in java, you need use the same technic with your favorite languge:
public static void injectDataInGherkinExamples(String filename, Map<Integer, List<String[]>> examplesTable) {
try {
if (!examplesTable.isEmpty()) {
final Path filePath = getFeaturePath(filename);
final String fileContent = new String(Files.readAllBytes(filePath), Constants.DEFAULT_ENDODING);
String lang = getFeatureLanguage(fileContent);
LOGGER.info(lang);
StringBuilder examplesString;
final String[] scenarioOutlines = "fr".equals(lang) ? fileContent.split(SCENARIO_OUTLINE_SPLIT_FR) : fileContent.split(SCENARIO_OUTLINE_SPLIT);
for (final Entry<Integer, List<String[]>> examples : examplesTable.entrySet()) {
examplesString = new StringBuilder();
examplesString.append(" ");
for (int j = 0; j < examples.getValue().size(); j++) {
examplesString.append(SCENARIO_EXAMPLE_COLUMNS_SEPARATOR);
examplesString.append(j + 1);
for (final String col : examples.getValue().get(j)) {
examplesString.append(SCENARIO_EXAMPLE_COLUMNS_SEPARATOR);
examplesString.append(col);
}
examplesString.append(SCENARIO_EXAMPLE_COLUMNS_SEPARATOR + "\n ");
}
scenarioOutlines[examples.getKey() + 1] = scenarioOutlines[examples.getKey() + 1].replaceAll("(" + DATA + "\r?\n.*\r?\n)[\\s\\S]*(" + DATA_END + ")",
"$1" + examplesString.toString() + "$2");
}
try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath.toString()), Constants.DEFAULT_ENDODING));) {
int i = 0;
bw.write(scenarioOutlines[i]);
while (++i < scenarioOutlines.length) {
if ("fr".equals(lang)) {
bw.write(SCENARIO_OUTLINE_SPLIT_FR + scenarioOutlines[i]);
} else {
bw.write(SCENARIO_OUTLINE_SPLIT + scenarioOutlines[i]);
}
}
}
}
} catch (final IOException e) {
LOGGER.error("error GherkinFactory.injectDataInGherkinExamples()", e);
}
}
you can find all source here on github.

Stéphane GRILLON
- 11,140
- 10
- 85
- 154