那么对于单个模块回归测试而言.需要哪些过程可以自动化?分析如下:
自动化流程:
[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