那麼對于單個子產品回歸測試而言.需要哪些過程可以自動化?分析如下:
自動化流程:
[1]:模拟器控制-啟動或關閉Windows phone 模拟器.
[2]: 自動在模拟器上安裝并執行測試用例應用程式.
[3]: 收集測試結果
[4]: 解除安裝測試用應用程式前恢複模拟器
<a target="_blank" href="http://blog.51cto.com/attachment/201201/102955771.png"></a>
now,如果我們把這個過程自動化處理.問題就出現了如何在不使用Application Development Tool情況把XAP安裝包安裝到模拟器或真機上? 同樣針對模拟器控制.如何能夠在代碼自動控制并運作XAP安裝包?
針對這個問題.Windows phone 類庫中提供一個Microsoft.SmartDevice.Connectivity.DLL用來實作與模拟器或裝置進行互動.現在針對原有解決方案添加一個命名為DeveiceAutomationTest_Desktop的Console類型應用程式用來做回歸測試自動化的控制台.解決方案結構如下:
<a target="_blank" href="http://blog.51cto.com/attachment/201201/103020767.png"></a>
要實作對模拟器互動控制 需要在控制台應用程式添加對Microsoft.SmartDevice.Connectivity.DLL引用,引用位址C:\Program Files (x86)\Common Files\microsoft shared\Phone Tools\CoreCon\10.0\Bin:
<a target="_blank" href="http://blog.51cto.com/attachment/201201/103027348.png"></a>
添加引用後,實作對應裝置控制的操作.連接配接并安裝初始化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;
}
擷取資料控制Device對象後.可以連接配接裝置并把XAP包安裝并初始化到裝置中:
/// Connection Emulator Device
/// <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...");
//Device Connection
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...");
//currentHost.Close();
}
Main方法執行的流程.首先部署XAP停止正在應用程式.并從裝置中移除.然後連接配接裝置 部署XAP包. 執行TEstCase測試用例.擷取測試用例回報結果.解除安裝目前應用程式并清空資料狀态.回複測試運作前.測試效果:
<a target="_blank" href="http://blog.51cto.com/attachment/201201/103034708.png"></a>
正常運作時如果模拟器沒有運作會自動打開模拟器 安裝XAP包運作測試用例.當控制台擷取測試結果後.解除安裝目前目前測試用例.一個完整回歸測試流程就建立成功了.而發現可以把整個執行流程全部自動化代碼控制.當然可以把回歸測試在執行多條資料時多次自動化執行.整個執行流程控制台操作如下:
<a target="_blank" href="http://blog.51cto.com/attachment/201201/103042605.png"></a>
在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)
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));
}
一整個回歸測試自動化流程建立完畢",編譯通過執行:
<a target="_blank" href="http://blog.51cto.com/attachment/201201/103049299.png"></a>
well.可以看到把整個回歸測試整個流程完全自動化用Code處理了. 而控制台應用就類似自動化時可以複用處理的腳本一樣.可以複用.依然能夠看到控制台中存在幾個需要及時制定的變量類似.XAP 包路徑等.也可以封裝可見的UI指定.當我們修改稽核玩測試用例後.就可以通過該控制台應用程式.定期執行自動化回歸的測試.生成測試報告.很有效保證代碼內建時品質.本篇隻是提供自動化解決一種方案.
源代碼見附件。
首先來看看應用程式安裝執行過程.目前隻能通過Application Development Tool部署真機上:
<a href="http://down.51cto.com/data/2359659" target="_blank">附件:http://down.51cto.com/data/2359659</a>
本文轉自chenkaiunion 51CTO部落格,原文連結:http://blog.51cto.com/chenkai/763076