码迷,mamicode.com
首页 > 其他好文 > 详细

selenium之如何监听断言

时间:2015-12-15 16:53:50      阅读:280      评论:0      收藏:0      [点我收藏+]

标签:

一.概述

     测试用的执行通过与否,需要对比期望结果和实际结果的值,因此在自动化测试中我们需要进行断言来获得执行的结果,

那么如果在执行的过程中有异常,该怎么办呢,我们是否要查看异常信息呢,于是乎如何得到我们所需要的异常信息成为

我们需要思考的问题,该怎么解决呢,其实so eazy!tesgNG的测试用例监听器可以帮助我们解决这个问题。

二.监听断言类AssertionListener

技术分享
  1 public class AsserttionListener extends TestListenerAdapter{
  2     
  3     private Log log=new Log(this.getClass());
  4 
  5     @Override
  6     public void onTestStart(ITestResult result) {
  7         // TODO Auto-generated method stub
  8         Assertion.begin();
  9     }
 10 
 11     @Override
 12     public void onTestFailure(ITestResult tr) {
 13         // TODO Auto-generated method stub
 14         this.handleAssertion(tr);
 15     }
 16 
 17     @Override
 18     public void onTestSkipped(ITestResult tr) {
 19         // TODO Auto-generated method stub
 20         this.handleAssertion(tr);
 21     }
 22 
 23     @Override
 24     public void onTestSuccess(ITestResult tr) {
 25         // TODO Auto-generated method stub
 26         this.handleAssertion(tr);
 27     }
 28     
 29     private int index;
 30     
 31     private boolean isNew=false;
 32     
 33     /**
 34      * 得到测试类所需的测试异常信息
 35      * @param tr
 36      */
 37     private void handleAssertion(ITestResult tr){
 38         Throwable throwable=tr.getThrowable();
 39         if(!Assertion.flag||throwable!=null){
 40             if(throwable==null){
 41                 throwable=new Throwable();
 42                 isNew=true;
 43             }
 44             StackTraceElement[] alltrace=new StackTraceElement[0];
 45             for (Error e : Assertion.errors) {
 46                 alltrace=this.getAllStackTraceElement(tr, e, null, alltrace);
 47             }
 48             if(!isNew){
 49                 alltrace=this.getAllStackTraceElement(tr, null, throwable, alltrace);
 50             }else{
 51                 isNew=false;
 52             }
 53             throwable.setStackTrace(alltrace);
 54             tr.setThrowable(throwable);
 55             Assertion.flag=true;
 56             Assertion.errors.clear();
 57             tr.setStatus(ITestResult.FAILURE);
 58         }
 59     }
 60 
 61     /**
 62      * 得到测试类需要的StackTraceElement数组
 63      * @param tr
 64      * @param e
 65      * @param throwable
 66      * @param alltrace
 67      * @return
 68      */
 69     private StackTraceElement[] getAllStackTraceElement(ITestResult tr,Error e,Throwable throwable,StackTraceElement[] alltrace){
 70         StackTraceElement[] traces=(e==null?throwable.getStackTrace():e.getStackTrace());
 71         StackTraceElement[] et=this.getKeyStackTrace(tr, traces);
 72         String msg=(e==null?throwable.getMessage():e.getMessage());
 73         StackTraceElement[] message=new StackTraceElement[]{new StackTraceElement("message : "+msg+" in method : ",tr.getMethod().getMethodName(),tr.getTestClass().getRealClass().getSimpleName(),index)};
 74         index=0;
 75         alltrace=this.merge(alltrace, et);
 76         alltrace=this.merge(alltrace, message);
 77         return alltrace;
 78     }
 79     
 80     /**
 81      * 根据测试类名获得该测试类的StackTraceElement数组
 82      * @param tr
 83      * @param stackTraceElements
 84      * @return
 85      */
 86     private StackTraceElement[] getKeyStackTrace(ITestResult tr,StackTraceElement[] stackTraceElements){
 87         List<StackTraceElement> ets=new ArrayList<StackTraceElement>();
 88         for (StackTraceElement stackTraceElement : ets) {
 89             if(stackTraceElement.getClassName().equals(tr.getTestClass().getName())){
 90                 ets.add(stackTraceElement);
 91                 index=stackTraceElement.getLineNumber();
 92             }
 93         }
 94         StackTraceElement[] et=new StackTraceElement[ets.size()];
 95         for (int i = 0; i < et.length; i++) {
 96             et[i]=ets.get(i);
 97         }
 98         return et;
 99     }
100     
101     /**
102      * 合并两个StackTraceElement数组
103      * @param trace1
104      * @param trace2
105      * @return
106      */
107     private StackTraceElement[] merge(StackTraceElement[] trace1,StackTraceElement[] trace2){
108         StackTraceElement[] ste=new StackTraceElement[trace1.length+trace2.length];
109         for (int i = 0; i < trace1.length; i++) {
110             ste[i]=trace1[i];
111         }
112         for (int i = 0; i < trace2.length; i++) {
113             ste[trace1.length+i]=trace2[i];
114         }
115         return ste;
116     }
View Code

 

技术分享
 1 public class Assertion {
 2     
 3     public static Log log=new Log(Assertion.class);
 4     
 5     public static boolean flag=true;
 6     
 7     public static List<Error> errors=new ArrayList<Error>();
 8     
 9     public static void begin(){
10         flag=true;
11     }
12     
13     public static void end(){
14         Assert.assertTrue(flag);
15     }
16     
17     public static void verifyEquals(Object actual, Object expected){
18         try{
19             Assert.assertEquals(actual, expected);
20         }catch(Error e){
21             errors.add(e);
22             flag = false;
23         }
24     }
25     
26     public static void verifyEquals(Object actual, Object expected, String message){
27         try{
28             Assert.assertEquals(actual, expected, message);
29             log.info("方法验证成功!");
30         }catch(Error e){
31             errors.add(e);
32             flag = false;
33             log.info("方法验证失败!");
34         }
35     }
36 
37 }
View Code

 

selenium之如何监听断言

标签:

原文地址:http://www.cnblogs.com/liwu/p/5048698.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!