Java⾃动化测试系列[v1.0.0][ZTestReport测试报告]
获取报告框架
ZTestReport 源码Clone地址为,其中ZTestReport.java和其template是我们需要的关键
修改报告框架
根据我的需要,在源码基础上进⾏了稍微修改,其中⼏个注释的地⽅需要注意,将其集成进⾃⼰的⾃动化框架时需要做相应的修改package;
import Gson;
import GsonBuilder;
import*;
import XmlSuite;
import*;
import SimpleDateFormat;
import*;
import Matcher;
public class TestReport implements IReporter {
private long currentTime = System.currentTimeMillis();
private SimpleDateFormat formatter =new SimpleDateFormat ("yyyy年-MM⽉-dd⽇-HH时mm分ss秒");
private Date date =new Date(currentTime);
private String reportdate = formatter.format(date);
// 定义⽣成测试报告的路径和⽂件名,为兼容Windows和Linux此处使⽤File.separator代替分隔符
private String path = Property("user.dir")+File.separator+reportdate+".html";
// 定义html样式模板所在路径
private String templatePath = Property("user.dir")+File.separator+"template";
private int testsPass =0;
private int testsFail =0;
private int testsSkip =0;
private String beginTime;
private long totalTime;
private String name ="PaaS平台⾃动化测试";
/**
public TestReport(){
long currentTime = System.currentTimeMillis();
SimpleDateFormat formatter = new SimpleDateFormat ("yyyy年-MM⽉-dd⽇-HH时mm分ss秒");
Date date = new Date(currentTime);
name = formatter.format(date);
}
public TestReport(String name){
this.name = name;
if(this.name==null){
long currentTime = System.currentTimeMillis();
SimpleDateFormat formatter = new SimpleDateFormat ("yyyy年-MM⽉-dd⽇-HH时mm分ss秒");
Date date = new Date(currentTime);
this.name = formatter.format(date);
}
}
java switch case string
*/
@Override
public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory){
List<ITestResult> list =new ArrayList<ITestResult>();
for(ISuite suite : suites){
Map<String, ISuiteResult> suiteResults = Results();
for(ISuiteResult suiteResult : suiteResults.values()){
ITestContext testContext = TestContext();
IResultMap passedTests = PassedTests();
testsPass = testsPass + passedTests.size();
IResultMap failedTests = FailedTests();
testsFail = testsFail + failedTests.size();
IResultMap skippedTests = SkippedTests();
testsSkip = testsSkip + skippedTests.size();
IResultMap failedConfig = FailedConfigurations();
list.addAll(this.listTestResult(passedTests));
list.addAll(this.listTestResult(failedTests));
list.addAll(this.listTestResult(skippedTests));
list.addAll(this.listTestResult(failedConfig));
}
}
this.sort(list);
this.outputResult(list);
}
private ArrayList<ITestResult>listTestResult(IResultMap resultMap){
Set<ITestResult> results = AllResults();
return new ArrayList<ITestResult>(results);
}
private void sort(List<ITestResult> list){
Collections.sort(list,new Comparator<ITestResult>(){
@Override
public int compare(ITestResult r1, ITestResult r2){
StartMillis()> r2.getStartMillis()){
return1;
}else{
return-1;
}
}
});
}
private void outputResult(List<ITestResult> list){
try{
List<ReportInfo> listInfo =new ArrayList<ReportInfo>();
int index =0;
for(ITestResult result : list){
String tn = TestContext().getCurrentXmlTest().getParameter("testCase");
if(index==0){
SimpleDateFormat formatter =new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");                    beginTime = formatter.format(new StartMillis()));
index++;
}
long spendTime = EndMillis()- StartMillis();
totalTime += spendTime;
String status =Status());
List<String> log = Output(result);
for(int i =0; i < log.size(); i++){
log.set(i, (i).replaceAll("\"","\\\\\""));
}
Throwable throwable = Throwable();
if(throwable!=null){
log.String().replaceAll("\"","\\\\\""));
StackTraceElement[] st = StackTrace();
for(StackTraceElement stackTraceElement : st){
log.add(("    "+ stackTraceElement).replaceAll("\"","\\\\\""));
}
}
ReportInfo info =new ReportInfo();
info.setName(tn);
info.setSpendTime(spendTime+"ms");
info.setSpendTime(spendTime+"ms");
info.setStatus(status);
info.InstanceName());
info.Name());
info.Method().getDescription());
info.setLog(log);
listInfo.add(info);
}
Map<String, Object> result =new HashMap<String, Object>();
result.put("testName", name);
result.put("testPass", testsPass);
result.put("testFail", testsFail);
result.put("testSkip", testsSkip);
result.put("testAll", testsPass+testsFail+testsSkip);
result.put("beginTime", beginTime);
result.put("totalTime", totalTime+"ms");
result.put("testResult", listInfo);
Gson gson =new GsonBuilder().disableHtmlEscaping().setPrettyPrinting().create();
String template =ad(templatePath);
BufferedWriter output =new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File
(path)),"UTF-8"));            template = placeFirst("\\$\\{resultData\\}", Matcher.Json(result)));
output.write(template);
output.flush();
output.close();
}catch(IOException e){
e.printStackTrace();
}
}
private String getStatus(int status){
String statusString =null;
switch(status){
case1:
statusString ="成功";
break;
case2:
statusString ="失败";
break;
case3:
statusString ="跳过";
break;
default:
break;
}
return statusString;
}
public static class ReportInfo {
private String name;
private String className;
private String methodName;
private String description;
private String spendTime;
private String status;
private List<String> log;
public String getName(){
return name;
return name;
}
public void setName(String name){
this.name = name;
}
public String getClassName(){
return className;
}
public void setClassName(String className){
this.className = className;
}
public String getMethodName(){
return methodName;
}
public void setMethodName(String methodName){
}
public String getSpendTime(){
return spendTime;
}
public void setSpendTime(String spendTime){
this.spendTime = spendTime;
}
public String getStatus(){
return status;
}
public void setStatus(String status){
this.status = status;
}
public List<String>getLog(){
return log;
}
public void setLog(List<String> log){
this.log = log;
}
public String getDescription(){
return description;
}
public void setDescription(String description){
this.description = description;
}
}
private String read(String path){
File file =new File(path);
InputStream is =null;
StringBuffer sb =new StringBuffer();
try{
is =new FileInputStream(file);
int index =0;
byte[] b =new byte[1024];
while((index = is.read(b))!=-1){
while((index = is.read(b))!=-1){
sb.append(new String(b,0, index));
}
String();
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}finally{
try{
if(is !=null){
is.close();
}
}catch(IOException e){
e.printStackTrace();
}
}
return null;
}
}
HTML模板template
template⽂件是和ZTestReport.java⼀起使⽤的,他可以将TestNG的测试结果按照template的样式转换成HTML格式的报告⽅式⼀:在测试类添加监听
package;
import DOMConfigurator;
import*;
import Assert;
import*;
import KeyActionsUtil;
import static KeyActionsUtil.*;
import List;
import static MysqlService.linkToMysqlPage;
import static KeyBoardUtil.pressTabKey;
import static LogUtil.info;
import static MySQLService.*;
import static ScrollBarUtil.scrolltoBottom;
import static WaitElementUtil.sleep;
// @Listeners({util.TestReport.class})
public class Test_Mysql {
static{
}
@BeforeClass
public void setUp()throws Exception {
WebDriver driver = KeyActionsUtil.initBrowser("chrome");
linkToMysqlPage(driver,"yangdawei","alex005x");
sleep(2000);
}
@Test(priority =0, description ="测试创建mysql数据库服务1CPU2G")
public void test_CreateMysqlInstance()throws Exception {
create_New_Instance_Button(driver).click();
info("点击创建实例按钮...");
sleep(1000);
info("等待3秒...");
instance_Name_in_Create_Instance_Dialog(driver).sendKeys("automationtest");
info("输⼊实例名:automationtesta");
sleep(1000);

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。