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. Git的简单的基本使用

    前言: 接触了Android Studio,自然是知道了Github这个网站,这个网站有许多大神们做的开源库,我们只需要简单地引入就是可以使用到这些开源库从而实现酷炫的效果,最近也是刚接触到Git的使 ...

  2. Hibernate查询对象的方法浅析

    Hibernate 查询对象是根据对象的id查询的,只要你有id (id唯一),则无论你是否其他字段与传过来的对象一致,都会查到该id在数据库对应的对象.若是在关联查询中,所关联表的id为空,即所查表 ...

  3. 如何让phpmyadmin输入密码再进入

    分类: wamp 对于很多不熟悉PHP环境安装的朋友来说,用集成环境可以更快的上手,更方便的搭建PHP的运行环境,但是,WAMP的集成环境仅仅是将底层基础工作做好了,有些个别关键的配置操作并没有集成到 ...

  4. BC高精确度函数使用。

    bc是Binary Calculator的缩写.bc*函数的参数都是操作数加上一个可选的 [int scale],比如string bcadd(string $left_operand, string ...

  5. 使用Botkit和Rasa NLU构建智能聊天机器人

    欢迎大家前往云+社区,获取更多腾讯海量技术实践干货哦~ 我们每天都会听到关于有能力涉及旅游.社交.法律​​.支持.销售等领域的新型机器人推出的新闻.根据我最后一次查阅的数据,单单Facebook Me ...

  6. i2c总线的oled12864屏的u8x8运用总结

    github网址链接 https://github.com/olikraus/u8g2/wiki/u8x8reference#print 用到的库文件 #ifdef U8X8_HAVE_HW_SPI ...

  7. python_如何在循环引用中管理内存?

    案例: python中通过引用计数来回收垃圾对象,在某些环形数据结构(树,图--),存在对象间的循环引用,比如树的父节点引用子节点,子节点同时引用父节点,此时通过del掉引用父子节点,两个对象不能被立 ...

  8. Linux PHP多版本切换 超简单办法

    今天在帮别人安装一个不知所谓的东西时碰到,三版本的PHP环境,我感觉那个人也是666哒,他使用的是AMH快速开发工具 有图有真相!!! 然后就顺便写下怎么快速,简便切换php版本 首先:find命令找 ...

  9. linkin大话面向对象--构造器详解

       对象的产生格式:类名称  对象名 = new  类名称(); 因为有(),所以是方法,实际上它就是构造方法,并且是非私有的构造方法.如:CellPhone cp = new CellPhone( ...

  10. RChain节点通信机制(上)

    在介绍RChain的通信机制之前,先简单介绍一些以太坊的通信机制,它包括以下几个方面,如下详细了解以太坊的通信机制,可以查看https://github.com/ethereum/devp2p/blo ...