Selenium生成Report的利器 - ExtentReports

通常来说如果需要看Selenium的运行报告无非以下几种:

1.Eclipse+Junit 这种最简单但是局限在于只能在Eclipse里看,这个就是用最原生的Junit单元测试框架

2.Junit+Ant 用Ant来驱动Selenium执行,会生成一个xml,通过xsl样式表来形成一个html的报告,报告比较全面,但是样式和内容的自定义能力比较差

3.Testng的报告,也是Html的格式

4.自定义的report,基本上用assert或者log来结合做一个report,直接写html标签来实现,这样的格式自定义比较强,但是需要编码功底,至于样式就看个人能力了

这里我推荐一个jar包 Extentreport  基于这个jar包来生成的html不仅美观而且可读取强,如果不满意里面的排列和内容还可以自定义,是个生成html report的利器

首先是下jar包,网上搜一下就有了extentreports-java-2.40.0.jar  这里需要下连java源文件也带上的 方便我们自定义report

然后新建一个class文件,这里我直接贴一个例子上来


import com.relevantcodes.extentreports.DisplayOrder;
import com.relevantcodes.extentreports.ExtentReports;
import com.relevantcodes.extentreports.ExtentTest;
import com.relevantcodes.extentreports.LogStatus;
import com.relevantcodes.extentreports.NetworkMode;

public class ExtenseReport {

    static ExtentReports extent;
    static String reportLocation = "report/ExtentReport.html";
    static String imageLocation = "../img/Feedback.png";

    
    private ExtentReports createReport() {
        return extent;
    }
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        extent = new ExtentReports(reportLocation, NetworkMode.OFFLINE);

        String img = test.addScreenCapture(imageLocation);
        test.log(LogStatus.INFO, "Image", "Image example: " + img);
        
        extent.endTest(test);
        
        ExtentTest test2 = extent.startTest("My First Test", "Sample description");
        String aimg = test2.addScreenCapture(imageLocation);
        System.out.println(img);
        test2.log(LogStatus.INFO, "Image", "Image example: " + aimg);

        extent.endTest(test2);
        
        extent.flush();
    }

}
 

上面贴的是2个测试用例,用例里包含正确和错误的测试用例以及把图片贴进去的例子,这个是单纯的没有用到测试框架的例子

下面列举一个用到了Testng的例子

public class SingleLogTest extends BaseExample {
    private final String filePath = "file-path.html";
    
    @BeforeClass
    public void beforeClass() {
        extent = new ExtentReports(filePath, true);
        
        extent.startReporter(ReporterType.DB, "extent.db");
        
        extent.addSystemInfo("Host Name", "Anshoo");
    }
    
    @Test
    public void passTest() {
        test = extent.startTest("passTest");
        test.log(LogStatus.PASS, "Pass");
        
        Assert.assertEquals(test.getRunStatus(), LogStatus.PASS);
    }
    
    @Test
    public void intentionalFailure() {
        test = extent.startTest("intentionalFailure");
        test.log(LogStatus.FAIL, "Fail");
        
        Assert.assertEquals(test.getRunStatus(), LogStatus.PASS);
    }
}public abstract class BaseExample {
    protected ExtentReports extent;
    protected ExtentTest test;

    @AfterMethod
    protected void afterEachTest(ITestResult result) {
        if (!result.isSuccess()) {
            test.log(LogStatus.FAIL, result.getThrowable());
        }
        
        extent.endTest(test);        
        extent.flush();
    }
    
    @AfterSuite
    protected void afterSuite() {
        extent.close();
    }
}

这里说明一下就是因为Testng里面有Assert,而这个Assert的结果是无法直接输送到report里的,所以就要用到@AfterMethod来获取到每次执行的@Test中的结果来人为的把这个结果人为输送到report中去。

贴一下最后生成的结果,感觉还是不错的,之后继续尝试着修改这个jar来符合我们自己想要的样式。

 

posted on 2016-01-06 17:45  GoldenRazor  阅读(1809)  评论(0编辑  收藏  举报

导航