如何在testNG报告中包含故障屏幕截图 [英] How can I include a failure screenshot to the testNG report

查看:740
本文介绍了如何在testNG报告中包含故障屏幕截图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我以这种方式截取我的测试失败的屏幕截图:

currently I am taking screenshots of my test failures this way:

@AfterMethod(alwaysRun=true)
public void catchExceptions(ITestResult result){
    Calendar calendar = Calendar.getInstance();
    SimpleDateFormat formater = new SimpleDateFormat("dd_MM_yyyy_hh_mm_ss");
    String methodName = result.getName();
    if(!result.isSuccess()){
        File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        try {
            FileUtils.copyFile(scrFile, new File((String) PathConverter.convert("failure_screenshots/"+methodName+"_"+formater.format(calendar.getTime())+".png")));
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }
}

我可以加入自己的截图进入TestNG报告链接或图片?如果是的话怎么样?

Can I include my own screenshots into the TestNG report link or pic? If yes how?

我在网上找到的只是FEST框架。但由于我已经截取屏幕截图,我不想使用另一个框架。

All I found on that online is the FEST framework. But since I am already taking the screenshots I dont want to use another framework.

推荐答案

是的,你可以包含你的链接在testng报告中截图。

Yes, you can include the link to your screenshot in testng report.

您需要调用 org.testng.Reporter.log 方法将超链接写入 testng 报告通过使用@注释您的测试类或所有测试类的父级监听器({yourListener.class})或将监听器添加到 testng.xml

You need to call org.testng.Reporter.log method to write the hyperlink to the testng report either by annotating your test class or parent of all testclasses with @Listeners({yourListener.class}) or by adding the listener to your testng.xml.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="default">
  <listeners>
    <listener class-name="ScreenshotListener" />
  </listeners>
  <test name="Test">
    <packages>
      <package name="someTests.*"/>
    </packages>
  </test>
</suite>

您需要先创建一个Listener类并将其添加到testng。您可以从testng.org获取详细信息。搜索听众。

You need to first create a Listener class and add it to testng. You can get details for that from testng.org. Search for listener.

创建该类后,您应该在其中编写一个覆盖 ontestfailure 方法的方法。只要testng识别出故障,就会执行此方法中的代码。

Once you create that class, you should write a method in it which overrides the ontestfailure method. The code inside this method will be executed whenever testng identifies a failure.

您可以调用屏幕截图抓取方法并使用 Reporter.log 将超链接放到该屏幕截图中。然后,您可以在失败的测试用例详细信息下找到此链接。

You can call your screenshot grabbing method and use Reporter.log to put the hyperlink to that screenshot. Then you can find this link under the failed testcases details.

import java.io.*;
import java.util.*;
import java.text.*;
import org.apache.commons.io.FileUtils;

import org.openqa.selenium.*;

import org.testng.*;

public class ScreenshotListener extends TestListenerAdapter {
    @Override
    public void onTestFailure(ITestResult result) {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat formater = new SimpleDateFormat("dd_MM_yyyy_hh_mm_ss");
        String methodName = result.getName();
        if(!result.isSuccess()){
            File scrFile = ((TakesScreenshot)SomeStaticWebDriver.driver).getScreenshotAs(OutputType.FILE);
            try {
                String reportDirectory = new File(System.getProperty("user.dir")).getAbsolutePath() + "/target/surefire-reports";
                File destFile = new File((String) reportDirectory+"/failure_screenshots/"+methodName+"_"+formater.format(calendar.getTime())+".png");
                FileUtils.copyFile(scrFile, destFile);
                Reporter.log("<a href='"+ destFile.getAbsolutePath() + "'> <img src='"+ destFile.getAbsolutePath() + "' height='100' width='100'/> </a>");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

这篇关于如何在testNG报告中包含故障屏幕截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆