testng执行测试用例的时候,如果用例执行失败会自动截图,方便后续排查问题

1.首先定义一个截图类:

package com.rrx.utils;

import java.io.File;
import java.io.IOException;
import java.util.Date;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;

public class ScreenShort {
public WebDriver driver;

public ScreenShort(WebDriver driver) {
this.driver = driver;
}

public 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 file=new File("C://Users//Administrator//workspace//Selenium"+File.separator+"test-output");
//System.out.println(File.separator);//就是一个/
if (!file.exists())
file.mkdirs();

String screenPath = file.getAbsolutePath() + "\\" + screenName;
this.takeScreenshot(screenPath);
}
}

2.实现testng的listener接口并重写OnTestFailure方法

package com.rrx.utils;

import org.testng.ITestResult;
import org.testng.TestListenerAdapter;

public class Listener extends TestListenerAdapter {
@Override
public void onTestFailure(ITestResult tr){

try {
ScreenShort sc=new ScreenShort(DriverFactory.getDriver());
sc.takeScreenshot();

} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
}
}

3.加上监听

@Listeners({ DotTestListener.class })

或者直接在testNG配置文件中添加

<listeners>
<listener class-name="com.rrx.utils.Listener" />
</listeners>

Webdriver+Testng实现测试用例失败自动截图功能的更多相关文章

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

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

  2. testng 失败自动截图

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

  3. Selenium2+python自动化67-用例失败自动截图【转载】

    前言: 装饰器其实就是一个以函数作为参数并返回一个替换函数的可执行函数 上一篇讲到用装饰器解决异常后自动截图,不过并没有与unittest结合,这篇把截图的装饰器改良了下,可以实现用例执行失败自动截图 ...

  4. Unittest 支持 case 失败后自动截图功能的另外两种方式

    原生的unittest框架是不支持case失败后自动截图的功能的,网上看了大家的解决办法,大体上分为两种:1.要么加装饰器2.也有人封装断言这里我们看看还有没有其他的更加方便的方法值得大家一起探讨一下 ...

  5. selenium 利用testNG对异常进行自动截图

    哈哈哈,很久没写博客了,懒了. 因为一些原因最近需要把监听事件重新整理一下,开始没细想,直接copy网上的,其实结果发现报错很多,或者是达不到效果,然后把之前的代码翻出来,仔细看了一下.下面给一些需要 ...

  6. QTP场景恢复之用例失败自动截图

    以下代码是在QC里运行QTP来执行脚本过程,当执行过程中发现用例失败后就会自动截图,然后把用例返回到最初始的状态,模拟了场景恢复的机制 Class QCImageErrorCapture Dim qt ...

  7. selenium2入门 断言失败自动截图 (四)

    一般web应用程序出错过后,会抛出异常.这个时候能截个图下来,当然是极好的. selenium自带了截图功能. //获取截图file File scrFile= ((TakesScreenshot)d ...

  8. selenium2 断言失败自动截图 (四)

    一般web应用程序出错过后,会抛出异常.这个时候能截个图下来,当然是极好的. selenium自带了截图功能. //获取截图file File scrFile= ((TakesScreenshot)d ...

  9. TestNG实现用例运行失败自动截图(转载)

    转载自:https://blog.csdn.net/galen2016/article/details/70193684 重写Listener的onTestFailure方法 package com. ...

随机推荐

  1. 集合或数组转成String字符串

    1.将集合转成String字符串 String s=""; for (int i = 0; i < numList.size(); i++) { if (s=="& ...

  2. Composer的入门与使用

    一 什么是composer composer是一种php的包管理工具, 类似于Java的maven, Ubuntu的apt等, 可以方便的解决php的包管理, 管理依赖关系等问题. 二 使用compo ...

  3. POJ - 1681: Painter's Problem (开关问题-高斯消元)

    pro:开关问题,同上一题. 不过只要求输出最小的操作步数,无法完成输出“inf” sol:高斯消元的解对应的一组合法的最小操作步数. #include<bits/stdc++.h> #d ...

  4. slf4j的使用

    1.导入jar包 <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api< ...

  5. RabbitMQ全网资料收集

    RabbitMQ是一个由erlang开发的AMQP(Advanced Message Queue )的开源实现.AMQP 的出现其实也是应了广大人民群众的需求,虽然在同步消息通讯的世界里有很多公开标准 ...

  6. linux下禁用网卡的启用网卡的一些方法

    第一种方法: 这种方法主要是在不重启的情况下会一直生效,适用于服务器. #禁用网卡eth0 sudo ifconfig eth0 down #启用网卡eth0 sudo ifconfig eth0 u ...

  7. 引擎设计跟踪(九.14.3.3) Deferred shading的一些小细节

    1.ambient light 之前的shader里面, 方向光会加上ambient 的计算. 但是如果没有方向光, 就没有ambient. 这是把全局方向光改为点光源之后发现的, 因为透明物体的fo ...

  8. 2019软件工程第二次作业(VS2017中对C++的单元测试)

    建立工程,分别编写cpp和头文件 cpp文件中的代码如下: #include<iostream> #include"test.h" using namespace st ...

  9. go 的数据类型

    bool string int int8 int16 int32(rune) int64 uint uint8(byte) uint16 uint32 uint64 uintptr:无符号整型,用于存 ...

  10. Go 知识点

    必须在源文件中非注释的第一行指明这个文件属于哪个包,如:package main. package main表示一个可独立执行的程序,每个 Go 应用程序都包含一个名为 main 的包. main 函 ...