WatiN和HttpWatch交互簡介

Httpwatch是一款強大的網頁數據分析工具,它能夠在不改變瀏覽器和網絡設置的基礎上捕捉http和https數據。查看底層的http數據,包括headers, cookies, cache等,同時統計發送接收請求時間,並提供完備的日誌記錄系統。同時該工具具備完備的COM接口,用於給用戶經過編程的方式操縱httpwatch.編程

Httpwatch 自動化對象介紹:瀏覽器

Controller 類:老是控制的起點,它用於建立httpwatch插件接口或讀取log文件,經過IE屬性和Firefox屬性的new()方法或者Attach()方法返回Plugin實例對象。cookie

Plugin 類:是httpwatch與瀏覽器交互的接口。提供了方法用於開始和中止http流量的記錄,還有不少方法和屬性管理httpwatch的日誌文件和配置自動化記錄。網絡

Log 類: Plugin對象的Log屬性是Log對象的入口。包含了一系列經過httpwatch記錄的日誌。Log對象中具備不少重要的類,好比Pages, Entries類。ide

Entry 類: 每一個Log對象都包含一組Entry對象,它表明的是單個HTTP事務的詳情(好比http請求)工具

經過 WatiN和httpwatch結合進行自動化測試,能夠很大程度擴展自動化測試的範圍,好比在自動化測試過程當中驗證連接的連通性,網頁傳輸速率和時間,查看網頁的詳細信息等。測試

C#操做httpwatchui

  1. 添加httpwatch自動化類庫到項目中
  2. 建立與httpwatch工具的聯繫

    建立新的IE接口:頂部添加引用  using HttpWatch;url

1             Controller control = new Controller();
2             Plugin plugin = control.IE.New();
View Code

    建立新的Firefox接口:spa

1             Controller control = new Controller();
2             Plugin plugin = control.Firefox.New();
View Code

    附加一個已存在的IE窗口:

1             SHDocVw.IWebBrowser2 ieBrowser = new SHDocVw.InternetExplorer();
2             ieBrowser.Visible = true;  //Required to see new window
3             Controller control = new Controller();
4             Plugin plugin = control.IE.Attach(ieBrowser);
View Code

    附加一個已存在的Firefox窗口:

1             Controller control = new Controller();
2             Plugin plugin = control.Firefox.Attach(firefoxBrowser);
View Code

  3. 經過接口讀取http詳細信息

Example:

 1         public static HttpWatchInfo RunHttpWatch(string url)
 2         {
 3             Controller control = new Controller();
 4             Plugin plugin = control.IE.New();
 5 
 6             plugin.Log.EnableFilter(false);
 7             plugin.Record();
 8             plugin.GotoURL(url);
 9             control.Wait(plugin, -1);
10             plugin.Stop();
11 
12             HttpWatchInfo httpWatchSummary = new HttpWatchInfo();
13             if (plugin.Log.Pages.Count != 0)
14             {
15                 Summary summary = plugin.Log.Pages[0].Entries.Summary;
16                 httpWatchSummary.URL = url;
17                 httpWatchSummary.Time = summary.Time;
18                 httpWatchSummary.RoundTrips = summary.RoundTrips;
19                 httpWatchSummary.BytesReceived = summary.BytesReceived;
20                 httpWatchSummary.BytesSent = summary.BytesSent;
21                 httpWatchSummary.CompressionSavedBytes = summary.CompressionSavedBytes;
22 
23                 IList<string> codes = new List<string>();
24                 for (int i = 0; i < summary.StatusCodes.Count; i++)
25                     codes.Add(summary.StatusCodes[i].Result);
26                 httpWatchSummary.StatusCode = codes;
27 
28                 IList<HttpWatchDetail> httpWatchDetail = new List<HttpWatchDetail>();
29                 for (int i = 0; i < plugin.Log.Entries.Count; i++)
30                 {
31                     HttpWatchDetail detail = new HttpWatchDetail();
32                     detail.URL = plugin.Log.Entries[i].URL;
33                     detail.Result = plugin.Log.Entries[i].Result;
34                     detail.Time = plugin.Log.Entries[i].Time.ToString();
35                     detail.Error = plugin.Log.Entries[i].Error;
36                     try
37                     {
38                         detail.Content = plugin.Log.Entries[i].Content.Data;
39                     }
40                     catch
41                     {
42                         detail.Content = null;
43                     }
44                     httpWatchDetail.Add(detail);
45                 }
46                 httpWatchSummary.Details = httpWatchDetail;
47             }
48             plugin.CloseBrowser();
49             return httpWatchSummary;
50         }
View Code
相關文章
相關標籤/搜索