本篇介绍监听器ReportNg:该监听器的作用是,替换testng自带的报告,生成更清楚直观的测试报告。
先看下testng自带报告:
在项目\test-output中index.html样式
在项目\test-output\suite名称\test名称.html样式
现在使用reportng替换testng自带报告:
1.需要的jar包如下,导入到build path中:
下载地址:
2.xml文件配置,添加监听器
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<!-- 该xml实现使用ReportNG替换TestNG自带报告 -->
<suite name="suite1" >
<test name="test" preserver-order="true">
<classes >
<class name="com.test.testng" >
<methods>
<include name="Login" />
</methods>
</class>
</classes>
<!-- 主要是需要添加以下两个监听器,listener类在reportng-1.1.5.jar中 -->
<listeners>
<listener class-name="org.uncommons.reportng.HTMLReporter" />
<listener class-name="org.uncommons.reportng.JUnitXMLReporter" />
</listeners>
</test>
</suite>
3.其他脚本文件无需做任何改变,举个简单的登录的例子
public class testng {
WebDriver driver;
@BeforeTest
public void beforeTest() {
driver = new FirefoxDriver();
driver.get("http://156.57.66.45:7711");
driver.manage().window().maximize();// 窗口最大化
}
@Test(priority=1)
public void login() throws InterruptedException {
driver.findElement(By.id("username")).sendKeys("999111");// 输入登录用户名
driver.findElement(By.id("password")).sendKeys("111111");// 输入登录密码
Thread.sleep(2000);
driver.findElement(By.id("loginButton")).click(); // 点击登录
Reporter.log("登录成功");
}
@AfterTest
public void afterTest() throws InterruptedException {
Thread.sleep(2000);
driver.findElement(By.className("icon-logout")).click();
}
}
Reporter.log("登录成功");
}
@AfterTest
public void afterTest() throws InterruptedException {
Thread.sleep(2000);
driver.findElement(By.className("icon-logout")).click();
}
}
4.运行testng.xml文件,在项目的test-output/html文件夹下,打开index.html,(这里一定注意是在html文件夹下,而不是test-output下,因为此时自带的testng报告还是会生成,要不然找错地方会误以为使用的监听器无效)
有两个界面,一个overview,可查看总体运行结果,点击相应链接可以查看到详细的信息
一个log output,只要在代码中使用到Reporter.log(""),都会将打印信息写到这里。