Oct
20
2008
--

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)
Rating: 6.0/10 (6 votes cast)
Oct
12
2008
--

“The parameter is incorrect” error when typing into a WebEdit

Sometimes QTP throws a “The parameter is incorrect” error when setting a text in a WebEdit

The parameter is incorrect error

The parameter is incorrect error

Below is a test textbox to reproduce the issue

WebEdit with Max Length 4:

The error can be reproduced using below code

'Will work fine
Browser("KnowledgeInbox").Page("KnowledgeInbox").WebEdit("txtMaxTest").Set "1234"
 
'Will throw a parameter is incorrect error
Browser("KnowledgeInbox").Page("KnowledgeInbox").WebEdit("txtMaxTest").Set "12345"

There are two root causes of this issue

Root Cause #1
Trying to set a text in a textbox greater than it’s max length would raise the error. The sample textbox shown in this article has Max length of 4. That is the reason why the 2nd line of the code shown above throws an error

Root Cause #2
This root cause is specific to firefox browser. If ReplayType (in Tools->Options->Web (Tab) -> Advacned…(button)) is set to Mouse then an set operation on a WebEdit in Firefox will produce this error. The code below can be used to reproduce the error

'Will work fine
Setting.WebPackage("ReplayType") = 1 'Browser
Browser("KnowledgeInbox").Page("KnowledgeInbox").WebEdit("txtMaxTest").Set "1234"
 
Setting.WebPackage("ReplayType") = 2 'Mouse
'Will throw a parameter is incorrect error even when we type the same text
Browser("KnowledgeInbox").Page("KnowledgeInbox").WebEdit("txtMaxTest").Set "1234"
Rating: 8.9/10 (15 votes cast)
Sep
24
2008
--

General run error when clicking on button

General run error are hard to debug and any extra information available to fix the issue is always helpful. Sometimes QTP starts throwing General run error in click events of WebButton. Digging into the source of the issue using the technique mentioned in Debugging General run error we would see a “Element not found” error in the debug pane.

When you check exist on the button it would still return True but the click would throw a general run error. I have not been to formalize what causes this issue but there is a small dirty workaround to fix

Browser("Browser").Page("Page").WebButton("Login").Highlight
Browser("Browser").Page("Page").WebButton("Login").Click

Note: You should only use this workaround in case you are getting a general run error. This should never be used as a practice

Rating: 7.1/10 (7 votes cast)
Sep
23
2008
--

Implementing Callback in VBScript

Callback is a function which is executed on completion of a registered event. VBScript is not a event driven language, which means we are limited in terms of events for which callback can be implemented.

This article will demonstrate how to implement a callback on finish/terminate event. This finish/terminate event could be one of the following

  • The script finishes/terminates
  • A function finishes/terminates
  • A class finishes/terminates

CallBack implementation

We will create a special CallBack class for this

Class Callback
	'The object which called
	Dim Caller
 
	'Code to be executed during callback
	Dim CallBackCode
 
	'When the class ends execute the callback code
	Sub Class_Terminate()
		Execute CallBackCode
	End Sub
End Class

Executing code when script ends
If we want a Finalize function to be called when the script ends we can use the below code

Dim OC
Set OC = New Callback
OC.CallBackCode = "Call Finalize(0)"
 
Function Finalize(ByVal ExitCode)
	Msgbox "The code end with exit code - "  & ExitCode
End Function
 
Msgbox "Script ends here"

Executing code when a function ends

Function Test()
	Dim OC
	Set OC = New CallBack
	OC.CallBackCode = "Msgbox ""Function Test ended"""
 
	x = 2/0
	Msgbox "Unreachable code"
End Function
 
Call Test()

The advantage of implementing this call back is that they are guaranteed to be executed even in case the script end with a error or the function ends with an error. In QTP we can similarly execute a code at the end of the Test by declaring the variable in one of the associate global libraries.

Note: The approach works on the concept that all variables are destroyed once the scope in which they exist ends. It is important to make sure the CallBack object is always taken in a variable declared (as shown in code “Dim OC”)

Executing the Callback on a Class method

In case we want a class method to be called on a class object we can use the caller property

Class Test
     Function CallMe()
            Msgbox "You called me"
     End Function
End Class
 
Dim oTest
Set oTest = new Test
 
Dim OC
Set OC = New CallBack
Set OC.Caller = oTest
OC.CallBackCode = "Call Caller.CallMe"
Rating: 7.2/10 (6 votes cast)
Written by Tarun Lalwani in: VBScript | Tags: , , , , , , , , , , , ,
Sep
21
2008
0

WebFile incosistent errors

This issue is one of the very strange one that happens sometimes and works sometimes. So you have a line of code setting the file path in a WebFile element

Browser("B").Page("P").WebFile("F").Set "C:\Test.txt"
Browser("B").Page("P").WebButton("Upload").Click

Above code might work fine sometimes, but few times you might find your web application throwing an error ex - “File not found”. I am not very sure what causes this issue but there are three possible solutions I could find for above issues

Solution #1

Highlighting the WebFile object before setting the value sometimes helps

Browser("B").Page("P").WebFile("F").Highlight
Browser("B").Page("P").WebFile("F").Set "C:\Test.txt"
Browser("B").Page("P").WebButton("Upload").Click

Solution #2

Changing the ReplayType to 2 also can help in certain cases

Setting.WebPackage("ReplayType").Click
Browser("B").Page("P").WebFile("F").Set "C:\Test.txt"
Browser("B").Page("P").WebButton("Upload").Click

Solution #3

This one is a bit tricky one and the way it works is that it use the Browser button to select the actual file

Function SetWebFile(oWebFile, oBrw, sFileText)
	x = oWebFile.Object.clientHeight \ 2
	y = oWebFile.Object.clientWidth - 5
 
	oWebFile.Click x,y
	oBrw.Dialog("text:=Choose file") _
			.WinEdit("attachedtext:=File &name:","nativeclass:=Edit") _
			.Set "c:\comp1.xml"
 
	oBrw.Dialog("text:=Choose file").WinButton("text:=&Open").Click
End Function
 
Call SetWebFile(Browser("B").Page("P").WebFile("F"), Browser("B"), "C:\Test.txt")
Rating: 10.0/10 (1 vote cast)