testng 异常 截图
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 异常 截图的更多相关文章
- Python+Selenium学习--异常截图
前言 Webdriver 提供错误截图函数get_screenshot_as_file(),可以帮助我们跟踪bug,在脚本无法继续执行时候, get_screenshot_as_file()函数将截取 ...
- testng入门教程8 TestNG异常测试
TestNG跟踪异常处理代码提供了一个选项.可以测试是否需要代码抛出异常或不抛出. @Test注释expectedExceptions 参数一起使用.现在,让我们来看看@Test(expectedEx ...
- testng失败截图,注解方式调用。
今天一整天都在研究testng失败截图的方法,参考网上的前辈们的资料,加上自己的理解,终于搞出来了. package com.dengnapianhuahai; /** * 自定义注释 * */ im ...
- TestNG异常测试
用@Test(expectedExceptions = xxx) 声明 package com.janson; import org.testng.annotations.Test; public c ...
- selenium 利用testNG对异常进行自动截图
哈哈哈,很久没写博客了,懒了. 因为一些原因最近需要把监听事件重新整理一下,开始没细想,直接copy网上的,其实结果发现报错很多,或者是达不到效果,然后把之前的代码翻出来,仔细看了一下.下面给一些需要 ...
- selenium遇到异常自动截图
最近要在框架中添加case失败时,要自动截图,主要又两种方式,思想都是在抛异常的时候,捕获到异常,并作页面截图处理.今天坐下总结. 一.第一种方式,重写onException方法 只针对webdriv ...
- TestNG 入门教程
原文出处:http://www.cnblogs.com/TankXiao/p/3888070.html 阅读目录 TestNG介绍 在Eclipse中在线安装TestNG 在Eclipse中离线安装T ...
- JAVA中的异常及处理异常的方法
异常 这是我老师的喜好:就是说一上来就拿一张图给大家看看,过过瘾-_- 这是一张: 异常分类图 来,这里还有一张带中文的常见异常截图!!! 1:先来说说什么是异常吧: 其实就是"阻止当前方法 ...
- 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 ...
随机推荐
- php中PHPMailer发送带附件的电子邮件方法
摘要: 本文讲的是php中PHPMailer发送带附件的电子邮件方法, .首先到http://phpmailer.worxware.com/ 下载最新版本的程序包 2.下载完成后,找到class.ph ...
- dedecms环境优化
路径:dedecms/dede/templates/index_body.htm <script type="text/javascript">function sho ...
- MySql 修改外键 支持级联删除
首先必须要有外键,InnoDB甚么的都不说了,直接上修改句子. 要先删除该外键,然后添加. 具体原因貌似是因为不支持alter外键的操作. ALTER TABLE `t_terminal` DROP ...
- Python3基础知识之字符串
1.运算符 * >>> b=a*5>>> b'pythonpythonpythonpythonpython'>>> b.replace('t',' ...
- JXLS使用方法(文件上传读取)xlsx文件读取
1.官方文档:http://jxls.sourceforge.net/reference/reader.html 2.demo git地址:https://bitbucket.org/leonate/ ...
- Linux - ubuntu中vi不能正常使用方向键与退格键的问题
一度怀疑是键盘坏了! 之前安装solaris也是这个问题! 重新安装vim就可以了! $sudo apt-get remove vim-common $sudo apt-get install vim
- python_6_字符串
什么是字符串? --一般用户输入数据和一堆有意义或者没有意义的文字符号组合 对字符串有哪些操作? --字符串本身不能修改,可以切片,可以读取 -- .capitalize() ...
- vs2005配置OpenCv2.3.1
编译OpenCv 1 用CMake导出VC++项目文件 运行cmake-gui,设置where is the source code路径为OpenCV安装路径(本文档假定安装位置为:c:\OpenCV ...
- 安卓studio导入jra包和so包,百度地图so包加载
导入so包 这个我只接受测试可用的一种方法 第一步:把so包放在libs目录下,可以是文件夹也可以是单独的一个个so文件 然后在src同级的目录下找到build.gradle文件下如下信息 sourc ...
- Spring 4.x (一)
1 Spring是什么? Spring是一个开源框架 Spring是为简化企业级应用开发而生的,使用Spring可以使得简单的JavaBean能够实现以前只有EJB才能实现的功能. Spring是一个 ...