selenium - 配置 ExtentReports 以提供准确的测试状态和失败截图

标签 selenium selenium-webdriver testng extentreports extent

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

我有一个带有 TestNG 的简单测试框架,使用 TestBase 类来完成繁重的工作以保持测试简单。我希望以一种简单的方式实现 ExtentReports,使用 TestNG ITestResult 接口(interface)来报告 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中的相​​关部分:
公共(public)类 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

问题是:
  • 左侧菜单中未记录失败测试的名称
  • 尽管正确拍摄,但未显示屏幕截图
  • 它报告通过测试的通过和意外
  • 最佳答案

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

    关于selenium - 配置 ExtentReports 以提供准确的测试状态和失败截图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40329017/

    相关文章:

    python-3.x - 为什么使用selenium的 headless 浏览器无法获取页面源代码?

    python - 如何在 Selenium 中保存浏览器 session ?

    python - 在测试用例中运行所有 selenium 测试

    maven - 使用 testng 提供程序在surefire中禁用junit执行

    javascript - Selenium 无法通过 XPath 识别元素

    java - 如何从知道其值的单元格地址中捕获@id - Webdriver java

    python-3.x - 如何在 Selenium + Python 中的新选项卡上打开网址列表?

    python - 从位于网站的图表中解析表格项时遇到问题

    java - 如何自定义 Allure 报告以向 Allure 状态报告添加新状态?

    java - 使用 ThreadCount TestNG 限制并行测试的数量