首页 > 代码库 > selenium自动化-java-封断言类2
selenium自动化-java-封断言类2
封装断言类
1 package streamax.com; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import org.testng.Assert; 7 /* 8 * 断言类 9 * 被調用判斷 10 * 11 */ 12 public class Assertion { 13 14 public static boolean flag = true; 15 16 public static List<Error> errors = new ArrayList<Error>(); 17 18 public static void verifyEquals(Object actual, Object expected){ 19 try{ 20 Assert.assertEquals(actual, expected); 21 }catch(Error e){ 22 errors.add(e); 23 flag = false; 24 } 25 } 26 27 public static void verifyEquals(Object actual, Object expected, String message){ 28 try{ 29 Assert.assertEquals(actual, expected, message); 30 }catch(Error e){ 31 errors.add(e); 32 flag = false; 33 } 34 } 35 36 }
2 监听类
1 package streamax.com; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import org.testng.ITestResult; 7 import org.testng.TestListenerAdapter; 8 /* 9 * 监听类 10 */ 11 public class AssertionListener extends TestListenerAdapter { 12 13 @Override 14 public void onTestStart(ITestResult result) { 15 Assertion.flag = true; 16 Assertion.errors.clear(); 17 } 18 19 @Override 20 public void onTestFailure(ITestResult tr) { 21 this.handleAssertion(tr); 22 } 23 24 @Override 25 public void onTestSkipped(ITestResult tr) { 26 this.handleAssertion(tr); 27 } 28 29 @Override 30 public void onTestSuccess(ITestResult tr) { 31 this.handleAssertion(tr); 32 } 33 34 private int index = 0; 35 36 private void handleAssertion(ITestResult tr){ 37 if(!Assertion.flag){ 38 Throwable throwable = tr.getThrowable(); 39 if(throwable==null){ 40 throwable = new Throwable(); 41 } 42 StackTraceElement[] traces = throwable.getStackTrace(); 43 StackTraceElement[] alltrace = new StackTraceElement[0]; 44 for (Error e : Assertion.errors) { 45 StackTraceElement[] errorTraces = e.getStackTrace(); 46 StackTraceElement[] et = this.getKeyStackTrace(tr, errorTraces); 47 StackTraceElement[] message = new StackTraceElement[]{new StackTraceElement("message : "+e.getMessage()+" in method : ", tr.getMethod().getMethodName(), tr.getTestClass().getRealClass().getSimpleName(), index)}; 48 index = 0; 49 alltrace = this.merge(alltrace, message); 50 alltrace = this.merge(alltrace, et); 51 } 52 if(traces!=null){ 53 traces = this.getKeyStackTrace(tr, traces); 54 alltrace = this.merge(alltrace, traces); 55 } 56 throwable.setStackTrace(alltrace); 57 tr.setThrowable(throwable); 58 Assertion.flag = true; 59 Assertion.errors.clear(); 60 tr.setStatus(ITestResult.FAILURE); 61 } 62 } 63 64 private StackTraceElement[] getKeyStackTrace(ITestResult tr, StackTraceElement[] stackTraceElements){ 65 List<StackTraceElement> ets = new ArrayList<StackTraceElement>(); 66 for (StackTraceElement stackTraceElement : stackTraceElements) { 67 if(stackTraceElement.getClassName().equals(tr.getTestClass().getName())){ 68 ets.add(stackTraceElement); 69 index = stackTraceElement.getLineNumber(); 70 } 71 } 72 StackTraceElement[] et = new StackTraceElement[ets.size()]; 73 for (int i = 0; i < et.length; i++) { 74 et[i] = ets.get(i); 75 } 76 return et; 77 } 78 79 private StackTraceElement[] merge(StackTraceElement[] traces1, StackTraceElement[] traces2){ 80 StackTraceElement[] ste = new StackTraceElement[traces1.length+traces2.length]; 81 for (int i = 0; i < traces1.length; i++) { 82 ste[i] = traces1[i]; 83 } 84 for (int i = 0; i < traces2.length; i++) { 85 ste[traces1.length+i] = traces2[i]; 86 } 87 return ste; 88 } 89 }
selenium自动化-java-封断言类2
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。