selenium截图模糊_selenium遇到异常自动截图

最近要在框架中添加case失败时,要自动截图,主要又两种方式,思想都是在抛异常的时候,捕获到异常,并作页面截图处理。今天坐下总结。

一、第一种方式,重写onException方法

只针对webdriver的异常截图,该方法由于只针对webdriver抛的异常时才能截图,有一定的限制

a.继承AbstractWebDriverEventListener类,重写onException方法,

public voidonException(java.lang.Throwable throwable,

WebDriver driver){

Throwable cause=throwable.getCause();

/*

String cause = throwable.getClass().getName();

ScreenshotException ec = new ScreenshotException(cause);

*/

System.out.println(throwable.getClass().getName());

System.out.println("onException=" +cause);

Date currentTime= newDate();

SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd-hh-mm-ss");

String dateString=formatter.format(currentTime);

File scrFile=((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);try{

File screenshot= new File("D:/ddd/"

+ dateString + ".png");

FileUtils.copyFile(scrFile,screenshot);

}catch(IOException e) {

e.printStackTrace();

}

}

b.测试类,要用EventFiringWebDriver ,并注册MyListen

public static voidmain(String args[]) {

Strin

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值