zoukankan      html  css  js  c++  java
  • selenium之如何监听断言

    一.概述

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

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

    我们需要思考的问题,该怎么解决呢,其实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
  • 相关阅读:
    WPF Template模版之DataTemplate与ControlTemplate【一】
    C#中的几种锁:用户模式锁、内核模式锁、动态计数、监视锁
    WPF 基础面试题及答案(一)
    .net core 微服务参考文章
    Encoder-Decoder for Trajectory Prediction [closed]
    Prediction of Pedestrian Trajectory in a Crowded Environment Using RNN Encoder-Decoder
    Encoder-Decoder LSTM for Trajectory Prediction
    How to Develop an Encoder-Decoder Model for Sequence-to-Sequence Prediction in Kera
    Social LSTM using PyTorch for Vehicle Data
    Social LSTM implementation in PyTorch
  • 原文地址:https://www.cnblogs.com/liwu/p/5048698.html
Copyright © 2011-2022 走看看