ReportNG 是一个配合TestNG运行case后自动帮你在test-output文件内生成一个相对较为美观的测试报告!
ReportNG 里面Log 是不支持中文的,我改过ReportNG.jar源码,具体方法看最下面,也可以找我直接要jar!
话不多说直接上
环境准备:
1,你需要这些架包
解释:有人会问现在ReportNG版本不是1.1.4吗?为什么我这里是1.1.5呢,这里是因为我改过这里面的源码,(为什么要改源码?后面告诉你)
2,先写一个简单的case,比如打开百度,下面这个代码看上去不难吧!这是第二步前提是你能运行它
package Test;
import org.junit.After;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.testng.annotations.Test;
public class case1 {
WebDriver driver;
@Test
public void Open() throws InterruptedException
{
System.setProperty("webdriver.friefox.bin","C:\\Program Files\\Mozilla Firefox\\friefox.exe");
FirefoxProfile fp = new FirefoxProfile();
WebDriver driver = new FirefoxDriver(fp);
driver.get("http://www.baidu.com");
driver.findElement(By.id("kw")).sendKeys("testerhome");
}
@Test
public void Open() throws InterruptedException
{
System.setProperty("webdriver.friefox.bin","C:\\Program Files\\Mozilla Firefox\\friefox.exe");
FirefoxProfile fp = new FirefoxProfile();
WebDriver driver = new FirefoxDriver(fp);
driver.get("http://www.baidu.com");
driver.findElement(By.id("kw")).sendKeys("testerhome");
Reporter.log("测试1通过");
}
@Test
public void Open1() throws InterruptedException
{
System.setProperty("webdriver.friefox.bin","C:\\Program Files\\Mozilla Firefox\\friefox.exe");
FirefoxProfile fp = new FirefoxProfile();
WebDriver driver = new FirefoxDriver(fp);
driver.get("http://www.baidu.com");
driver.findElement(By.id("kw")).sendKeys("testerhome");
Reporter.log("测试2通过");
}
@Test
public void Open2() throws InterruptedException
{
System.setProperty("webdriver.friefox.bin","C:\\Program Files\\Mozilla Firefox\\friefox.exe");
FirefoxProfile fp = new FirefoxProfile();
WebDriver driver = new FirefoxDriver(fp);
driver.get("http://www.baidu.com");
driver.findElement(By.id("k1w")).sendKeys("testerhome");
Reporter.log("测试3通过");
}
@After
public void quit() throws InterruptedException
{
driver.quit();
}
}
3,配置testNG.xml,这个文件是testNG的配置文件,
<?xml version="1.0" encoding="UTF-8"?>
<suite name="test" parallel="true">
<test name="test" preserver-order="true">
<classes>
//你也可以多个
<class name="包名.case名字"/>
<class name="包名.case名字"/>
<class name="包名.case名字"/>
</classes>
<listeners>
//这是你需要加的东西
<listener class-name="org.uncommons.reportng.HTMLReporter" />
<listener class-name="org.uncommons.reportng.JUnitXMLReporter" />
</listeners>
</test> <!-- Test -->
</suite> <!-- Suite -->
4,然后运行testNG.XML ,再看test-output文件夹 里面的 html文件下面的index.html
报错信息:
你自己System.out的东西都可以写到这里:
![](http://testerhome.com/photo/2015/5b91b80856210d0e0cb1a28a28b15c48.jpg)
![](http://testerhome.com/photo/2015/271a3ad7908664eee806e8120eb19136.jpg)
如果你的报告是乱码,那么你不要急,方法在下面:
在使用ReportNG替换TestNG自带报告时如果报告中含中文,则会乱码,很是不爽,所以把ReportNG的源码下载下来调试。
原来以为是velocity模板的问题,结果对比发现模板没有任何问题,再通过跟踪生成报告过程的代码发现是在将模板文件替换后输出到页面时未转码导致的,修改方法如下:
修改AbstractReporter中的generateFile这个方法中的代码如下:
现在是这样的:
protected void generateFile(File file,
String templateName,
VelocityContext context) throws Exception
{
Writer writer = new BufferedWriter(new FileWriter(file));
try
{
Velocity.mergeTemplate(classpathPrefix + templateName,
ENCODING,
context,
writer);
writer.flush();
}
finally
{
writer.close();
}
}
修改成下面这样,然后编译好新的jar文件
protected void generateFile(File file,
String templateName,
VelocityContext context) throws Exception
{
//Writer writer = new BufferedWriter(new FileWriter(file));
//encoding to utf-8
OutputStream out=new FileOutputStream(file);
Writer writer = new BufferedWriter(new OutputStreamWriter(out,"utf-8"));
try
{
Velocity.mergeTemplate(classpathPrefix + templateName,ENCODING,context,writer);
writer.flush();
}
finally
{
writer.close();
}
}
这样生成的报告就不会乱码了。