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

Internet explorer replay issue

There are times when below Navigate statement in QTP runs fine but does not Navigate to the actual website

Browser("XXX").Navigate "http://www.knowledgeinbox.com"

Now there can be different possible root cause for this issue. This article will take you throw most of them one by one

Possible Root Cause #1

There is a child modal dialog in IE waiting for user action. The below script in QTP demonstrates how to recreate this issue

'Create a modal dialog on IE window
Browser("Browser").Navigate "javascript:void(alert('Test dialog'))"
 
'Below navigate would be execute without error but
'the actual website will not be browsed.
'In our case we raise the dialog box using javascript
'navigation but in real world scenario this could be
'a security dialog popup or someother popup
Browser("Browser").Navigate "www.knowledgeinbox.com"
IE Modal dialog

IE Modal dialog

The test results window won’t show any error for the 2nd Navigate line

Test results summary without error

Test results summary without error

Possible Root Cause #2

QTP “BHOManager Class” Add-on is disabled in IE. This BHO manager allows QTP to interact with IE window and perform operations inside it. To check the status of the same, open a new IE window and go to Tools->Manage Add-ons…

IE Manage Add-ons

IE Manage Add-ons

In case the BHOManager Class Add-on is disabled, enabled it and close all open IE windows. This will fix the navigate issue.

Possible Root Cause #3

Another mysterious root cause can be because of corruption of the windows user profile. Though it is unknown what gets corrupted in the profile but the solution to this is to delete your profile and create a new one.

Trying to delete a user profile just in hope that it would make things work is not something anyone would try to do first. But to make sure that deleting the profile would help you can first login to the system with a different user who has never used that system and after that verify if the Navigate statements are working in QTP.

Possible Root Cause #4

QTP DLL registration information in registry is corrupted. To repair all the DLL registry run the below file from run window

%ProgramFiles%\Mercury Interactive\QuickTest Professional\bin\quickTestProfessional.bat

Run QuickTestProffesional.bat

Run QuickTestProffesional.bat

Note: This works for QTP 9.2 or lower versions only. With QTP 9.5 HP has removed these files

Possible Root Cause #5

If you had a recent upgrade from IE6 to IE7, the upgrade might be causing the issue. Try and uninstall IE7 and see if it helps. Also if you are using QTP9.x (Except 9.5) make sure you have Tabbed browsing disabled

Possible Root Cause #6

IE7 introduce a new security featured named protected mode. When protected mode is enabled IE process runs with very low privileges. This can cause recording as well as replay issues for a QTP script. Disable the the protected mode by editing the security zone

Disable IE protected moded

Disable IE protected moded

Possible Root Cause #7

User Access control (UAC) is enabled on the Windows vista machine. Go to control panel and make sure UAC checkbox is cleared

Disable User Access control (UAC)

Disable User Access control (UAC)

Possible Root Cause #8

Another possible root cause is that complete QTP installation is corrupt and needs to be repaired. To do that follow the below mentioned step

  • Uninstall QTP from control panel
  • Restart the system
  • Clean the system registry using QTP Clean Uninstall tool
  • Restart the system
  • Reinstall QTP

Possible Root Cause #9

This is something that I would never check until unless it is very important to get IE working on the same machine only. The possible root cause could be a corrupted OS. You should consider getting your machine rebuilt by asking the System administrator

Rating: 9.0/10 (1 vote cast)