selenium截图功能实现
一.背景
下面代码是重写的方法,此类需要继承 estListenerAdapter 监听类,然后重写 onTestFailure() 方法,先获取测试用例的驱动,然后截图,存放于指定路径,之后将截图附在 reportng 的报告中显示。重要代码如下所示:
二.重写方法
@Override
public synchronized void onTestFailure(ITestResult result){
// 获取当前 Object 类型的测试类
Object currentClass = result.getInstance();
// 获取当前测试类驱动
WebDriver currentDriver = ( (SimpleInitiator) (currentClass) ).driver;
// 得到需要日期格式
Date currentDate = new Date();// 新建一个 Date,得到当前日期信息
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd(HH-mm-ss)");// 指定日期格式
String formatDate = simpleDateFormat.format(currentDate);// 样式化当前日期格式
// 截