testng 失败自动截图
来源:互联网 发布:linux shell 循环 编辑:程序博客网 时间:2024/04/27 20:03
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代码:
View Code
第二种方法是在eclipse run config 添加如下参数-listener com.dbyl.libarary.utils.TestNGListener
这样就能实现case失败自动截图
这样,这个框架能够实现一些基本操作,下一步还需要实现失败重试 ,配合虚拟机
0 0
- testng 失败自动截图
- testng 失败自动截图
- Webdriver+Testng实现测试用例失败自动截图功能
- selenium+testng自动化运行失败时自动截图
- Webdriver+Testng实现测试用例失败自动截图功能
- TestNG监听器实现用例运行失败自动截图、重运行功能
- TestNG监听器实现用例运行失败自动截图、重运行功能
- TestNG监听器实现失败自动截图、重跑、自定义html结果文件功能
- TestNG监听器实现用例运行失败自动截图、重运行功能
- TestNG监听器实现失败自动截图、重跑、自定义html结果文件功能
- TestNG监听器实现失败自动截图、重跑、自定义html结果文件功能
- TestNG监听器实现用例运行失败自动截图、重运行功能
- TestNG实现用例运行失败自动截图和重跑
- testng对失败时截图处理
- testng对失败时截图处理
- TestNG-使用Listener获取失败截图
- testng对失败时截图处理
- 如何解决testng执行用例报错自动截图问题
- Uri转化为filepath的方法,及压缩图片的方法
- Handler详解之四
- Eclipse Java EE IDE for Web Developers.配置Tomcat全攻略
- 高并发 高负载 网站系统架构
- Attempting to badge the application icon but haven't received permission from the user to badge the
- testng 失败自动截图
- iLeGeND
- mysql多表联查
- 《三体》读后感
- 自定义View
- 二维码识别项目——数据库建立及报表生成
- Maven 坐标和依赖
- 输入框和键盘
- 以太网端口在空闲时的波形