VisualStudio2010新增了UI測試功能。想必很多人都嘗試過了。作為錄制/回放工具的一種,我想用它在web應用上也嘗試一下。雖然知道web上的自動化測試歷來是極其麻煩的一種,但考慮到操作系統是ms家的、瀏覽器是ms家的、自動化工具是ms家的甚至C#語言也是ms家的,說不定兼容性能好一些。經過試用之后,發現錄制的腳本可以在IDE環境中執行(在測試管理器里應該也是可以的,不過仍然沒有脫離環境),但還沒有達到我進行無人值守測試的“遠大目標”。于是查閱資料寫了一個簡易的執行框架。工作流程是這樣的:
將這個框架編譯成一個exe文件,并加入計劃任務管理器中定時執行,框架完成的工作是根據配置文件依次執行腳本并給出執行結果的日志。
后續又做了一些額外的工作是,自動獲取最新版本,自動解壓部署等。把這些都放入一個批處理文件中。所以最后在計劃任務中執行的是批處理文件。不過那與visualstudio2010無關了。
框架完整代碼如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;
using System.Xml;
namespace autotest
{
class Program
{
static void Main(string[] args)
{
string logFilePath = @"log\"; //日志寫入固定目錄"log"
string logFileName;
string dllPath = @"auto_dll\"; //dll文件取自固定子目錄"auto_dll"
string dllName, dllFullName, dllEnterClass, dllRecordClass;
string xmlFileName = @"auto_dll\config.xml"; //取特定配置文件config.xml
int sleepTime; //每個腳本執行完畢后的延時
string dllDescription; //從配置文件中讀取,用于日志顯示
if (!Directory.Exists(logFilePath))
{
Directory.CreateDirectory(logFilePath); //檢查和創建日志文件夾
}
logFileName=logFilePath + "log_" + DateTime.Now.Year.ToString() + "_" + DateTime.Now.Month.ToString() + "_" + DateTime.Now.Day.ToString() + ".log"; ;
StreamWriter sw;
if (!File.Exists(logFileName))
{
sw = File.CreateText(logFileName); //檢查和創建日志文件
}
else
{
sw = File.AppendText(logFileName);
}
sw.WriteLine("[啟動時間]: " + DateTime.Now.ToString());
sw.WriteLine("---------------------------------------------------------------------------------------");
XmlDocument xmlDoc = new XmlDocument();
try
{
xmlDoc.Load(xmlFileName);
}
catch //處理xml解析出錯異?!渲梦募e誤直接退出程序
{
sw.WriteLine("xml文件解析錯誤,系統退出!");
sw.Close();
System.Environment.Exit(0);
}
XmlNodeList xnl = xmlDoc.GetElementsByTagName("NeedTest"); //查找xml中NeedTest節點
foreach (XmlNode Node in xnl.Item(0).ChildNodes) //遍歷節點下所有子節點,循環
{
XmlNodeList xnl1 = Node.ChildNodes;
dllName = xnl1.Item(0).InnerText;
dllFullName = dllPath + dllName + ".dll";
dllEnterClass = dllName + ".tm";
dllRecordClass = dllName + ".CodedUITest1"; //取得dll全路徑名稱、dll中的兩個類名稱固定采用tm和CodedUITest1
try
{
sleepTime = Convert.ToInt16(xnl1.Item(1).InnerText); //轉換字符串為延時時間數字值,如有異常默認設置為10;
}
catch
{
sleepTime = 10;
}
dllDescription = xnl1.Item(2).InnerText;
sw.WriteLine(dllName + ": " + dllDescription); //取得dll描述信息寫入日志
try
{
Assembly ass1 = Assembly.LoadFrom(dllFullName); //加載dll
sw.WriteLine(dllName + ".dll 加載成功");
Type MyAppType = ass1.GetType(dllEnterClass);
Type MyAppType1 = ass1.GetType(dllRecordClass);
object obj = Activator.CreateInstance(MyAppType); //創建tm對象和CodedUITest1對象
object obj1 = Activator.CreateInstance(MyAppType1);
try
{
MyAppType.GetMethod("Init").Invoke(obj, null); //執行tm對象中的Init方法
MyAppType1.GetMethod("CodedUITestMethod1").Invoke(obj1, null); //執行CodedUITest1對象中的CodedUITestMethod1方法
}
catch(Exception e) //處理執行失敗異常,寫入日志
{
sw.WriteLine(e.Message);
sw.WriteLine("回放過程失敗");
}
finally //清理,避免一個腳本執行不成功引起后續腳本都不能執行成功
{
try
{
MyAppType.GetMethod("Cleanup").Invoke(obj, null);
}
catch { throw new System.Exception("Cleanup執行異常"); }
}
}
catch(Exception ee) //捕捉全過程所有異常,寫入日志
{
sw.WriteLine(ee.Message);
sw.WriteLine(dllName+".dll 運行失敗");
sw.WriteLine();
System.Threading.Thread.Sleep(new TimeSpan(0, 0, 0, sleepTime, 0));
continue;
}
sw.WriteLine(dllName+"運行成功"); //成功,寫入日志
sw.WriteLine();
System.Threading.Thread.Sleep(new TimeSpan(0, 0, 0, sleepTime, 0));
} //下一循環
sw.Close(); //關閉日志文件
}
}
}之所以考慮這樣,是因為開發部門已經實現了持續集成,我也希望能做到持續的回歸測試。經過一段時間的實驗證明,這個框架本身工作還算穩定,但執行結果嘛——就只能“呵呵”了。具體原因,請等待我在下一篇吐槽。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。