package xu.zhi.hua.report;
import org.testng.IReporter;
import org.testng.ISuite;
import org.testng.ISuiteResult;
import org.testng.ITestResult;
import org.testng.collections.Lists;
import org.testng.collections.Maps;
import org.testng.xml.XmlSuite;
import xu.zhi.hua.testTestNg.entity.TestCaseEntity;
import xu.zhi.hua.testTestNg.entity.XmlTestClassEntity;
import xu.zhi.hua.testTestNg.entity.XmlTestEntity;
import xu.zhi.hua.testTestNg.entity.XmlSuiteEntity;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
public class SelfDefineReport implements IReporter {
public void generateReport(List list, List suites, String s) {
System.out.println("----------------> report:");
System.out.println("suites.size:"+suites.size());
parseISuite(suites);
}
public void parseISuite(List iSuites){
ArrayList xmlSuiteEntities = new ArrayList();
for(ISuite iSuite : iSuites ){
//解析顶层Xml中的Suite信息
XmlSuiteEntity xmlSuiteEntity = new XmlSuiteEntity();
xmlSuiteEntity.setSuiteName(iSuite.getName());
xmlSuiteEntity.setStartTime("");
xmlSuiteEntity.setEndTime("");
ArrayList suiteTestEntities = null;
suiteTestEntities = parseSuiteLevelEntity(iSuite);
xmlSuiteEntity.setXmlTestEntity(suiteTestEntities);
}
}
public ArrayList parseSuiteLevelEntity(ISuite iSuite){
List allTestCaseResults = new ArrayList();
ArrayList suiteTestEntities = new ArrayList();
Map suiteResults = iSuite.getResults();
//解析xml文件中的test部分
for (Map.Entry suiteResult : suiteResults.entrySet()) {
ArrayList xmlTestClassEntities = null;
XmlTestEntity xmlTestEntity = new XmlTestEntity();
xmlTestEntity.setTestName(suiteResult.getKey());
ISuiteResult sr = suiteResult.getValue();
allTestCaseResults.addAll(sr.getTestContext().getPassedTests().getAllResults());
allTestCaseResults.addAll(sr.getTestContext().getFailedTests().getAllResults());
allTestCaseResults.addAll(sr.getTestContext().getSkippedTests().getAllResults());
xmlTestClassEntities = parseTestClassLevelEntity(allTestCaseResults);
xmlTestEntity.setXmlTestClassEntities(xmlTestClassEntities);
suiteTestEntities.add(xmlTestEntity);
}
//suiteTestEntities = parseTestCaseLevelEntity(allTestCaseResults);
return suiteTestEntities;
}
public ArrayList parseTestClassLevelEntity(List allTestCaseResults){
ArrayList xmlTestClassEntities = new ArrayList();
Map> testCaseClassMap = null;
//将测试类及对应的测试方法放到map映射中
testCaseClassMap = parseTestCaseClassMap(allTestCaseResults);
for (Map.Entry> mapping : testCaseClassMap.entrySet()) {
ArrayList testCaseEntities = null;
XmlTestClassEntity xmlTestClassEntity = new XmlTestClassEntity();
xmlTestClassEntity.setClassName(mapping.getKey());
//处理测试类下面的所有测试方法
testCaseEntities = parseTestCaseEntity(mapping.getValue());
xmlTestClassEntity.setTestCaseEntities(testCaseEntities);
xmlTestClassEntities.add(xmlTestClassEntity);
}
return xmlTestClassEntities;
}
public Map> parseTestCaseClassMap(List allTestCaseResults){
Map> map = Maps.newHashMap();
for (ITestResult result : allTestCaseResults) {
String className = result.getTestClass().getName();
List list = map.get(className);
if (list == null) {
list = Lists.newArrayList();
map.put(className, list);
}
list.add(result);
}
return map;
}
public ArrayList parseTestCaseEntity(List testCaseResults){
ReportWriter rw = new ReportWriter();
ArrayList testCaseEntities = new ArrayList();
for(ITestResult testResult : testCaseResults){
TestCaseEntity testCaseEntity = new TestCaseEntity() ;
Throwable error = testResult.getThrowable();
if (error != null) {
//System.out.println("errMes--------->:"+rw.getExceptionTrace(testResult));
testCaseEntity.setException(rw.getExceptionTrace(testResult));
}
String parameter=rw.getParameters(testResult);
if(!parameter.equals("")){
testCaseEntity.setParameter(parameter);
//alc.setDesc(rw.getDescFromParameter(result));
}else {
testCaseEntity.setParameter("parameter is null");
//alc.setDesc("result is null");
}
//描述
// String desc = rw.getDescFromTestAnnotation(testResult);
// if(!desc.equals(""))
// {
// testCaseEntity.setDesc(desc);
// }else{
// testCaseEntity.setDesc("description is null");
// }
//分类
String category = rw.getNameFromCategoryAnnotation(testResult);
if(!category.equals(""))
{
testCaseEntity.setCategory(category);
}else{
testCaseEntity.setCategory("category is null");
}
//作者
String author = rw.getNameFromAuthorAnnotation(testResult);
if(!author.equals(""))
{
testCaseEntity.setAuthor(author);
}else{
testCaseEntity.setAuthor("author is null");
}
testCaseEntity.setCaseName(testResult.getName());
testCaseEntity.setResult(testResult.getStatus());
DateFormat df = new SimpleDateFormat(ReportWriter.DATE_FORMAT);
testCaseEntity.setStartTime(df.format(new Date(testResult.getStartMillis())));
testCaseEntity.setEndTime(df.format(new Date(testResult.getEndMillis())));
testCaseEntity.setElapsedTime(testResult.getEndMillis()-testResult.getStartMillis());
//testCaseEntity.setLogs(getALLog(testResult));
testCaseEntities.add(testCaseEntity);
}
return testCaseEntities;
}
}