本篇承接上篇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對象:
- /// <summary>
- /// Get Current Support Platform Device Object
- /// </summary>
- /// <param name="localId">Region Local Id</param>
- /// <returns>Device Object</returns>
- public static Device GetSupportPlatformDeviceObj(int localId,bool isDevice)
- {
- //Note By chenkai:
- //1033 is the LCID for English, United States and 1031 is the LCID for German, Germany.
- //Please Check List of Locale ID (LCID) Values as Assigned by Microsoft.
- //http://msdn.microsoft.com/zh-cn/goglobal/bb964664.aspx
- //Now Use English is 1031
- if (localId == 0)
- localId = 1031;
- Device currentDeviceObj = null;
- DatastoreManager datastoreManagerObj = new DatastoreManager(localId);
- Platform wp7Platform = datastoreManagerObj.GetPlatforms().Single(p => p.Name == "Windows Phone 7");
- if (wp7Platform != null)
- {
- if (isDevice)
- currentDeviceObj = wp7Platform.GetDevices().Single(x => x.Name == "Windows Phone Device");
- else
- currentDeviceObj = wp7Platform.GetDevices().Single(x => x.Name == "Windows Phone Emulator");
- }
- return currentDeviceObj;
- }
localid是在初始化靜態 DatastoreManager 對象時是一個區域設置標識符 (LCID),這是一個標識語言、國家和/或地區的整數. 固然這裏採用1031德國地區.針對這個區域Localid的設置請參考官方給出的List of Locale ID (LCID) Values as Assigned by Microsoft 列表.
獲取數據控制Device對象後.能夠鏈接設備並把XAP包安裝並初始化到設備中:
- /// <summary>
- /// Connection Emulator Device
- /// </summary>
- /// <param name="deviceObj">Device Object</param>
- /// <param name="applicationGuid">Application Guid</param>
- /// <param name="iconFilePath">IconFile Path</param>
- /// <param name="xapFileName">XapFile Name</param>
- /// <returns>return is Connection Device</returns>
- public static void ConnectionDeviceInstallApp(Device deviceObj,Guid applicationGuid,string iconFilePath,string xapFileName)
- {
- if (!string.IsNullOrEmpty(applicationGuid.ToString()))
- {
- try
- {
- deviceObj.Connect();
- ApplicationManager.InstallApplicationAndLaunch(deviceObj, applicationGuid, iconFilePath, xapFileName);
- }
- finally
- {
- deviceObj.Disconnect();
- }
- }
- }
這個方法中須要提供Device設備操做對象.以及XAP安裝包和對應應用程序圖標的文件路徑. 其實這裏並不必定非得采用控制檯的方式.也能夠作一個Winform以可見UI的方式.至於Guid則是測試項目WP7AutomationTest_Demo.Test-WMAppmainfest.xml文件中的ProductId相對應:
- <App xmlns="" ProductID="{b81d87a6-3afd-4f21-9a60-3c5ae0921fd2}" Title="WP7AutomationTest_Demo.Test"/>
該方法中執行了兩個操做.連接設備.安裝XAP包到設備中.安裝XAP操做經過ApplicationManager類中InstallApplicationAndLaunch方法 .定義以下:
- public static void InstallApplicationAndLaunch(Device deviceObj, Guid applicationGuid, string iconFileName, string xapFileName)
- {
- if (!string.IsNullOrEmpty(applicationGuid.ToString()))
- {
- //Install and Luanch
- UnstallApplicationAndClear(deviceObj, applicationGuid);
- deviceObj.InstallApplication(applicationGuid, applicationGuid, "AutomationAPP", iconFileName, xapFileName);
- RemoteApplication currentApplication = deviceObj.GetApplication(applicationGuid);
- if (currentApplication != null)
- currentApplication.Launch();
- }
- }
經過Device對象初始化XAP 並Lauch,.在執行安裝XAP以前 若是正在運行則當即中斷.若是已經安裝該應用程序則需刪除卸載:
- public static void UnstallApplicationAndClear(Device deviceObj, Guid applicationGuid)
- {
- if(!string.IsNullOrEmpty(applicationGuid.ToString()))
- {
- if (deviceObj.IsApplicationInstalled(applicationGuid))
- {
- RemoteApplication remoteApplication = deviceObj.GetApplication(applicationGuid);
- if (remoteApplication != null)
- {
- //Remove Application
- remoteApplication.TerminateRunningInstances();
- remoteApplication.Uninstall();
- }
- }
- }
- }
well.如此封裝號對設備的初始化的控制.開始執行ProgramMain 方法:
- static void Main(string[] args)
- {
- //Base ApplicationInfo
- string xapfile = @"..\..\..\WP7AutomationTest_Demo.Test\Bin\Debug\WP7AutomationTest_Demo.Test.xap";
- string iconfile = @"..\..\..\WP7AutomationTest_Demo.Test\Bin\Debug\WApplicationIcon.png";
- ApplicationBaseInfo currentBaseInfo = new ApplicationBaseInfo(xapfile, iconfile, new Guid("b81d87a6-3afd-4f21-9a60-3c5ae0921fd2"));
- //DeviceManager Object
- Device deviceObj = DeviceManager.GetSupportPlatformDeviceObj(1031,false);
- using (ServiceHost currentHost = new ServiceHost(typeof(TestResultReportService)))
- {
- currentHost.Open(); 13: Console.WriteLine("Current Service is Open...");
- Console.WriteLine("Connecting to Windows phone Emluator Device...");
- try
- {
- //Device Connection
- deviceObj.Connect();
- Console.WriteLine("Windows phone Emluator Device Connected...");
- ApplicationManager.InstallApplicationAndLaunch(deviceObj, currentBaseInfo.ApplicationGuid, currentBaseInfo.IconFilePath, currentBaseInfo.XapFilePath);
- Console.WriteLine("Wait For Run TestCase Compated...");
- lastendTestRunEvent.WaitOne(12000);
- Console.WriteLine("Test Case is Run Complate...");
- ApplicationManager.UnstallApplicationAndClear(deviceObj, currentBaseInfo.ApplicationGuid);
- Console.WriteLine("Application is UnStall and Clear...");
- }
- finally
- {
- deviceObj.Disconnect();
- }
- //currentHost.Close();
- }
Main方法執行的流程.首先部署XAP中止正在應用程序.並從設備中移除.而後鏈接設備 部署XAP包. 執行TEstCase測試用例.獲取測試用例反饋結果.卸載當前應用程序並清空數據狀態.回覆測試運行前.測試效果:
正常運行時若是模擬器沒有運行會自動打開模擬器 安裝XAP包運行測試用例.當控制檯獲取測試結果後.卸載當前當前測試用例.一個完整迴歸測試流程就創建成功了.而發現能夠把整個執行流程所有自動化代碼控制.固然能夠把迴歸測試在執行多條數據時屢次自動化執行.整個執行流程控制檯操做以下:
在main方法同時創建一個WCF服務.該服務的主要目的是在單元測試用例測試完成後通知控制檯中止調用. 另一個目的是把SUTF框架的測試結果傳遞給控制檯.存在一個測試結果文件中保存在本地並輸出到控制檯中:
- public class TestResultReportService : ITestResultReportService
- {
- public void SaveTestResultFile(string filename, string filecontent)
- {
- var outputfile = Path.Combine(@"..\..\..\DeveiceAutomationTest_Desktop\Bin\Debug\", filename);
- if (File.Exists(outputfile))
- {
- File.Delete(outputfile);
- }
- File.WriteAllText(outputfile, filecontent);
- Console.WriteLine(filecontent);
- }
- public void GetFinalTestResult(bool failure, int failures, int totalScenarios, string message)
- {
- Console.WriteLine(message);
- Program.EndTestRun();
- }
- }
而針對測試用例應用程序WP7AutomationTest_Demo.Test須要額外引用該服務.從新創建一個TestBaseReportService類用來重寫TestReportingProvider實體類中方法實現.測試結果經過服務以消息的形式傳遞給控制檯.保存在文件中: 首先添加引用:
- using Microsoft.Silverlight.Testing;
- using Microsoft.Silverlight.Testing.Harness;
- using Microsoft.Silverlight.Testing.Service;
- using Microsoft.VisualStudio.TestTools.UnitTesting;
重寫方法:
- public class TestBaseReportService:TestReportingProvider
- {
- //ServiceClient
- UnitTestService.TestResultReportServiceClient reportClient = new UnitTestService.TestResultReportServiceClient();
- public TestBaseReportService(TestServiceProvider serviceProvider) : base(serviceProvider) { }
- public override void ReportFinalResult(Action<ServiceResult> callback, bool failure, int failures, int totalScenarios, string message)
- {
- this.IncrementBusyServiceCounter();
- reportClient.SaveTestResultFileCompleted += (se, x) =>
- { 12: this.DecrementBusyServiceCounter();
- };
- reportClient.GetFinalTestResultAsync(failure, failures, totalScenarios, message);
- base.ReportFinalResult(callback, failure, failures, totalScenarios, message);
- }
- public override void WriteLog(Action<ServiceResult> callback, string logName, string content)
- {
- this.IncrementBusyServiceCounter();
- reportClient.SaveTestResultFileCompleted += (s, e) =>
- {
- this.DecrementBusyServiceCounter();
- };
- reportClient.SaveTestResultFileAsync(logName, content);
- base.WriteLog(callback, logName, content);
- }
- }
ok。定義重寫服務以後還須要Silverlight Unit TESt FrameWork框架進行測試結果的關聯.這很關鍵.SUTF框架將經過該服務來提交報告. 在mainpage定義輸出的頁面添加以下Code:
- var reportSetting = UnitTestSystem.CreateDefaultSettings();
- if (reportSetting!=null)
- {
- reportSetting.TestService.RegisterService(TestServiceFeature.TestReporting, new TestBaseReportService(reportSetting.TestService));
- }
一整個迴歸測試自動化流程創建完畢",編譯經過執行:
well.能夠看到把整個迴歸測試整個流程徹底自動化用Code處理了. 而控制檯應用就相似自動化時能夠複用處理的腳本同樣.能夠複用.依然可以看到控制檯中存在幾個須要及時制定的變量相似.XAP 包路徑等.也能夠封裝可見的UI指定.當咱們修改審覈玩測試用例後.就能夠經過該控制檯應用程序.按期執行自動化迴歸的測試.生成測試報告.頗有效保證代碼集成時質量.本篇只是提供自動化解決一種方案.
源代碼見附件。
首先來看看應用程序安裝執行過程.目前只能經過Application Development Tool部署真機上: