首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >高级ExtentReports逻辑在Selenium WebDriver中的实现

高级ExtentReports逻辑在Selenium WebDriver中的实现
EN

Stack Overflow用户
提问于 2019-08-09 00:12:40
回答 1查看 537关注 0票数 1

我正在寻找一种方法来实现高级ExtentReports与测试步骤和结果数据以外的基本设置。

我目前有一个工作的ExtentReports报告,并正在寻找一种使用ExtentReports (Logger)类实现和显示测试步骤的方法。我尝试将(Logger.log)语句添加到类中,但无法查看ExtentReports文件中的测试步骤。

代码语言:javascript
复制
public static WebDriver driver;
public static ExtentReports extentReports;
public static ExtentTest extentTest;
public static ExtentHtmlReporter htmlReporter;
public static ExtentTest test;
public static ExtentTest logger;

@BeforeSuite
public void beforeSuite(){
         htmlReporter = new ExtentHtmlReporter(new File(("reports/Test Automation Report.html")));
         htmlReporter.loadXMLConfig(new File("extent-config.xml"));
         extentReports=new ExtentReports();
         extentReports.attachReporter(htmlReporter);
         extentReports.setSystemInfo("Environment", "QA");
         htmlReporter.config().setChartVisibilityOnOpen(true);
         htmlReporter.config().setDocumentTitle("AutomationTesting.in Demo Report");
         htmlReporter.config().setReportName("My Own Report");
         htmlReporter.config().setTestViewChartLocation(ChartLocation.TOP);
         htmlReporter.config().setTheme(Theme.DARK);
    }

    @BeforeMethod
    public void before(Method method) {
        extentTest=extentReports.createTest(method.getName().toString());
        driver = DriverType.chromeDriver();
        driver.manage().timeouts().setScriptTimeout(10,TimeUnit.SECONDS);
        IRISBase base = new IRISBase(driver);
        base.fmaLoad();
    }

    @AfterMethod
    public void getResult(ITestResult result) throws Exception {
        if (driver!=null){
            System.out.println("deleteAllCookies");
            driver.manage().deleteAllCookies();
            }

        if (result.getStatus() == ITestResult.FAILURE)
        {
            System.out.println(">>>Test Failed"); //Which Test Failed and Why (Method Name Output)
            extentTest.log(Status.FAIL,  MarkupHelper.createLabel(result.getName() + " Test case FAILED due to below issues:", ExtentColor.RED));
            extentTest.fail(result.getThrowable());
            System.out.println("Automation Test Run: " + result.getMethod().toString()); //Testing and Results (Relevant)
        }
        else if (result.getStatus() == ITestResult.SUCCESS)
        {
            System.out.println(">>>Test Passed");
            extentTest.log(Status.PASS, MarkupHelper.createLabel(result.getName() + " Test Case PASSED", ExtentColor.GREEN));
            logger.info(MarkupHelper.createLabel(result.getName() + " Test Case PASSED", ExtentColor.GREEN));
            System.out.println("====================================================================================================="); 
            System.out.println("Automation Test Run: " + result.getMethod().toString()); //Testing and Results
            System.out.println("=====================================================================================================");
        }
        else if (result.getStatus() == ITestResult.SKIP)
        {
            System.out.println(">>>Test Skipped");
            extentTest.log(Status.SKIP, MarkupHelper.createLabel(result.getName() + " Test Case SKIPPED", ExtentColor.BLUE));
            System.out.println("Automation Test Run: " + result.getMethod().toString()); //Testing and Results
        }

        if(driver!=null) {
            driver.manage().deleteAllCookies();
        }
    }

    @AfterTest
    public void testComplete() {
        extentReports.flush();
        driver.manage().deleteAllCookies();
        driver.quit();
    }

    private static File fileWithDirectoryAssurance(String directory, String filename) {

        Date date = new Date() ;
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss") ;

        File dir = new File(directory);
        if (!dir.exists()) dir.mkdirs();
        return new File(directory + "/" + filename);
    }
}

期望的ExtentReports文件

实际ExtentReports文件

EN

回答 1

Stack Overflow用户

发布于 2019-08-09 08:43:02

将ExtentTest的对象用于日志记录示例:

注意:更改您的@change方法

代码语言:javascript
复制
    public static ExtentReports extent;
    public static ExtentTest logger;
    @BeforeSuite(alwaysRun = true)
    public void config(){
        extent = new ExtentReports(path, true);
        extent.addSystemInfo("Host Name", System.getProperty("user.name"));
        extent.addSystemInfo("Environment", "API Testing");
        extent.loadConfig(new File(XMLPath));}

   @AfterMethod(alwaysRun = true)
public void getResult(ITestResult result) {
    if (result.getStatus() == ITestResult.SUCCESS) {
        logger.log(LogStatus.PASS, "Test case is passed " + result.getName());

    } else if (result.getStatus() == ITestResult.FAILURE) {
        logger.log(LogStatus.FAIL, "Test case is failed " + result.getName());
        logger.log(LogStatus.FAIL, "Test case is failed " + result.getThrowable());
    } else if (result.getStatus() == ITestResult.SKIP) {
        logger.log(LogStatus.SKIP, "Test case is Skiped " + result.getName());
    }
    extent.endTest(logger);
}

@AfterSuite(alwaysRun = true)
public void endReport() {
    extent.flush();
    // SendEmail.Email(ReportPath);
    extent.close();
    log.setinstanceNull();
}

使用ExtentTest对象记录测试用例中的测试步骤,导入基类并使用ExtentTest的对象,如下面的示例测试用例

代码语言:javascript
复制
logger.log(LogStatus.INFO, message);
logger.log(LogStatus.ERROR, message);

要在开始测试用例之前获得测试用例列表,请使用extent.startTest方法,其中区段是ExtentReports的对象

代码语言:javascript
复制
@Test(groups = { "sanityuserfolw" })
public void testcase() {

    logger = extent.startTest("TestCase Name", "test case summary");



    resp = part_study.sampletest(input);
    logger.log(LogStatus.INFO, "response is "+resp.getStatusCode());
    assertEquals(resp.getStatusCode(), 200, "Error in adding study " + resp.statusLine());

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57422070

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档