相信用过Selenium WebDriver 的朋友都应该知道如何使用WebDriver API实现Take Screenshot的功能。

在这篇文章里,我主要来介绍对failed tests实现 take screenshot的功能, 并且我们也高大上一回,做成注解的形式。

效果如下:

目录
前提
Maven 配置
Example
    简单类图
     TakeScreenshotOnFailure
CustomTestListener
WebDriverHost
TestBase
DemoListenerTest
TestNG.xml
Run as TestNG

前提

  • JDK 1.7
  • Maven 3

Maven 配置

        <dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.9.5</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.46.0</version>
</dependency>

Example

简单类图

首先创建一个类 如下 TakeScreenshotOnFailure.java

package test.demo;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; /**
* Annotation informs CustomTestListener to take screenshot if test method fails. File name is method name + png suffix.
* Test class must implement WebDriverHost in order for the screenshot to work.
*
* @author wadexu
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(value =
{ElementType.METHOD})
public @interface TakeScreenshotOnFailure {
}

##转载注明出处: http://www.cnblogs.com/wade-xu/p/4861024.html

然后创建一个自己的CustomTestListener 继承TestNG 的 TestListenerAdapter 重写onTestFailure()方法, 加入take screenshot的功能

代码如下

package test.demo;

import java.io.File;
import java.lang.reflect.Method; import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver; import org.testng.ITestResult;
import org.testng.TestListenerAdapter; /**
*
* @Description: A custom test listener extends TestListenerAdapter for take screen shot on failed test case.
* @author wadexu
*
* @updateUser
* @updateDate
*/
public class CustomTestListener extends TestListenerAdapter { private static String fileSeperator = System.getProperty("file.separator");
protected static final Logger logger = Logger.getLogger(CustomTestListener.class); @Override
public void onTestFailure(ITestResult result) {
super.onTestFailure(result);
Object o = result.getInstance();
WebDriver driver = ((WebDriverHost) o).getWebDriver(); Method method = result.getMethod().getConstructorOrMethod().getMethod();
TakeScreenshotOnFailure tsc = method.getAnnotation(TakeScreenshotOnFailure.class); if (tsc != null) {
String testClassName = getTestClassName(result.getInstance().toString()).trim(); String testMethodName = result.getName().toString().trim();
String screenShotName = testMethodName + ".png"; if (driver != null) {
String imagePath =
".." + fileSeperator + "Screenshots" + fileSeperator + "Results"
+ fileSeperator + testClassName + fileSeperator
+ takeScreenShot(driver, screenShotName, testClassName);
logger.info("Screenshot can be found : " + imagePath);
}
}
} /**
* Get screen shot as a file and copy to a new target file
*
* @Title: takeScreenShot
* @param driver
* @param screenShotName
* @param testClassName
* @return screenShotName
*/
public static String takeScreenShot(WebDriver driver,
String screenShotName, String testClassName) {
try {
File file = new File("Screenshots" + fileSeperator + "Results");
if (!file.exists()) {
logger.info("File created " + file);
file.mkdir();
} File screenshotFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
File targetFile = new File("Screenshots" + fileSeperator + "Results" + fileSeperator + testClassName, screenShotName);
FileUtils.copyFile(screenshotFile, targetFile); return screenShotName;
} catch (Exception e) {
logger.error("An exception occured while taking screenshot " + e.getCause());
return null;
}
} /**
* Get a test class name
*
* @Title: getTestClassName
* @param testName
* @return testClassName
*/
public String getTestClassName(String testName) {
String[] reqTestClassname = testName.split("\\.");
int i = reqTestClassname.length - 1;
String testClassName = reqTestClassname[i].substring(0, reqTestClassname[i].indexOf("@"));
logger.info("Required Test Name : " + testClassName);
return testClassName;
} }

接口类 WebDriverHost

package test.demo;

import org.openqa.selenium.WebDriver;

/**
* Class implementing that interface is expected to return instance of web driver used in last test method.
*
* @author wadexu
*/
public interface WebDriverHost { /**
* Returns instance of web driver used in last test method.
*
* @return WebDriver
*/
public WebDriver getWebDriver();
}

##转载注明出处: http://www.cnblogs.com/wade-xu/p/4861024.html

测试基类TestBase

package test.demo;

import java.io.File;
import java.io.IOException; import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass; /**
* @Description: Abstract test base class, Test class must extends this class.
* Initialize a firefox webDriver for tutorial purpose only.
* @author wadexu
*
* @updateUser
* @updateDate
*/
public abstract class TestBase implements WebDriverHost{ protected WebDriver webDriver = null; @Override
public WebDriver getWebDriver() {
return webDriver;
} @BeforeClass
public void setUp() throws IOException {
FirefoxProfile firefoxProfile = new FirefoxProfile();
// use proxy
firefoxProfile.setPreference("network.proxy.type", 1);
firefoxProfile.setPreference("network.proxy.http", "10.51.1.000");
firefoxProfile.setPreference("network.proxy.http_port", "8080"); //get rid of google analytics, otherwise it's too slow to access to cnblogs website
firefoxProfile.addExtension(new File(getClass().getClassLoader().getResource("no_google_analytics-0.6-an+fx.xpi").getFile())); webDriver = new FirefoxDriver(firefoxProfile);
} @AfterClass
public void tearDown() {
webDriver.close();
} }

DemoListenerTest 测试类

package test.demo;

import static org.testng.Assert.assertTrue;

import org.testng.annotations.Test;

/**
* @Description: Demo a Test using custom Test Listener with an annotation TakeScreenshotOnFailure
* @author wadexu
*
* @updateUser
* @updateDate
*/
public class DemoListenerTest extends TestBase{ @Test
@TakeScreenshotOnFailure
public void testFail() {
webDriver.get("http://www.cnblogs.com/"); //Fail this test method for tutorial purpose only
assertTrue(false);
} }

TestNG 的 xml 文件 配置如下,加入listener监听

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<!--<suite name="DMP_Test_Suite" -->
<suite name="Demo_Test_Suite" parallel="false">
<listeners>
<listener class-name="test.demo.CustomTestListener" />
</listeners>
<test name="Demo_Test">
<classes>
<class name="test.demo.DemoListenerTest" />
</classes>
</test>
</suite>

##转载注明出处: http://www.cnblogs.com/wade-xu/p/4861024.html

Run as TestNG

断言故意错误 让test fail

控制台输出

2015-10-08 15:04:29,896 INFO [test.demo.CustomTestListener] - Required Test Name : DemoListenerTest
2015-10-08 15:04:31,985 INFO [test.demo.CustomTestListener] - Screenshot can be found : ..\Screenshots\Results\DemoListenerTest\testFail.png

图片以测试方法命名, 存在含类名的路径下

测试方法Pass 或者 fail但没加Annotation 都不会截屏。

感谢阅读,如果您觉得本文的内容对您的学习有所帮助,您可以点击右下方的推荐按钮,您的鼓励是我创作的动力。

##转载注明出处: http://www.cnblogs.com/wade-xu/p/4861024.html

基于WebDriver&TestNG 实现自己的Annotation @TakeScreenshotOnFailure的更多相关文章

  1. 基于webdriver的jmeter性能测试-通过jmeter实现jar录制脚本的性能测试

    续接--基于webdriver的jmeter性能测试-Eclipse+Selenium+JUnit生成jar包 在进行测试前先将用于支持selenium录制脚本运行所需的类包jar文件放到jmeter ...

  2. linux搭建phantomjs+webdriver+testng+ant自动化工程

    因为项目的原因,需要将脚本在linux环境无浏览器化去跑,那么原有的在windows系统下有浏览器化的自动化脚本场景就不适用了,这里给出linux系统下搭建phantomjs+webdriver+te ...

  3. selenium webdriver testng自动化测试数据驱动

    selenium webdriver testng自动化测试数据驱动 selenium webdriver testng自动化测试数据驱动 一.数据驱动测试概念 数据驱动测试是相同的测试脚本使用不同的 ...

  4. 基于 webdriver 的测试代码日常调试方python 篇

    看到论坛有人写了JAVA的测试代码日常设计,就给大家分享一下偶自己平时是如何测试测试代码的.主要基于python语言.基于 webdriver 的日常调试在 python交互模式下非常方便,打开pyt ...

  5. nightwatch 基于Webdriver的端到端自动化测试框架

    nightwatch 是使用nodejs编写的,基于Webdriver api 的端到端自动化测试框架 包含以下特性 清晰的语法,基于js 以及css 还有xpath 的选择器 内置测试runner, ...

  6. Guiceberry+Webdriver+TestNG

    1. Guiceberry Leverage Guice to achieve Dependency Injection on your JUnit tests https://code.google ...

  7. Selenium WebDriver TestNg Maven Eclipse java 简单实例

    环境准备 前提条件Eclipse 已经安装过 TestNg ,Maven 插件 新建一个普通的java项目 点击右键 configure->convert to Maven Project 之后 ...

  8. Webdriver+testNG+ReportNG+Maven+SVN+Jenkins自动化测试框架的pom.xml配置

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  9. Webdriver+Testng实现测试用例失败自动截图功能

    testng执行测试用例的时候,如果用例执行失败会自动截图,方便后续排查问题 1.首先定义一个截图类: package com.rrx.utils; import java.io.File;impor ...

随机推荐

  1. .NET涉及的一些名词

    本文在最为概略的层次上对.NET涉及的一些名词进行解释, 包括: 通用语言基础架构(Common Language Infrastructure, CLI). 虚拟执行系统(Virtual Execu ...

  2. bash 取文件特定行

    比如,想要取某文件10-20行 可以用sed sed -n '10,20p' XXX.txt 非常方便!

  3. 新冲刺Sprint3(第六天)

    一.Sprint介绍 商家功能模块继续完善着,加快了工作的步伐. 二.Sprint周期 看板: 燃尽图:

  4. 备受SQL青睐的“1”

    写在前面:所用sql语句皆是在oracle 11g r1 数据库中实验. 在sql书写中,经常会用到数字1,例如 count(1),select 1, where 1=1等等,这样做有何好处呢?下面我 ...

  5. LintCode Find Minimum In Rotated Sorted Array

    1. 画图, 直观. 2. 讨论数组为空或者个数为零. 3. 讨论首尾, 若为翻转过的则进行查找直到最后两个数进行比较, 取小者. public class Solution { /** * @par ...

  6. centos 6.8 安装 nginx-1.11.4

    yum -y install gcc-c++ wget http://nginx.org/download/nginx-1.11.4.tar.gz wget  https://www.openssl. ...

  7. html中的空格显示问题

    像这种,从后台查询出来的数据中间有好几个空格,但在页面上显示的时候就只剩一个空格了,这种显示肯定是不合适的,相关的html代码如下: <c:forEach items="${list} ...

  8. 在项目中导入MRC的文件时解决办法

    1.由于在项目中要使用到第三方框架和其他的类的时候,而它用的是MRC的时候,其最简便的方法:完成从MRC到ARC的转换. 1.点击工程文件,进入到工程的设置里面. 2.看见Build Phases,就 ...

  9. AJAX跨域

    AJAX跨域AJAX不允许跨域访问. 跨域是指浏览器B显示的是服务器S1的数据,全是从S1取得的数据则是同域:但如果B显示的S1上的数据的某个比如img是从S2上取得的数据,则是跨域.端口不一样也是跨 ...

  10. CSS背景色渐变

      试了下 渐变色  ,主要确定开始位置  结束位置,以及对应的color-stop;  以下是兼容不同浏览器的代码片段       代码:<style type="text/css& ...