概述
K1q*a` KK8A:J0 Httpwatch是一款强大的网页数据分析工具,安装后集成在IE和Firefox工具栏中。它可以在不改变浏览器和网络设置的基础上捕捉Http和Https数据。查看底层的HTTP数据,包括headers,cookies,cache等等,同时统计发送接收请求的时间,并提供完备的日志记录系统。同时该款工具具有完备的COM接口,用于给用户通过编程的方式操纵httpwatch。
8a#Z"QqN_fS051Testing软件测试网1_'s;H;Q0o Uo
)B)Yi(ef4s
vcfL1?0
d3^
f0L6]7w0Httpwatch自动化对象介绍
BXaC6[3n{0 Httpwatch自动化对象的结构如下:51Testing软件测试网d2PDL l g
JY1MW$f NL
O].E
`:?9R!n0
k9K4arr0Controller类
b)Hv)Mb
g r]
T0 Controller类总是控制的起点,它用于创建Httpwatch插件接口或者读取log文件,通过IE属性和Firefox属性的new()方法或者Attch()方法返回Plugin实例对象
:o%qeic7|7vq'Tq0Plugin类51Testing软件测试网@_srYne]U
Plugin类是Httpwatch与浏览器交互的接口。它提供了方法用于开始和停止http流量的记录。还有很多方法和属性用于管理Httpwatch的日志文件和配置自动化记录。
l*{7@L$Ig7N0Log类
pa~0tOsQ~4k%P-~ `0 Plugin对象的Log属性是Log对象的入口。它包含了一系列通过Httpwatch记录的日志。Log对象中具有很多重要的类,比如Pages,Entries类。51Testing软件测试网WC |p.X lT`5a']
51Testing软件测试网+_"B L6O5ge$M
Entry类51Testing软件测试网2mS3w+SBn C9@
每个Log对象都包含一组Entry对象,它代表的是单个HTTP事务的详情。(比如http请求)51Testing软件测试网'Z2hq4p7Z/Rt
后面我们会通过实例来进一步说明该类的方法和属性。
)B`(xpEK7rfg0 51Testing软件测试网\Cwu,w
通过把Watin和Httpwatch结合进行自动化测试,可以很大程度的扩展自动化测试的范围,比如在自动化测试过程中验证链接的连通性,网页传输的速率和时间,查看网页的详细信息等等。51Testing软件测试网Z;`!P1|r+\r
MEh2{
!b
xxW%Q#obe0使用C#操作Httpwatch
~1G1OfQ'h*`0 使用C#操纵httpwatch大致分为以下几个步骤:51Testing软件测试网/qJ~B*x8O'Y*D_D
1) 添加HTTPwath自动化类库到你的项目中51Testing软件测试网 F#Dr'M8Yc+P@
2) 创建与Httpwatch工具的联系
[Xp8IF,x0hC0创建新的IE接口51Testing软件测试网/p$C'[|`_?/B*@
\0~7_?} f[Z&y(n;b+{0
HttpWatch.Controller controller =newHttpWatch.Controller(); !FOn5IN!Rp
n;u0HttpWatch.Plugin plugin = controller.IE.New(); h;GelIU
E#z0 |
#U#r?4AdLH
v0创建新的Firefox接口51Testing软件测试网9t wo Ep a1i o
`w)o
j*@0
HttpWatch.Controller controller =newHttpWatch.Controller(); 8_o5L;Y{e8}*k!Q?)a0HttpWatch.Plugin plugin = controller.Firefox.New("");51Testing软件测试网K~i
VRW9\ |
#FF'M2U9PYv8y0附加一个已存在的IE窗口
Hmv/V4`01k'u'xM{)`}0
SHDocVw.IWebBrowser2 ieBrowser =newSHDocVw.InternetExplorer(); %O$H7r9Y+f'c0ieBrowser.Visible =true; // Required to see the new window 5{,N/j}%V8XTG)K0 . . . Q)P(c,jC0iR0HttpWatch.Controller controller =newHttpWatch.Controller();51Testing软件测试网s;Wv6I#k{i HttpWatch.Plugin plugin = controller.IE.Attach(ieBrowser); ?&ks,]/F!G0 |
?)~X
m8x0附加一个已存在的Firefox窗口51Testing软件测试网j0d/KY+C1TH:`jx~(N
51Testing软件测试网L%V2A V8]+_HttpWatch.Controller controller =newHttpWatch.Controller(); \
u?"^a3{W0HttpWatch.Plugin plugin = controller.Firefox.Attach("FirefoxProfileName"); m1PI @@S0{sV0 51Testing软件测试网
I,@^$h} |
51Testing软件测试网%q1n0B,s&l:D3o]3) 通过接口读取HTTP详细信息51Testing软件测试网4^~oI
x:q)AT
51Testing软件测试网2}"pk`
l^.O gIplugin.Log.Save("E:\\baidu.hwl");51Testing软件测试网3[vr`:{D z[#Y(c foreach(HttpWatch.Pagepageinplugin.Log.Pages) #s,q&@Z7?o6YkU
R0{ 0On5c9@*i.Z0Console.WriteLine(page.Title);//输出网页的标题51Testing软件测试网
Mu\#OIZ2D } ~%juL \U0 |
%J1Q;S Cp K051Testing软件测试网O(?!mf6J;ab2r
51Testing软件测试网w;xyqV
Watin与Httpwatch结合
3P{4g;v1lM"O8ydg$A0 我们依然以访问百度网站为例,并进行查询操作.最后输出访问过程中HTTP请求各个资源的时间和状态。我们可以加入判断,判断哪些请求的状态有异常,哪些请求的时间过长。(请使用IE6,IE7好像有些问题)具体代码如下:51Testing软件测试网n5JV^i*Ux5f6t
51Testing软件测试网6R:g:|vKH~8|n
using System.Collections.Generic; o1B3h
vm`X3u&TX0using System.Linq;51Testing软件测试网(gP!rx7s5y+g using System.Text;51Testing软件测试网6Z"OU1rg using WatiN.Core; 2OZ'kvVHoQ0namespace ConsoleApplication151Testing软件测试网
h6\)fG{[t,D {51Testing软件测试网IG&~P4GE@ k class Program51Testing软件测试网Cq1O| U4p^0?5? {51Testing软件测试网:?| Nc"Zl1@}7W"v(b [STAThread] *T(hx c1x p0 static void Main(string[] args)51Testing软件测试网X
M~A:\Law {51Testing软件测试网 | I.^-bg //新建IE实例 &L;}*w p8~_0 IE ie = new IE();51Testing软件测试网9d)r D C'Dca //建立Httpwatch控制类 4u+h@9@x$x9g0 HttpWatch.Controller controller = new HttpWatch.Controller();51Testing软件测试网OdV3z'W,U@6J
D //将ie实例附加到Httpwatch接口,从而使Watin和httpwatch与同一IE建立关联 .G2H$Hj;uo1t8xb,Gy0 HttpWatch.Plugin plugin = controller.IE.Attach((SHDocVw.IWebBrowser2)ie.InternetExplorer); 8N;fEyk)w0 //Httpwatch开始记录51Testing软件测试网-G
B||c&x8slw%i D iu0y plugin.Clear();51Testing软件测试网6F.E O3d{'f#xtsst
rr plugin.Record(); B lrL~0 //导航到百度51Testing软件测试网)qV%Z*kb7YHy ie.GoTo("www.baidu.com"); Wu@l5ZuD/s$c0 //搜索“watin” z7W]%]{4U0 ie.TextField(Find.ById("kw")).TypeText("Watin"); 8l&Z^rR
|0 ie.Button(Find.ById("su")).Click();51Testing软件测试网(^p_l
]RT //停止httpwatch记录51Testing软件测试网$K.s*k(h`:~1H plugin.Stop();51Testing软件测试网k1e^T{Mk //创建Log实例 V&p8g)j2@$UW0 HttpWatch.Log log = plugin.Log; Y:\
I8H8?,ih,j3z0 //输出请求异常的url51Testing软件测试网NU+u$N5}Q)_F Console.WriteLine("请求异常的地址:"); }bp;q&@OI5L0 foreach (HttpWatch.Page page in log.Pages)51Testing软件测试网:H,mpN3h)@X&}6^ {51Testing软件测试网;[od*C@)w 51Testing软件测试网V'f\7}:_#N
Z(A H Console.WriteLine("------------"+page.Title+"--------------");51Testing软件测试网oUs
Mn9Q foreach (HttpWatch.Entry entry in page.Entries)51Testing软件测试网e0P1hwa#pD m { 4XTv,D0MqXt0 if (entry.Result != "200" & entry.Result != "(Cache)")51Testing软件测试网%C _"~
a7lJH#~;U { V c/{3T4FW_ID0 Console.WriteLine(entry.URL + " : " + entry.Result); fI0Ko:}4N4ZQ)a0 }51Testing软件测试网8aqbV
S'P5\ } !aM#dm!^{0 51Testing软件测试网cx;KU0m\6xS'u"{ }51Testing软件测试网f%B0sb^4W4y Console.WriteLine("\r\n\r\n"); 6kk S`&U3H2{ Y0 //输出请求超过0.1s的url51Testing软件测试网9dvj1m_3qB Console.WriteLine("请求超过0.1s的地址:");51Testing软件测试网Pmv WT+s&} foreach (HttpWatch.Page page in log.Pages) g)?1a9nX$}*U0 { V M9fDL0 51Testing软件测试网%w Gaorm^/_ Console.WriteLine("------------" + page.Title + "--------------"); F}(D.@}\p0 foreach (HttpWatch.Entry entry in page.Entries)51Testing软件测试网\QG*M#r {51Testing软件测试网z}:ZZ|Pk0N uB if (entry.Time > 0.1) zSipE2@3Oz0 {51Testing软件测试网&nbmIk
O Console.WriteLine(entry.URL + " : " + entry.Time + "s"); 8bc2h+fO9a)D*Qh\0 }51Testing软件测试网[HY'V WA3W5Evi }51Testing软件测试网:wbaH1pE,v$k } r.CA$?:j2|+J*y0 Console.ReadLine();51Testing软件测试网M]h
sc8l3k7a] } p
cN2i*U!oJ$]0 } LfSS'nn0}51Testing软件测试网8PJ `o Eb 51Testing软件测试网 ]1_H&O1_6\%N |
`*b7P0m'}0结果显示如下:
"E6B/ke{"qnT051Testing软件测试网-jG9H kc
[9P7K
L,\(w'?G8@8wt9?0结束语51Testing软件测试网Z ev
|#T8D
我在本篇文章中只是列举了httpwatch的一些简单应用,有对http信息分析有更深入需求的可以参考HttpWatch Automation Reference,
进一步了解httpwatch的自动化对象。51Testing软件测试网%t:|K#[|3P P"y
oe6q+f^
up.}0资源及资源51Testing软件测试网
{9f8S.i,L(@Syl
1. Httpwatch官网
"j!mf|t%U#q/]02. Using HttpWatch with WatiN
uAFs0u+V8w3F0&||;w:?-|,c
B3k-s0