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. nyoj CO-PRIME 莫比乌斯反演

    CO-PRIME 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 This problem is so easy! Can you solve it? You are ...

  2. Android SDK版本和ADT版本

    Android SDK版本和ADT版本   Android早期的版本号有点“混乱”,比如Android 2.2对应的ADT版本为ADT-0.9.9而Android 2.3对应的的ADT版本则突然“跃迁 ...

  3. mfc线程

    1.生成线程 方式1. HANDLE hthread; //线程句柄 hthread=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)threadFunc,NU ...

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

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

  5. 将XML文件中的内容转换为Json对象

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Xml;u ...

  6. bzoj 3234: [Ahoi2013]立方体

    题目链接:http://www.lydsy.com:808/JudgeOnline/problem.php?id=3234 题意:求长方体交的表面积. 思路:flood-fill const int ...

  7. ZOJ 2182 Cable TV Network(无向图点割-最大流)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2182 题意:给出一个无向图,问最少删掉多少个顶点之后图变得不连通 ...

  8. .net 常用Response.ContentType

    来源:http://blog.csdn.net/navy235/article/details/5982319 不同的ContentType 会影响客户端所看到的效果.默认的ContentType为 ...

  9. NYOJ 128 前缀式计算

    前缀式计算 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 先说明一下什么是中缀式: 如2+(3+4)*5这种我们最常见的式子就是中缀式. 而把中缀式按运算顺序加上括 ...

  10. Ext.net 异常统一管理,铥掉可恶的 Request Failure

    Ext.net 异常统一管理,铥掉可恶的 Request Failure 看着这样的框框是不是很不爽 灭他.也不难.. .如果全部页面都有继承一个自定义的父类 ..那整个项目代码量就只有几行了.. 单 ...