Header Ads

Commonly Used Function/Methods in QTP

In this post I am trying to figure out few commonly used methods/functions or commands used in QTP.
These methods generally used for making QTP work smoothly.

The following methods empowers us to organize our tests,strengthen the tests and perform desired actions based on our requirements.

While designing a framework maintaining the script or to make it stabilized, these methods are really important. Mostly these methods are inexpensive in terms of cost of usage.Rather they are non comparable with the expensive steps perform.





Activate
It activates dialog boxes/Windows or pages.This is one of the precondition step before we do any set of operation on UAT. This operation will set the the window or page on front so that QTP can perform next set of operation viz actual step

Example:
 
Dialog("Login").Activate
Window("Flight Reservation").Activate

Syntax is: ParentObject.Activate
Exist

This method checks the availability of an object on the application. Prior to any step if we perform this precondition check ,we can avoid lots of delay or failure.Based on this we can set custom message to notify the test engineer that the corresponding object exist or not. Example:
 
dim Check_condition [as boolean]
Check_condition=Dialog("Login").exist
if(Check_condition) then
do something
else
msgbox "Object does not exist"
End if
Syntax is: variableName=Object.Exist
This command will return true if the condition satisfied else return false. We can use a timeout syntax here. I mean if we know that the object will appear after some 10 seconds after the last step performed,we can put a time out value: Example:
 Window("Flight Reservation").Exist(5)
Select
It selects an item from combo box or list box
Syntax is: variableName=Object.Select "item"
 Window("Flight Reservation").WinComboBox("Fly From:").Select "London"
Window("Flight Reservation").WinComboBox("Fly To:").Select "Kolkata"
Minimize,Restore,Move
Minimize: It will minimize the test window. Restore: This command will restore or maximize the test window. Move:This will move the test window to a position in the screen . It will move the test window with respect to screen
Window("Flight Reservation").minimize
Window("Flight Reservation").Restore
Window("Flight Reservation").Move(x,y)
//here x,y are the coordinates.
GetVisibleText
It returns Text from the specified area. This is feature of QTP,I would prefer GETROProperty("Value" or "text") Syntax: Variable =object hierarchy.getvisibletext
dim x
//[as String]
x=Window("Flight Reservation").WinComboBox("Fly From:").GetVisibleText
msgbox x
SET
This is a very tricky method or command of QTP. May be multitasking command. We can use it in three ways..

  1. Setting value to textbox or editbox
  2. Radio button selection
  3. Select or deselect checkboxes
//Example of textbox
Dialog ("Login").WinEdit("Agent Name").Set "AskQTP"
//Example of setting radio button
Window("Flight Reservation").WinRadioButton("Business").Set
//Example of checking checkboxes
Window ("Flight Reservation"). Dialog("Open Order").WinCheckBox("Order No.").Set "ON"


Custom Function-CloseQTP
This method is a custom function to close QTP process in a system.
Function CloseQTP()
   Set objService = GetObject("winmgmts:\\.\root\CIMV2")
   Set objProcessAllQTP = objService .ExecQuery ("Select * from Win32_Process Where Name = 'QTPro.exe'")
   For Each objProcess in objProcessAllQTP 
   objProcess.Terminate()
   Next
   Set objService = Nothing
   Set objProcessAllQTP = Nothing
End Function
Custom Function-CloseBrowser
This is also one custom method to close open Browser.
Function CloseAllBrowsers(BrowserName)
Dim WshShell, oExec
Set WshShell = CreateObject("WScript.Shell")
if(BrowserName=InternetExplorer) then
Set oExec = WshShell.Exec("taskkill /f /im iexplore.exe")
else if(BrowserName=FireFox) then
Set oExec = WshShell.Exec("taskkill /f /im firefox.exe")
else if(BrowserName=Chrome) then
Set oExec = WshShell.Exec("taskkill /f /im chrome.exe")
wait 10
End Function
or else we can also follow the below process:
Function CloseQTP(BrowserName)
if(BrowserName=InternetExplorer) then
BrowserProcess=iexplore.exe"
else if(BrowserName=FireFox) then
BrowserProcess=firefox.exe"
else if(BrowserName=Chrome) then
BrowserProcess=chrome.exe"
end if
Set objService = GetObject("winmgmts:\\.\root\CIMV2")
Set objProcessAllBrowser = objService .ExecQuery ("Select * from Win32_Process Where Name = 'BrowserProcess'")
For Each objProcess in objProcessAllBrowser 
objProcess.Terminate()
Next
Set objService = Nothing
Set objProcessAllQTP = Nothing
End Function
Powered by Blogger.