天天看点

Silverlight测试——利用Ranorex实现数据驱动测试

本篇来说说关于Silverlight测试——利用Ranorex工具实现TestDataDriver[TDD-数据驱动测试俗称黑盒测试].

<a target="_blank" href="http://blog.51cto.com/attachment/201201/094355296.jpg"></a>

谈到测试 对于开发人员. 应该了解和我们日常编程息息相关的测试: Unit Test[单元测试] 和 DataDriver Test[TDD] TDD是敏捷的实现方式XP[极限编程]的重要组成部分, 目前Visual Studio中对Unit Test支持.下图显示了Visual Studio 2010中提供的帮助测试客户的各种解决方案:

<a target="_blank" href="http://blog.51cto.com/attachment/201201/094410577.png"></a>

在Mix10大会中微软发布了Silverlight 4版本. 但是关注测试这块依然是一个让广大DVp开发者头大的问题. 微软4月份更新的工具包Arp版本就已经包含了Unit Test测试框架.运行在VS2010中,但是目前这个Test unit FrameWork框框架不能VisualStudio功能相集成.Team Foundation Server的2010与Silverlight单元测试整合也存在相关问题.  因为在测试这块发现足多很多不实际问题. 微软也是感到有些气短 在一次发言专门提到": 如下原文:

The recommended way for developers to run tests of their Silverlight applications is by building and running the test application. Visual Studio has a separate set of integrated test tools that are not connected to the Silverlight Unit Test Framework, which is the same for all previous releases of the Silverlight testing work.

<a target="_blank" href="http://blog.51cto.com/attachment/201201/094418485.jpg"></a>

&lt;2.1&gt;了解TDD测试

TDD缩写是Test Driver Development 是XP编程中一个非常重要组成部分.

严格意义来说真正和程序员编码有限测试应该UnitTEst 单元测试. 程序员有责任编写功能代码,同时也就有责任为自己的代码编写单元测试.  最终交互时 我们不仅要交互功能代码 还要交付这些相关功能代码的Unit Test.

Unit TEst面对的对象是程序员. 这样一来程序员就知道测试对象某一个实现Function函数中功能结构特点. 来进行编写用例. 就是所谓的白盒测试.白盒测试针对程序的逻辑结构设计测试用例,用逻辑覆盖率来衡量测试的完整性

而TDD的特点针对的对象是和代码业务无法的测试人员. 当我们把一个Silverlight Application交给测试MM时 她对软件中涉及的功能结构是未知的, 这时测试案例好坏我觉得不仅用的合适测试方法 而且和个人的经验又有极大关系. 在程序接口录入信息.根据定义测试规格输出相关信息. 而其中程序操作规则是已经制定好的. 其实这就是黑盒测试.黑盒测试着眼于程序外部结构,不考虑内部逻辑结构,主要针对软件界面和软件功能进行测试。

&lt;2.2&gt;关于Ranorex工具

<a target="_blank" href="http://blog.51cto.com/attachment/201201/094431760.jpg"></a>

<a target="_blank" href="http://www.ranorex.com/">Ranorex Web Site:-[官方站点]</a>

<a target="_blank" href="http://www.ranorex.com/support/user-guide-20.html">Ranorex UserGuid Sample[用户指导案例 很详细关于测试各个方面 建议参考]</a>

<a target="_blank" href="http://www.ranorex.com/download.html">Rannorex Download Link [下载连接-Version:2.3.4-30天试用版]</a>

<a target="_blank" href="http://blog.jibin.net/post/25/1/1/">Ranorex Genyen Key Tool[30天试用版破解工具下载]</a>

<a target="_blank" href="http://blog.51cto.com/attachment/201201/094439520.jpg"></a>

看到这个TestComplete简介我就想起打包工具中installshield霸王大哥一般. 面对两个篇幅的配置信息 我基本就已经 提前到下.来看一下TestComplete霸王特点:

TestComplete是一个自动测试管理工具,完全支持项目层级(project level)的单元测试、功能测试、回归测试(regression testing)、分布式测试以及HTTP性能测试。它是专门为应用程序开发人员和测试人员设计的。从开发代码、发布到维护的整个过程 .

对于Silverlight测试已经覆盖了目前的所有版本 以及。NetFrameWork支持也覆盖所有版本.

天啊.TestComplete是一个霸王测试工具不是没有道理 不费上一天两天的功夫去了解短时间内你是玩转不起来的. 所以我因为这个特点就抛弃了霸王的TestComplete 转而投向Ranorex小巧温柔的怀抱/

<a target="_blank" href="http://blog.51cto.com/attachment/201201/094447603.jpg"></a>

&lt;3.1&gt;新建一个C# Automation Solution

对工具Ranorex安装完成后 你可以采用破解工具进行破解. 完成后启动Ranorex.进入主界面:

<a target="_blank" href="http://blog.51cto.com/attachment/201201/094458130.jpg"></a>

这块工具基本和VisualStudio基本类似.对于.NEt程序员来说运用并不难. 在左上角点击“File”——&gt;”NEW”——&gt;”Soluction”:从打开页面可以看出Ranorex工具支持C# VB.NET、Python三种语言编写脚本同时还支持WorkFlow工作流. 选择C#节点 并选中 C# For Automatic项目:

<a target="_blank" href="http://blog.51cto.com/attachment/201201/094505279.jpg"></a>

创建一个Ranorex C# Test Automatic项目命名为:SLAutomaticDemo:窗体分布

<a target="_blank" href="http://blog.51cto.com/attachment/201201/100827816.jpg"></a>

创建一个C# Automatic项目后 看一下项目结构:

<a target="_blank" href="http://blog.51cto.com/attachment/201201/100835623.jpg"></a>

Program.CS文件是C#语言编写的测试脚本文件.也是整个项目的入口处Main方法所在类. 另外一个很重要的文件Recording1文件则记录录制SL界面操作元素事件并自动转换成可操作的C#代码. 从Program.CS文件Main方法可以看出. 主程序就是调用Recording1文件中的Recording1类Start方法来实现脚本的自动化执行的.Program.cs 代码:

public static int Main(string[] args)   

{   

Keyboard.AbortKey = System.Windows.Forms.Keys.Pause;   

int error = 0;   

string logFileName = "Test.rxlog";   

Report.Setup(ReportLevel.Info, logFileName, true);   

try   

//调用Recording1文件中REcording类Start()执行对脚本的调用.很简单.   

Recording1.Start();   

}   

catch (ImageNotFoundException e)   

Report.Error(e.ToString());   

Report.LogData(ReportLevel.Error, "Image not found", e.Feature);   

Report.LogData(ReportLevel.Error, "Searched image", e.Image);   

error = -1;   

<a target="_blank" href="http://blog.51cto.com/attachment/201201/100846446.jpg"></a>

当然在录制我们需要一个测试的Silverlight Application. 为了达到演示目的该Silverlight应用程序界面实现但是一个加法操作: 界面如下:

<a target="_blank" href="http://blog.51cto.com/attachment/201201/100853199.png"></a>

新建一个Silverlight Application即可. 输入第一个数和第二个数 点击计算按钮就把二者计算结果显示在最后一个文本框. 这是页面操作一个过程.分解成Reaorex录制步骤分为 TextBox1获得焦点——&gt;输入第一个数——&gt;第二个文本框获得焦点——&gt;输入数字 ——&gt;点击计算按钮 ——&gt;显示数据 既然搞清我们要录制固定Silverlight 界面UI操作具体步骤 ,下一步就是利用Ranorex工具进行录制这个动作整个过程点击ReCord按钮 Ranoex工具就会最小化 右下角就出现一个记录窗体:

<a target="_blank" href="http://blog.51cto.com/attachment/201201/100900618.jpg"></a>

在这有人要困惑的问.为何要录制这个页面计算过程?

做过黑盒测试驱动编程朋友应该都熟悉. 当我们把已经完成功能代码的程序交给测试MM时 她是不知道内部的代码结构. 如何能测试出界面和功能性上Bug. 测试MM负责不断录入变化数据 而根据测试案例中编写的测试规格来查看输出结果 来判定程序是否存在Bug.

你明白了吧. 既然测试过程数据时不断变化的 我们现在要做的就是利用Ranorex工具录制测试案例不变测试规则.

当我们打开录制时就可以在已经运行成功页面进行上面拆分步骤进行操作.: 录制完成后即可点击Stop终止录制过程,. 

当把Silverlight页面三步计算操作录制完成 我们看一下Ranorex工具对页面事件操作的记录:

<a target="_blank" href="http://blog.51cto.com/attachment/201201/100908559.png"></a>

录制Silverlight 页面UI元素流程:

<a target="_blank" href="http://blog.51cto.com/attachment/201201/100915160.jpg"></a>

查看计录Silverlight UI元素的窗体记录:

<a target="_blank" href="http://blog.51cto.com/attachment/201201/100929467.png"></a>

根节点是承载页面WebFrom对应承载的Silverlight comtrol UI元素 分别涉及到四个 三个TExtBox和一个Button按钮.和我们操作步骤记录完全吻合.

录制好Silverlight页面UI元素的操作规则后 我们工作基本完成一半.

&lt;3.2&gt;编辑录制行为

当完成UI元素操作规则录制后. 下一步只需要利用Ranorex工具编辑录制中行为变成动态在C#语言中可运用的代码属性 供我们操作.

可以对Silverlight事件窗体做简单的操作就可把我们记录转换成编码中可操作的DataDriver: 选中我们在TextBox1/2页面输入操作keySequence :

<a target="_blank" href="http://blog.51cto.com/attachment/201201/100937172.png"></a>

选中Key Sequence右键点击——&gt;Convert to User Code Item 后查看Recording1文件一下源代码:

<a target="_blank" href="http://blog.51cto.com/attachment/201201/100945727.jpg"></a>

源码中添加两个属性:

// Create a new property 'FirstNum'    

public static string FirstNum    

get;set;    

}    

// Create a new property 'LastNum'    

public static string SecondNum    

{    

你应该明白这个属性封装但是我们在录制规则中对TextBox1和TextBox2的值. 当我们把一个key Sequences利用Ranorex工具Convert user Code 在右边属性窗体设置属性生成方法:

<a target="_blank" href="http://blog.51cto.com/attachment/201201/100952337.png"></a>

同理设置SecondNum属性的方法 发现源码中生成代码:

public static void SetFirstnum()   

 //Your code here. Code inside this method will not be changed by the code generator.   

 Report.Info("Key sequence '34'.");   

 Keyboard.Press("34");   

  }   

 public static void SetSecondnum()   

 {   

  //Your code here. Code inside this method will not be changed by the code generator.   

 Report.Info("Key sequence '56'.");   

  Keyboard.Press("56");   

  } 

修改如上代码吧 对应key值替换成自动生成属性:

 Keyboard.Press(Firstnum);   

 }    

  {   

 Keyboard.Press(Secondnum);   

ok.到这那么录像关于加法运算中运算的参数1和参数1 都可以通过属性封装在C#编码中进行控制 测试中涉及到多次循环测试 以不同数据录入测试Bug 回头看看Program.cs文件Main方法:

...  

try  

{  

//动态设置两个输入计算的参数  

Record1.FirstName = row["FirstNum"].ToString();  

Record1.LastName = row["LastNum"].ToString();  

//循环调用Start方法  

Record1.Start();  

}  

catch (RanorexException e)  

...  

如上当我们做好循环调用可执行测试程序.剩下就是需要根据测试案例制作测试数据录入进行测试. ok。这里利用SqlServer2005数据库来制作一个数据测试表 存储测试数据 建立测试表:

USE master   

go   

CREATE TABLE TestCodeDate   

(   

id INT NOT NULL PRIMARY KEY IDENTITY(1,1),   

Firstnum VARCHAR(50) NOT NULL,--参数1   

Secondnum VARCHAR(50) NOT NULL,--参数2   

Resultnum VARCHAR(50) NOT NULL,--计算结果   

TestDate DATETIME NOT NULL DEFAULT(GETDATE())--测试时间   

)   

go 

下一步就是在测试程序获得与本地Sql2005数据库数据并循环自动化测试. 剩下工作就很简单了 只需在Program.cs文件中获取数据 并循环录入    循环调用自动化测试程序自动执行 :

...............................   

//TODO: Code here - for example:   

//获取数据方式 循环执行测试 [获取数据方法]   

System.Collections.Generic.List&lt;DataDriverEntity.DataDriverModel&gt; getdatalist=Program.GetTestDataList();   

if(getdatalist.Count&gt;0)   

foreach(DataDriverEntity.DataDriverModel getmodel in getdatalist)   

//设置DAtaDriver 属性    

Recording1.Firstnum=getmodel.Firstnum.ToString();   

Recording1.Secondnum=getmodel.Secondnum.ToString();   

//重复调用 [测试]   

catch (ImageNotFoundException e)    

................. 

源码中DataDriverEntity.DataDriverModel是我新建一个实体类用来封装测试数据 定义如下:

/// &lt;summary  

/// 数据测试实体  

/// &lt;/summary&gt;  

public class DataDriverModel  

public int Id { get; set; }  

public int Firstnum { get; set; }  

public int Secondnum { get; set; }  

public int Sumnum { get; set; }  

public DateTime CreateDate { get; set; }  

由此一个简单对Silverlight页面实现DataDriver Test的程序基本完成了.

&lt;3.2&gt;编译执行

当做了整个测试流程. 可以操作菜单中进行编译: Build Soluction 查看输出:

<a target="_blank" href="http://blog.51cto.com/attachment/201201/101004209.png"></a>

编译成功后. 我们可以直接在Ranorex工具点击运行:

<a target="_blank" href="http://blog.51cto.com/attachment/201201/101022495.jpg"></a>

编译成后也可以Ranorex为了保证整个流程重复执行. Ranorex工具把整个流程封装EXE可执行文件. 很方便我们调用执行. 很方便吧.

就此利用Ranorex工具完成实现 Silverlight Application Data Driver Test.

<a target="_blank" href="http://blog.51cto.com/attachment/201201/101029222.jpg"></a>

因为Ranorex工具退出相对较晚 所以目前基本上没有相关的中文资料和文档可以参考. 这对一些.Net程序员更熟悉的是Visual Studio编码工具操作. Ranorex也考虑到这点特别这这个2.3.4版本中对Visual Sutdio加以集成.虽然只是简单集成 但这对我们更熟悉Visal Studio.而言可以说一个很大进步.

如果不喜欢Ranoex工具编译环境 你可以换成你熟悉的Visual Studio中照样可以做 Silverlight Test Driver工作.

首先在Silverlight Solution中建立一个独立Test Applcation 项目类型是一个输出Comsole Application:

<a target="_blank" href="http://blog.51cto.com/attachment/201201/101037431.png"></a>

项目建立成功后在新建的Comsole Application中添加对Ranorex工具DLL引用:

<a target="_blank" href="http://blog.51cto.com/attachment/201201/101052181.png"></a>

 从引用窗体中能看到Ranoex.Core.DLL:

<a target="_blank" href="http://blog.51cto.com/attachment/201201/101100529.png"></a>

进入Main方法 添加对Ranoex.Core.DLL的引用:

//添加引用   

using System.Drawing;   

using Ranorex; 

修改Main方法用来返回执行状态:

[STAThread]    

static int Main(string[] args)  

Main方法中调用系统附件的中计算器来进行计算. 下面是从Ranorex 复制过来的脚本代码:L

 try    

//调用附件计算器   

System.Diagnostics.Process.Start("calc.exe");    

Form form = Host.Local.FindChild&lt;Ranorex.Form&gt;("Calculator");    

form.Activate();    

Button button = form.FindChild&lt;Ranorex.Button&gt;("2");    

button.Click();    

button = form.FindChild&lt;Ranorex.Button&gt;("*");    

button = form.FindChild&lt;Ranorex.Button&gt;("3");    

button = form.FindChild&lt;Ranorex.Button&gt;("=");    

catch (RanorexException e)    

Console.WriteLine(e.ToString());    

error = -1;    

return error; 

you see!就这末简单 只需通过Ranoex工具进行录制把录制后自动生成的测试脚本Copy到Visual Studio中. 即可方便调用运行.

<a target="_blank" href="http://blog.51cto.com/attachment/201201/101112462.jpg"></a>

参考资料:

<a href="http://code.msdn.microsoft.com/silverlightut">Silverlight Unit Test Framework</a>

<a target="_blank" href="http://weblogs.asp.net/scottgu/archive/2008/04/02/unit-testing-with-silverlight.aspx">Silverlight and Unit Test</a>

<a target="_blank" href="http://blogs.msdn.com/b/gisenberg/archive/2008/07/17/ui-automation-in-silverlight-part-ii-the-easy-way.aspx">UI Automation in Silverlight - Part II (The Easy Way)</a>

<a href="http://timheuer.com/blog/archive/2009/02/26/silverlight-testing-frameworks.aspx">Testing Silverlight Applications</a>

<a target="_blank" href="http://developer.yahoo.com/dotnet/silverlight/2.0/unittest.html">Silverlight Unit Test Review</a>

<a target="_blank" href="http://reddevnews.com/blogs/rdn-express/2010/06/plug-in-to-silverlight-ui-automation.aspx">Plug-in to Silverlight UI Automation</a>

<a target="_blank" href="http://www.ranorex.com/support/support-center.html">Ranorex Use Guide Support Center</a>

源码和测试视频见附件。

<a href="http://down.51cto.com/data/2359679" target="_blank">附件:http://down.51cto.com/data/2359679</a>

本文转自chenkaiunion 51CTO博客,原文链接:http://blog.51cto.com/chenkai/764591