WebDriver - 添加失败截图
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 - 添加失败截图的更多相关文章
- pytest文档47-allure报告添加用例失败截图
前言 使用 selenium 做 web 自动化的时候,很多小伙伴希望用例失败的时候能截图,把异常截图展示到allure报告里面. pytest 有个很好的钩子函数 pytest_runtest_ma ...
- testng优化:失败重跑,extentReport+appium用例失败截图,测试报告发邮件
生成的单html方便jenkins集成发邮件,= = 构建失败发邮件 参考:https://blog.csdn.net/galen2016/article/details/77975965 步骤: 1 ...
- reportNG定制化之失败截图及日志
先从github上拉下 reportNg的源代码 reportng 拉下源码后我们使用IDEA进行导入 1.reportng.properties 增加部分类表项 这里我们直接在末尾添加 log=L ...
- python web自动化测试中失败截图方法汇总
在使用web自动化测试中,用例失败则自动截图的网上也有,但实际能落地的却没看到,现总结在在实际应用中失败截图的几种方法: 一.使用unittest框架截图方法: 1.在tearDown中写入截图的 ...
- selenium测试报告(含通过率统计图和失败截图)
前言: 介绍的是含饼状统计图及失败截图的测试报告文件. 原文地址:https://testerhome.com/topics/9984 此版本增加了如下功能 测试报告完全汉化,包括错误日志的中文处理 ...
- testng失败截图,注解方式调用。
今天一整天都在研究testng失败截图的方法,参考网上的前辈们的资料,加上自己的理解,终于搞出来了. package com.dengnapianhuahai; /** * 自定义注释 * */ im ...
- Selenium2+python自动化66-装饰器之运行失败截图【转载】
前言 对于用例失败截图,很多小伙伴都希望在用例执行失败的时候能自动截图,想法是很好的,实现起来并不是那么容易. 这里分享下我的一些思路,当然目前还没找到完美的解决方案,我的思路是用装饰器去解决,希望有 ...
- unittest实现用例运行失败截图
把这个方法放到父类basecase(unittest.TestCase)就行了 #coding: utf-8 import unittest, random, os, traceback from s ...
- python unittest addCleanup中也加失败截图功能
在python web自动化测试中失败截图方法汇总一文中提到了失败截图的方法 但在实际测试中,如果我们的测试用例中加了addCleanups动作,如果addCleanups中动作失败了,就不会截图.那 ...
随机推荐
- java提高篇---ArrayList
一.ArrayList概述 ArrayList是实现List接口的动态数组,所谓动态就是它的大小是可变的.实现了所有可选列表操作,并允许包括 null 在内的所有元素.除了实现 List 接口外,此类 ...
- centOS安装Mysql指南
centOS安装Mysql指南 说明:使用操作系统centOS6.4 32位系统:mysql:mysql-5.7.10-linux-glibc2.5-i686.tar.gz; 一.准备 下载mysql ...
- 【Web】简谈如何监听浏览器的关闭
> 参考的优秀文章 beforeunload实现关闭离开的提示 想起以前做的一个小系统,一个企业内部小型的测试系统,让考生在给定时间内完成考试,如果考生中退出,那么下次进来可以利用剩余的考试时间 ...
- 改变了一下blog的主题,很开心
调整了一下博客的样式,之前一直想改变下,一直不会这次终于摸索出来一点,不过是最简单的,就是在管理里面的设置,可以定制css代码,修改修改,页面就跟着你的想法走了,有时间好好修改下,暂时这个样子,简单大 ...
- 传递给系统调用的数据区域太小。 (异常来自 HRESULT:0x8007007A)
在做结构体向字节数组转换的时候,常遇到"传递给系统调用的数据区域太小"的错误,究其原因是因为英文与汉字的编码方式不同,一个汉字等于两个字节,而一个英文字母等于1个字节.所以,对于如 ...
- SqlSever基础 order by之后再orderby,双重排序,对排序好的数据中再次进行排序
镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ ...
- (转)Genymotion安装virtual device的“unable to create virtual device, Server returned Http status code 0”的解决方法
网络原因无法下载virtual device,status 为0表示服务器没有响应.FQ下载吧,有VPN的小伙伴推荐这种. 或者直接手动下载ova虚拟机文件,然后将虚拟机文件导入到virtualbox ...
- Windows控制台程序“选定模式”的问题
最近用Nodejs写了个代理程序,一直用的好好的,木有问题,今天突然发现不能用了,使用telnet去连代理的端口也能连通,可是服务就是不能正常使用,提示连接超时. 当时猜测是Nodejs的某个地方阻塞 ...
- MyBatis 动态SQL查询,多条件,分页
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-/ ...
- EntityFramework 开始小试
1 Install-Package EntityFramework 2 创建实体类 public class Blog { public int BlogId { get; set; } public ...