testNG里有一个异常监听类,失败时会执行类里的相关方法

DriverBase  截图类
TestngListenerScreen  异常监听类
Test1 测试类

1.DriverBase类
package com.cmall.screenshot;

import com.cmall.appium.DriverFactory;
import com.cmall.appium.Helper;
import com.cmall.jdjr.pages.Modules.Integration.HomePage;
import com.cmall.utils.LogUtil;
import com.cmall.utils.PropertyUtil;
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.pagefactory.AppiumFieldDecorator;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.support.PageFactory;
import org.testng.annotations.Test; import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.TimeUnit; public class DriverBase
{ private static AndroidDriver<MobileElement> driver = null;
private Helper helper;
private LogUtil log = new LogUtil(DriverBase.class);
/**
* 获取driver
* */
public AndroidDriver<MobileElement> getDriver() {
return Test1.mdriver;
} /**
* 自动截图
* */
public void takeScreenShot(String methodName) {
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() + "_" + methodName + "_" + dateStr + ".png";
//因为我们截图是需要用到driver的,所以这里需要获取driver,这个driver是获取的当前对象的driver
takeScreenShot((TakesScreenshot) this.getDriver(), path); } /**
* 传入参数截图
* */
public void takeScreenShot(TakesScreenshot drivername, String path) {
String currentPath = PropertyUtil.getString("screenPic_dir");
File scrFile = drivername.getScreenshotAs(OutputType.FILE);
try {
FileUtils.copyFile(scrFile, new File(currentPath + "\\" + path));
} catch (Exception e) {
e.printStackTrace();
} finally {
log.error("<a href=" + currentPath + " target=_blank>Failed Screen Shot</a>");
System.out.println("截图成功");
}
}
public void setDriver(AndroidDriver<MobileElement> driver){
this.driver = driver;
}
}

2.TestngListenerScreen类

package com.cmall.screenshot;

import org.testng.ITestContext;
import org.testng.ITestResult;
import org.testng.TestListenerAdapter; public class TestngListenerScreen extends TestListenerAdapter
{
@Override
public void onTestSuccess(ITestResult tr)
{
super.onTestSuccess(tr);
} // 主要是用到这个方法了,当你报错时他会监听到,然后就会执行截图操作
@Override
public void onTestFailure(ITestResult tr)
{
super.onTestFailure(tr);
System.out.println("####################################################");
System.out.println(tr);
System.out.println("####################################################");
takeScreenShot(tr,tr.getMethod().getMethodName());//第二个参数表示哪个类产生的异常
}
@Override
public void onTestSkipped(ITestResult tr) {
super.onTestSkipped(tr);
} @Override
public void onTestStart(ITestResult result) {
super.onTestStart(result);
} @Override
public void onStart(ITestContext testContext) {
super.onStart(testContext);
} @Override
public void onFinish(ITestContext testContext) {
super.onFinish(testContext);
} private void takeScreenShot(ITestResult tr,String methodName) {
DriverBase driverBase = (DriverBase) tr.getInstance();
driverBase.takeScreenShot(methodName); }
}

3.Test1测试类

package com.cmall.screenshot;

import com.cmall.appium.DriverFactory;
import com.cmall.appium.Helper;
import com.cmall.appium.MultideviceManage;
import com.cmall.http.LogUtil;
import com.cmall.jdjr.pages.Modules.Integration.HomePage;
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.pagefactory.AppiumFieldDecorator;
import org.openqa.selenium.support.PageFactory;
import org.testng.annotations.Listeners; import java.util.concurrent.TimeUnit;
import org.testng.annotations.Test; @Listeners({ TestngListenerScreen.class })
public class Test1 extends DriverBase
{
static AndroidDriver<MobileElement> mdriver = null;
private LogUtil log = new LogUtil(Test.class);
MultideviceManage m = new MultideviceManage();
Helper helper;
public Test1(){
log.info("---------屏幕截图测试类---------------");
}
@Test
public void test(){
mdriver = DriverFactory.initDriver(4723,"TWGDU16B26001079");
HomePage homePage = new HomePage();
PageFactory.initElements(new AppiumFieldDecorator(mdriver, 20 , TimeUnit.SECONDS), homePage);
// int a=1/0;//没预测到的--会截图
try {
Thread.sleep(2000);
helper = new Helper(mdriver);
helper.clickonElement(homePage.精选);
// Assert.assertEquals(1,2);//会截图
//throw new IllegalArgumentException("参数长度不是7位"); //不会截图
int b=1/0;//能预测到被catch到的 不会截图
} catch (Exception e) {
e.printStackTrace();
}
}
}

4.配置xml文件

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="TestngListenerScreen" verbose="1" >
<listeners>
<listener class-name="com.cmall.screenshot.TestngListenerScreen"></listener>
</listeners>
<test name = "Test" >
<classes>
<class name="com.cmall.screenshot.ScreenTest"/>
</classes>
</test>
</suite>

5.把xml文件配置在pom.xml里

 

testng 异常 截图的更多相关文章

  1. Python+Selenium学习--异常截图

    前言 Webdriver 提供错误截图函数get_screenshot_as_file(),可以帮助我们跟踪bug,在脚本无法继续执行时候, get_screenshot_as_file()函数将截取 ...

  2. testng入门教程8 TestNG异常测试

    TestNG跟踪异常处理代码提供了一个选项.可以测试是否需要代码抛出异常或不抛出. @Test注释expectedExceptions 参数一起使用.现在,让我们来看看@Test(expectedEx ...

  3. testng失败截图,注解方式调用。

    今天一整天都在研究testng失败截图的方法,参考网上的前辈们的资料,加上自己的理解,终于搞出来了. package com.dengnapianhuahai; /** * 自定义注释 * */ im ...

  4. TestNG异常测试

    用@Test(expectedExceptions = xxx) 声明 package com.janson; import org.testng.annotations.Test; public c ...

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

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

  6. selenium遇到异常自动截图

    最近要在框架中添加case失败时,要自动截图,主要又两种方式,思想都是在抛异常的时候,捕获到异常,并作页面截图处理.今天坐下总结. 一.第一种方式,重写onException方法 只针对webdriv ...

  7. TestNG 入门教程

    原文出处:http://www.cnblogs.com/TankXiao/p/3888070.html 阅读目录 TestNG介绍 在Eclipse中在线安装TestNG 在Eclipse中离线安装T ...

  8. JAVA中的异常及处理异常的方法

    异常 这是我老师的喜好:就是说一上来就拿一张图给大家看看,过过瘾-_- 这是一张: 异常分类图 来,这里还有一张带中文的常见异常截图!!! 1:先来说说什么是异常吧: 其实就是"阻止当前方法 ...

  9. Entity Framework 6 执行Linq to Entities异常"p__linq__1 : String truncation: max=0, len=2, value='测试'"

    场景再现 我需要查询公司名称包含给定字符串的公司,于是我写了下面的测试小例子: var condition = "测试"; var query = from b in db.Com ...

随机推荐

  1. 宝塔服务器管理助手Linux面版-使用教程

    在顺利安装宝塔服务器linux面板之后,我们打开这个面板,UI界面设计的很简介,所有命令一看就知道是干什么用的,和我们以前用过的虚拟主机管理后台是很像的. 方法/步骤 1 使用方法如下: 面板地址:h ...

  2. ADO.NET复习总结(3)--参数化SQL语句--防止sql注入式攻击

    1.SQL 注入 2.使用参数化的方式,可以有效防止SQL注入,使用类parameter的实现类SqlParameter Command的属性parameters是一个参数集合. 3.举例<查询 ...

  3. g4e基础篇#6 了解Git历史记录

    章节目录 前言 1. 基础篇: 为什么要使用版本控制系统 Git 分布式版本控制系统的优势 Git 安装和设置 了解Git存储库(Repo) 起步 1 – 创建分支和保存代码 起步 2 – 了解Git ...

  4. scss 初学笔记 二 混合宏

    混合宏 格式  @mixin 定义混合宏 (相当于变量声明 var  $ ?) //不带参数混合宏 @mixin borderRadius{ -webkit-border-radius: 5px; b ...

  5. python_14_生成器

    什么是生成器? -- 动态的生成有规律的列表和元组,查询多少才会生成多少数据,不需要时数据不存在 - 大到10几万数据,就省空间了 什么是列表生成式? -- [ handle_i_result for ...

  6. Windows核心编程&进程

    1. 进程的定义 说白了进程就是一个正在运行的执行程序,包含内核对象和独立的地址空间,内核对象负责统计和管理进程信息,地址空间包括所有可执行文件或DLL 模块的代码和数据.动态内存分配(线程堆和栈的分 ...

  7. 第一个简单的maven项目

    学习一个新的东西,最快的方式就是实践.所以我们也不用多说什么了,直接拿一个项目来练手.下面的整理取自maven权威指南,在一堆maven资料中,我觉得这本书写的最好. 简介 我们介绍一个用Maven ...

  8. java常用类--正则表达式

    正则表达式到底是什么? 在编写处理字符串的程序或网页时,经常会有查找符合某些复杂规则的字符串的需要.正则表达式就是用于描述这些规则的工具.换句话说,正则表达式就是记录文本规则的代码. 很可能你使用过W ...

  9. Maven初步

    Maven初入 maven 是一个项目管理工具, 它包含了一个 项目对象模型(Project Object Model POM), 一组标准集合, 一个项目生命周期(Project Lifecycle ...

  10. rem与@media 的优缺点

    首先:   如果我们在做单独移动端网站或者app的时候  我建议  使用 rem  ; 他能让我们在手机各个机型的适配方面:大大减少我们代码的重复性,是我们的代码更兼容. 下面两个图一个调试在常用的机 ...