TestNG失败用例自动截图

本文介绍了一种在TestNG框架中实现自动化截图的方法。当测试用例失败时,该方法会自动生成截图并保存到指定目录下,方便后续的问题定位。具体步骤包括:1. 创建一个用于截图的工具类ScreenShotOnFailure;2. 编写监听类TestngRetryListener并在测试脚本中启用它。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参考:https://blog.youkuaiyun.com/wangxin1982314/article/details/50247245

1. 首先写一个截屏方法 

 1 public class ScreenShotOnFailure {
 2     public static final String SCREEN_SHOT_PATH = "test-output/screen-shot";
 3     public static String SCREEN_SHOT_NAME = null;
 4     
 5     public static void takeScreenShot() throws IOException {
 6         File screenshotDir = new File(SCREEN_SHOT_PATH);
 7         if(!screenshotDir.exists()) {
 8             screenshotDir.mkdirs();
 9         }
10         
11         SimpleDateFormat smf = new SimpleDateFormat("yyyyMMddHHmmss");        
12         SCREEN_SHOT_NAME = String.valueOf(smf.format(new Date())) + ".jpg";
13         FileUtils.copyFile(((TakesScreenshot)browser.getWebDriver()).getScreenshotAs(OutputType.FILE),
14                 new File(SCREEN_SHOT_PATH + "/"+ SCREEN_SHOT_NAME));    
15     }
16 }

 

2. 新建一个监听类,重写onTestFailure方法  

 1 import org.testng.ITestContext;
 2 import org.testng.ITestListener;
 3 import org.testng.ITestResult;
 4 import static com.crewbudget.bean.ScreenShotOnFailure.SCREEN_SHOT_NAME;
 5 import static com.crewbudget.bean.ScreenShotOnFailure.SCREEN_SHOT_PATH;
 6 
 7 public class TestngRetryListener implements ITestListener{
 8     public void onTestFailure(ITestResult result) {
 9         try {
10             ScreenShotOnFailure.takeScreenShot();
11             System.out.println(result.getMethod().getMethodName()+"failed, the screenshot saved in "
12                     + SCREEN_SHOT_PATH +" screenshot name: "
13                     + SCREEN_SHOT_NAME);
14         } catch (Exception e) {
15             e.printStackTrace();
16         }
17     }
18 
19     @Override
20     public void onTestStart(ITestResult result) {
21         // TODO Auto-generated method stub
22         
23     }
24 
25     @Override
26     public void onTestSuccess(ITestResult result) {
27         // TODO Auto-generated method stub
28         
29     }
30 
31     @Override
32     public void onTestSkipped(ITestResult result) {
33         // TODO Auto-generated method stub
34         
35     }
36 
37     @Override
38     public void onTestFailedButWithinSuccessPercentage(ITestResult result) {
39         // TODO Auto-generated method stub
40         
41     }
42 
43     @Override
44     public void onStart(ITestContext context) {
45         // TODO Auto-generated method stub
46         
47     }
48 
49     @Override
50     public void onFinish(ITestContext context) {
51         // TODO Auto-generated method stub
52         
53     }
54 }

3. 在测试脚本中添加监听

  

 

 

 

转载于:https://www.cnblogs.com/cheese320/p/8892649.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值