Looping in Gherkin
- 5 years ago
Nobody replied with a solution (I was hoping for one using hooks), but I figured one out anyway.
To repeat a cucumber test with different input data, you can do something like this (your maven may live in a different place, as will your project):
public static void main(String[] args) {
String dosCommand = "C:\\workarea\\Maven\\apache-maven-3.3.3\\bin\\mvn.cmd test -Dcucumber.options=\"--tags @tag\"";
String directoryName = "C:\\Users\\ttoth\\workspace\\looper\\";
System.out.println("Starting Repeater.main: " + dosCommand + " at directory: " + directoryName);
for (int i=0; i<3; i++) {
writeFile(directoryName + "index.txt", i + "\n");
System.out.println(i + ". Repeater.main: executing " + dosCommand);
runDosCommand(dosCommand, directoryName);
String filename = "index.txt";
System.out.println("Repeater.main: file " + filename + ": " + readFile(directoryName + filename, true));
filename = "target\\surefire-reports\\TEST-com.abc.cuke.looper.AppTest.xml";
String xmlContents = readFile(directoryName + filename, true);
int begin = xmlContents.indexOf("<testsuite");
int end = xmlContents.indexOf(">", begin);
System.out.println("main: file " + filename + ": " + xmlContents.substring(begin, end).replace("name=\"", "\nname=\""));
}
}Where:
public static String runDosCommand(String command, String directoryPath) {
System.out.println("runDosCommand: " + command + " at " + directoryPath);
String result = "";
try {
String[] envp = null;
File directory = new File(directoryPath);
Process process = Runtime.getRuntime().exec(command, envp, directory);
BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()));
String standardOutput;
System.out.println("Standard output: ");
while ((standardOutput = stdInput.readLine()) != null) {
System.out.println(standardOutput);
result = result + standardOutput + "\n";
}
if (stdError.ready()) { System.err.println("Standard error: "); }
while ((standardOutput = stdError.readLine()) != null) {
System.err.println(standardOutput);
result = result + standardOutput + "\n";
}
} catch (Exception ex) {
System.err.println("Failed. Repeater.runCommand command failed (" + command + "). "+ ex.getMessage());
ex.printStackTrace(System.err);
}
return result;
}
public static void writeFile(String fileName, String text) {
try (BufferedWriter bw = new BufferedWriter(new FileWriter(fileName))) {
bw.write(text);
System.out.println("Finished writing to file " + fileName);
} catch (IOException e) {
e.printStackTrace();
}
}
public static String readFile(String pathWithFileName, boolean quietFlag) {
File file = new File(pathWithFileName);
String line;
StringBuilder stringBuilder = new StringBuilder("");
try (BufferedReader bufferedReader = new BufferedReader(new FileReader(file))){
while ((line = bufferedReader.readLine()) != null) {
if (!quietFlag) { System.out.println(line); }
stringBuilder.append(line).append("\n");
}
} catch (IOException ex) {
System.err.println("Error. Could not read file '"+ pathWithFileName + ". " + ex.getMessage());
ex.printStackTrace();
}
return stringBuilder.toString();
}