天天看點

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