如何生成自定义的jvm-cucumber HTML,如何通过Jenkins发送并通过ftp将JSON结果发送?结果生成应该在整个mvn黄瓜套件执行之后发生,以便我可以从测试执行完成后生成的json中捕获通过/失败的结果。
我使用延长黄瓜来实现这一目的-
这是扩展的黄瓜代码,并在您的库中创建此类-
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import com.github.mkolisnyk.cucumber.runner.AfterSuite;
import com.github.mkolisnyk.cucumber.runner.BeforeSuite;
import org.junit.runner.Description;
import org.junit.runner.Runner;
import org.junit.runner.notification.RunNotifier;
import cucumber.api.junit.Cucumber;
public class ExtendedCucumberRunner extends Runner {
private Class clazz;
private Cucumber cucumber;
public ExtendedCucumberRunner(Class clazzValue) throws Exception {
clazz = clazzValue;
cucumber = new Cucumber(clazzValue);
}
@Override
public Description getDescription() {
return cucumber.getDescription();
}
private void runPredefinedMethods(Class annotation) throws Exception {
if (!annotation.isAnnotation()) {
return;
}
Method[] methodList = this.clazz.getMethods();
for (Method method : methodList) {
Annotation[] annotations = method.getAnnotations();
for (Annotation item : annotations) {
if (item.annotationType().equals(annotation)) {
method.invoke(null);
break;
}
}
}
}
@Override
public void run(RunNotifier notifier) {
try {
runPredefinedMethods(BeforeSuite.class);
} catch (Exception e) {
e.printStackTrace();
}
cucumber.run(notifier);
try {
runPredefinedMethods(AfterSuite.class);
} catch (Exception e) {
e.printStackTrace();
}
}
}
这里是运行程序类代码,将用于从mvn命令开始,它将基于以@ SUMMARY-开头的黄瓜标签捕获摘要。
import libs.ExtendedCucumberRunner; import libs.FTP; import libs.RemoteSSH; import com.github.mkolisnyk.cucumber.reporting.CucumberResultsOverview; import com.github.mkolisnyk.cucumber.runner.AfterSuite; import com.github.mkolisnyk.cucumber.runner.BeforeSuite; import com.github.mkolisnyk.cucumber.runner.ExtendedCucumberOptions; import cucumber.api.CucumberOptions; import org.apache.commons.io.FileUtils; import org.junit.runner.RunWith; import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import java.text.SimpleDateFormat; import java.util.*; import static libs.BaseTest.tagCounts; import static com.automation.steps.Common.failedCount; @RunWith(ExtendedCucumberRunner.class) @ExtendedCucumberOptions(jsonReport = "target/Destination/cucumber.json", overviewReport = true, outputFolder = "target/ext-cucumber-results" ,retryCount = 0) @CucumberOptions( plugin = {"pretty","html:target/Destination","json:target/Destination/cucumber.json"}, strict = true, monochrome = true ) public class Runner { @BeforeSuite public static void manageResults() throws IOException { String directory=System.getProperty("user.dir"); if (new File("target/Destination/cucumber.json").exists()) { String contents = new String(Files.readAllBytes(Paths.get("target/Destination/cucumber.json"))).toString().trim(); if (!contents.equals("")) { long timestamp = System.currentTimeMillis() / 1000; FileUtils.copyFile(new File("target/Destination/cucumber.json"), new File("target/Destination/cucumber_" + timestamp + ".json")); } } } @AfterSuite public static void copy_json_generate_report() throws Exception { String directory=System.getProperty("user.dir"); CucumberResultsOverview results = new CucumberResultsOverview(); results.setOutputDirectory("target/ext-cucumber-results"); results.setOutputName("cucumber-results"); results.setSourceFile(directory+"/target/Destination/cucumber.json"); results.execute(); String format_tag_name=""; if(System.getenv("TAG_NAME")==null){ format_tag_name="Local--"; }else { format_tag_name = System.getenv("TAG_NAME"). replaceAll("@", "").replaceAll(",", "-") .replaceAll("\\[", "") .replaceAll("]", ""); } String destination_path = ""; Date today = Calendar.getInstance().getTime(); SimpleDateFormat unix_format = new SimpleDateFormat("yyyyMMdd"); long timestamp = System.currentTimeMillis() / 1000; if (failedCount==0) { destination_path ="/tmp/QA_RESULTS/"+unix_format.format(today)+"/"+format_tag_name+"_" + timestamp + ".json"; }else{ destination_path ="/tmp/QA_RESULTS/"+unix_format.format(today)+"/"+format_tag_name+"_" + timestamp + "--F.json"; } System.out.println("After class is being run now- to copy json files!!!"); if (new File(directory+"/target/Destination/cucumber.json").exists()) { System.out.println(directory+"/target/Destination/cucumber.json --exits!"); // CommonUtils.createFile(GenerateHTMLReport(),"Destination/CucumberSummaryReport.html"); String contents = new String(Files.readAllBytes(Paths.get(directory+"/target/Destination/cucumber.json"))).toString().trim(); if (!contents.equals("")) { RemoteSSH ssh = new RemoteSSH(); List<String> check_file_commands = Arrays.asList("mkdir -p /tmp/QA_RESULTS/"+unix_format.format(today) ); List<String> check_file_logs = ssh.SSHClient(check_file_commands); String local_path = directory+"/target/Destination/cucumber.json"; System.out.println(local_path); FTP ftp = new FTP(); ftp.SCPUpload(local_path,destination_path); }else{ System.out.println("File is empty!"); } } try { Runtime.getRuntime().exec("TASKKILL /F /IM xxx.exe"); } catch (IOException e) { System.out.println("Killing xxxx.exe error out"); } } public static String GenerateHTMLReport(){ String html_string = "<!-- CSS Code: Place this code in the document's head (between the 'head' tags) -->\n" + "<style>\n" + "table.GeneratedTable {\n" + " width: 100%;\n" + " background-color: #ffffff;\n" + " border-collapse: collapse;\n" + " border-width: 2px;\n" + " border-color: #ffcc00;\n" + " border-style: solid;\n" + " color: #000000;\n" + "}\n" + "\n" + "table.GeneratedTable td, table.GeneratedTable th {\n" + " border-width: 2px;\n" + " border-color: #ffcc00;\n" + " border-style: solid;\n" + " padding: 3px;\n" + "}\n" + "\n" + "table.GeneratedTable thead {\n" + " background-color: #ffcc00;\n" + "}\n" + "</style>\n" + "\n" + "<!-- HTML Code: Place this code in the document's body (between the 'body' tags) where the table should appear -->\n" + "<table class=\"GeneratedTable\">\n" + " <thead>\n" + " <tr>\n" + " <th>Functionality Group</th>\n" + " <th>Passed</th>\n" + " <th>Passed With Warning</th>\n" + " <th>Failed</th>\n" + " <th>Script Issue</th>\n" + " <th>Env Issue</th>\n" + " <th>Warning</th>\n" + " </tr>\n" + " </thead>\n" + " <tbody>\n" + " <tr>\n"; System.out.println(tagCounts.size()); for (Map.Entry<String,List<Integer>> entry : tagCounts.entrySet()) { html_string = html_string + " <tr><td>" + entry.getKey() + "</td>\n" + " <td>" + entry.getValue().get(0) + "</td>\n" + " <td>" + entry.getValue().get(1) + "</td>\n" + " <td>" + "Script Issue Value" + "</td>\n" + " <td>" + "Env Issue Value" + "</td></tr>\n"; } html_string = html_string + " </tr>\n" + " </tbody>\n" + "</table>\n" + "\n"; return html_string; } }
现在在Jenkins配置中,将以下内容添加到可编辑电子邮件配置加载项的内容部分中
<html>
<body>
<p>Pls refer the below results-</p>
<a href="${BUILD_URL}/cucumber-html-reports/overview-features.html">${BUILD_URL}/cucumber-html-reports/overview-features.html</a>
<p>Note: Copy and paste the above link in chrome or Firefox. The report won't work in IE.</p>
</body>
</html>
${FILE,path="target/CucumberSummaryReport.html"}
${FILE,path="target/ext-cucumber-results/cucumber-results-feature-overview.html"}