写这篇微博之前,自动化测试代码中有对于失败测试截图的功能,但是却散落在各个catch语句块中。不便于以后的扩展和维护,AOP思想里说明是面向切面编程,把公共的组件提取出来,可以单独修改维护。

但是直到我看了http://www.cnblogs.com/zhangfei/p/4271923.html文章后,觉得应该尝试下了。在此感谢博主张飞的倾情分享!

步骤一,在公共utility包中加入截图方法

几个重点 a. 明确截图文件存放的位置和名字   b. 调用webdriver 下的getScreenshotAs方法截图,并将截图文件copy到指定目录下

直接上张飞的code!

 public class ScreenShot {
public WebDriver driver; public ScreenShot(WebDriver driver) {
this.driver = driver;
} private void takeScreenshot(String screenPath) {
try {
File scrFile = ((TakesScreenshot) driver)
.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File(screenPath));
} catch (IOException e) {
System.out.println("Screen shot error: " + screenPath);
}
} public void takeScreenshot() {
String screenName = String.valueOf(new Date().getTime()) + ".jpg";
File dir = new File("test-output/snapshot");
if (!dir.exists())
dir.mkdirs();
String screenPath = dir.getAbsolutePath() + "/" + screenName;
this.takeScreenshot(screenPath);
}
}

步骤二,定义一个类,实现TestNG的TestListenerAdapter接口

几个重点: a.得到当前要监听接口的webdriver对象 b.进而去调用utility类的截图方法

public class DotTestListener extends TestListenerAdapter{

	@Override
public void onTestFailure(ITestResult tr) {
try {
loginTest tb = (loginTest) tr.getInstance();
WebDriver driver=tb.getDriver();
System.out.println("listener is running"); ScreenShot ssh = new ScreenShot(driver);
ssh.takeScreenshot();
System.out.println("take screenshot ok!"); } catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} } }

步骤三,使用监听器

TestNG有两种方式使用监听器

方式一   在 testng.xml 中使用 TestNG 监听器

TestNG 通过 testng.xml 配置所有的测试方法。Testng.xml 提供了 listeners 和 listener 标签用来添加自定义的监听器。下面示范的是本文示例代码中包含的 testng.xml 文件。

 <suite name="TestNGSample">
<listeners>
<listener class-name="listeners.OSFilter" />
<listener class-name="listeners.ProgressTracker" />
</listeners>
<test name="ProgressTracker Demo">
<classes>
<class name="tests.SampleTest" />
</classes>
</test>
</suite>

方式二  在源代码中使用 TestNG 监听器

通过 @Listeners 注释,可以直接在 Java 源代码中添加 TestNG 监听器。下面示范的是本文示例代码中如何使用 @Listeners 注释。

 @Listeners({ OSFilter.class, ProgressTracker.class })
public class SampleTest { @Test(groups = { OSNames.OS_LINUX })
public void test1() {
sleep(5000);
System.out.println(">>>test1");
}

我用的是第二种方式。

参考文章:

http://www.cnblogs.com/zhangfei/p/4271923.html

(转)testng对失败时截图处理的更多相关文章

  1. testng对失败时截图处理

    1.截图类: public class ScreenShot { public WebDriver driver; public ScreenShot(WebDriver driver) { this ...

  2. TestNG监听器实现用例运行失败自动截图、重运行功能

    注: 以下内容引自 http://blog.csdn.net/sunnyyou2011/article/details/45894089 (此非原出处,亦为转载,但博主未注明原出处) 使用Testng ...

  3. testng 失败自动截图

    testng执行case failed ,testng Listener会捕获执行失败,如果要实现失败自动截图,需要重写Listener的onTestFailure方法 那么首先新建一个Listene ...

  4. testng增加失败重跑机制

    注: 以下内容引自 http://www.yeetrack.com/?p=1015 testng增加失败重跑机制 Posted on 2014 年 10 月 31 日 使用Testng框架搭建自动测试 ...

  5. phpStudy启动失败时的解决方法

    phpStudy启动失败时的解决方法 phpStudy启动失败,原因一是防火墙拦截,二是80端口已经被别的程序占用,如IIS,迅雷等:三是没有安装VC9运行库,php和apache都是VC9编译.解决 ...

  6. 当插入数据失败时,防止mysql自增长字段的自增长的方法

    问题描述: 当mysql设置了自增长字段时(注意:一个表中只能设置一个自增长字段,可以不是主键,但必须是键 ),如果插入数据失败,那么自增长字段仍然会占用这个自增长值,再次成功插入数据时就会造成断层. ...

  7. 【百度地图API】当地址解析失败时,如何调用search方法查找地址

    原文:[百度地图API]当地址解析失败时,如何调用search方法查找地址 有个朋友问我,当地址解析失败时,应该如何处理呢?比如,他想搜索“南宁市青秀区”. --------------------- ...

  8. 通过Jenkins跑Jmeter接口测试脚本,我想当有接口跑失败时Jenkins发送邮件通知,这个如何弄呢

    通过Jenkins跑Jmeter接口测试脚本,我想当有接口跑失败时Jenkins发送邮件通知,这个如何弄呢

  9. 浅析调用JSR303的validate方法, 验证失败时抛出ConstraintViolationException

    废话不多说,直接进入正题:如何使用JSR303的validate,进行数据校验,失败后直接抛出异常加入流转信息中,并在form页面提示出来. 首先我们为了启用验证,需要向 项目中添加Bean验证的实现 ...

随机推荐

  1. Caffe应用篇----文件格式转换

    网友文章: 我们手中有的一般都是图片数据,jpg.bmp格式等,但caffe常使用的数据是db格式(leveldb/lmdb),因此首先我们要将自己数据转换成caffe可运行的格式文件.别捉鸡,caf ...

  2. ExtJs6.0.0随笔

    环境:extJs6.0.0GPL,对应SenchaCmd-6.0.2-windows-64bit(注意版本不能太高). 步骤: 1.安装senchaCmd 2.运行生成demo: http://doc ...

  3. 将Linux命令的结果作为下一个命令的参数

    查询所有的pid并杀死. jps -l | grep bdcsc2-native-demo | awk '{print $1}' | xargs kill -9 KISS:keep it short ...

  4. SQL Server 数据库的安全管理(登录、角色、权限)

    ---数据库的安全管理 --登录:SQL Server数据库服务器登录的身份验证模式:1)Windows身份验证.2)Windows和SQL Server混合验证 --角色:分类:1)服务器角色.服务 ...

  5. fastcgi 性能初配 504 gateway time-out

    情况一:由于nginx默认的fastcgi进程响应缓冲区太小造成 这种情况下导致fastcgi进程被挂起,如果fastcgi服务队这个挂起处理不是很好的话,就可能提示"504 Gateway ...

  6. table 细边框

    table { border-collapse: collapse; border: none; width: 200px; } td { border: solid #000 1px; }

  7. Javascript use strict模式和对象

    use strict 只能出现在脚本代码的开始或者函数体的开始.任何实体语句之前.Javascript的具体实现将它们解析为解释器自有的指令.这个指令的目的是说明后续的代码将会解析为严格代码. ECM ...

  8. XML序列化与反序列化

    public static class XmlHelper { private static void XmlSerializeInternal(Stream stream, object o, En ...

  9. VC++ Post 方法 上传数据到web服务器

    最近在做一个项目,需要与WEB服务器交互一些信息.其中一项就是文件的上传与下载.现来一个上传的代码 #include "stdio.h" #include "WinSoc ...

  10. Graded Browser Support

    ( The YUI Target Environments Matrix is here) About the Browser Test Baseline and Operating Systems ...