WebDriver失败截图可以通过两种方式实现:

1. Use WebdriverEventListener

第一步:创建自己的WebDriverEventListener

创建自己的WebDriverEventListener 重写Onexception 方法, 当webdriver 遇到异常的时候执行截图动作。

import java.io.File;
import java.io.IOException;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date; import org.apache.commons.io.FileUtils;
import org.apache.log4j.LogManager;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.internal.Base64Encoder;
import org.openqa.selenium.remote.ScreenshotException;
//import org.openqa.selenium.support.events.AbstractWebDriverEventListener;
import org.openqa.selenium.support.events.WebDriverEventListener; public class CustomWebDriverEventListener implements WebDriverEventListener { @Override
public void onException(Throwable paramThrowable, WebDriver paramWebDriver) {
// TODO Auto-generated method stub
Throwable cause = paramThrowable.getCause();
if (cause instanceof ScreenshotException) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd-hh-mm-ss");
String dateString = formatter.format(new Date());
File of = new File(dateString + "-exception.png");
FileOutputStream out = null;
try {
out = new FileOutputStream(of);
out.write(new Base64Encoder().decode(((ScreenshotException) cause)
.getBase64EncodedScreenshot()));
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} }
第二步:通过EventFiringWebDriver给webdriver注册自己的listener
WebDriver driver = new RemoteWebDriver(new URL(remoteUrl), capabilities);

WebDriverEventListener eventListener = new CustomWebDriverEventListener();

driver = new EventFiringWebDriver(driver).register(eventListener);

注册完就OK了,driver遇到异常的时候就执行异常监控中的截图操作,当然其他事件中的操作也会执行。

2. Use TestNG/JUnit listener

第一步:创建自己的TestListenerAdapter 或者custom rule

TestNG就是创建TestListenerAdapter,重写Onfailure方法, 如果用的是Junit就创建对应的test rule,此处只举例TestNG,Junit的可参照(http://www.ltesting.net/ceshi/open/kygncsgj/selenium/2012/0824/205449_2.html)

Onfailure方法添加截图功能

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List; import org.apache.log4j.LogManager;
import org.openqa.selenium.WebDriver;
import org.testng.IAnnotationTransformer;
import org.testng.IResultMap;
import org.testng.IRetryAnalyzer;
import org.testng.ITestContext;
import org.testng.ITestNGMethod;
import org.testng.ITestResult;
import org.testng.Reporter;
import org.testng.TestListenerAdapter;
import org.testng.annotations.ITestAnnotation; public class MyListener extends TestListenerAdapter { @Override
public synchronized void onTestFailure(ITestResult result) {
LogManager.getLogger(this.getClass()).info("In failed method");
Object currentClass = result.getInstance();
((TestBase) currentClass).takeScreenShot(true);
LogManager.getLogger(this.getClass()).info("Out failed method");
} }

两者比较:

WebdriverEventListener:

1.  可以截图所有webdriver异常的图片,但是不能截取Assert.fail 失败的图片

2. 如果使用while等待某种条件是的报错也会截图,可能造成截图过多

3. 只能用于Webdriver,如果使用remote driver可能不行

4. 坚挺的事件很多,方便很多操作

测试框架TestNG,Junit

1. 用例失败就截图,和Webdriver异常关联不大

WebDriver - 添加失败截图的更多相关文章

  1. pytest文档47-allure报告添加用例失败截图

    前言 使用 selenium 做 web 自动化的时候,很多小伙伴希望用例失败的时候能截图,把异常截图展示到allure报告里面. pytest 有个很好的钩子函数 pytest_runtest_ma ...

  2. testng优化:失败重跑,extentReport+appium用例失败截图,测试报告发邮件

    生成的单html方便jenkins集成发邮件,= = 构建失败发邮件 参考:https://blog.csdn.net/galen2016/article/details/77975965 步骤: 1 ...

  3. reportNG定制化之失败截图及日志

    先从github上拉下 reportNg的源代码 reportng  拉下源码后我们使用IDEA进行导入 1.reportng.properties 增加部分类表项 这里我们直接在末尾添加 log=L ...

  4. python web自动化测试中失败截图方法汇总

    在使用web自动化测试中,用例失败则自动截图的网上也有,但实际能落地的却没看到,现总结在在实际应用中失败截图的几种方法: 一.使用unittest框架截图方法:   1.在tearDown中写入截图的 ...

  5. selenium测试报告(含通过率统计图和失败截图)

    前言: 介绍的是含饼状统计图及失败截图的测试报告文件. 原文地址:https://testerhome.com/topics/9984 此版本增加了如下功能 测试报告完全汉化,包括错误日志的中文处理 ...

  6. testng失败截图,注解方式调用。

    今天一整天都在研究testng失败截图的方法,参考网上的前辈们的资料,加上自己的理解,终于搞出来了. package com.dengnapianhuahai; /** * 自定义注释 * */ im ...

  7. Selenium2+python自动化66-装饰器之运行失败截图【转载】

    前言 对于用例失败截图,很多小伙伴都希望在用例执行失败的时候能自动截图,想法是很好的,实现起来并不是那么容易. 这里分享下我的一些思路,当然目前还没找到完美的解决方案,我的思路是用装饰器去解决,希望有 ...

  8. unittest实现用例运行失败截图

    把这个方法放到父类basecase(unittest.TestCase)就行了 #coding: utf-8 import unittest, random, os, traceback from s ...

  9. python unittest addCleanup中也加失败截图功能

    在python web自动化测试中失败截图方法汇总一文中提到了失败截图的方法 但在实际测试中,如果我们的测试用例中加了addCleanups动作,如果addCleanups中动作失败了,就不会截图.那 ...

随机推荐

  1. NSIS学习记录の----NSIS多语言安装以及详解

    NSIS多语言安装,很多教程提供了详细的代码,但是代码中某些语句的含义我还是不很明白,作为一个吃螃蟹的人,我做一个解释,避免很多小伙伴和我哟U一样的误区,以下结论都是自己根据实践得来,若发现理解错误, ...

  2. FileInputstream的available()方法

    摘自:http://greemranqq.iteye.com/blog/2051487

  3. 使用Windows安装的最高版本IE内核加载内嵌页(转载)

    客户端程序内嵌Webbrowser控件时,默认情况都是使用IE7兼容模式打开网页的.但是IE7有很多新的特性不支持,导致无法正常显示出来,所以需要强制使用高版本的IE内核来加载.渲染. void Ch ...

  4. 改变HTML

    改变HTML 1.改变HTML输出流 JavaScript可以创建动态的HTML内容.(注意:不要在加载文档后使用document.write(),这会覆盖文档.) <%@ page langu ...

  5. .Net操作注册表--un

    C#操作注册表 导入命名空间 Using MicroSoft.Win32;//64位系统装的64位版本

  6. HTML(Open Method)翻译自MSDN

    Open Method Opens a new window and loads the document specified by a given URL. Navigates the app wi ...

  7. BZOJ 2584: [Wc2012]memory(扫描线+线段树)

    题目链接:http://www.lydsy.com:808/JudgeOnline/problem.php?id=2584 题意:给出平面n个线段,任意两个线段严格不相交,且每个线段不平行于坐标轴.移 ...

  8. MD5加密算法

    package com.bao.tools.encryption; import java.security.MessageDigest;import java.security.NoSuchAlgo ...

  9. 关于MySQL数据库如何按时间查询

    这里做了几个测试 select * from simingpai where TIMESTAMP(createTime) >= '2015-9-6'; select * from simingp ...

  10. wooyunAPI

    经常要爬去乌云的信息,但是每次都是硬爬,写完了发现乌云有提供API的,整理给大家: 1. WooYun Api是什么 通过WooYun开放的Api接口,其它网站或应用可以根据自己获取的权限调用WooY ...