男儿欲遂平生志,五经勤向窗前读。这篇文章主要讲述Appium失败截图及重试机制封装相关的知识,希望能为你提供帮助。
一、失败截图封装
1、主要封装了失败之后的文件名、重写了失败之后消息、失败了以后做个截图,最后置为失败,并且存放到相对路径下、截图操作,未把失败用例至为Fail,主要代码如下:
文章图片
文章图片
1 package cn.hysh.appium.testng; 2 3 import org.testng.Assert; 4 import org.testng.Reporter; 5 6 import cn.hysh.appium.base.androidDriverBase; 7 import cn.hysh.appium.base.hyshPath; 8 9 10 public class Assertion{ 11privateAndroidDriverBase driver; 12public Assertion(AndroidDriverBase driver){ 13this.driver=driver; 14} 15//注意断言的失败不是一个exception,而是一个error 16publicvoid assertEquals(Object actual, Object expected,String fileName){ 17try{ 18Assert.assertEquals(actual, expected); 19}catch(AssertionError e){ 20fail(fileName); 21} 22} 23 24publicvoid assertEquals(Object actual, Object expected, String fileName,String message){ 25try{ 26Assert.assertEquals(actual, expected, message); 27}catch(AssertionError e){ 28fail(fileName,message); 29} 30} 31 32publicvoid verifyEquals(Object actual, Object expected,String fileName){ 33try{ 34Assert.assertEquals(actual, expected); 35}catch(AssertionError e){ 36try { 37driver.takeScreen(hyshPath.path, "\images\"+Thread.currentThread().getId()+fileName); 38} catch (Exception e1) { 39e1.printStackTrace(); 40} 41} 42} 43publicvoid verifyEquals(Object actual, Object expected,String fileName,String message){ 44try{ 45Assert.assertEquals(actual, expected, message); 46}catch(AssertionError e){ 47try { 48driver.takeScreen(hyshPath.path, "\images\"+Thread.currentThread().getId()+fileName); 49} catch (Exception e1) { 50e1.printStackTrace(); 51} 52} 53} 54/** 55失败了以后做个截图,最后置为失败,并且存放到相对路径下 56*/ 57public void fail(String fileName){ 58try { 59Long date=System.currentTimeMillis(); 60System.out.println(date); 61driver.takeScreen("D:/autotest/images",Thread.currentThread().getId()+"_"+date+fileName); 62} catch (Exception e1) { 63e1.printStackTrace(); 64} 65Assert.fail(); 66} 67public void fail(String fileName,String message){ 68try { 69Long date=System.currentTimeMillis(); 70System.out.println(date); 71driver.takeScreen("D:/autotest/images",Thread.currentThread().getId()+"_"+date+fileName); 72} catch (Exception e1) { 73e1.printStackTrace(); 74} 75Assert.fail(message); 76} 77 }
View Code2、如何应用?主要代码如下:
文章图片
文章图片
1 public class AppTest { 2AndroidDriverBase driver; 3Assertion myAssert; 4@Parameters({"udid","port"}) 5@BeforeClass 6public void beforeClass(String udid,String port){ 7ProUtil p=new ProUtil(hyshPath.globalPath); 8String server=p.getPro("server"); 9String capsPath=hyshPath.capsPath; 10String input="com.example.android.softkeyboard/.SoftKeyboard"; 11try { 12driver=new AndroidDriverBase(server, port, capsPath, udid, input); 13myAssert=new Assertion(driver); 14driver.implicitlyWaitDefault(); 15} catch (Exception e) { 16SendMail sm=new SendMail(); 17sm.send("driver失败", e.getMessage()); 18} 19} 20@DataProvider 21public Object[][] loginData() throws Exception{ 22Object[][] ob=ExcelUtil.getTestData("C:\Users\Administrator\Desktop/test.xlsx", "Sheet1"); 23return ob; 24} 25@Test(dataProvider="loginData") 26public void t001_login(String caseNumber,String caseName,String username,String password,String assertValue,String filename) throws Exception{ 27ExcelUtil eu=new ExcelUtil("C:\Users\Administrator\Desktop/test.xlsx", "Sheet1"); 28Login l=new Login(driver); 29Home home=l.loginTest(username,password); 30try { 31myAssert.assertEquals(driver.getPageSouce().contains(assertValue), true, filename); 32eu.setCellData(Integer.valueOf(caseNumber), eu.getLastColumnNum(), "测试通过"); 33} catch (Error e) { 34eu.setCellData(Integer.valueOf(caseNumber), eu.getLastColumnNum(), "测试失败"); 35Assert.fail(e.getMessage()); 36} 37 38} 39@Test(enabled=false) 40public void t002_search(){ 41Home home=new Home(driver); 42Boolean flag=home.searchTest("test"); 43myAssert.assertEquals(flag==true, true, "searcherror"); 44 45} 46@Test(enabled=false) 47public void t003_attention(){ 48Articlear=new Article(driver); 49boolean result=ar.attention(); 50myAssert.assertEquals(result==true, true, "attentionerror"); 51} 52@Test(enabled=false) 53public void t004_personInfo(){ 54EditPersonInfo edinfo=new EditPersonInfo(driver); 55boolean flag=edinfo.modifyPersonInfo(); 56myAssert.assertEquals(flag, true, "personinfoerror"); 57System.out.println(1); 58 59} 60 61@AfterMethod 62public void restart(){ 63String appPackge=(String)driver.getCapabilities().getCapability(AndroidMobileCapabilityType.APP_PACKAGE); 64String appActivity=(String)driver.getCapabilities().getCapability(AndroidMobileCapabilityType.APP_ACTIVITY); 65driver.startActivity(appPackge,appActivity); 66} 67 68@AfterClass 69public void afterClass(){ 70driver.resetApp(); 71driver.quit(); 72} 73 74 }
View Code二、重试监听,主要应用了testng的监听机制
1、RetryListener实现了IAnnotationTransformer接口,做了判断,如果retry为null,就把retry设置为TestngRetry对象,主要代码如下:
文章图片
文章图片
1 import java.lang.reflect.Constructor; 2 import java.lang.reflect.Method; 3 4 import org.testng.IRetryAnalyzer; 5 import org.testng.annotations.ITestAnnotation; 6 import org.testng.internal.annotations.IAnnotationTransformer; 7 8 9 public class RetryListener implements IAnnotationTransformer { 10 11@Override 12public void transform(ITestAnnotation annotation, Class testClass, 13Constructor testConstructor, Method testMethod) { 14IRetryAnalyzer retry = annotation.getRetryAnalyzer(); 15if (retry == null) { 16annotation.setRetryAnalyzer(TestngRetry.class); 17} 18} 19 }
View Code2、此外还要TestNGRetry实现IRetryAnalyzer接口,主要重写了重试次数(最大的重试次数),主要代码如下:
文章图片
文章图片
1 import org.testng.IRetryAnalyzer; 2 import org.testng.ITestResult; 3 import org.testng.Reporter; 4 5 public class TestngRetry implements IRetryAnalyzer { 6//private static Logger logger = Logger.getLogger(TestngRetry.class); 7private int retryCount = 1; 8privateint maxRetryCount=3; 9@Override 10public boolean retry(ITestResult result) { 11if (retryCount < = maxRetryCount) { 12String message = "running retry for‘" + result.getName() + "‘ on class " + this.getClass().getName() + " Retrying " 13+ retryCount + " times"; 14//logger.info(message); 15Reporter.setCurrentTestResult(result); 16Reporter.log("RunCount=" + (retryCount + 1)); 17retryCount++; 18return true; 19} 20return false; 21} 22 23 }
View Code3、如何应用?代码如下:
1 public class RetryTest { 2 3@Test(retryAnalyzer=TestngRetry.class) 4public void test(){ 5System.out.println("xx"); 6Assert.fail(); 7} 8 }
4、重写了TestNG监听类,主要是测试类失败后,成功的、跳过的、开始、完成的(重试之后报告的整理)重写,主要代码如下:
文章图片
文章图片
1 import java.text.SimpleDateFormat; 2 import java.util.ArrayList; 3 import java.util.Arrays; 4 import java.util.Date; 5 import java.util.HashSet; 6 import java.util.Iterator; 7 import java.util.Set; 8 9 import org.testng.ITestContext; 10 import org.testng.ITestResult; 11 import org.testng.Reporter; 12 import org.testng.TestListenerAdapter; 13 import org.testng.log4testng.Logger; 14 15 public class TestngListener extends TestListenerAdapter { 16private static Logger logger = Logger.getLogger(TestngListener.class); 17public static final String CONFIG = "config.properties"; 18 19@Override 20public void onTestFailure(ITestResult tr) { 21super.onTestFailure(tr); 22logger.info(tr.getName() + " Failure"); 23} 24 25@Override 26public void onTestSkipped(ITestResult tr) { 27super.onTestSkipped(tr); 28logger.info(tr.getName() + " Skipped"); 29} 30 31@Override 32public void onTestSuccess(ITestResult tr) { 33super.onTestSuccess(tr); 34logger.info(tr.getName() + " Success"); 35} 36 37@Override 38public void onTestStart(ITestResult tr) { 39super.onTestStart(tr); 40logger.info(tr.getName() + " Start"); 41} 42/** 43成功之后测试报告整理 44*/ 45@Override 46public void onFinish(ITestContext testContext) { 47super.onFinish(testContext); 48ArrayList< ITestResult> testsToBeRemoved = new ArrayList< ITestResult> (); 49Set< Integer> passedTestIds = new HashSet< Integer> (); 50for (ITestResult passedTest : testContext.getPassedTests().getAllResults()) { 51logger.info("PassedTests = " + passedTest.getName()); 52passedTestIds.add(getId(passedTest)); 53} 54 55Set< Integer> failedTestIds = new HashSet< Integer> (); 56for (ITestResult failedTest : testContext.getFailedTests().getAllResults()) { 57logger.info("failedTest = " + failedTest.getName()); 58int failedTestId = getId(failedTest); 59 60if (failedTestIds.contains(failedTestId) || passedTestIds.contains(failedTestId)) { 61testsToBeRemoved.add(failedTest); 62} else { 63failedTestIds.add(failedTestId); 64} 65} 66 67for (Iterator< ITestResult> iterator = testContext.getFailedTests().getAllResults().iterator(); iterator.hasNext(); ) { 68ITestResult testResult = iterator.next(); 69if (testsToBeRemoved.contains(testResult)) { 70logger.info("Remove repeat Fail Test: " + testResult.getName()); 71iterator.remove(); 72} 73} 74 75 76} 77private int getId(ITestResult result) { 78int id = result.getTestClass().getName().hashCode(); 79id = id + result.getMethod().getMethodName().hashCode(); 80id = id + (result.getParameters() != null ? Arrays.hashCode(result.getParameters()) : 0); 81return id; 82} 83 }
View Code5、如何应用?主要代码如下:
1 @Listeners(TestngListener.class) 2 public class RetryTest { 3 4@Test(retryAnalyzer=TestngRetry.class) 5public void test(){ 6System.out.println("xx"); 7Assert.fail(); 8} 9 }
【Appium失败截图及重试机制封装】
推荐阅读
- 3-物联网开发标配方案(APP程序介绍)
- appium常用封装
- Android Jetpack 组建介绍——Lifecycler
- wap2app-- wap2app的原生标题头无法隐藏
- XSLT xsl(apply-template元素)
- XPath语法
- XPath相对路径
- XPath节点
- XPath表达式