5 easy steps to integrate Cucumber Report with Karate and dynamic environment switching

<dependency><groupId>net.masterthought</groupId><artifactId>cucumber-reporting</artifactId><version>5.7.2</version><scope>test</scope></dependency>
function fn() {if (!env) {env = 'staging';}var config = {
// variables & api paths
}//'karate.env' - Get system propertyvar env = karate.env; karate.log('karate.env system property was:', env);if (env == 'prod') {config.baseUrl = 'https://xyz.mgapis.com/';} else if (env == 'staging') {config.baseUrl = 'https://abc.net/';}karate.configure('connectTimeout', 5000);karate.configure('readTimeout', 5000);return config;}
System.setProperty("karate.env",System.getProperty("karate.env"));
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 junit.framework.TestCase.assertTrue;
public class TestRunner {@Test
public void testParallel() {
File file = new File("target/cucumber-html-reports/");
String[] myFiles;
if(file.isDirectory()){
myFiles = file.list();
for (int i=0; i<myFiles.length; i++) {
File myFile = new File(file, myFiles[i]);
System.out.println("Deleteing files: "+myFile);
myFile.delete();
}
}

String karateOutputPath = "target/surefire-reports/";
long starttime = System.nanoTime();
long endtime = System.nanoTime();
System.setProperty("karate.env",System.getProperty("karate.env"));
Results results = Runner.path("classpath:api/abc/test/")
.outputCucumberJson(true)
.parallel(1);
TestRunner.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<String>(jsonFiles.size()); jsonFiles.forEach(file -> jsonPaths.add(file.getAbsolutePath())); Configuration config = new Configuration(new File("target"), "Report-Name"); ReportBuilder reportBuilder = new ReportBuilder(jsonPaths, config); reportBuilder.generateReports();
}
}
mvn clean test -Dtest=TestRunner -DargLine="-Dkarate.env=ENV_NAME"
mvn clean test -Dtest=TestRunner -DargLine="-Dkarate.env=prod"
mvn clean test -Dtest=TestRunner -DargLine="-Dkarate.env=staging"

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store