selenium java项目_Selenium Web 自动化 - 项目实战(一)

packagecom.demo.test.plugins.arrow;importjava.io.BufferedWriter;importjava.io.File;importjava.io.FileWriter;importjava.io.IOException;importjava.io.PrintWriter;importjava.text.DecimalFormat;importjava.text.NumberFormat;importjava.util.ArrayList;importjava.util.Arrays;importjava.util.Collection;importjava.util.Comparator;importjava.util.HashMap;importjava.util.HashSet;importjava.util.List;importjava.util.Map;importjava.util.Set;importorg.testng.IInvokedMethod;importorg.testng.IReporter;importorg.testng.IResultMap;importorg.testng.ISuite;importorg.testng.ISuiteResult;importorg.testng.ITestClass;importorg.testng.ITestContext;importorg.testng.ITestNGMethod;importorg.testng.ITestResult;importorg.testng.Reporter;importorg.testng.collections.Lists;importorg.testng.internal.Utils;importorg.testng.log4testng.Logger;importorg.testng.xml.XmlSuite;importcom.demo.test.plugins.arrow.utils.ConfigReader;importcom.thoughtworks.qdox.JavaDocBuilder;importcom.thoughtworks.qdox.model.DocletTag;importcom.thoughtworks.qdox.model.JavaClass;importcom.thoughtworks.qdox.model.JavaMethod;/*** Reported designed to render self-contained HTML top down view of a testing

* suite.*/

public class PowerEmailableReporter implementsIReporter {private static final Logger L = Logger.getLogger(PowerEmailableReporter.class);//~ Instance fields ------------------------------------------------------

privatePrintWriter m_out;private intm_row;privateInteger m_testIndex;private Set testIds = new HashSet();private List allRunTestIds = new ArrayList();private JavaDocBuilder builder = newJavaDocBuilder();//~ Methods --------------------------------------------------------------

/**Creates summary of the run*/

public void generateReport(List xml, Listsuites, String outdir) {try{

m_out=createWriter(outdir);

}catch(IOException e) {

L.error("output file", e);return;

}

ConfigReader cr=ConfigReader.getInstance();

builder.setEncoding(cr.getSrouceCodeEncoding());

builder.addSourceTree(newFile(cr.getSourceCodeDir()));

startHtml(m_out);

generateSuiteSummaryReport(suites);

testIds.clear();

generateMethodSummaryReport(suites);

testIds.clear();

generateMethodDetailReport(suites);

testIds.clear();

endHtml(m_out);

m_out.flush();

m_out.close();

}protected PrintWriter createWriter(String outdir) throwsIOException {newFile(outdir).mkdirs();return new PrintWriter(new BufferedWriter(new FileWriter(new File(outdir, "power-emailable-report.html"))));

}/*** Creates a table showing the highlights of each test method with links to

* the method details*/

protected void generateMethodSummaryReport(Listsuites) {

startResultSummaryTable("methodOverview");int testIndex = 1;for(ISuite suite : suites) {if (suites.size() > 1) {

titleRow(suite.getName(),5);

}

Map r =suite.getResults();for(ISuiteResult r2 : r.values()) {

ITestContext testContext=r2.getTestContext();

String testName=testContext.getName();

m_testIndex=testIndex;

resultSummary(suite, testContext.getSkippedConfigurations(), testName,"skipped", " (configuration methods)");

resultSummary(suite, testContext.getSkippedTests(), testName,"skipped", "");

resultSummary(suite, testContext.getFailedConfigurations(), testName,"failed", " (configuration methods)");

resultSummary(suite, testContext.getFailedTests(), testName,"failed", "");

resultSummary(suite, testContext.getPassedTests(), testName,"passed", "");

testIndex++;

}

}

m_out.println("");

}/**Creates a section showing known results for each method*/

protected void generateMethodDetailReport(Listsuites) {for(ISuite suite : suites) {

Map r =suite.getResults();for(ISuiteResult r2 : r.values()) {

ITestContext testContext=r2.getTestContext();if (r.values().size() > 0) {

m_out.println("

" + testContext.getName() + "

");

}

resultDetail(testContext.getFailedConfigurations());

resultDetail(testContext.getFailedTests());

resultDetail(testContext.getSkippedConfigurations());

resultDetail(testContext.getSkippedTests());

resultDetail(testContext.getPassedTests());

}

}

}/***@paramtests*/

private voidresultSummary(ISuite suite, IResultMap tests, String testname, String style, String details) {if (tests.getAllResults().size() > 0) {

StringBuffer buff= newStringBuffer();

String lastClassName= "";int mq = 0;int cq = 0;

Map methods = new HashMap();

Set setMethods = new HashSet();for(ITestNGMethod method : getMethodSet(tests, suite)) {

m_row+= 1;

ITestClass testClass=method.getTestClass();

String className=testClass.getName();if (mq == 0) {

String id= (m_testIndex == null ? null : "t" +Integer.toString(m_testIndex));

titleRow(testname+ " — " + style + details, 5, id);

m_testIndex= null;

}if (!className.equalsIgnoreCase(lastClassName)) {if (mq > 0) {

cq+= 1;

m_out.print("

" + " 1) {

m_out.print(" rowspan=\"" + mq + "\"");

}

m_out.println(">" + lastClassName + "

" +buff);

}

mq= 0;

buff.setLength(0);

lastClassName=className;

}

Set resultSet =tests.getResults(method);long end =Long.MIN_VALUE;long start =Long.MAX_VALUE;for(ITestResult testResult : tests.getResults(method)) {if (testResult.getEndMillis() >end) {

end=testResult.getEndMillis();

}if (testResult.getStartMillis()

start=testResult.getStartMillis();

}

}

mq+= 1;if (mq > 1) {

buff.append("

");

}

String description=method.getDescription();

String testInstanceName= resultSet.toArray(new ITestResult[] {})[0].getTestName();//Calculate each test run times, the result shown in the html//report.

ITestResult[] results = resultSet.toArray(newITestResult[] {});

String methodName=method.getMethodName();if(setMethods.contains(methodName)) {

methods.put(methodName, methods.get(methodName)+ 1);

}else{

setMethods.add(methodName);

methods.put(methodName,0);

}

String parameterString= "";int count = 0;

ITestResult result= null;if (results.length >methods.get(methodName)) {

result=results[methods.get(methodName)];int testId =getId(result);for(Integer id : allRunTestIds) {if (id.intValue() ==testId)

count++;

}

Object[] parameters=result.getParameters();boolean hasParameters = parameters != null && parameters.length > 0;if(hasParameters) {for(Object p : parameters) {

parameterString= parameterString + Utils.escapeHtml(p.toString()) + " ";

}

}

}int methodId =method.getTestClass().getName().hashCode();

methodId= methodId +method.getMethodName().hashCode();if (result != null)

methodId= methodId + (result.getParameters() != null ? Arrays.hashCode(result.getParameters()) : 0);

buff.append("

" + qualifiedName(method) + " " + (description != null && description.length() > 0 ? "(\"" + description + "\")" : "") + "" + (null == testInstanceName ? "" : "
(" + testInstanceName + ")") + "" + this.getAuthors(className, method) + "" + resultSet.size() + "" + "" + (count == 0 ? "" : count) + "" + "" + parameterString + "" + "" + start + "" + "" + (end - start) + "" + "");

}if (mq > 0) {

cq+= 1;

m_out.print("

" + " 1) {

m_out.print(" rowspan=\"" + mq + "\"");

}

m_out.println(">" + lastClassName + "

" +buff);

}

}

}/**Starts and defines columns result summary table*/

private voidstartResultSummaryTable(String style) {

tableStart(style,"summary");

m_out.println("

ClassMethodAuthors# of
ScenariosRunning Counts" + "ParametersStartTime
(ms)");

m_row= 0;

}privateString qualifiedName(ITestNGMethod method) {

StringBuilder addon= newStringBuilder();

String[] groups=method.getGroups();int length =groups.length;if (length > 0 && !"basic".equalsIgnoreCase(groups[0])) {

addon.append("(");for (int i = 0; i < length; i++) {if (i > 0) {

addon.append(", ");

}

addon.append(groups[i]);

}

addon.append(")");

}return "" + method.getMethodName() + " " +addon;

}private voidresultDetail(IResultMap tests) {for(ITestResult result : tests.getAllResults()) {

ITestNGMethod method=result.getMethod();int methodId =getId(result);

String cname=method.getTestClass().getName();

m_out.println("

" + cname + ":" + method.getMethodName() + "

");

Set resultSet =tests.getResults(method);

generateForResult(result, method, resultSet.size());

m_out.println("

back to summary

");

}

}private void generateForResult(ITestResult ans, ITestNGMethod method, intresultSetSize) {

Object[] parameters=ans.getParameters();boolean hasParameters = parameters != null && parameters.length > 0;if(hasParameters) {

tableStart("result", null);

m_out.print("

");for (int x = 1; x <= parameters.length; x++) {

m_out.print("

Parameter #" + x + "");

}

m_out.println("

");

m_out.print("

");for(Object p : parameters) {

m_out.println("

" + Utils.escapeHtml(p.toString()) + "");

}

m_out.println("

");

}

List msgs =Reporter.getOutput(ans);boolean hasReporterOutput = msgs.size() > 0;

Throwable exception=ans.getThrowable();boolean hasThrowable = exception != null;if (hasReporterOutput ||hasThrowable) {if(hasParameters) {

m_out.print("

1) {

m_out.print(" colspan=\"" + parameters.length + "\"");

}

m_out.println(">");

}else{

m_out.println("

");

}if(hasReporterOutput) {if(hasThrowable) {

m_out.println("

Test Messages

");

}for(String line : msgs) {

m_out.println(line+ "
");

}

}if(hasThrowable) {boolean wantsMinimalOutput = ans.getStatus() ==ITestResult.SUCCESS;if(hasReporterOutput) {

m_out.println("

" + (wantsMinimalOutput ? "Expected Exception" : "Failure") + "

");

}

generateExceptionReport(exception, method);

}if(hasParameters) {

m_out.println("

");

}else{

m_out.println("

");
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值