Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to find out error pages using QTP
#1
Solved: 11 Years, 4 Months, 3 Weeks ago
Hi All,

In our project, developers have recently changed the hard coded values href of links to some generalized urls. Now the requirement is that we need to validate the links of all web pages if they navigate to right url or will lead to any error pages. This can be done through Web Link validator tool but it can work on only static links and that too it cannot detect custom error pages (eg: browser title is Custom 404 error page). Through qtp, we can get the info about the links that are leading to custom error pages provided a page given.

To achieve the same, in the given page, the qtp fetches all links, opens each link in new window validates for the correctness of it and closes the new window. Since a page can contain some hundreds of links we are thinking to use Web validator link tool in our script which can do the operation in the back ground.

Not sure how to do this, just wanted to check if any such option is available. If anyone has experienced this issue, request you to share the details.

Thx in adv,

Sams
Reply
#2
Solved: 11 Years, 4 Months, 3 Weeks ago
We need to find out that whether the link is broken or not.

Please thru the below

https://www.learnqtp.com/how-to-identify...ks-in-qtp/

thanks
ssvali
Reply
#3
Solved: 11 Years, 4 Months, 3 Weeks ago
Hi

Will it able to find even custom error 404 pages also?
Reply
#4
Solved: 11 Years, 4 Months, 3 Weeks ago
Mod Update: This is from cache of KnowledgeInbox site (which seems to no more exist now). The article used to exist at knowledgeinbox(dot)com/articles/qtp/how-to/web/checking-all-links-on-a-web-page/
 
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. 

Code:
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
'********************************************


If you have extensive functional test cases in your automation, you can also go for 3rd party tool called  Xenu.
You can find more information about it at the below location.
http://home.snafu.de/tilman/xenulink.html

Regards,
Uday
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pages and Frames adding _1, _2, _3... zunebuggy 1 1,696 05-30-2017, 12:17 AM
Last Post: Vichu M J
  How to find and change text in PDF document using QTP UFT automation VBscript alexwhite 0 12,071 02-18-2017, 04:20 AM
Last Post: alexwhite
  Unable to find Application Area In QTP Jyobtech 0 2,167 01-10-2015, 04:03 AM
Last Post: Jyobtech
  I want to compare string present on two pages excellentpawan 3 3,531 07-23-2013, 02:33 PM
Last Post: ssvali
  Verify images get properly loaded or not(Broken Images) in respective pages nilanjans 4 4,140 01-17-2013, 10:51 AM
Last Post: nilanjans

Forum Jump:


Users browsing this thread: 1 Guest(s)