使用带有selenium java的Extent Reports 3.0版本,为什么所有测试步骤都记录在我的第一个测试方法中?

时间:2018-04-16 15:50:41

标签: selenium-extent-report

@BeforeSuite
public void setUpConfig()
{
    report = createInstance("TestSummaryReport.html");

    if(properties.ReadPropertiesFile("Browser").equalsIgnoreCase("Chrome"))
    {
        System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir")+"\\"+properties.ReadPropertiesFile("ChromeDriver"));
        driver = new ChromeDriver();
    }else if(properties.ReadPropertiesFile("Browser").equalsIgnoreCase("Firefox"))
    {
        System.setProperty("webdriver.gecko.driver", System.getProperty("user.dir")+"\\"+properties.ReadPropertiesFile("FirefoxDriver"));
        driver = new FirefoxDriver();
    }else if(properties.ReadPropertiesFile("Browser").equalsIgnoreCase("IE"))
    {
        System.setProperty("webdriver.ie.driver", System.getProperty("user.dir")+"\\"+properties.ReadPropertiesFile("InternetExplorerDriver"));

        InternetExplorerOptions options = new InternetExplorerOptions();
        options.setCapability(InternetExplorerDriver.IE_ENSURE_CLEAN_SESSION, true);
        options.setCapability(InternetExplorerDriver.ELEMENT_SCROLL_BEHAVIOR, 1);
        options.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
        options.setCapability(CapabilityType.SUPPORTS_JAVASCRIPT, true);
        options.setCapability(CapabilityType.SUPPORTS_FINDING_BY_CSS, true);
        options.setCapability(InternetExplorerDriver.REQUIRE_WINDOW_FOCUS, true);

        driver = new InternetExplorerDriver(options);
    }

    driver.manage().window().maximize();
    driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
    driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

    report.setSystemInfo("MachineName", properties.ReadPropertiesFile("MachineName"));
    report.setSystemInfo("IP", properties.ReadPropertiesFile("IP"));
    report.setSystemInfo("OS", properties.ReadPropertiesFile("OS"));
    report.setSystemInfo("Database", properties.ReadPropertiesFile("Database"));
    report.setSystemInfo("ApplicationServer", properties.ReadPropertiesFile("ApplicationServer"));
    report.setSystemInfo("BI Version", properties.ReadPropertiesFile("BIVersion"));
    report.setSystemInfo("QA Build No.", properties.ReadPropertiesFile("QABuild"));
    report.setSystemInfo("Authentication Mode", properties.ReadPropertiesFile("Mode"));
    report.setSystemInfo("Browser", properties.ReadPropertiesFile("Browser"));
}

@BeforeClass
public void beforeClass()
{
    parentTest = report.createTest(getClass().getSimpleName());
}

@BeforeMethod
public void startTestCaseExecution(Method method)
{
    logger = parentTest.createNode(method.getName());
    helper = new Helper(driver, logger);
}

@AfterMethod
public void EndTestCaseExecution(ITestResult result) throws IOException
{   
    if (result.getStatus() == ITestResult.FAILURE)
    {
        TakesScreenshot screen = (TakesScreenshot) driver;
        File src = screen.getScreenshotAs(OutputType.FILE);
        String screenshotPath = System.getProperty("user.dir")+"\\results\\screenshots\\failed\\"+helper.getCurrentDateTime()+"_"+result.getMethod().getMethodName()+".png";
        File target = new File(screenshotPath);
        FileUtils.copyFile(src, target);

        logger.fail(result.getThrowable());
        logger.log(Status.FAIL, "Screenshot : ", MediaEntityBuilder.createScreenCaptureFromPath(screenshotPath).build());
        logger.log(Status.FAIL, MarkupHelper.createLabel(" Test case - "+result.getName()+" FAILED", ExtentColor.RED));
    }
    else if (result.getStatus() == ITestResult.SKIP)
    {
        logger.skip(result.getThrowable());
        logger.log(Status.SKIP, MarkupHelper.createLabel(" Test Case - "+result.getName()+" SKIPPED", ExtentColor.ORANGE));
    }
    else
    {
        logger.log(Status.PASS, MarkupHelper.createLabel(" Test Case - "+result.getName()+" PASSED SUCCESSFULLY", ExtentColor.GREEN));
    }

}

@AfterSuite
public void afterExecution()
 {
     report.flush();

 }

//这是我在我的TesTNg Annotations中使用的ExtentReportsManager类     protected static ExtentReports createInstance(String fileName)     {         htmlReporter = new ExtentHtmlReporter(System.getProperty(" user.dir")+" \ results \" + getCurrentDateTime()+" _" + fileName);         。htmlReporter.config()setTestViewChartLocation(ChartLocation.TOP);         。htmlReporter.config()setChartVisibilityOnOpen(真);         。htmlReporter.config()setTheme(Theme.STANDARD);         。htmlReporter.config()setDocumentTitle(文件名);         htmlReporter.config()setEncoding(" UTF-8&#34)。         htmlReporter.config()setReportName(文件名)。         htmlReporter.setAppendExisting(真);

    report = new ExtentReports();
    report.attachReporter(htmlReporter);

    return report;
}

private static String getCurrentDateTime()
{
    Date currentDate = new Date();
    DateFormat formatter = new SimpleDateFormat("E dd MMM yyyy HH:mm:ss");
    String formattedDate = formatter.format(currentDate);
    String modifiedDate = formattedDate.replace(" ", "_").replace(":", "_");
    return modifiedDate;
}

1 个答案:

答案 0 :(得分:1)

因为您已经创建了单个测试类,所以默认情况下它将采用父类下的所有步骤,直到您没有定义另一个测试类。

创建另一个测试类以保持测试方法之间的分离:

parentTest = report.createTest("Test Name");