WatiN和HttpWatch交互简介
Httpwatch是一款强大的网页数据分析工具,它可以在不改变浏览器和网络设置的基础上捕捉http和https数据。查看底层的http数据,包括headers, cookies, cache等,同时统计发送接收请求时间,并提供完备的日志记录系统。同时该工具具有完备的COM接口,用于给用户通过编程的方式操纵httpwatch.
Httpwatch 自动化对象介绍:
Controller 类:总是控制的起点,它用于创建httpwatch插件接口或读取log文件,通过IE属性和Firefox属性的new()方法或者Attach()方法返回Plugin实例对象。
Plugin 类:是httpwatch与浏览器交互的接口。提供了方法用于开始和停止http流量的记录,还有很多方法和属性管理httpwatch的日志文件和配置自动化记录。
Log 类: Plugin对象的Log属性是Log对象的入口。包含了一系列通过httpwatch记录的日志。Log对象中具有很多重要的类,比如Pages, Entries类。
Entry 类: 每个Log对象都包含一组Entry对象,它代表的是单个HTTP事务的详情(比如http请求)
通过 WatiN和httpwatch结合进行自动化测试,可以很大程度扩展自动化测试的范围,比如在自动化测试过程中验证链接的连通性,网页传输速率和时间,查看网页的详细信息等。
C#操作httpwatch
- 添加httpwatch自动化类库到项目中
- 创建与httpwatch工具的联系
创建新的IE接口:顶部添加引用 using HttpWatch;
1 Controller control = new Controller(); 2 Plugin plugin = control.IE.New();
创建新的Firefox接口:
1 Controller control = new Controller(); 2 Plugin plugin = control.Firefox.New();
附加一个已存在的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);
附加一个已存在的Firefox窗口:
1 Controller control = new Controller(); 2 Plugin plugin = control.Firefox.Attach(firefoxBrowser);
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 }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。