1. 程式人生 > >Appium失敗截圖及重試機制封裝(二)

Appium失敗截圖及重試機制封裝(二)

analyze ret boolean 做了 ktr assert public false fail

一、失敗截圖封裝

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 { 11 private AndroidDriverBase driver; 12 public Assertion(AndroidDriverBase driver){ 13 this.driver=driver; 14 } 15 //註意斷言的失敗不是一個exception,而是一個error 16 public void assertEquals(Object actual, Object expected,String fileName){
17 try{ 18 Assert.assertEquals(actual, expected); 19 }catch(AssertionError e){ 20 fail(fileName); 21 } 22 } 23 24 public void assertEquals(Object actual, Object expected, String fileName,String message){ 25 try
{ 26 Assert.assertEquals(actual, expected, message); 27 }catch(AssertionError e){ 28 fail(fileName,message); 29 } 30 } 31 32 public void verifyEquals(Object actual, Object expected,String fileName){ 33 try{ 34 Assert.assertEquals(actual, expected); 35 }catch(AssertionError e){ 36 try { 37 driver.takeScreen(hyshPath.path, "\\images\\"+Thread.currentThread().getId()+fileName); 38 } catch (Exception e1) { 39 e1.printStackTrace(); 40 } 41 } 42 } 43 public void verifyEquals(Object actual, Object expected,String fileName,String message){ 44 try{ 45 Assert.assertEquals(actual, expected, message); 46 }catch(AssertionError e){ 47 try { 48 driver.takeScreen(hyshPath.path, "\\images\\"+Thread.currentThread().getId()+fileName); 49 } catch (Exception e1) { 50 e1.printStackTrace(); 51 } 52 } 53 } 54 /** 55 失敗了以後做個截圖,最後置為失敗,並且存放到相對路徑下 56 */ 57 public void fail(String fileName){ 58 try { 59 Long date=System.currentTimeMillis(); 60 System.out.println(date); 61 driver.takeScreen("D:/autotest/images",Thread.currentThread().getId()+"_"+date+fileName); 62 } catch (Exception e1) { 63 e1.printStackTrace(); 64 } 65 Assert.fail(); 66 } 67 public void fail(String fileName,String message){ 68 try { 69 Long date=System.currentTimeMillis(); 70 System.out.println(date); 71 driver.takeScreen("D:/autotest/images",Thread.currentThread().getId()+"_"+date+fileName); 72 } catch (Exception e1) { 73 e1.printStackTrace(); 74 } 75 Assert.fail(message); 76 } 77 }
View Code

2、如何應用?主要代碼如下:

技術分享圖片
 1 public class AppTest {
 2     AndroidDriverBase driver;
 3     Assertion myAssert;
 4     @Parameters({"udid","port"})
 5     @BeforeClass
 6     public void beforeClass(String udid,String port){
 7         ProUtil p=new ProUtil(hyshPath.globalPath);
 8         String server=p.getPro("server");
 9         String capsPath=hyshPath.capsPath;
10         String input="com.example.android.softkeyboard/.SoftKeyboard";
11         try {
12             driver=new AndroidDriverBase(server, port, capsPath, udid, input);
13             myAssert=new Assertion(driver);
14             driver.implicitlyWaitDefault();
15         } catch (Exception e) {
16             SendMail sm=new SendMail();
17             sm.send("driver失敗", e.getMessage());
18         }
19     }
20     @DataProvider
21     public Object[][] loginData() throws Exception{
22         Object[][] ob=ExcelUtil.getTestData("C:\\Users\\Administrator\\Desktop/test.xlsx", "Sheet1");
23         return ob;
24     }
25     @Test(dataProvider="loginData")
26     public void t001_login(String caseNumber,String caseName,String username,String password,String assertValue,String filename) throws Exception{
27         ExcelUtil eu=new ExcelUtil("C:\\Users\\Administrator\\Desktop/test.xlsx", "Sheet1");
28         Login l=new Login(driver);
29         Home home=l.loginTest(username,password);
30         try {
31             myAssert.assertEquals(driver.getPageSouce().contains(assertValue), true, filename);
32             eu.setCellData(Integer.valueOf(caseNumber), eu.getLastColumnNum(), "測試通過");
33         } catch (Error e) {
34             eu.setCellData(Integer.valueOf(caseNumber), eu.getLastColumnNum(), "測試失敗");
35             Assert.fail(e.getMessage());
36         }
37         
38     }
39     @Test(enabled=false)
40     public void t002_search(){
41         Home home=new Home(driver);
42         Boolean flag=home.searchTest("test");
43         myAssert.assertEquals(flag==true, true, "searcherror");
44 
45     }
46     @Test(enabled=false)
47     public void t003_attention(){
48         Article    ar=new Article(driver);
49         boolean result=ar.attention();
50         myAssert.assertEquals(result==true, true, "attentionerror");
51     }
52     @Test(enabled=false)
53     public void t004_personInfo(){
54         EditPersonInfo edinfo=new EditPersonInfo(driver);
55         boolean flag=edinfo.modifyPersonInfo();
56         myAssert.assertEquals(flag, true, "personinfoerror");
57         System.out.println(1);
58         
59     }
60     
61     @AfterMethod
62     public void restart(){
63         String appPackge=(String)driver.getCapabilities().getCapability(AndroidMobileCapabilityType.APP_PACKAGE);
64         String appActivity=(String)driver.getCapabilities().getCapability(AndroidMobileCapabilityType.APP_ACTIVITY);
65         driver.startActivity(appPackge,appActivity);
66     }
67     
68     @AfterClass
69     public void afterClass(){
70         driver.resetApp();
71         driver.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
12     public void transform(ITestAnnotation annotation, Class testClass, 
13                     Constructor testConstructor, Method testMethod) {
14         IRetryAnalyzer retry = annotation.getRetryAnalyzer();
15         if (retry == null) {
16             annotation.setRetryAnalyzer(TestngRetry.class);
17         }        
18     }
19 }
View Code

2、此外還要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);
 7     private int retryCount = 1;
 8     private  int maxRetryCount=3;
 9     @Override
10     public boolean retry(ITestResult result) {
11         if (retryCount <= maxRetryCount) {
12             String message = "running retry for  ‘" + result.getName() + "‘ on class " + this.getClass().getName() + " Retrying "
13                     + retryCount + " times";
14             //logger.info(message);
15             Reporter.setCurrentTestResult(result);
16             Reporter.log("RunCount=" + (retryCount + 1));
17             retryCount++;
18             return true;
19         }
20         return false;
21     }
22 
23 }
View Code

3、如何應用?代碼如下:

1 public class RetryTest {
2     
3     @Test(retryAnalyzer=TestngRetry.class)
4     public void test(){
5         System.out.println("xx");
6         Assert.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 {
16     private static Logger logger = Logger.getLogger(TestngListener.class);
17     public static final String CONFIG = "config.properties";
18 
19     @Override
20     public void onTestFailure(ITestResult tr) {
21         super.onTestFailure(tr);
22         logger.info(tr.getName() + " Failure");
23     }
24 
25     @Override
26     public void onTestSkipped(ITestResult tr) {
27         super.onTestSkipped(tr);
28         logger.info(tr.getName() + " Skipped");
29     }
30 
31     @Override
32     public void onTestSuccess(ITestResult tr) {
33         super.onTestSuccess(tr);
34         logger.info(tr.getName() + " Success");
35     }
36 
37     @Override
38     public void onTestStart(ITestResult tr) {
39         super.onTestStart(tr);
40         logger.info(tr.getName() + " Start");
41     }
42     /**
43             成功之後測試報告整理
44     */
45     @Override
46     public void onFinish(ITestContext testContext) {
47         super.onFinish(testContext);
48         ArrayList<ITestResult> testsToBeRemoved = new ArrayList<ITestResult>();
49         Set<Integer> passedTestIds = new HashSet<Integer>();
50         for (ITestResult passedTest : testContext.getPassedTests().getAllResults()) {
51             logger.info("PassedTests = " + passedTest.getName());
52             passedTestIds.add(getId(passedTest));
53         }
54 
55         Set<Integer> failedTestIds = new HashSet<Integer>();
56         for (ITestResult failedTest : testContext.getFailedTests().getAllResults()) {
57             logger.info("failedTest = " + failedTest.getName());
58             int failedTestId = getId(failedTest);
59 
60             if (failedTestIds.contains(failedTestId) || passedTestIds.contains(failedTestId)) {
61                 testsToBeRemoved.add(failedTest);
62             } else {
63                 failedTestIds.add(failedTestId);
64             }
65         }
66 
67         for (Iterator<ITestResult> iterator = testContext.getFailedTests().getAllResults().iterator(); iterator.hasNext();) {
68             ITestResult testResult = iterator.next();
69             if (testsToBeRemoved.contains(testResult)) {
70                 logger.info("Remove repeat Fail Test: " + testResult.getName());
71                 iterator.remove();
72             }
73         }
74 
75 
76     }
77     private int getId(ITestResult result) {
78         int id = result.getTestClass().getName().hashCode();
79         id = id + result.getMethod().getMethodName().hashCode();
80         id = id + (result.getParameters() != null ? Arrays.hashCode(result.getParameters()) : 0);
81         return id;
82     }
83 }
View Code

5、如何應用?主要代碼如下:

1 @Listeners(TestngListener.class)
2 public class RetryTest {
3     
4     @Test(retryAnalyzer=TestngRetry.class)
5     public void test(){
6         System.out.println("xx");
7         Assert.fail();
8     }
9 }

Appium失敗截圖及重試機制封裝(二)