QTP - Chapter XIV - XV


Functions
--> Functions are used to script maintainability and reusability
Types of Functions:-
1. QTP Built in Functions [VB Script Chapter]
2. User Defined Functions
User Defined Functions: -
Syntax: - [public/ private] Function ([Parameters])

[Exit Function]
End Function
Public: - Indicates that the function procedure is accessible to all other procedures in all scripts.
Private: - Indicates that the function procedure is accessible only to other procedures in the script where it is
declared / if the function is a member of a class, and that the function procedure is accessible only to the other
procedures in that class.
Function: - name of the function
Parameters: - List of variables representing arguments that are passed to the function procedure when it is called
Statement: - Any group of statements to be executed with in the body of the function procedure
Exit Function: - It causes an immediate exit a function.



Function Parameters: - Function parameters are used to retrieving the data from Calling Function to Called
Function and sending result from Called Function to Calling Function.
Types of Function Parameters: -
1. ByVal
2. ByRef
Note: - We create function parameters in called function
ByVal: - It retrieves data from calling function to called function
Note*: - Any changes to ByVal it limits to the called function
ByRef: - It retrieves data from calling function to called function and also sends test results from called function to
calling function
Note*: - Any changes to ByRef will be accessible inside the called function as well as calling function.
Note*: - Default parameter type is ByRef
Ex: -
         

ScriptNo30: - Write a function to find given file existence
MsgBox FileExist ("E:\01 My Scripts\1.txt")
Function FileExist (sFilePath)
Dim objFile
Set objFile = CreateObject("Scripting.FileSystemObject")
FileExist = objFile. FileExists (sFilePath)
Set objFile = Nothing
End Function

ScriptNo31: - Write a script per calculating mathematical results per given values and operations.
MsgBox Calculation (10,20,"+")
MsgBox Calculation (10,20,"-")
MsgBox Calculation (10,20,"*")
MsgBox Calculation (10,20,"/")
MsgBox Calculation (10,0,"/")
MsgBox Calculation (10,"$","/")
Function Calculation (iVal1,iVal2,sOperator)
Select Case sOperator
Case "+"
Calculation = iVal1 + iVal2
Case "-"
Calculation = iVal1 - iVal2
Case "*"
Calculation = iVal1 * iVal2
Case "/"
Calculation = iVal1 / iVal2
Case "/"
Calculation = iVal1 / iVal2
Case "/"
Calculation = iVal1 / iVal2
If Err.number <> 0 Then
Calculation = Err.Description
Err.Clear
End If
Case Else
Calculation = "Invalid Operator"
End Select
End Function

Library Files: - It contains all functions
Creating Library File: -
Method 1: -Open Notepad declare the functions and save the file with “.vbs” extension
Method 2: -Start-->All Programs-->QTP-->Tools-->Quick Test Script Editor-->Click New Button-->Declare the
Functions-->Save the File with “.qfl” extension
Method 3: -
QTP-->New Dropdown-->Select Function Library-->Declare the Functions-->Save the file with “.qfl”
extension
Associating Library Files: - After creating library files we need to associate a required test script
By Navigation: -
File Menu-->Setting-->Resources-->click on Add button-->Browse and open require library file vbs/ qfl
-->Click on apply and ON.
By Script:- Syntax: - ExecuteFile “Path of the library file”
Ex: - ExecuteFile” E:\01 My Scripts\1.vbs”

Difference between By Navigation and By Script


Note: - If associate library file and execute a test script, QTP first executes library files and executes test script
Note: - If we associate multiple library files it executes down to top
Ex: - Sample1.vbs
Sample2.vbs
First it executes sample 2 then sample1
Note: - we can declare variables in library file [Global variable]
RegisterUserFunc: - To define user defined methods by using functions.
RegisterUserFunc "Activex","Set","Sample"
Function Sample(obj,sString)
Sample = obj.Type(sString)
End Function
Window ("Flight Reservation").ActiveX("MaskEdBox").Set("121213")

ScriptNo32: - Write a library file for OMBS_Login, Create a Bank, Delete a Bank, OMBS_Logout
OMBS_Library: -
********************************************************************************
'Function Name : OMBS_Login
'Function Description: This function validates OMBS login
'Parameters : User name, password and User Type
'Return Value: True / False
'Design Date: 02/14/2013
'Author: Sreekar
'Tool used : QTP 10
'Comments:
********************************************************************************

Function OMBS_Login(sUsrName,sPwd,sUsrType)
SystemUtil.Run "iexplore","http://192.168.1.50:8050/unifi/"
Set obj=Browser("OnlineNetBanking").Page("OnlineNetBanking")
If obj.Exist Then
Reporter.ReportEvent micPass,"Open OMBS application","OMBS application is opened"
obj.WebEdit("UserName").Set sUsrName
obj.WebEdit("Password").SetSecure sPwd
sAllItems=obj.WebList("UserType").GetROProperty("all items")
If InStr(sAllItems,sUsrType)>0 Then
obj.WebList("UserType").Select sUsrType
obj.WebButton("Submit").Click
If Browser("WelcomePage").Page("WelcomePage").Image("Back").Exist(5) Then
Reporter.ReportEvent micFail,"Enter user deatils and click on Submit
button","Given user credentials are invalid"
OMBS_Login=False
SystemUtil.CloseDescendentProcesses
Else
Reporter.ReportEvent micPass,"Enter user deatils and click on Submit
button","Welcome page is opened"
OMBS_Login=True
End If
Else
Reporter.ReportEvent micFail,"Select User type",sUsrType&" item is not found in User
type dropdown"
OMBS_Login=False
SystemUtil.CloseDescendentProcesses
End If
Else
Reporter.ReportEvent micFail,"Open OMBS application","OMBS application is not opened"
OMBS_Login=False
SystemUtil.CloseDescendentProcesses
End If
End Function

*********************************************************************************

'Function Name : OMBS_Logout
'Function Description: This function validates OMBS Logout
'Parameters : N/A
'Return Value: True / False
'Design Date: 02/14/2013
'Author: Sreekar
'Tool used : QTP 10
'Comments:
'*********************************************************************************
Function OMBS_Logout()
If Browser("WelcomePage").Page("WelcomePage").Exist Then
Browser("WelcomePage").Close

While Browser("WelcomePage").Exist(2)=True
Wend
If Not Browser("WelcomePage").Exist(2) Then
Reporter.ReportEvent micPass,"Close application","Application is closed"
OMBS_Logout=True
Else
Reporter.ReportEvent micFail,"Close application","Application is not closed"
OMBS_Logout=False
SystemUtil.CloseDescendentProcesses
End If
Else
Reporter.ReportEvent micFail,"Click Logout link","Logout link not found"
OMBS_Logout=False
SystemUtil.CloseDescendentProcesses
End If
End Function
'**************************************************************************
'Function Name: OMBS_CreateBank
'Function Description: This function validates Create Bank functionality
‘Parameters: sBankId,sBankName,sAddr,iPhone,sLoc
'Return Value: True / False
'Design Date: 02/15/2013
'Author: Sreekar
'Tool used : QTP 10
'Comments:
'**************************************************************************
Note***: - Where we unable to identify objects with Object Spy we have a “ClickOnText”.
Function OMBS_CreateBank(sBankId,sBankName,sAddr,iPhone,sLoc)
Set obj=Browser("WelcomePage").Page("WelcomePage")
If obj.Exist Then

Reporter.ReportEvent micPass,"Validate Welcome page is opened”, “Welcome page is opened"
obj.Frame("Contents").ActiveX("MenuList").ClickOnText "Banks Database"
obj.Frame("Contents").ActiveX("MenuList").ClickOnText "New Banks"
If obj.Frame("Main").WebButton("AddNew").Exist Then
Reporter.ReportEvent micPass,"Click New Banks link","New bank Entry page is
opened"
obj.Frame("Main").WebButton("AddNew").Click
obj.Frame("Main").WebEdit("BankId").Set sBankId
obj.Frame("Main").WebEdit("Name").Set sBankName
obj.Frame("Main").WebEdit("Addres").Set sAddr
obj.Frame("Main").WebEdit("PhoneNo").Set iPhone
obj.Frame("Main").WebEdit("Location").Set sLoc
obj.Frame("Main").WebButton("Submit").Click
If obj.Frame("Main").WebElement("BankDatabaseEntrySucceded").Exist Then
Reporter.ReportEvent micPass,"Enter bank details and click on Submit
button","New bank is created"
OMBS_CreateBank=True
Else
Reporter.ReportEvent micFail,"Enter bank details and click on Submit
button","New bank is not created"
OMBS_CreateBank=False
End If
obj.Frame("Contents").ActiveX("MenuList").ClickOnText "Banks Database"
Else
Reporter.ReportEvent micFail,"Click New Banks link","New bank Entry page is not
opened"
OMBS_CreateBank=False
End If
Else
Reporter.ReportEvent micFail,"Create Bank","Welcome page is not opened"
OMBS_CreateBank=False
End If
End Function
'**************************************************************************
'Function Name: OMBS_DeleteBank
'Function Description: This function validates Delete Bank functionality
‘Parameters: sBankId
'Return Value: True / False
'Design Date: 02/15/2013
'Author: Sreekar
'Tool used : QTP 10
'Comments:
'**************************************************************************
Function OMBS_DeleteBank(sBankId)
Dim obj,sItems
Set obj=Browser("WelcomePage").Page("WelcomePage")
If obj.Exist Then
Reporter.ReportEvent micPass,"Delete Bank","Welcome page is opened"
obj.Frame("Contents").ActiveX("MenuList").ClickOnText "Banks Database"
obj.Frame("Contents").ActiveX("MenuList").ClickOnText "Delete Banks"
If obj.Frame("Main").WebButton("DeleteSubmit").Exist Then
Reporter.ReportEvent micPass,"Click on Delete banks link","Delete bank page is
opened"
sItems=obj.Frame("Main").WebList("BankId").GetROProperty("All Items")
If InStr(sItems,sBankId)>0 Then
obj.Frame("Main").WebList("BankId").Select sBankId
obj.Frame("Main").WebButton("DeleteSubmit").Click
obj.Frame("Main").WebButton("Delete").Click
If obj.Frame("Main").WebElement("YourBanksDeletedSucceeded").Exist Then
Reporter.ReportEvent micPass,"Delete bank","Bank is deleted"
OMBS_DeleteBank=True
End If
End Function
'**************************************************************************
'Function Name: OMBS_DeleteBank
'Function Description: This function validates Delete Bank functionality
‘Parameters: sBankId
'Return Value: True / False
'Design Date: 02/15/2013
'Author: Sreekar
'Tool used : QTP 10
'Comments:
'**************************************************************************
Function OMBS_DeleteBank(sBankId)
Dim obj,sItems
Set obj=Browser("WelcomePage").Page("WelcomePage")
If obj.Exist Then
Reporter.ReportEvent micPass,"Delete Bank","Welcome page is opened"
obj.Frame("Contents").ActiveX("MenuList").ClickOnText "Banks Database"
obj.Frame("Contents").ActiveX("MenuList").ClickOnText "Delete Banks"
If obj.Frame("Main").WebButton("DeleteSubmit").Exist Then
Reporter.ReportEvent micPass,"Click on Delete banks link","Delete bank page is
opened"
sItems=obj.Frame("Main").WebList("BankId").GetROProperty("All Items")
If InStr(sItems,sBankId)>0 Then
obj.Frame("Main").WebList("BankId").Select sBankId
obj.Frame("Main").WebButton("DeleteSubmit").Click
obj.Frame("Main").WebButton("Delete").Click
If obj.Frame("Main").WebElement("YourBanksDeletedSucceeded").Exist Then
Reporter.ReportEvent micPass,"Delete bank","Bank is deleted"
OMBS_DeleteBank=True

TC1_LoginLogut: - [Script for OMBS library]
'**************************************************************************
'Test Name : Login and Logout
'Test Description:
'Design Date: 02/14/2013
'Author: Sreekar
'Tool used : QTP 10
'Comments:
'**************************************************************************
If OMBS_Login("admin","511c647fd6c24ad6a136c42a4164f453","Administrator") Then
Call OMBS_Logout()
End If
TC2_CreateBank_DeleteBank: - [Script for OMBS library]
'**************************************************************************
'Test Name: Create and Delete bank
'Test Description:
'Design Date: 02/15/2013
'Author: Sreekar
'Tool used : QTP 10
'Comments:
'**************************************************************************
If OMBS_Login("admin","511db8845bb8f06fba66629e40b7edda","Administrator") Then
If OMBS_CreateBank("push1234","push1234","test","1234567890","test") Then
Call OMBS_DeleteBank("push1234")
End If
Call OMBS_Logout()
End If



                                                      QTP - Chapter XV


                                         Batch Testing and Debugging
Batch Testing: -
-->It is a process of executing multiple test scripts in a single execution
-->We can implement batch testing in below ways:
1. By using Test Batch Runner Tool
2. By using Driver Script

Test Batch Runner Tool: - It is inbuilt tool in QTP

Prerequisite in QTP: - Tools-->Options-->Run-->Check allow other HP product to Run Test and
Components Check Box

Navigation: - Start-->All Program--> QuickTest Professional-->Tools--> Test Batch Runner-->Click Add
Button-->Browse and Select require Scripts-->Save the script with .mtb [Mercury Test Batch] extension
-->Click Run.

Limitation: - unable to execute partial script, that is required actions in required scripts unable to execute
Driver Script: -
Executing Required Action: -
Syntax: - LoadAndRunAction “Test Script Path and Name”, “Action Name”
Ex: - LoadAndRunAction “E:\01 My Scripts\Batch40”, “Action1”

Example: - For this example create sample1 and sample2 scripts with actions


--> Save above file as RunManager.xls in Preferred location
--> Save the below script as DriverScript in required path

DriverScript.mts: -
Set objXL = CreateObject ("Excel.Application")
Set objWrkBk = objXL.Workbooks.Open("E:\01 My Scripts\Batch40")
Set objWrkSht = objWrkBk.Worksheets("Sheet1")
iRowCnt = objWrkSht.UsedRange.Rows.Count
For index = 2 to iRowCnt
bStatus = UCase(objWrkSht.Cells(index,3))
If bStatus = "YES" Then
sScript = objWrkSht.Cells(index,1)
sAction = objWrkSht.Cells(index,2)
LoadAndRunAction sScript, sAction
End If
Next
objWrkBk.Close
objXL.Quit

Set objWrkSht = Nothing
Set objWrkBk = Nothing
Set objXL = Nothing

InitScript.vbs: -
Set objQTP = CreateObject ("QuickTest.Application")
objQTP. Launch
objQTP. Visible = True
objQTP. Open "E:\01 My Scripts\Batch40\DriverScript.mts"
objQTP. Test. Run
objQTP. Quit
Set objQTP = Nothing.

Debugging: - After the scripts are developed to make sure that the scripts are correct and we need to debug the script.
Scripts can have Two Types of Errors: -
1. Syntactical Error
2. Logical Error
Syntactical Errors can be identified by Running Scripts or Saving Script and verify in Information Pane.
Logical Error we need to apply debug options. Debugging is a process of executing the test script in user defined
fashion/ way with some temporary break in order, to identify the error.
To do the same, QTP has provided below debug options.



Step Commands: -
a. Step Into (F11): - It is used for executing a single step. If that step is a function call or action call then,
it will step in to the function or action
b. Step Out (Shift+F11): - It is used to executing all the remaining statements from the position of the
pointer in a function or an action and steps out of the function or an action.
c. Step Over (F10): - It is used for executing a single step. If that step is a function call or an action call
then, it will execute the complete block of statements in that function or an action and steps out of that
function or an action.
Break Point (F9): - It is a feature by QTP, which is used for breaking the execution temporally
ExitTest: - It is used to terminate the current script execution.
ExitRun: - It is used to terminate the Batch process
ExitAction: - It is used to terminate the current Action execution.
Run from Step (Ctrl+F5): - It is used to execute from required step
Navigation: - Place the pointer in required line-->right click and select Run from Step (or) Press Ctrl + F5
Debug from Step: - It is used to debug from required step
Navigation: - Place the pointer in required line-->Right Click and select debug from step
Run Current Action: - It is used to execute a required Action
Navigation: - Select required action-->Automation menu -->Run Current Action.

No comments :

Post a Comment