生成的报告最后的样子是:

 

里面加了截图, 将每一步骤的截图,(所以方法里多加了一个截屏方法)。 加入到报告中,这样更清晰明了。

首先 pom文件中需引用:
<!-- 报告输出 -->
<dependency>
<groupId>com.relevantcodes</groupId>
<artifactId>extentreports</artifactId>
<version>2.41.1</version>
</dependency>



public  void runtest() throws InterruptedException {

//生成报告
ExtentReports extent=new ExtentReports("./demo.html", NetworkMode.OFFLINE);
ExtentTest test = extent.startTest("智联卓聘测试报告,(*^__^*) ");
try {
driver=new ChromeDriver();
String strUrl="http://c.highpin.cn/";
Thread.sleep(2000);
driver.get(strUrl);
driver.manage().window().maximize();
Thread.sleep(2000);
driver.findElement(By.name("Logon_UserEmail")).clear();
driver.findElement(By.name("Logon_UserEmail")).sendKeys("testzp@qq.com");
test.log(LogStatus.PASS,"输入用户名"+"截图 -- " + test.addScreenCapture(snapshot((TakesScreenshot)driver,"input_username.png")));
driver.findElement(By.name("Logon_Password")).clear();
driver.findElement(By.name("Logon_Password")).sendKeys("wxl1234567");
test.log(LogStatus.PASS,"输入密码"+"截图 -- " + test.addScreenCapture(snapshot((TakesScreenshot)driver,"input_password.png")));
driver.findElement(By.id("Logon_PostCode")).clear();
driver.findElement(By.id("Logon_PostCode")).sendKeys("1234");
driver.findElement(By.cssSelector(".CLoginBtn")).click();
test.log(LogStatus.PASS,"点击登录按钮"+"截图 -- " + test.addScreenCapture(snapshot((TakesScreenshot)driver,"login_click.png")));
Thread.sleep(5000);
String imgPath=snapshot((TakesScreenshot)driver,"LoginPass.png");
test.log(LogStatus.PASS,"登录成功"+"截图 -- " + test.addScreenCapture(imgPath)); //成功之后打印报告
}catch (java.lang.Exception e)
{
e.printStackTrace();
// 记录错误信息
String imgPath=snapshot((TakesScreenshot)driver,"LoginFail.png");
test.log(LogStatus.FAIL, "截图 -- " + test.addScreenCapture(imgPath));
test.log(com.relevantcodes.extentreports.LogStatus.INFO, "截图 -- " + test.addScreenCapture(imgPath));
}
extent.endTest(test);
extent.flush();
extent.close();
}

//selenium 截屏方法
public String snapshot(TakesScreenshot drivername, String filename)
{
// this method will take screen shot ,require two parameters ,one is driver name, another is file name
String Screenshot=null;
File scrFile=drivername.getScreenshotAs(OutputType.FILE);
try{
Screenshot=filename;
FileUtils.copyFile(scrFile,new File(filename));
}catch(IOException e){
// TODO Auto-generated catch block
System.out.println("Can't save screenshot");
e.printStackTrace();
}
finally
{
return Screenshot;
}
}
另一个截屏方法,写的比较好,我没有用这个方法,这个方法传的是三个参数 哈哈

/**
* @param driver -- 浏览器对象
* @param screenShotName -- 截图的文件名
* @return destImagePath -- 截图的存放路径
* @Description: 屏幕截图方法(动态注入到测试类的方法)
*/
public static String captureScreenShot(WebDriver driver, String reportDir, String screenShotName) {
TakesScreenshot ts = (TakesScreenshot) driver;
File sourceImage = ts.getScreenshotAs(OutputType.FILE);
String destImagePath = reportDir + "/" + screenShotName + ".png";
File destImage = new File(destImagePath);
try {
FileUtils.copyFile(sourceImage, destImage);
} catch (IOException e) {
e.printStackTrace();
}
destImagePath = screenShotName + ".png";
return destImagePath;
}