Windows phone 應用開發[10]-自動化測試

    本篇承接上篇Windows phone 應用開發[9]-單元測試,當在Unit TEst 肯定了測試用例的方案.在單個模塊單元測試經過開始走模塊之間集成時.其實不少測試工做就能夠開發人員手中開始向測試人員轉移.相似針對單個模塊,測試團隊能夠根據已經肯定測試用例批量執行.獲得實際結果與指望結果進行比較.而這個過程測試執行者無需瞭解代碼如何執行.也就是俗稱黑盒測試.ios

    而對於單個模塊的迴歸測試而言.爲了提升效率引入自動化測試的概念.惋惜的是目前並無任何可用於Windows phone Application應用程序的自動化測試工具.曾在Silverlight中使用過自動化測試工具-Ranorex,花費點時間嘗試經過Ranorex在WP 上執行自動化測試.發現存在不少應用程序與測試工具之間不少不可控的問題. sql

    那麼對於單個模塊迴歸測試而言.須要哪些過程能夠自動化?分析以下:app

自動化流程:框架

[1]:模擬器控制-啓動或關閉Windows phone 模擬器.ide

[2]: 自動在模擬器上安裝並執行測試用例應用程序.工具

[3]: 收集測試結果單元測試

[4]: 卸載測試用應用程序前恢復模擬器測試

 

 

    now,若是咱們把這個過程自動化處理.問題就出現瞭如何在不使用Application Development Tool狀況把XAP安裝包安裝到模擬器或真機上? 一樣針對模擬器控制.如何可以在代碼自動控制並運行XAP安裝包?ui

    針對這個問題.Windows phone 類庫中提供一個Microsoft.SmartDevice.Connectivity.DLL用來實現與模擬器或設備進行交互.如今針對原有解決方案添加一個命名爲DeveiceAutomationTest_Desktop的Console類型應用程序用來作迴歸測試自動化的控制檯.解決方案結構以下:this

    要實現對模擬器交互控制 須要在控制檯應用程序添加對Microsoft.SmartDevice.Connectivity.DLL引用,引用地址C:\Program Files (x86)\Common Files\microsoft shared\Phone Tools\CoreCon\10.0\Bin:

添加引用後,實現對應設備控制的操做.鏈接並安裝初始化XAP到模擬器中,添加一個設備控制類DeviceManager類,首先要根絕設備類型模擬器或真機獲取設備的控制設備的Device對象:

 

  
  
  
  
  1. /// <summary>     
  2. /// Get Current Support Platform Device Object     
  3. /// </summary>     
  4. /// <param name="localId">Region Local Id</param>     
  5. /// <returns>Device Object</returns>     
  6. public static Device GetSupportPlatformDeviceObj(int localId,bool isDevice)     
  7. {     
  8.     //Note By chenkai:     
  9.     //1033 is the LCID for English, United States and 1031 is the LCID for German, Germany.   
  10.     //Please Check List of Locale ID (LCID) Values as Assigned by Microsoft.    
  11.     //http://msdn.microsoft.com/zh-cn/goglobal/bb964664.aspx    
  12.     //Now Use English is 1031    
  13.     if (localId == 0)    
  14.         localId = 1031;    
  15.     Device currentDeviceObj = null;    
  16.     DatastoreManager datastoreManagerObj = new DatastoreManager(localId);    
  17.     Platform wp7Platform = datastoreManagerObj.GetPlatforms().Single(p => p.Name == "Windows Phone 7");    
  18.     if (wp7Platform != null)    
  19.     {    
  20.         if (isDevice)    
  21.             currentDeviceObj = wp7Platform.GetDevices().Single(x => x.Name == "Windows Phone Device");    
  22.         else    
  23.             currentDeviceObj = wp7Platform.GetDevices().Single(x => x.Name == "Windows Phone Emulator");    
  24.     }    
  25.     return currentDeviceObj;    

    localid是在初始化靜態 DatastoreManager 對象時是一個區域設置標識符 (LCID),這是一個標識語言、國家和/或地區的整數. 固然這裏採用1031德國地區.針對這個區域Localid的設置請參考官方給出的List of Locale ID (LCID) Values as Assigned by Microsoft 列表.

    獲取數據控制Device對象後.能夠鏈接設備並把XAP包安裝並初始化到設備中:

  
  
  
  
  1. /// <summary>     
  2. /// Connection Emulator Device     
  3. /// </summary>     
  4. /// <param name="deviceObj">Device Object</param>     
  5. /// <param name="applicationGuid">Application Guid</param>     
  6. /// <param name="iconFilePath">IconFile Path</param>     
  7. /// <param name="xapFileName">XapFile Name</param>     
  8. /// <returns>return is Connection Device</returns>     
  9. public static void ConnectionDeviceInstallApp(Device deviceObj,Guid applicationGuid,string iconFilePath,string xapFileName)    
  10. {    
  11.     if (!string.IsNullOrEmpty(applicationGuid.ToString()))    
  12.     {    
  13.         try    
  14.         {    
  15.             deviceObj.Connect();    
  16.            ApplicationManager.InstallApplicationAndLaunch(deviceObj, applicationGuid, iconFilePath, xapFileName);    
  17.         }    
  18.         finally    
  19.         {    
  20.             deviceObj.Disconnect();    
  21.         }    
  22.     }    

    這個方法中須要提供Device設備操做對象.以及XAP安裝包和對應應用程序圖標的文件路徑. 其實這裏並不必定非得采用控制檯的方式.也能夠作一個Winform以可見UI的方式.至於Guid則是測試項目WP7AutomationTest_Demo.Test-WMAppmainfest.xml文件中的ProductId相對應:

  
  
  
  
  1. <App xmlns="" ProductID="{b81d87a6-3afd-4f21-9a60-3c5ae0921fd2}" Title="WP7AutomationTest_Demo.Test"/> 

    該方法中執行了兩個操做.連接設備.安裝XAP包到設備中.安裝XAP操做經過ApplicationManager類中InstallApplicationAndLaunch方法 .定義以下:

  
  
  
  
  1. public static void InstallApplicationAndLaunch(Device deviceObj, Guid applicationGuid, string iconFileName, string xapFileName)     
  2. {     
  3.     if (!string.IsNullOrEmpty(applicationGuid.ToString()))     
  4.     {     
  5.         //Install and Luanch     
  6.         UnstallApplicationAndClear(deviceObj, applicationGuid);     
  7.         deviceObj.InstallApplication(applicationGuid, applicationGuid, "AutomationAPP", iconFileName, xapFileName);     
  8.         RemoteApplication currentApplication = deviceObj.GetApplication(applicationGuid);     
  9.         if (currentApplication != null)    
  10.             currentApplication.Launch();    
  11.     }    

    經過Device對象初始化XAP 並Lauch,.在執行安裝XAP以前 若是正在運行則當即中斷.若是已經安裝該應用程序則需刪除卸載:

  
  
  
  
  1. public static void UnstallApplicationAndClear(Device deviceObj, Guid applicationGuid)     
  2. {     
  3.     if(!string.IsNullOrEmpty(applicationGuid.ToString()))     
  4.     {     
  5.         if (deviceObj.IsApplicationInstalled(applicationGuid))     
  6.         {     
  7.             RemoteApplication remoteApplication = deviceObj.GetApplication(applicationGuid);     
  8.             if (remoteApplication != null)     
  9.             {    
  10.                 //Remove Application    
  11.               remoteApplication.TerminateRunningInstances();    
  12.                 remoteApplication.Uninstall();    
  13.             }    
  14.         }    
  15.     }    

    well.如此封裝號對設備的初始化的控制.開始執行ProgramMain 方法:

  
  
  
  
  1. static void Main(string[] args)     
  2. {     
  3.     //Base ApplicationInfo     
  4.     string xapfile = @"..\..\..\WP7AutomationTest_Demo.Test\Bin\Debug\WP7AutomationTest_Demo.Test.xap";     
  5.     string iconfile = @"..\..\..\WP7AutomationTest_Demo.Test\Bin\Debug\WApplicationIcon.png";     
  6.     ApplicationBaseInfo currentBaseInfo = new ApplicationBaseInfo(xapfile, iconfile, new Guid("b81d87a6-3afd-4f21-9a60-3c5ae0921fd2"));     
  7.     //DeviceManager Object     
  8.     Device deviceObj = DeviceManager.GetSupportPlatformDeviceObj(1031,false);    
  9.     using (ServiceHost currentHost = new ServiceHost(typeof(TestResultReportService)))    
  10.     {    
  11.         currentHost.Open();  13:                  Console.WriteLine("Current Service is Open...");    
  12.         Console.WriteLine("Connecting to Windows phone Emluator Device...");   
  13.         try    
  14.         {    
  15.             //Device Connection    
  16.             deviceObj.Connect();    
  17.             Console.WriteLine("Windows phone Emluator Device Connected...");    
  18.            ApplicationManager.InstallApplicationAndLaunch(deviceObj, currentBaseInfo.ApplicationGuid, currentBaseInfo.IconFilePath, currentBaseInfo.XapFilePath);    
  19.             Console.WriteLine("Wait For Run TestCase Compated...");       
  20.             lastendTestRunEvent.WaitOne(12000);       
  21.             Console.WriteLine("Test Case is Run Complate...");    
  22.             ApplicationManager.UnstallApplicationAndClear(deviceObj, currentBaseInfo.ApplicationGuid);    
  23.             Console.WriteLine("Application is UnStall and Clear...");                       
  24.         }    
  25.         finally    
  26.         {    
  27.             deviceObj.Disconnect();    
  28.         }    
  29.         //currentHost.Close();    
  30.     } 

    Main方法執行的流程.首先部署XAP中止正在應用程序.並從設備中移除.而後鏈接設備 部署XAP包. 執行TEstCase測試用例.獲取測試用例反饋結果.卸載當前應用程序並清空數據狀態.回覆測試運行前.測試效果:

    正常運行時若是模擬器沒有運行會自動打開模擬器 安裝XAP包運行測試用例.當控制檯獲取測試結果後.卸載當前當前測試用例.一個完整迴歸測試流程就創建成功了.而發現能夠把整個執行流程所有自動化代碼控制.固然能夠把迴歸測試在執行多條數據時屢次自動化執行.整個執行流程控制檯操做以下:

    在main方法同時創建一個WCF服務.該服務的主要目的是在單元測試用例測試完成後通知控制檯中止調用. 另一個目的是把SUTF框架的測試結果傳遞給控制檯.存在一個測試結果文件中保存在本地並輸出到控制檯中:

  
  
  
  
  1. public class TestResultReportService : ITestResultReportService     
  2. {     
  3.     public void SaveTestResultFile(string filename, string filecontent)     
  4.     {     
  5.         var outputfile = Path.Combine(@"..\..\..\DeveiceAutomationTest_Desktop\Bin\Debug\", filename);     
  6.         if (File.Exists(outputfile))     
  7.         {     
  8.             File.Delete(outputfile);     
  9.         }    
  10.         File.WriteAllText(outputfile, filecontent);    
  11.         Console.WriteLine(filecontent);    
  12.     }          
  13.     public void GetFinalTestResult(bool failure, int failures, int totalScenarios, string message)    
  14.     {    
  15.         Console.WriteLine(message);    
  16.         Program.EndTestRun();    
  17.     }    

    而針對測試用例應用程序WP7AutomationTest_Demo.Test須要額外引用該服務.從新創建一個TestBaseReportService類用來重寫TestReportingProvider實體類中方法實現.測試結果經過服務以消息的形式傳遞給控制檯.保存在文件中: 首先添加引用:

  
  
  
  
  1. using Microsoft.Silverlight.Testing;     
  2. using Microsoft.Silverlight.Testing.Harness;     
  3. using Microsoft.Silverlight.Testing.Service;     
  4. using Microsoft.VisualStudio.TestTools.UnitTesting; 

重寫方法: 

  
  
  
  
  1. public class TestBaseReportService:TestReportingProvider     
  2. {     
  3.     //ServiceClient     
  4.     UnitTestService.TestResultReportServiceClient reportClient = new UnitTestService.TestResultReportServiceClient();     
  5.     public TestBaseReportService(TestServiceProvider serviceProvider) : base(serviceProvider) { }     
  6.  
  7.     public override void ReportFinalResult(Action<ServiceResult> callback, bool failure, int failures, int totalScenarios, string message)     
  8.     {     
  9.         this.IncrementBusyServiceCounter();    
  10.         reportClient.SaveTestResultFileCompleted += (se, x) =>    
  11.         {  12:                  this.DecrementBusyServiceCounter();    
  12.         };    
  13.         reportClient.GetFinalTestResultAsync(failure, failures, totalScenarios, message);    
  14.         base.ReportFinalResult(callback, failure, failures, totalScenarios, message);    
  15.     }    
  16.  
  17.     public override void WriteLog(Action<ServiceResult> callback, string logName, string content)    
  18.     {    
  19.         this.IncrementBusyServiceCounter();    
  20.         reportClient.SaveTestResultFileCompleted += (s, e) =>    
  21.         {    
  22.             this.DecrementBusyServiceCounter();    
  23.         };    
  24.         reportClient.SaveTestResultFileAsync(logName, content);    
  25.         base.WriteLog(callback, logName, content);    
  26.     }    

    ok。定義重寫服務以後還須要Silverlight Unit TESt FrameWork框架進行測試結果的關聯.這很關鍵.SUTF框架將經過該服務來提交報告. 在mainpage定義輸出的頁面添加以下Code:

  
  
  
  
  1. var reportSetting = UnitTestSystem.CreateDefaultSettings();     
  2.  if (reportSetting!=null)     
  3.  {     
  4.      reportSetting.TestService.RegisterService(TestServiceFeature.TestReporting, new TestBaseReportService(reportSetting.TestService));     
  5.  } 

    一整個迴歸測試自動化流程創建完畢",編譯經過執行:

    well.能夠看到把整個迴歸測試整個流程徹底自動化用Code處理了. 而控制檯應用就相似自動化時能夠複用處理的腳本同樣.能夠複用.依然可以看到控制檯中存在幾個須要及時制定的變量相似.XAP 包路徑等.也能夠封裝可見的UI指定.當咱們修改審覈玩測試用例後.就能夠經過該控制檯應用程序.按期執行自動化迴歸的測試.生成測試報告.頗有效保證代碼集成時質量.本篇只是提供自動化解決一種方案.

    源代碼見附件。

 

    首先來看看應用程序安裝執行過程.目前只能經過Application Development Tool部署真機上:

相關文章
相關標籤/搜索