哈哈哈,很久没写博客了,懒了。

  因为一些原因最近需要把监听事件重新整理一下,开始没细想,直接copy网上的,其实结果发现报错很多,或者是达不到效果,然后把之前的代码翻出来,仔细看了一下。下面给一些需要的小伙伴整理一下思路:

  1、首先我们用到的是testng里面的监听,所以这个毋庸置疑

  2、我们需要重新他的监听事件

  3、用例中肯定需要加入监听事件

这三点是网上公认的,但是怎么做确没有说明白。

  首先看一下我的目录结构:

图片中我有4个类,

1、driverBase是一个基类,里面是driver的初始化,截图的方法也在里面。

2、selectDriver是根据浏览器不同返回不同的driver

3、testngListenerScreen是testng的监听类,他继承了TestListenerAdapter类,把里面的方法进行了重写。主要他会去监听你测试类是否报错,如果报错他就会去调用我们driverbase下面的截图方法。

4、testlogin 不用说,我们的测试类,他需要继承我们的driverBase基类,去拿里面的driver。

这个说完了先看代码,首先从我们基类开始:

package com.mushishi.selenium.base;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;
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;

/**
 * @author mushishi
 * */
public class driverBase {
	public WebDriver driver;

	/**
	 * 获取driver
	 * */
	public WebDriver getDriver() {
        return driver;
    }

	/**
	 * 自动截图
	 * */
    public void takeScreenShot() {
        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() + "_" + dateStr + ".png";
    //因为我们截图是需要用到driver的,所以这里需要获取driver,这个driver是获取的当前对象的driver takeScreenShot((TakesScreenshot) this.getDriver(), path); } /** * 传入参数截图 * */ public void takeScreenShot(TakesScreenshot drivername, String path) { String currentPath = System.getProperty("user.dir"); // get current work File scrFile = drivername.getScreenshotAs(OutputType.FILE); try { FileUtils.copyFile(scrFile, new File(currentPath + "\\" + path)); } catch (Exception e) { e.printStackTrace(); } finally { System.out.println("截图成功"); } } }

  上面的代码,可能有那么一两个地方看不懂,都没关系,我写备注了。主要是以类名字和时间组合起来命名,并且把当前运行的对象的driver获取到,用来截图操作。

下面看selectDriver的类:

package com.mushishi.selenium.base;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class SelectDriver {
	public WebDriver driverName(String browser){
		if(browser.equalsIgnoreCase("fireFox")){
              //设置环境变量,并且返回driver System.setProperty("webdriver.firefox.marionette","D:\\java\\geckodriver\\geckodriver-v0.14.0-win64\\geckodriver.exe"); return new FirefoxDriver(); }else{ System.setProperty("webdriver.chrome.driver", "D:\\java\\chromedriver_win32\\chromedriver.exe"); return new ChromeDriver(); } } }

  上面的没什么可看的,就是根据传入的对象生成一个driver,这个我用的3.0的selenium,所以需要这么设置。这个根据自己情况。

下面看监听类的: 1 package com.mushishi.selenium.base;


 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);
     }
 
    //主要是用到这个方法了,当你报错时他会监听到,然后就会执行截图操作,这里的 ITestresult tr是testng里的,获取到的是当前运行对象,你这样理解,他就是
//我当前运行的类这个对象 @Override public void onTestFailure(ITestResult tr) { super.onTestFailure(tr); System.out.println(tr); takeScreenShot(tr); }
//所以这里我执行截图的时候就获取到了我运行过程中实力对象的driver了,懂了吧。所以当我再回去调用我基类的对象时,那么driver就是同一个了 private void takeScreenShot(ITestResult tr) { driverBase b = (driverBase) tr.getInstance(); // driver = b.driver; b.takeScreenShot(); } @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); } }

最后一个类,测试类:

 package com.mushishi.selenium.testCase;

 import org.openqa.selenium.By;
 import org.openqa.selenium.WebElement;
 import org.openqa.selenium.interactions.Actions;
 import org.testng.annotations.Listeners;
 import org.testng.annotations.Test;

 import com.mushishi.selenium.ProUtil;
 import com.mushishi.selenium.base.SelectDriver;
 import com.mushishi.selenium.base.TestNGListenerScreen;
 import com.mushishi.selenium.base.driverBase;

 @Listeners({ TestNGListenerScreen.class })
 public class testLogin extends driverBase {
     SelectDriver select = new SelectDriver();
     public testLogin(){
         if(driver == null){
         driver = select.driverName("chrome");
         }
     }
     public void loginScript(String username,String userpass) throws Exception{
         String url = "http://www.imooc.com";
         driver.get(url);
         driver.manage().window().maximize();
         driver.findElement(By.id("js-signin-btn")).click();
         Thread.sleep(2000);
         WebElement user = this.element(this.byStr("username"));
         user.isDisplayed();
         WebElement password = this.element(this.byStr("userpass"));
         password.isDisplayed();
         WebElement loginButton = this.element(this.byStr("loginbutton"));
         loginButton.isDisplayed();
         user.sendKeys(username);
         password.sendKeys(userpass);
         loginButton.click();
         Thread.sleep(3000);
         WebElement header = this.element(this.byStr("header"));
         header.isDisplayed();
         Actions actions = new Actions(driver);
         actions.moveToElement(header).perform();
         String userInfo = this.element(this.byStr("nameInfo")).getText();
         System.out.println(userInfo);
         if(userInfo.equals("xxxx")){
             System.out.println("登陆成功");
         }else{
             System.out.println("登陆失败");

         }
         driver.close();
     }

     /**
      * 封装By by
      * */
     public By byStr(String username){
         ProUtil properties = new ProUtil("element.properties");
         String locator = properties.getPro(username);
         String locatorType = locator.split(">")[0];
         String locatorValue = locator.split(">")[1];
         if(locatorType.equals("id")){
             return By.id(locatorValue);
         }else if(locatorType.equals("name")){
             return By.name(locatorValue);
         }else if(locatorType.equals("className")){
             return By.className(locatorValue);
         }else{
             return By.xpath(locatorValue);
         }
     }

     /**
      * 封装Element
      * */
     public WebElement element(By by){
         WebElement ele = driver.findElement(by);
         return ele;
     }

     @Test
     public void loginpage() throws Exception{
         this.loginScript("xxxx", "xxxx");
     }
 }

这个是测试类,不知道你懂了么?因为我很多都封装了的,所以看不到,但是不影响大家理解testng自动截图思想。动手操作吧。

  

selenium 利用testNG对异常进行自动截图的更多相关文章

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

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

  2. Selenium通过监听事件实现自动截图

    需要继承extends TestListenerAdapter这个类 代码如下package com.mushishi.selenium.util; import java.util.ArrayLis ...

  3. TestNG失败用例自动截图

    参考:https://blog.csdn.net/wangxin1982314/article/details/50247245 1. 首先写一个截屏方法 public class ScreenSho ...

  4. Selenium2+python自动化67-用例失败自动截图【转载】

    前言: 装饰器其实就是一个以函数作为参数并返回一个替换函数的可执行函数 上一篇讲到用装饰器解决异常后自动截图,不过并没有与unittest结合,这篇把截图的装饰器改良了下,可以实现用例执行失败自动截图 ...

  5. selenium遇到异常自动截图

    最近要在框架中添加case失败时,要自动截图,主要又两种方式,思想都是在抛异常的时候,捕获到异常,并作页面截图处理.今天坐下总结. 一.第一种方式,重写onException方法 只针对webdriv ...

  6. testng 失败自动截图

    testng执行case failed ,testng Listener会捕获执行失败,如果要实现失败自动截图,需要重写Listener的onTestFailure方法 那么首先新建一个Listene ...

  7. 使用appium和testng实现Android自动截图

    简单介绍 需求场景是:当测试安卓应用的脚本得到失败结果时,对当前手机屏幕截图,便于查找问题. 实现方式是:1)定义一个父类UITest,作为所有测试类的父类.在父类中UITest中定义一个截图的方法, ...

  8. TestNG监听器实现用例运行失败自动截图、重运行功能

    注: 以下内容引自 http://blog.csdn.net/sunnyyou2011/article/details/45894089 (此非原出处,亦为转载,但博主未注明原出处) 使用Testng ...

  9. IDEA+Java:Selenium+Maven+TestNG基本WebUI自动化测试环境搭建

    IDEA+java:Selenium+Maven+TestNG 本文介绍的测试环境,应该是最基本的测试环境了,也是很多文章都有写,这里做一个完整的图文配置整理,方便阅读理解! 使用maven的好处,由 ...

随机推荐

  1. Visual Studio 2017 新特性

    全新的安装体检 VS2017更好的支持了按需安装的特点,用户可以仅选择需要的功能安装,节省了不少的Disk 最小的安装仅有几百兆,但也支持20多种编程语言的编辑和源码管理 支持创建自定义的离线安装包 ...

  2. Windows 10 IoT Serials 7 – 如何用树莓派制作家庭流媒体播放器

    Windows 10平台引入了AllJoyn开源软件框架,它提供了一组服务可以创建动态近端网络,让设备可以相互连接实现功能交互.目前,AllJoyn开源软件框架由AllSeen联盟负责管理.AllSe ...

  3. Previous operation has not finished; run 'cleanup' if it was interrupted

    在使用myeclipse的时候,点击保存的时候,控制台窗口总是弹出这个svn :Previous operation has not finished; run 'cleanup' if it was ...

  4. ng2响应式表单-翻译与概括官网REACTIVE FORMS页面

    本文将半翻译半总结的讲讲ng2官网的另一个未翻译高级教程页面. 原文地址. 文章目的是使用ng2提供的响应式表单技术快速搭出功能完善丰富的界面表单组件. 响应式表单是一项响应式风格的ng2技术,本文将 ...

  5. 分块编码(Transfer-Encoding: chunked)

    参考链接: HTTP 协议中的 Transfer-Encoding 分块传输编码 一.背景: 持续连接的问题:对于非持续连接,浏览器可以通过连接是否关闭来界定请求或响应实体的边界:而对于持续连接,这种 ...

  6. JavaScript对象的深浅复制

    前言 从层次上来看,对象的复制可以简单地分为浅复制和深复制,顾名思义,浅复制是指只复制一层对象的属性,不会复制对象中的对象的属性,对象的深复制会复制对象中层层嵌套的对象的属性. 在复制对象时,除了要复 ...

  7. css中的inline-block

    div { display: inline-block; *display: inline; *zoom: 1; } Basic Support包含值:none | inline | block | ...

  8. Luogu2723丑数Humble Numbers【归并排序】

    Luogu2723丑数Humble Numbers 题目背景 对于一给定的素数集合 S = {p1, p2, ..., pK},考虑一个正整数集合,该集合中任一元素的质因数全部属于S.这个正整数集合包 ...

  9. POPTEST老李分享修改dns ip的vbs代码

    POPTEST老李分享修改dns ip的vbs代码   poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣,请大家咨 ...

  10. 老李分享:Uber究竟是用什么开发语言?

    poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣,请大家咨询qq:908821478,咨询电话010-845052 ...