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. C#获取当前页面的url

    C#获取当前页面的url string a= Request.ApplicationPath; // / string b = Request.CurrentExecutionFilePath; // ...

  2. Bad apple for CSharp

    前言:记得10年的时候我还在上学,有一天逛csdn看到了字符版的badapple,感觉这东西好NB啊,然后就下载了一份,最近整理博客就把他整理博客,原作者是谁真心不知道,这是在果壳看到的. Bad A ...

  3. linux下查看分区信息和剩余空间大小

    1. 查看Linux系统分区信息,使用命令“fdisk -l” 2.使用命令”df -l和df -h“具体查看分区使用状况.实际这两个命令具有一样的作用区别是显示的容量单位不一样,当然也可以直接使用明 ...

  4. ord函数-php

    摘录自http://php.net/manual/zh/function.ord.php (PHP 4, PHP 5, PHP 7) ord — 返回字符的 ASCII 码值 说明 int ord ( ...

  5. NSIntger CGFloat NSNumber

    NSIntger  CGFloat  NSNumber 1.NSIntger  (long) %ld NSInteger a=; NSLog(@"----------%ld",(l ...

  6. GMM及EM算法

    GMM及EM算法 标签(空格分隔): 机器学习 前言: EM(Exception Maximizition) -- 期望最大化算法,用于含有隐变量的概率模型参数的极大似然估计: GMM(Gaussia ...

  7. TeeChart曲线平滑 Line.Smoothed

    需要注意的是,在加载点之前,需要设置Smoothed属性为false 等点加载完成之后,再设置Smoothed属性为true, //如果直接设置Smoothed为true再去加载点的话,曲线就完全不显 ...

  8. 使用source Insight工具创建uboot工程。

    首先在linux下面解压uboot的代码.不能在Windows下面解压,因为Windows的文件名是不区分大小写的. 然后,创建网络驱动器,这样就能在Windows下访问linux的文件夹了.方法:通 ...

  9. 二叉树hdu1710

    学习二叉树,看了两天也不明白,唉!acm之路让我体验到要付出巨大的努力,废话不多说,看我网上找到的代码: 此题题意很明确,给你先序遍历,中序遍历,求后序遍历.但代码就让我找不到东西了. http:// ...

  10. So easy Webservice 7.CXF 发布WebService

    (一)使用ServerFactoryBean 方式实现发布WS服务 1.新建项目,添加cxf jar包到项目中 2.编写服务实现类 /** * CXF WebService * 不用注解 * @aut ...