配置ExtentReports以提供准确的测试状态和故障屏幕截图

Steerpike

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

我使用TestNG有一个简单的测试框架,使用TestBase类进行繁重的工作以使测试保持简单。我希望以简单的方式实现ExtentReports,使用TestNG ITestResult接口报告通过,失败和未知。

以下是示例测试,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();
}

这是报告:

在此处输入图片说明

问题是:

  1. 测试失败的名称未记录在左侧菜单中
  2. 尽管已正确拍摄,但屏幕截图仍未显示
  3. 它报告通过测试的通过和意外
卡尔提克

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

ExtentReports-屏幕截图不在报告中-损坏的图像

Android屏幕截图和屏幕截图权限

使用屏幕截图测试UI

如何在 ExtentReports 4.0.9 中增加屏幕截图缩略图的大小

使用rspec,capybara和poltergeist进行的测试将返回空的html和空的屏幕截图

iTunes Connect中缺少屏幕截图状态

如何在ButtonBar中更改标签-附有简单的测试用例和屏幕截图

UIScrollView不能在横向模式下滚动-附带测试代码和屏幕截图

FAB不进行动画处理-附带测试代码和屏幕截图

在使用JAVA和Cucumber进行测试的每个步骤之后,如何捕获屏幕截图?

颤抖的黄金测试捕获空的屏幕截图

仅在测试失败时创建屏幕截图

关闭Rails系统测试中的屏幕截图

无法从休眠状态恢复 - 屏幕出现故障

Ubuntu:屏幕截图和编辑工具

如何拍摄,裁剪和分享屏幕截图?

Selenium Webdriver和PHPUnit的屏幕截图

如何同时保存和复制屏幕截图?

将工具栏添加到Google的导航抽屉示例中-带有测试代码和屏幕截图

如何设置capybara-webkit来测试ajax调用并获取屏幕截图(加载了CSS和javascript的html)

具有通用状态栏的Android屏幕截图

如何检查电子屏幕截图的允许状态(macOS catalina)

在SpecRun / SpecFlow测试执行报告中插入屏幕截图

Facebook的屏幕截图测试使用大于23的api失败

如何在UI测试(Xcode)上关闭自动屏幕截图

设置针对Facebook屏幕截图测试Android的gradle插件的问题

python在tearDown()方法中获取测试失败的屏幕截图

Azure DevOps 测试报告嵌入附件/屏幕截图

如何将故障屏幕截图包含到testNG报告中