Find Broken Links using QTP without directly opening the links

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
'This is the main page
Browser("Browser").Page("HomePage").Sync

'Find out all the links in the page using ChildObjects
Set oLink = Description.Create
oLink("micclass").Value = "Link"
Set oAllLinks = Browser("Browser").Page("HomePage").ChildObjects(oLink)

'Find out the count of links
iTotalLinks = oAllLinks.Count

'Loop through all the links to find if the link is broken or not
For i=0 to iTotalLinks - 1

'Find out the url for the link
sURL = oAllLinks(i).GetROProperty("url")

'Create a WinHTTP Request using the link's URL
Set objWinHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
objWinHTTP.Open "GET", sURL, False
objWinHTTP.SetRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MyApp 1.0; Windows NT 5.1)"

'Send the Request to the Server and capture the response
objWinHTTP.Send
iReturnVal = objWinHTTP.Status

'Find out if the Link exists or is broken
If iReturnVal = 200 Then
msgbox "Link - " & sURL & " Exists"
ElseIf iReturnVal = 404 Then
msgbox "Link - " & sURL & " is Broken"
Else
msgbox "Code" - iReturnVal
End If

Set objWinHTTP = Nothing

Next
唐胡璐 wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
分享创造价值,您的支持将鼓励我继续前行!