Sunday, February 10, 2013

Views


There are two views in QTP for creating tests, viz.
1.    Keyword View
2.    Expert view

Keyword View:
It’s view which allows you to create tests in modular or table format. Keyword View is QTP’s default test procedure interface. It displays the automation steps of a test procedure as a descriptive tree of actions and functions. The tree contains columns listing the action or function name, parameters, and comments. This mode is useful for the beginners. This view allows the user to select the objects either from the application or from the Object Repository and the user can select the methods to be performed on those objects. The script is automatically generated. Users can also set checkpoints from the keyword view. Users without technical knowledge may be able to understand the Keyword View, but more experienced users and users needing to perform more complicated actions may need to switch to Expert View.
The Keyword view comprises of a tabular view with rows and columns. Each step is represented in a row and each column tells about the different parts of the step. The steps (each row) can be view in understandable English and they can be edited as per your requirements. As you record your test, QuickTest generated steps in the Keyword View representing each operation you performed in the application under test.


Expert View: 
Expert view is simply enables you to write tests as scripts. It displays the test script in VB script format. Expert view is an alternative to Keyword view. The Expert View displays the same steps and objects as the Keyword View, but in a different format. But as the name suggests this view is for people who are expert in VB scripting.
In Expert View, QTP allows display and editing of the test’s source code using VBScript. All test actions can be edited here except for the root Global action. Expert View acts as an IDE for the test. It includes many standard IDE features, such as breakpoints.

Saturday, February 9, 2013

Working With Data Table

AddSheet: This method is used to add a new sheet to the data table.
Syntax: DataTable.AddSheet(“Sheet Name”)
Eg: DataTable.AddSheet(“MySheet”)
Note: All the methods of data table object will have impact only during Runtime

DeleteSheet: This method is used to delete a sheet from the data table.

Syntax: DataTable.DeleteSheet(“” or )
Eg: DataTable.DeleteSheet(“Global”)
(OR) DataTable.DeleteSheet(1)

Import: This method is used to import the contents of an excel file into data table. While importing from an excel file we need to ensure the following things:
1.The number of sheets in excel file & in data table should be equal or more.
2.The sheet names may not be same. (We can modify the sheet names after importing)
3.The parameters names should be same.
Syntax: DataTable.Import(“”)
Eg: DataTable.Import(“D:\QTP\input.xls”)
Steps to import the data manually:
1.Right click --> Import from file --> Click on OK
3.Select the file name from which the data needs to be imported
4.Click on Open

ImportSheet: This method is used to import the data from the specific sheet of the excel file to the specific sheet of the data table.
Syntax: DataTable.ImportSheet “”,
Eg: DataTable.ImportSheet “D:\QTP\input.xls”,2,1

Steps to import manually:
1.Right click in the required sheet of data table
2.Select Sheet --> Import --> From File --> Click on OK
3.Select the required file from which the data needs to be imported
4.Select the required sheet name from the dropdown
5.Click on OK

Export: This method is used to export the contents of the data table to an excel file. Even if the file is not available, it will create the file & export. If the file is available it will over ride.
Syntax: DataTable.Export(“”)
Eg: DataTable.Export(“D:\QTP\output.xls”)

Steps to export manually:
1.Right click in the data table
2.Select File --> Export
3.Provide the File Name --> Click on Save

ExportSheet: 
This method is used to export the contents of a specific sheet of the data table to the excel file.
Syntax: DataTable.ExportSheet “”,
Eg: DataTable. ExportSheet “D:\QTP\output.xls”,2

Steps to import manually:
1.Right click in the required sheet of data table
2.Select Sheet --> Export
3.Provide the File Name --> Click on Save

GetCurrentRow: This method is used to identify the current active row number
Eg: ActRow=DataTable.GetCurrentRow
msgbox ActRow

GetRowCount: This method is used to get the number of records available in the data table.
Eg: RowCnt=DataTable.GetRowCount
msgbox RowCnt
Note: Methods like GetCurrentRow, GetRowCount, etc defaultly works on global sheets.


LocalSheet: 
This method is used to work with the corresponding local sheet from which action we execute the script.
Eg: RowCnt=DataTable.LocalSheet.GetRowCount
msgbox RowCnt

GetSheetCount: This method is used to get the number of sheets available in the data table.
Eg: SheetCnt=DataTable.GetSheetCount
msgbox SheetCnt

SetCurrentRow: This method is used to set the focus to the specified row of the ‘data table-global sheet’.
Syntax: Datatable.SetCurrentRow()
Eg: Datatable.SetCurrentRow(17)

SetNextRow: This method is used to set the focus to the immediate next row of the current active row.
Syntax: DataTable.SetNextRow

SetPrevRow: This method is used to set the focus to the previous row of the current active row.
Syntax: DataTable.SetPrevRow

Value: This method is used for 2 purposes.
1. To read the value from the data table.
Syntax: Val=DataTable.Value(“”,“”)
Eg: Val=DataTable.Value(“Num1”,2)
msgbox Val
Note: We will not define the row number. By default it will pick the value of the current focus row.

2. To write the value into the data table.
Syntax: DataTable.Value(“”,“”)= “Value”
Eg: DataTable.Value(“Result”,”Action1”)=“Pass”
(OR) DataTable.Value(3,2)=“Pass”

Script to perform DDT manually:

DataTable.Import("D:\Practise\QTP Testing\input.xls")
RC=DataTable.GetRowCount
msgbox RC
For i=1 to RC
DataTable.SetCurrentRow(i)
VbWindow(“VbWindow”).VbEdit(“VbEdit”).SetDataTable.Value (“num1”,1)
VbWindow(“VbWindow”).VbEdit(“VbEdit_2”).SetDataTable.Value(“num2”,dtGlobalSheet)
VbWindow(“VbWindow”).VbButton(“Add”).Click
ExpVal=Datatable.Value(“ExpRes”,1)
ActVal=VbWindow(“VbWindow”).VbEdit(“VbEdit_3”).GetROProperty(“text”)
DataTable.Value(“ActRes”,1)=ActVal
If ExpVal=ActVal Then
DataTable.Value(“Result”,1)=”Pass”
Else
DataTable.Value(“Result”,1)=”Fail”
End If
Next
DataTable.Export(“D:\Practice\QTP Testing\output.xls”)

Thursday, February 7, 2013

Register User Function

Register User Function is used to append a new method (Function/ Procedure) or to take priority over of a presented method for a certain QTP Class during run session. Register User Function is relevant for Test Objects similar to VbEdit, VbList, WebEdit, WebList, Dialog, Window and Browser etc…

To Register a Function, foremost define a Function and mark Register User Function declaration at the end of Function Library or in test script. You can Register a New Method for a Test Object Class or Use an Existing Method Name to take priority over the existing Functionality of the specified Method.

If the Function is defined and it is not registered, it operate as a Global Function, you can monitor all your defined functions in Step Generator (F7) if they are globalized. Once the Function is registered, you can’t see the registered function in Step Generator.

The Register Method applicable only in test Script or in Function Library. QTP clears all Registrations at the beginning of Every Run Session.

Syntax: RegisterUserFunc TOClass, MethodName, FunctionName, SetAsDefault

Arguments:
ToClass: To which Class you want to register the method.
Method Name: String which you like to see in methods List for that class.
Function Name: The name of the user defined function which you have defined.

Note: The Function should be located in your Action or in a Library file which you have associated to your Test Script.
SetAsDefault: Indicating that the registered function should be used as a default method for test object/not

Examples:
1. The following Example describes how to append a new method to QTP Class

Sub AddNewMethod (Object, ParameterValue)
If Object.Exist Then
Reporter.ReportEvent micPass, "Object Exist", "XXXXXXXXX Object is existed on this Page"
Else
Reporter.ReportEvent micFail, "Object Exist", "XXXXXXXXX Object is existed on this Page"
End If
End Sub

Declaration: RegisterUserFunc "WebEdit", "AddNewMethod","AddNewMethod",True

After finishing the delcaration of Function and its Registration save file as “FileName.vbs” and associate to the test script. You will find the registered new method in list of methods which supports WebEdit Class and displays as

Browser("QTP Descriptive Programs").Page("QTP Descriptive Programs").WebEdit("email").AddNewMethod

2. The Following Example describes how to add new function to an existing QTP Class/ method.

Function MySet (Object, x) [Ex: obj = Browser("MercuryTours").Page("FindFlights").WebEdit("Country")]
Dim y
Y = Object.GetROProperty("value")
Reporter.ReportEvent micDone, "previous value", y
MySet=Object.Set(x)
End Function

RegisterUserFunc "WebEdit", "Set", "MySet"

The behavior of the above function is to capture and report the value of an edit box before the new value is entered. We are registering the above function with existing set method of Test Object WebEdit i.e., modifying the Set Method behavior with newly distinct function.

Note: Once a new method is registered, the method becomes a recognized method to that specific Test Object until you unregister that method. If your method overrides an existing QTP method unregistering that method resets the method to its normal behavior. Unregistering new methods deletes that method from list of methods which supports by the Test Object. To Un-Register a Function make use of the following Syntax.

Syntax: UnRegisterUserFunc TOClass, MethodName
Arguments:
ToClass: To which Class you want to register the method.
Method Name: String which you like to see in methods List for that class.

Example: UnRegisterUserFunc "WebEdit", "Set" 

Friday, February 1, 2013

Document Object Model(DOM)

How to get the source code of a Webpage using QTP

print browser("name:=Gmail").page("title:=Gmail").Object.documentElement.innerHTML