今天一整天都在研究testng失败截图的方法,参考网上的前辈们的资料,加上自己的理解,终于搞出来了。

package com.dengnapianhuahai;
/**
* 自定义注释
*
*/
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME)
@Target(value = ElementType.METHOD)
public @interface MyAnnotation { }
package com.dengnapianhuahai;
* 测试类
*/
import org.testng.Assert;
import org.testng.annotations.Test;
//这里继承MyDriver类是因为MyDriver类里面实例化了driver,这里只是拿来用。
//MyDriver的作用就是生成一个全局变量的driver。
//Info 与 MyTestListener只是在用这个在MyDriver中定义好的WebDriver类型的变量driver而已。
public class Info extends MyDriver {
@Test
@MyAnnotation
public void open() {
driver.manage().window().maximize();
driver.get("http://www.baidu.com");
driver.getTitle();
Assert.assertEquals(driver.getTitle(), "百度一下,..你就知道"); }
}
package com.dengnapianhuahai;

import java.io.File;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
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;
import org.testng.ITestResult;
import org.testng.TestListenerAdapter; public class MyTestListener extends TestListenerAdapter {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");
String mDateTime = formatter.format(new Date()); @Override
public void onTestFailure(ITestResult result) {
super.onTestFailure(result); try {
//用getInstance()方法生产实例然后强制转换为需要的类型(MyDriver),以获得全局变量driver;
WebDriver driver = ((MyDriver) result.getInstance()).getMyDriver();
//result.getMethod().getConstructorOrMethod().getMethod();之所以用三个个体getMethod()方法,是因为类型与等号左边的不一致。
Method method = result.getMethod().getConstructorOrMethod().getMethod(); if (method.isAnnotationPresent(MyAnnotation.class)) { File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE); FileUtils.copyFile(scrFile, new File("e:\\" + mDateTime + ".png"));
}
} catch (Exception e) {
e.printStackTrace(); }
}
}
package com.dengnapianhuahai;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
/**
*
* @author 等那片花海
*
*作为一个全局变量driver
*/
public class MyDriver { WebDriver driver = new FirefoxDriver(); public WebDriver getMyDriver() {
return this.driver;
}
}
<suite name="TestNGSuite">
<listeners>
<listener class-name="com.dengnapianhuahai.MyTestListener" />
</listeners>
<test name="test1">
<classes>
<class name="com.dengnapianhuahai.Info" /> </classes>
</test>
</suite>

testng失败截图,注解方式调用。的更多相关文章

  1. 【TestNG】使用代码方式调用TestNG用例执行

    TestNG的用例除了直接运行之外,还可以使用代码来调用,这样做的好处在于我们可以将其嵌入其他代码中,来执行这些TestNG用例,方法如下: 1.直接调用用例类 范例:定义了两个测试用例类为Depen ...

  2. TestNG多线程测试-注解方式实现

    用@Test(invocationCount = x,threadPoolSize = y)声明,invocationCount表示执行次数,threadPoolSize表示线程池大小. packag ...

  3. testng优化:失败重跑,extentReport+appium用例失败截图,测试报告发邮件

    生成的单html方便jenkins集成发邮件,= = 构建失败发邮件 参考:https://blog.csdn.net/galen2016/article/details/77975965 步骤: 1 ...

  4. WebDriver - 添加失败截图

    WebDriver失败截图可以通过两种方式实现: 1. Use WebdriverEventListener 第一步:创建自己的WebDriverEventListener 创建自己的WebDrive ...

  5. springMVC注解方式+easyUI+MYSQL配置实例

    刚接触springMVC,使用的注解方式,也在学习阶段,所以把自己学习到的记下来.本文利用springMVC从数据库读取用户信息为例,分享一下. 1.准备相关架包及资源.因为使用springMVC+e ...

  6. 【spring boot】14.spring boot集成mybatis,注解方式OR映射文件方式AND pagehelper分页插件【Mybatis】pagehelper分页插件分页查询无效解决方法

    spring boot集成mybatis,集成使用mybatis拖沓了好久,今天终于可以补起来了. 本篇源码中,同时使用了Spring data JPA 和 Mybatis两种方式. 在使用的过程中一 ...

  7. springmvc2 一个控制器写多个方法(非注解方式)

    出处:http://blog.csdn.net/xuewenke/article/details/23895999 springmvc2 一个控制器写多个方法(非注解方式) 分类: spring 20 ...

  8. Shiro启用注解方式

    shiro验证权限方式一种是基于url配置文件: 例如: <bean id="shiroFilter" class="org.apache.shiro.spring ...

  9. HttpClient Get/Post方式调用Http接口

    本节摘要:本节主要分别介绍如何用get方式.post方式向http接口发送数据. preparation 1. 项目环境如下: myeclipse6.5 .tomcat5.0.system:xp.JD ...

随机推荐

  1. 如何使用github,简单教程

    前期准备:先行设置SSH KEY(请看我之前发的关于设置添加SSH的文章) 直接上命令: clone 已有仓库到身边的开发环境中 git clone git@github.com:<yourna ...

  2. iOS开发之--如何修改TabBarItem的title的字体和颜色/BarButtonItem的title的字体大小和颜色/添加背景图片,并添加点击方法

    在进行项目的过程中,我们往往会遇到各种各样的自定义颜色和字体,下面提供一种修改系统自带的TabBarItem的字体和颜色的方法,希望能帮到大家: [[UITabBarItem appearance] ...

  3. Python Scrapy 验证码登录处理

    一.Form表单分析 以豆瓣登录页面为例分析,豆瓣登录页是:https://accounts.douban.com/login,浏览器打开之后查看源码,查找登录的form表单HTML结构.如下: 包括 ...

  4. open() 函数以 w+ 模式打开文件

    这种模式打开文件时,会先清空文件,然后才执行读写操作,当我们要执行读操作时,需要通过 seek() 方法将读取指针移到前面,才能读取内容 [root@localhost ~]$ cat 1.txt # ...

  5. Android无线测试之—UiAutomator UiScrollable API介绍七

    滑动到某个对象 一.滑动到某个对象相关API 返回值 API 描述 boolean scrollIntoView(UiSelector selector) 滑动到条件元素所在位置,并且尽量让其居于屏幕 ...

  6. Myeclipse创建Maven项目

    1.右击new创建一个Maven Project: 点击Next 继续Next Finish 2.项目成功创建后,项目目录主要有:主代码目录.主资源目录.测试代码目录.测试资源目录.输出目录(代码编译 ...

  7. LeetCode 笔记系列一 Median of Two Sorted Arrays

    题目:There are two sorted arrays A and B of size m and n respectively. Find the median of the two sort ...

  8. Ubuntu 安装 GNOME 之后开机进不了桌面

    折腾了两晚上,终于解决了这个问题,进入了桌面. 问题是这样产生的:我安装了 ubuntu-gnome-shell 这个包,然后出现配置 display manager 的选项,有 gdm3 和 lig ...

  9. VMware虚拟机下安装CentOS7.0超详细图文教程

    1.本文说明: 官方的第一个文本档案.也就是0_README.txt,大概意思是这样(渣翻译,但是大概意思还是有的). CentOS-7.0-1406-x86_64-DVD.iso:这个镜像(DVD ...

  10. TCP requires two packet transfers to set up the connection before it can send data

    wHTTP重用现存连接来减少TCP建立时延. HTTP The Definitive Guide 4.2.3 TCP Connection Handshake Delays When you set ...