testng 失败自动截图
testng执行case failed ,testng Listener会捕获执行失败,如果要实现失败自动截图,需要重写Listener的onTestFailure方法
那么首先新建一个Listener 类,继承TestListenerAdapter
package com.dbyl.libarary.utils; import org.openqa.selenium.WebDriver;
import org.testng.ITestContext;
import org.testng.ITestResult;
import org.testng.TestListenerAdapter; /**
*
* @author Young
*
*/
public class TestNGListener extends TestListenerAdapter {
Log log = new Log(this.getClass()); @Override
public void onTestSuccess(ITestResult tr) {
log.info("Test Success");
super.onTestSuccess(tr);
} @Override
public void onTestFailure(ITestResult tr) {
log.error("Test Failure");
super.onTestFailure(tr);
takeScreenShot(tr);
} private void takeScreenShot(ITestResult tr) {
UITest b = (UITest) tr.getInstance();
WebDriver currentDirver = b.getDriver();
System.out.println(currentDirver.getTitle());
b.takeScreenShot(); } @Override
public void onTestSkipped(ITestResult tr) {
log.error("Test Skipped");
super.onTestSkipped(tr);
} @Override
public void onTestStart(ITestResult result) {
log.info("Test Finsh");
super.onTestStart(result);
} @Override
public void onStart(ITestContext testContext) {
log.info("Test Start");
super.onStart(testContext);
} @Override
public void onFinish(ITestContext testContext) {
log.info("Test Finish");
super.onFinish(testContext);
} }
我这里主要重写OnTestFailure的方法
添加了一个takeScreenShot的方法
接下来在UITest类中添加截图的具体实现方法
/**
*
*/
package com.dbyl.libarary.utils; import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;
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; /**
* @author Young
*
*/
public class UITest {
WebDriver driver;
Log log = new Log(this.getClass()); public WebDriver getDriver() {
return driver;
} /**
* init test case
*
* @param driver
*/
public void setDriver(WebDriver driver) {
this.driver = driver;
} public void init(WebDriver driver) {
log.info("Start WebDriver");
setDriver(driver);
} /**
* stop webdriver
*
* @param driver
*/
public void stop() {
log.info("Stop WebDriver");
driver.quit(); } /**
* @author Young
*/
public void takeScreenShot() {
SimpleDateFormat sf = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");
Calendar cal = Calendar.getInstance();
Date date = cal.getTime();
String dateStr = sf.format(date);
String path = this.getClass().getSimpleName() + "_" + dateStr + ".png";
takeScreenShot((TakesScreenshot) this.getDriver(), path);
} /**
* @author Young
* @param drivername
* @param path
*/
public void takeScreenShot(TakesScreenshot drivername, String path) {
// this method will take screen shot ,require two parameters ,one is
// driver name, another is file name
String currentPath = System.getProperty("user.dir"); // get current work
log.info(currentPath);
File scrFile = drivername.getScreenshotAs(OutputType.FILE);
// Now you can do whatever you need to do with it, for example copy
try {
log.info("save snapshot path is:" + currentPath + path);
FileUtils.copyFile(scrFile, new File(currentPath + "\\" + path));
} catch (Exception e) {
log.error("Can't save screenshot");
e.printStackTrace();
} finally {
log.info("screen shot finished");
}
} }
接下来在case中使用这个Listener,有两种办法, 第一种直接在case类中添加注解@Listeners({ TestNGListener.class })
case代码:
package com.dbyl.tests; import org.openqa.selenium.WebDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test; import com.dbyl.libarary.action.ViewHomePage;
import com.dbyl.libarary.utils.DriverFactory;
import com.dbyl.libarary.utils.TestNGListener;
import com.dbyl.libarary.utils.UITest;
//@Listeners({ TestNGListener.class })
public class loginTest extends UITest { WebDriver driver = DriverFactory.getChromeDriver(); @BeforeMethod(alwaysRun = true)
public void init() { super.init(driver);
ViewHomePage.setDriver(driver);
} @Test(groups = "loginTest")
public void loginByUerName() throws Exception {
ViewHomePage.viewMyProfile();
} @AfterMethod(alwaysRun = true)
public void stop() {
super.stop();
} }
第二种方法是在eclipse run config 添加如下参数-listener com.dbyl.libarary.utils.TestNGListener
这样就能实现case失败自动截图
这样,这个框架能够实现一些基本操作,下一步还需要实现失败重试 ,配合虚拟机
下载地址:https://github.com/tobecrazy/Demo
testng 失败自动截图的更多相关文章
- TestNG失败自动截图
转自:https://www.cnblogs.com/tobecrazy/p/4814813.html
- TestNG监听器实现用例运行失败自动截图、重运行功能
注: 以下内容引自 http://blog.csdn.net/sunnyyou2011/article/details/45894089 (此非原出处,亦为转载,但博主未注明原出处) 使用Testng ...
- Selenium2+python自动化67-用例失败自动截图【转载】
前言: 装饰器其实就是一个以函数作为参数并返回一个替换函数的可执行函数 上一篇讲到用装饰器解决异常后自动截图,不过并没有与unittest结合,这篇把截图的装饰器改良了下,可以实现用例执行失败自动截图 ...
- Webdriver+Testng实现测试用例失败自动截图功能
testng执行测试用例的时候,如果用例执行失败会自动截图,方便后续排查问题 1.首先定义一个截图类: package com.rrx.utils; import java.io.File;impor ...
- QTP场景恢复之用例失败自动截图
以下代码是在QC里运行QTP来执行脚本过程,当执行过程中发现用例失败后就会自动截图,然后把用例返回到最初始的状态,模拟了场景恢复的机制 Class QCImageErrorCapture Dim qt ...
- TestNG实现用例运行失败自动截图(转载)
转载自:https://blog.csdn.net/galen2016/article/details/70193684 重写Listener的onTestFailure方法 package com. ...
- appium自动化,失败自动截图
1.创建监听器类TestNGListener,重写onTestFailure方法,里面定义了 监听的driver ,截图文件路径和名称 package utils; import cases.Appi ...
- testng失败自动重试
使用的监听类有:IRetryAnalyzer.TestListenerAdapter.IAnnotationTransformer public class Retry implements IRet ...
- selenium2入门 断言失败自动截图 (四)
一般web应用程序出错过后,会抛出异常.这个时候能截个图下来,当然是极好的. selenium自带了截图功能. //获取截图file File scrFile= ((TakesScreenshot)d ...
随机推荐
- CephRGW 在多个RGW负载均衡场景下,RGW 大文件并发分片上传功能验证
http://docs.ceph.com/docs/master/radosgw/s3/objectops/#initiate-multi-part-upload 根据分片上传的API描述,因为对同一 ...
- 马虎将classname加到了id属性中,造成报错
今天做了一个瀑布流布局的小例子,自己在写代码的过程中一直报cannot read property 'style' of null,百度之后说是页面还没有加载完,但是我看了代码是写在window.on ...
- 【转】OpenGL超级宝典笔记——纹理映射Mipmap
原文地址 http://my.oschina.net/sweetdark/blog/177812 , 感谢作者,若非法转载请联系本人. 目录[-] Mipmapping Mipmap过滤 构建Mip层 ...
- 源码之Queue
看源码可以把python看得更透,更懂,想必也是开发人员的必经之路. 现在有个任务,写个线程池.使用Queue就能写一个最简单的,下面就来学学Queue源码. 源码之Queue: class Queu ...
- 大数据量冲击下Windows网卡异常分析定位
背景 mqtt的服务端ActiveMQ在windows上,多台PC机客户端不停地向MQ发送消息. 现象 观察MQ自己的日志data/activemq.log里显示,TCP链接皆异常断开.此时尝试从服务 ...
- BZOJ1257 [CQOI2007]余数之和sum
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- awk命令速查
awk与sed.grep一样都是为了加工数据流而做成的文本加工过滤器命令.awk会事先把输入的数据根据字段单位进行分割.在没有制定分割单位的情况下,以输入数据中的空格或Tab为分隔符.与sed相比,它 ...
- Android源码——AsynTask
AsyncTask<Params, Progress, Result>中三个参数为: Params 输入数据 Progress 过程数据 Result ...
- 在windows下使用linux命令,GnuWin32的使用.
http://sourceforge.net/projects/getgnuwin32 使用过linxu的伙计估计都会喜欢上linux各种各样强大的命令如:grep, sed,awk,diff和pat ...
- 【原】作为前端需要了解的B/S架构
其实B/S架构是属于后台方面的东西,不过作为一个前端,也是需要了解一下滴 C/S架构简要介绍 在了解什么是B/S架构之前,我们有必要了解一下什么是C/S架构: C/S架构是第一种比较早的软件架构,主要 ...