java+Appium错误截屏

该博客介绍了如何在TestNG测试框架中创建一个监听器类`ScreenshotListener`,该类实现了`IHookable`接口,当测试失败时自动截取页面快照并保存。通过在`testng.xml`配置文件中添加监听器的类路径,可以确保在执行测试用例时捕获并保存失败的截图,方便后期问题排查。

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

1、新建ScreenshotListener类,并实现IHookable接口

package com.aosschool.wechatmini.listener;

import cn.hutool.core.io.FileUtil;
import com.aosschool.wechatmini.common.BasePage;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.testng.IHookCallBack;
import org.testng.IHookable;
import org.testng.ITestResult;

import java.io.File;

/**
 * @author: wuqi
 * @date: Created in 2021/6/8
 * @description:
 * @version:
 */
public class ScreenshotListener implements IHookable { //IHookable是一个接口类,所以要重写run方法
    @Override
    public void run(IHookCallBack iHookCallBack, ITestResult iTestResult) { //IHookCallBack iHookCallBack, ITestResult iTestResult看成是new对象

        iHookCallBack.runTestMethod(iTestResult);

        if (iTestResult.getThrowable()!=null){
            Object object = iTestResult.getInstance();
            
            BasePage basePage = (BasePage) object;//case对象强转成父类BaseCase,向上转型
            TakesScreenshot Screenshot = basePage.androidDriver;
            File screenshotAs = Screenshot.getScreenshotAs(OutputType.FILE);

            File file = new File("src\\main\\resources"+System.currentTimeMillis()+".jpg");//图片保存位置

            FileUtil.copyFile(screenshotAs,file);

        }

    }
}

2、testng.xml添加listener地址

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="xxx小程序">
    <listeners>
        <listener class-name="com.aosschool.wechatmini.listener.ScreenshotListener"></listener>
    </listeners>

    <test name="精选题500">
        <classes>
            <class name="com.aosschool.wechatmini.cases.xueche.ShunXuLianxiCase">
            </class>
        </classes>
    </test>

</suite>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值