You are on page 1of 5

Checking all links on a website isn’t that easy as it might look.

The scripts demonstrates


on how to do it for a normal website. What it does is that it counts the links on the
homepage and then one by one click on the links and close the popup window in case it’s
opened in a new window.

Features:

• Closing the popup when clicking a link opens ones


• Workaround for determining if a popup is present or not. QTP provides “Exist”
function for checking if a object exists or not. For checking a popup browser we
can use Browser(”CreationTime:=1″).Exist and use
Browser(”CreationTime:=1″).Close the popup window. But the problem is that
sometimes QTP returns true even when the browser does not exists and using
close function closes the main browser window which leads to the failure of the
script. The script uses a workaround by comparing the hwnd of the main browser
and the popup browser and closing the popup only if the handles don’t match.
• Checks for normal 404 or page cannot be displayed messages in the source code
of the browser.

Limitation:

• More than 1 popup: Opening a website or a link which leads to other popup will
make the script fail as it closes on 1 popup. But with small enhancement it can be
easily changed to support these websites also
• Ignoring a link: Clicking on links like “Logout” is not excluded and hence may
fail the script
• Dynamic website: If clicking of a link changes the home page of a website then
the script will fail. Consider the case of www.google.com, now if you run the
script on this page it will fail becuase when the script clicks on one of the
languages link, the home page of website will change and even the no. of links
might change. This can be correct by two ways, one is to close all the browsers
every time and delete all cookies and then go for the next link and other ways
would be to delete the session cookies of the browser. For deleting session
cookies there is no software available except the one that I created and is present
on this website

Function CheckLinks (BrowserObject, BrowserPage)


CheckLinks = True
sPage = "micclass:=Page"
Dim s_URL, i_CreationTime
Dim s_LinkOuterText, s_LinkInnerText, s_Linkhref
s_URL = BrowserPage.GetROProperty("url")
i_CreationTime = 1
i_LinkCount = BrowserPage.Object.links.Length - 1

Dim i_Link

For i_Link = 0 To i_LinkCount


If Trim(BrowserPage.Object.links(i_Link).target) = "" Then
'Set the link to open i a new window so that we dont
'have any change in current window
BrowserPage.Object.links(i_Link).target = "_blank"
End If

BrowserPage.Object.links(i_Link).click
On Error Resume Next
Browser("CreationTime:=" & i_CreationTime).sync
Browser("CreationTime:=" & i_CreationTime).Page(sPage).sync
On Error GoTo 0
Dim s_LinkDetails

IHTML = Browser("CreationTime:=" &


i_CreationTime).Page(sPage).Object.Body.innerHTML
'Check if page was not able to be displayed
If (InStr(IHTML, "HTTP 404") <> 0) Or (InStr(IHTML, "cannot be
displayed") <> 0) Then
s_LinkDetails = "Link Broken" + vbCrLf + "Link Details:" +
vbCrLf
s_LinkDetails = s_LinkDetails + "OuterText: " +
s_LinkOuterText + vbCrLf
s_LinkDetails = s_LinkDetails + "InnerText: " +
s_LinkInnerText + vbCrLf
s_LinkDetails = s_LinkDetails + "href: " + s_Linkhref +
vbCrLf
s_LinkDetails = s_LinkDetails + "Links Open in New Browse: "
& bNewBrowser & vbCrLf
Reporter.ReportEvent micWarning, "Check Link(" & i_Link & ")
-> " & s_LinkOuterText , s_LinkDetails
CheckLinks = FALSE
Else
s_LinkDetails = "Link Working" + vbCrLf + "Link Details:" +
vbCrLf
s_LinkDetails = s_LinkDetails + "OuterText: " +
s_LinkOuterText + vbCrLf
s_LinkDetails = s_LinkDetails + "InnerText: " +
s_LinkInnerText + vbCrLf
s_LinkDetails = s_LinkDetails + "href: " + s_Linkhref +
vbCrLf
s_LinkDetails = s_LinkDetails + "Links Open in New Browse: "
& bNewBrowser & vbCrLf
Reporter.ReportEvent micPass, "Check Link(" & i_Link & ") ->
" & s_LinkOuterText , s_LinkDetails
End If

Browser("CreationTime:=1").Close ' Close the link open.


Next
End Function

Function CheckLinks2 (BrowserObject, BrowserPage)


CheckLinks2 = True

sPage = "micclass:=Page"
sBrowser = "creationtime:="
Dim orgURL, orgCreationTime
Dim i_Link, l_hWnd
Dim b_newBrowser
Dim s_LinkOuterText, s_LinkInnerText, s_Linkhref

s_URL = BrowserPage.GetROProperty("url")
l_hWnd = BrowserObject.GetROProperty("hwnd")

i_CreationTime = 1

i_LinkCount = BrowserPage.Object.links.Length - 1

For i_Link = 0 To i_LinkCount

If Trim(BrowserPage.Object.links(i_Link).target) = "" _
And InStr(BrowserPage.Object.links(i_Link).href,
"javascript:") = 0 Then
b_newBrowser = False
Else
b_newBrowser = TRUE
End If

s_LinkOuterText = BrowserPage.Object.links(i_Link).outerText
s_LinkInnerText = BrowserPage.Object.links(i_Link).innerText
s_Linkhref = BrowserPage.Object.links(i_Link).href

BrowserPage.Object.links(i_Link).click

'Doing Browser.Exist with CreationTime:=1 when the browser does


not
'exist will give TRUE so i though of work around to compare the
windows
'handle for the old browser and the new browser with
CreationTime:=1.
'If they are same that mean no other window was opened.
If b_newBrowser Then
sBrowser = "CreationTime:=" & i_CreationTime
l_newhWnd = CLng(Browser(sBrowser).GetROProperty("hwnd"))
If CLng(l_hWnd) = l_newhWnd Then
b_newBrowser = False
End If
End If

On Error Resume Next


If b_NewBrowser Then
sBrowser = "CreationTime:=" & i_CreationTime
Browser(sBrowser).sync
Browser(sBrowser).Page(sPage).sync
Else
BrowserObject.sync
BrowserPage.sync
End If
On Error GoTo 0

Dim s_LinkDetails

If b_NewBrowser Then
sBrowser = "CreationTime:=" & i_CreationTime
IHTML = Browser(sBrowser).Page(sPage).Object.Body.innerHTML
Else
IHTML = BrowserPage.Object.Body.innerHTML
End If

'Check if page was not able to be displayed you can update this
code any time
If (InStr(IHTML, "HTTP 404") <> 0) Or (InStr(IHTML, "cannot be
displayed") <> 0) Then
s_LinkDetails = "Link Broken" + vbCrLf + "Link Details:" +
vbCrLf
s_LinkDetails = s_LinkDetails + "OuterText: " +
s_LinkOuterText + vbCrLf
s_LinkDetails = s_LinkDetails + "InnerText: " +
s_LinkInnerText + vbCrLf
s_LinkDetails = s_LinkDetails + "href: " + s_Linkhref +
vbCrLf
s_LinkDetails = s_LinkDetails + "Links Open in New Browse: "
& bNewBrowser & vbCrLf
Reporter.ReportEvent micWarning, "Check Link(" & i_Link & ")
-> " & s_LinkOuterText , s_LinkDetails
CheckLinks2 = FALSE
Else
s_LinkDetails = "Link Working" + vbCrLf + "Link Details:" +
vbCrLf
s_LinkDetails = s_LinkDetails + "OuterText: " +
s_LinkOuterText + vbCrLf
s_LinkDetails = s_LinkDetails + "InnerText: " +
s_LinkInnerText + vbCrLf
s_LinkDetails = s_LinkDetails + "href: " + s_Linkhref +
vbCrLf
s_LinkDetails = s_LinkDetails + "Links Open in New Browse: "
& bNewBrowser & vbCrLf
Reporter.ReportEvent micPass, "Check Link(" & i_Link & ") ->
" & s_LinkOuterText , s_LinkDetails
End If

If b_NewBrowser Then
sBrowser = "CreationTime:=" & i_CreationTime
While CLng(Browser(sBrowser).GetROProperty("hwnd"))<>l_hwnd
On Error Resume Next
Browser(sBrowser).Sync
Browser(sBrowser).Page(sPage).Close
On Error GoTo 0
Browser(sBrowser).Close
'Tackle links that open a new browser and also opens a
popup using that
'i_CreationTime=i_CreationTime + 1
Wend

i_CreationTime = 1

Else
On Error Resume Next
BrowserObject.Navigate s_URL
BrowserObject.Sync
BrowserObject.Page.Sync
On Error GoTo 0
End If
Next
End Function

'********************* In QTP **************


Set BrowserObject = Browser("micClass:=Browser", "CreationTime:=0")
Set BrowserPage = BrowserObject.Page("micClass:=Page")
CheckLinks2 BrowserObject, BrowserPage
'********************************************

You might also like