配置ExtentReports以提供准确的测试状态和失败时的屏幕截图

时间:2016-10-30 12:23:39

标签: selenium selenium-webdriver testng extentreports extent

我在调整ExtentReports以提供所需的输出时遇到一些困难。

我有一个简单的TestNG测试框架,使用TestBase类来完成繁重工作以保持测试简单。我希望以简单的方式实现ExtentReports,使用TestNG ITestResult接口报告Pass,Fail和Unknown。

以下是示例测试,1次传递和1次故意失败:

public class BBCTest extends TestBase{

@Test
public void bbcHomepagePass() throws MalformedURLException {

    assertThat(driver.getTitle(), (equalTo("BBC - Home")));
}

@Test
public void bbcHomePageFail() throws MalformedURLException {

    assertThat(driver.getTitle(), (equalTo("BBC - Fail")));
}

以下是TestBase中的相​​关部分:     公共类TestBase实现Config {

protected WebDriver driver = null;
private Logger APPLICATION_LOGS = LoggerFactory.getLogger(getClass());
private static ExtentReports extent;
private static ExtentTest test;
private static ITestContext context;
private static String webSessionId;

@BeforeSuite
@Parameters({"env", "browser"})
public void beforeSuite(String env, String browser) {
    String f = System.getProperty("user.dir") + "\\test-output\\FabrixExtentReport.html";
    ExtentHtmlReporter h = new ExtentHtmlReporter(f);
    extent = new ExtentReports();
    extent.attachReporter(h);
    extent.setSystemInfo("browser: ", browser);
    extent.setSystemInfo("env: ", env);
}

@BeforeClass
@Parameters({"env", "browser", "login", "mode"})
public void initialiseTests(String env, String browser, String login, String mode) throws MalformedURLException {
    EnvironmentConfiguration.populate(env);
    WebDriverConfigBean webDriverConfig = aWebDriverConfig()
            .withBrowser(browser)
            .withDeploymentEnvironment(env)
            .withSeleniumMode(mode);

    driver = WebDriverManager.openBrowser(webDriverConfig, getClass());
    String baseURL = EnvironmentConfiguration.getBaseURL();
    String loginURL = EnvironmentConfiguration.getLoginURL();
    APPLICATION_LOGS.debug("Will use baseURL " + baseURL);

    switch (login) {
        case "true":
            visit(baseURL + loginURL);
            break;
        default:
            visit(baseURL);
            break;
    }
    driver.manage().deleteAllCookies();
}

@BeforeMethod
public final void beforeTests(Method method) throws InterruptedException {
    test = extent.createTest(method.getName());
    try {
        waitForPageToLoad();
        webSessionId = getWebSessionId();
    } catch (NullPointerException e) {
        APPLICATION_LOGS.error("could not get SessionID");
    }
}


@AfterMethod
public void runAfterTest(ITestResult result) throws IOException {
    switch (result.getStatus()) {
        case ITestResult.FAILURE:
            test.fail(result.getThrowable());
            test.fail("Screenshot below: " + test.addScreenCaptureFromPath(takeScreenShot(result.getMethod().getMethodName())));
            test.fail("WebSessionId: " + webSessionId);
            break;
        case ITestResult.SKIP:
            test.skip(result.getThrowable());
            break;
        case ITestResult.SUCCESS:
            test.pass("Passed");
            break;
        default:
            break;
    }
}

private String takeScreenShot(String methodName) {
    String path = System.getProperty("user.dir") + "\\test-output\\" + methodName + ".jpg";
    try {
        File screenshotFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(screenshotFile, new File(path));
    } catch (Exception e) {
        APPLICATION_LOGS.error("Could not write screenshot" + e);
    }
    return path;
}

@AfterClass
public void tearDown() {
    driver.quit();
}

@AfterSuite()
public void afterSuite() {
    extent.flush();
}

以下是报告:

enter image description here

问题是:

  1. 左侧菜单
  2. 未记录失败测试的名称
  3. 尽管正确拍摄,但仍未显示屏幕截图
  4. 报告通过测试的Pass和Unexpected

2 个答案:

答案 0 :(得分:1)

您需要进行多项更改。实例化ExtentReport&在除BeforeClass之外的任何配置方法中添加配置信息。

@BeforeTest
@Parameters({"env", "browser"})
public void initialiseTests(String env, String browser, String emulatorMode, String mode) {
    EnvironmentConfiguration.populate(env);
    WebDriverConfigBean webDriverConfig = aWebDriverConfig()
            .withBrowser(browser)
            .withDeploymentEnvironment(env)
            .withSeleniumMode(mode);

    driver = WebDriverManager.openBrowser(webDriverConfig, getClass());
    APPLICATION_LOGS.debug("Will use baseURL " + EnvironmentConfiguration.getBaseURL());
    try {
        visit(EnvironmentConfiguration.getBaseURL());
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
    driver.manage().deleteAllCookies();
}

在@BeforeMethod部分中初始化test = extent.startTest(testName);

@BeforeMethod
public void beforeM(Method m) {
        test = extent.startTest(m.getName());
}

并且,您可以在@AfterTest方法中调用extent.endTest(test);

    @AfterTest
    public void afterTests() {
        extent.endTest(test);

        extent.close();
    }

}

并且,以适当的状态记录您的步骤信息调用test.log(LogStatus, "your info to show");

答案 1 :(得分:1)

3.0版

大多数代码都是由创建此库的人提供的,我只是根据您的需要进行了修改。

public class TestBase {

    private static ExtentReports extent;
    private static ExtentTest test;

    @BeforeSuite
    public void runBeforeEverything() {
        String f = System.getProperty("user.dir")+ "/test-output/MyExtentReport.html";
        ExtentHtmlReporter h = new ExtentHtmlReporter(f);
        extent = new ExtentReports();
        extent.attachReporter(h);
    }

    @BeforeMethod
    public void runBeforeTest(Method method) {
        test = extent.createTest(method.getName());
    }

    @AfterMethod
    public void runAfterTest(ITestResult result) {
        switch (result.getStatus()) {
        case ITestResult.FAILURE:
            test.fail(result.getThrowable());
            test.fail("Screenshot below: " + test.addScreenCaptureFromPath(takeScreenShot(result.getMethod().getMethodName())));
            break;
        case ITestResult.SKIP:
            test.skip(result.getThrowable());
            break;
        case ITestResult.SUCCESS:
            test.pass("Passed");
            break;
        default:
            break;
        }
        extent.flush();
    }

    protected String takeScreenShot(String methodName) {
        String path = "./screenshots/" + methodName + ".png";
        try {
            File screenshotFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
            FileUtils.copyFile(screenshotFile, new File(path));
        } catch (Exception e) {
            APPLICATION_LOGS.error("Could not write screenshot" + e);
        }
        return path;
    }
}