How to check for error pages (Page cannot be displayed)?
This is the first article in my “How to?” series and i hope there would be many more that i would be able to write in future.
While testing any web application there can be instances where the application is not available or you get a page cannot be displayed, internal server error etc and need to take some action based on the same. I present a very generic function which returns True/False whether there is an error on the page or not. The best part about this function is that it is compatible with both IE and FF
'Author: Tarun Lalwani 'Website: www.KnowledgeInbox.com 'Function: IsPageCannotBeDisplayed 'Comments: To check if a Page cannot be displayed or some other keyword is present on the page 'Input: '@Brw - QTP Browser Test object '@PCD_Keyword - Keywords to be used for checking a page cannot ' be displayed error. 'Return: True/False in case error has occurred Public Function IsPageCannotBeDisplayed(oBrw, PCD_Keywords) Dim sUrl, sPageText, oPg IsPageCannotBeDisplayed = False If Not oBrw.Exist(0) Then Exit Function 'Get the page object Set oPg = oBrw.Page("micclass:=Page") 'A page might not exist when the dialog modal is present 'in the browser If Not oPg.Exist(0) Then Exit Function sURL = oBrw.Page("micclass:=Page").GetROProperty("URL") 'Special check for IE and FF URL checks If InStr(1, sURL, "shdoclc.dll", vbTextCompare) <> 0 or _ InStr(1, sURL, "about:neterror?", vbTextCompare) <> 0 Then IsPageCannotBeDisplayed = True 'IF IE then we will use HTML DOM to get the whole page text ElseIf InStr(1, oBrw.GetROProperty("version"),"internet explorer", vbTextCompare) Then sPageText = oPg.Object.documentElement.outerText Else 'We now assume it is Firefox. So object property is not available 'This approach won't work on IE sPageText = oPg.GetROProperty("outertext") End If Dim sKeyword For each sKeyword in PCD_Keywords 'Check if our keyword exist on the page If InStr(1, sPageText , sKeyword, vbTextCompare) Then IsPageCannotBeDisplayed = True Exit Function End if Next End Function 'Keywords for page cannot be displayed. Different application have different pages for 'page cannot be displayed. Make sure you a unique keyword is picked from the page Dim PCD_Keywords PCD_Keywords = Array("The page cannot be displayed", "Sorry page is not found", "Internal server error") 'Usage 'Msgbox IsPageCannotBeDisplayed(Browser("Mozilla Firefox Start"), PCD_Keywords) |
