验证网站链接有效性的QTP代码

上一篇 / 下一篇  2009-09-08 11:56:34 / 个人分类:自动化测试

51Testing软件测试网r]Mh'f[B)O\

Function CheckLinks2 (BrowserObject,BrowserPage)
zN [.tV/N M0 CheckLinks2=TRUE
qF z9x [s;n+DG0 Dim orgURL,orgCreationTime
q!N"z L6~Jd*Z1D/E1f0 Dim i_Link, l_hWnd
(l9{ uq*Y^)DE0 Dim b_newBrowser
'R I tT.y0 Dim s_LinkOuterText,s_LinkInnerText,s_Linkhref51Testing软件测试网 J.N1v j-UT5W2`

q&u9IY&}q0 s_URL=BrowserPage.GetROProperty("url")51Testing软件测试网x8_:U#R2u{P
 l_hWnd=BrowserObject.GetROProperty("hwnd")
8Q$j Lv5eD Gka0 
-a-{D@/H9G"`u0 i_CreationTime=1
#Q.B$oy([8egWo0 51Testing软件测试网9UtX#]Z dd!o-d
 i_LinkCount=BrowserPage.object.links.length - 151Testing软件测试网#I4t+G RfB_
 51Testing软件测试网2Yw?;lA4F"~9I
 For i_Link=0 to i_LinkCount

y$\ I Bo B!l){051Testing软件测试网W5}3jD F s*~

   If Trim(BrowserPage.object.links(i_Link).target)="" and Instr(BrowserPage.object.links(i_Link).href,"javascrīpt:")=0 Then51Testing软件测试网u?8B r([%r/aek
    b_newBrowser=False
d N&K7],X7E:|0   else
%}$P'` S+I&]0    b_newBrowser=TRUE51Testing软件测试网 ^\~V-h,Bd0A!z r'ok1y
     End If51Testing软件测试网I7Rw&^@ d[ N
 
3k@ kAi0TdN0   s_LinkOuterText=BrowserPage.object.links(i_Link).outerText51Testing软件测试网E-lVO,a
   s_LinkInnerText=BrowserPage.object.links(i_Link).innerText
p3_]8xP0   s_Linkhref=BrowserPage.object.links(i_Link).href51Testing软件测试网9l!R-?^Tf

51Testing软件测试网`"O]1z}w(P

   BrowserPage.object.links(i_Link).click

+p7JC;_ S\v0

S H,JPp2z2e F!X1i0   'Doing Browser.Exist with CreationTime:=1 when the browser does not exist will give TRUE so i though of work around to compare the windows51Testing软件测试网;U6i6e$O+u+U
   ' handle for the old browser and the new browser with CreationTime:=1. If they are same that mean no other window was opened.51Testing软件测试网:`8v^ S6v)}3dV
   If b_newBrowser Then
%}9B s)d1H q1X0Q&[0    l_newhWnd=Clng(Browser("CreationTime:=" & i_CreationTime).GetROProperty("hwnd"))
!A8m`;M}8Q:Q0    If  clng(l_hWnd) =l_newhWnd Then
(R0`wV]jd:o0     b_newBrowser=False51Testing软件测试网 Twm!t e'xb.o
    end if51Testing软件测试网 q"_]8Rtu8E
   end if
vn1T#gfS'p0   51Testing软件测试网i-fta6?9e Q
   On error resume next
*G@-fy`z%e0   If b_NewBrowser Then51Testing软件测试网Pmo#gH a'j)n.lx7H
    Browser("CreationTime:=" & i_CreationTime).sync51Testing软件测试网 zB`0z"K)E
    Browser("CreationTime:=" & i_CreationTime).Page("micClass:=Page").sync51Testing软件测试网r:X+n5KP@kY!?
   Else51Testing软件测试网3w M7g3_'N|oB
    BrowserObject.sync51Testing软件测试网;Zp@@'m
    BrowserPage.sync
2c2b ~ V@3s?^ds#P0   End if51Testing软件测试网6_Y8QDFj;|,~
   On error goto 051Testing软件测试网 r8QP1n qKC4P
   
W U L7BhBZ0   Dim s_LinkDetails51Testing软件测试网,`b6T]6L3w
 51Testing软件测试网XSR9~pGr5hh _
   If b_NewBrowser Then51Testing软件测试网)]*F(S"o!J%ti
    IHTML = Browser("CreationTime:=" & i_CreationTime).Page("micClass:=Page").object.Body.innerHTML51Testing软件测试网G`\hc!v
   else
N8rt;c(_0    IHTML = BrowserPage.object.Body.innerHTML
X W#`qzq.aaJ0   End If51Testing软件测试网$t9uq }/X

51Testing软件测试网b o)]4S ia)E1HoA

   'Check if page was not able to be displayed you can update this code any time
I@s:U E!q*G0   If (InStr(IHTML,"HTTP 404") <> 0) Or (InStr(IHTML,"cannot be displayed") <> 0)  Then51Testing软件测试网jPjzH-tH[t
    s_LinkDetails="Link Broken" + vbcrlf + "Link Details:" +vbcrlf
d mKN;Nx G v0    s_LinkDetails=s_LinkDetails+"OuterText: "+ s_LinkOuterText + vbcrlf
9|"@v9R*ibt_,uF[0    s_LinkDetails=s_LinkDetails+"InnerText: "+  s_LinkInnerText + vbcrlf51Testing软件测试网ln ~'Qcgs-O
    s_LinkDetails=s_LinkDetails+ "href: " + s_Linkhref+ vbcrlf51Testing软件测试网$o{q%m"JNv
    s_LinkDetails=s_LinkDetails+ "Links Open in New Browse: " & bNewBrowser & vbcrlf51Testing软件测试网ymrQR { ahR"}
    Reporter.ReportEvent micWarning,"Check Link(" & i_Link & ") -> " & s_LinkOuterText  ,s_LinkDetails51Testing软件测试网E-QMK'H%m
    CheckLinks2=FALSE51Testing软件测试网.~?3EN8kC O
   Else
!Qg[(f"M0T \+Uc7Z0    s_LinkDetails="Link Working" + vbcrlf + "Link Details:" +vbcrlf51Testing软件测试网O4?'I+X,qRW-f.V
    s_LinkDetails=s_LinkDetails+"OuterText: "+ s_LinkOuterText + vbcrlf51Testing软件测试网\WxW}D8|
    s_LinkDetails=s_LinkDetails+"InnerText: "+  s_LinkInnerText+ vbcrlf51Testing软件测试网UH%G Va w;A|
    s_LinkDetails=s_LinkDetails+ "href: " + s_Linkhref+ vbcrlf51Testing软件测试网 s#F_b]c&x"[
    s_LinkDetails=s_LinkDetails+ "Links Open in New Browse: " & bNewBrowser & vbcrlf
,_#v%_@ T.K!t%Q0    Reporter.ReportEvent micPass,"Check Link(" & i_Link & ") -> " & s_LinkOuterText  ,s_LinkDetails
kp!WH-T`];J |%^ O0   End If
g c4PAmn e V$W.D0 
f2D2SOAf{?0   If b_NewBrowser Then
x J Nq9b)P'ui~%X0    while Clng(Browser("CreationTime:=" & i_CreationTime).GetROProperty("hwnd"))<>l_hwnd
,Ep @_6A)u(RA@0     On error resume next51Testing软件测试网V!hRG P
     Browser("CreationTime:=" & i_CreationTime).Sync51Testing软件测试网*TQ6QIJ}
     Browser("CreationTime:=" & i_CreationTime).Page("micClass:=Page").Close
(\Xdna)l5W,x0     On error goto 0
acy6po}!S0     Browser("CreationTime:=" & i_CreationTime).Close
4gg ~2s3C8w B0m X0     'Tackle links that open a new browser and also opens a popup using that
7S'V,E9S-uD*\0     'i_CreationTime=i_CreationTime + 151Testing软件测试网DD ` q`0qg-`
    Wend
"C-ra oi Oq(@}0    
_*y8Vf\0    i_CreationTime=1
?.yJ uK8_0^/f0    
c:s.g5b^%r!D P G.A,`0   else51Testing软件测试网O*jd-T)g b z
    On error resume next51Testing软件测试网.C8cg5j4|k(U;N
    BrowserObject.Navigate s_URL51Testing软件测试网8R1d [e(Xlp
    BrowserObject.Sync51Testing软件测试网 C'?:Fy [b'?`
    BrowserObject.Page.Sync
+p:?,u rLc-u:q0    On error goto 0
^{ ?BGyYP0   end if51Testing软件测试网XeY7M5u!U
 Next
@!DW5|H@5c0End Function51Testing软件测试网!^ p3wK.k3A

51Testing软件测试网6q"}_)aCs|

 

2h{;`j"Xz0'*********************  InQTP**************8
2U9sn8iO/t0 Set BrowserObject = Browser("micClass:=Browser","CreationTime:=0")51Testing软件测试网-w^:TG%DRV8I
 Set BrowserPage = BrowserObject.Page("micClass:=Page")51Testing软件测试网J-n-FY:P B5R
 CheckLinks2 BrowserObject,BrowserPage
*x b0M]XPi0'*********************************************

TAG: 链接 有效 QTP qtp

 

评分:0

我来说两句

日历

« 2022-01-19  
      1
2345678
9101112131415
16171819202122
23242526272829
3031     

数据统计

  • 访问量: 37977
  • 日志数: 47
  • 建立时间: 2009-09-03
  • 更新时间: 2010-06-10

RSS订阅

Open Toolbar