-2
@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();

 }

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

    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;
}
4

1 回答 1

1

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

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

parentTest = report.createTest("Test Name");
于 2018-04-19T10:58:27.440 回答