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. HTML 5 <canvas> 标签

    <!DOCTYPE HTML> <html> <body> <canvas id="myCanvas">your browser d ...

  2. 如何从Android工程导出apk安装包

    http://jingyan.baidu.com/article/1876c852b3208b890b137606.html

  3. H5+混合移动app应用开发——坑我太甚

    用了MUI之后,才发现,那坑比我想象之中的要多得多,有些是H5的坑,有些是plus的坑,接下来我一一来吐槽一番. IOS下面,上拉的时候,速度稍微快一点,表头自动隐藏,等你不拉的时候又自动显示 这种情 ...

  4. python3的一些改动常用到的

    更多的内容会接下来说明,只举几个例子. https://docs.python.org/2/library/2to3.html 1. map的输出,要在前面加list转化 2. map(lambda ...

  5. 解决span的bug--不能自动换行的问题

    span标签元素不能自动换行,在超出父盒子的宽度后不能够自动换行 如下界面: 解决办法:将span属性加上display:block设置为行级元素:设置宽度然后在强制断行 效果如下:

  6. java1.8--OptionalInt,OptionalDouble,OptionalLong类

    OptionalInt,OptionalDouble,OptionalLong类的工作方式与Optional类十分类似,只不过他们是专门操作int,都变了,long类型的值而设计的.因此,他们分别定义 ...

  7. java里程碑之泛型--类型通配符

    1,泛型与数组在子类上面的对比 在整理java泛型的类型通配符之前,我们先来研究下java在数组设计上的一个不合理.我们来看下面代码: public class Test { public stati ...

  8. java中可变长参数的定义及使用方法

    JAVA中可以为方法定义可变长参数( Varargs)来匹配不确定数量的多个参数,其定义用“...”表示.其实,这类似于为方法传了一个数组,且在使用方法上也和数组相同,如下: public void ...

  9. Linux平台ORACLE INSTANT客户端安装

    下载安装文件 先去ORACLE官方网站下载所需版本的Instant Client Package 和 Instant Client Package - SQL*Plus安装包,(千万注意版本) htt ...

  10. 理解Object.defineProperty的作用

    对象是由多个名/值对组成的无序的集合.对象中每个属性对应任意类型的值.定义对象可以使用构造函数或字面量的形式: var obj = new Object; //obj = {} obj.name = ...