Reputation: 99
I have a requirement where in I need to download couple of files (done using karate feature file) and move them to a specific folder (done using java and calling it in the feature file).
In between that I need to check if all the expected files are downloaded. So what I was thinking is, if there is a way to generate report before moving them to the specific folder. So from the report I can come to know if it has passed or failed.
I have the code for generating cucumber report in the runner class and i tried to use the below in the feature file and it does not run.. Gives an error
* def report = Java.type('BI.BiTestRunner')
* def result3 = report.generateReport()
Error: BI.BiTestRunner failed due to: Arity error - expected: 1 actual: 0
Code in my runner class
package BI;
import com.intuit.karate.Results;
import com.intuit.karate.Runner;
import net.masterthought.cucumber.Configuration;
import net.masterthought.cucumber.ReportBuilder;
import org.apache.commons.io.FileUtils;
import org.junit.Test;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import static org.junit.Assert.assertTrue;
public class BiTestRunner {
@Test
public void testParallel() {
System.setProperty("karate.env", "BI");
Results results = Runner.path("classpath:BI/Fluent.feature").outputCucumberJson(true).tags("~@ignore").parallel(1);
generateReport(results.getReportDir());
assertTrue(results.getErrorMessages(), results.getFailCount() == 0);
}
public static void generateReport(String karateOutputPath) {
Collection<File> jsonFiles = FileUtils.listFiles(new File(karateOutputPath), new String[]{"json"}, true);
List<String> jsonPaths = new ArrayList(jsonFiles.size());
jsonFiles.forEach(file -> jsonPaths.add(file.getAbsolutePath()));
Configuration config = new Configuration(new File("target"), "qa-automation");
ReportBuilder reportBuilder = new ReportBuilder(jsonPaths, config);
reportBuilder.generateReports();
}
}
Can someone help me on this please?
Upvotes: 0
Views: 3613
Reputation: 83
@Vishnuprya k I know this is a pretty old topic, but I managed to make the generateReport working as expected (generating Cucumber report before test method run ends). Below is my code:
Test.class
public class AllTestsRunnerTest {
@Test
void testAll() {
Results results = Karate.run().relativeTo(getClass()).tags("~@ignore").outputCucumberJson(true).parallel(1);
generateReport(results.getReportDir());
assertEquals(0, results.getFailCount(), "Expected failures");
}
public static void generateReport(String karateOutputPath) {
Collection<File> jsonFiles = FileUtils.listFiles(new File(karateOutputPath), new String[] { "json" }, true);
List<String> jsonPaths = new ArrayList<>(jsonFiles.size());
jsonFiles.forEach(file -> jsonPaths.add(file.getAbsolutePath()));
Configuration config = new Configuration(new File("target"), "Your project name here");
ReportBuilder reportBuilder = new ReportBuilder(jsonPaths, config);
reportBuilder.generateReports();
}
}
pom.xml
<dependencies>
<!-- Functional / UI / API -->
<dependency>
<groupId>com.intuit.karate</groupId>
<artifactId>karate-junit5</artifactId>
<version>1.2.0</version>
<scope>test</scope>
</dependency>
<!-- Reporting -->
<dependency>
<groupId>net.masterthought</groupId>
<artifactId>cucumber-reporting</artifactId>
<version>5.3.1</version>
<scope>test</scope>
</dependency>
</dependencies>
Note: In my feature files I do not have the def report and def result3 directives set.
Hope this helps!
Upvotes: 0
Reputation: 58088
No, you can't do this. Until Karate completes, the files needed for generateReport()
will not even exist.
This will require some advanced Java - and you can refer the section in this guide with the title "Retry Framework".
With some custom Java code you should be able to take any failed tests at the end of a Runner invocation and re-try them, and merge the results back into the final aggregated report. See example code.
If the above does not help, please consider this as not supported by Karate.
Upvotes: 0