Anatomy of QTP :
Apart from these 3 add-ins
qtp is always compatible with standard windows
environment QTP serene is
divided in to 5 parts
1. Test Pane
2. Active Screen
3. Data Table
4. Debug Viewer Pane
5. Tool
Options
1) Test
Pane:
Test pane is an area
provided by Q.T.P, which is used for developing, viewing and
modifying the test script.
It represents the Test
script in 2 views.
1. Expert view
QTP Main
Window
Expert
view:
Expert view represents the
script in VB script format.
Keyboard
View:
It represents the scripts
using a graphical user interface, which is further divided,
into 4 parts.
1. Item
2. Operation
3. Value
4. Documentation
2) Active
Screen :
Active Screen is a feature
provided by Q.T.P which holds the snap shots related to
each and every script
statement and used for understanding the script easily as well as
enhancing the script
easily.
Features:-
It is used for understand
the script easily.
It is used for enhancing
the script easily.
Active
Screen capture level settings :
Write a program to disable
active screen ?
Dim x
Set x=createobject (“quicktest.application”)
x.launch
x.showpanescreen “activatescreen”, false
Wait 3
x.windowstate=”maximized”
x.visible=true
Set x=nothing
3. Data
Table :
A Data Table provides a
way to create data driven test cases. Data table is also called as
formula1 sheet, which is
developed by the third party and integrated with the Q.T.P. Each
test case has one global
data sheet which is accessible to all actions inside that test case
and each action has its
own private data table also known as local data table. The name
local data table is
somewhat misleading because it is in fact possible to access any
action’s local data table
from any other action, but the way of accessing the data becomes
a bit different.
QuickTest Professional
Unplugged
All powers within you,
you can do it
18
Figure 3.1 shows a sample
DataTable with 2 parameters, Username and Password
We can use most of the
formulas that work inside a typical Excel spreadsheet. But there
are some
differences between a
DataTable and an Excel spreadsheet. In fact a DataTable is
wrapped around an Excel
spreadsheet—which provides access functionality to the values
but does not expose the
Excel spreadsheet object model.
‘Gives the value of
Parameter1 stored in
‘The Global data table.
Data Table (“Parameter1”,
dtGlobalSheet)
‘Gives the value of
Parameter1 stored in
‘The current’s action
local data table.
Data Table (“Parameter1”,
dtLocalSheet)
The same Data Table cannot
have duplicate parameter names but we can use the
same name Parameters in
different sheets (Global Data Table and Local Data Table).
Each Data Table has only
1row enabled even when it is blank and the other rows get
enabled when data is
entered into a new row. A Data Table is stored as “Default.xls” file in
the test folder
When viewed in Excel, the
first row of the sheet contains the parameter names,
while QTP displays the
parameter as the column titles. Therefore, when viewed using
Excel, the 2nd row starts
the 1st row of data in the DataTable. The DataTable shown
above has only 2 data rows
enabled. Note that QTP makes a data row enabled by marking
the borders of the row in
the actual spreadsheet. A row with no data but with marked
borders is still
considered as an enabled row by QTP. To delete an enabled row we must
select the row and delete
it from the context menu which appears on right clicking the row.
Design
and run-time data table :
Design
time data table :
As the name suggest the
data table during the script design time is known as design time
data table.
Any changes to this are
saved when the script is saved.
Run-time
data table:
The run-time data table
contains a copy of the design time data table when a script is
executed. It may contain
values that are changed during script execution and are
presented in the test
result summary. The changes made to the data table during run-time
are not saved to design
time data table. Figure 4-3 shows a run-time data table from the
test results summary
When to
use the global or a local data table :
It is important to
understand in what situations the global or a local data table should be
used.
Consider the following two
scenarios
Scenario 1 - Log into the
application, book 1 ticket, log out. Repeat the scenario for
many users
Scenario 2 - Log into the
application, book 3 tickets, and log out
Scenario
1:
The Global data table is
better suited for this scenario where we have the user name,
password
and tickets details as the
parameters and we execute the scenario using a single action
(which does
everything) or multiple
actions (Login, booking and logout).
NOTE: We can use
an external spreadsheet as a Data table by
Scenario 2 :
A Local data table is
better suited for this scenario. Here a good approach would be to split
the test
into three actions: login,
booking and logout. Login and logout can use the username and
password
parameters from the global
data table and booking can use ticket detail parameters from
its local data table and
the action will be executed for all rows in its local data table.
Data
table object model:
QTP provides an object
model to access various properties and methods in a data
table:
There are three types of
objects
DataTable
- Represents all the global and local data tables in the test
DTSheet -
Represents a single sheet in the test
DTParameter
- Represents a single column in a sheet.
Each object has certain
functions available and certain properties associated with
it. These are explained in
detail in the QTP user manual.
Data
table formatting :
When data is entered into
the data table it automatically formats the value using
the best possible matching
format. For example, if "12345678901" is entered into a cell
then it would be auto
formatted to "1.23456789E+010". In situations where the formats are
important the data should
be entered with care. If data entered in the cell start with a single
quote (‘) then it is
always treated as a text and no format conversion is performed. We can
also define a specific
format by right clicking the cell or an entire column and then picking
a specific format from the
popup context menu.
1 How to access a parameter from the global data sheet
There are a variety of
ways to access a parameter from the global data table, most
of which are
presented in the following
code snippet:
'Methods of getting a
Data Table value
Val = DataTable.Value ("ParamName",
dtGlobalSheet)
Val = DataTable.Value ("ParamName","Global")
'By giving the sheet
index starting from 1 for the global sheet
Val = DataTable.Value ("ParamName", 1)
' Sheet name or id is a
optional parameter and is assumed
' to be as for global
data sheet in case not provided
Val = DataTable.Value ("ParamName")
' Value property is the
default property of the DataTable object
QuickTest Professional
Unplugged
All powers within you,
you can do it
24
' So DataTable
("ParamName", dtGlobalSheet) is
' Equivalent to
DataTable.Value ("ParamName", dtGlobalSheet)
Val = DataTable ("ParamName",
dtGlobalSheet)
Val = DataTable ("ParamName")
'Using the data table
object model
Val =
DataTable.GlobalSheet.GetParameter ("ParamName").Value
'Using the data table
object model
Val =
DataTable.GlobalSheet.GetParameter ("ParamName").ValueByRow(1)
2. How to access a parameter from a Local data sheet
'Various methods to get
data table value
Val = DataTable.Value ("ParamName",
dtLocalSheet)
Val = DataTable.Value ("ParamName","<LocalActionName>")
Val = DataTable ("ParamName",
dtLocalSheet)
Val = DataTable ("ParamName","<LocalActionName>")
'The local sheet of the
action which is executing this statement
Val =
DataTable.LocalSheet.GetParameter ("ParamName").value
3. How to check if a Sheet exists
'Function to check if
DataTable sheet exists
Function isSheetExists
(sheetName)
On error resume next
isSheetExists = TRUE
Err. Clear
Set objSheet=
DataTable.GetSheet (sheetName)
'In case error occurred
sheet does not exist
If err.
number<>0 then
isSheetExists = FALSE
End if
End Function
4. How to preserve format of data output to a data table
'This would be modified to
1.23456789E+010 due to auto formatting
DataTable ("ParamName") = "12345678901"
'This will not be auto
formatted and will be treated as text
DataTable ("ParamName") = "'"
& "12345678901"
5. How to check if a parameter exists in a specific sheet
'Check if a parameter
exists in data table
Function isParameterExists
(sheetName, paramName)
On error resume next
isParameterExists = TRUE
Err. Clear
ParamTotal =
DataTable.GetSheet (sheetName).GetParameter (paramName)
'In case of error the
parameter does not exist
QuickTest Professional
Unplugged
All powers within you,
you can do it
25
If err.
number<>0 then
isParameterExists = False
End if
End Function
6. Current iteration number of QTP script:
STR = "Current QTP
iteration: " & Environment ("TestIteration") & vbNewLine
& _
‘DataTable
("Param1", dtGlobalSheet) & vbNewLine & _
DataTable
("Param2", dtGlobalSheet)
MsgBox STR
7.How to export contents of a WebTable to a data sheet. Let’s assume
that the
first row of the data table contains the columns heading. We then add those
as
parameters of the data table:
‘Variable declaration
Dim i, j
Dim rowCount,
colCount
Dim cellText,
objTable
‘Get table object
Set objTable = Browser (“”).Page (“”_________________).WebTable
(“”)
‘Get the row count of
the webtable
rowCount =
objTable.RowCount
‘Get the column count of
the webtable header row
ColCount =
objTable.ColumnCount (1)
‘Create an output sheet
Set outSheet =
DataTable.AddSheet (“Output”)
‘Create Parameters based
on the 1st row of the web table
For i = 2 to colCount
cellText =
objTable.GetCellData (1,i)
‘Note in case the
CellText contains space in between
‘then QTP will
automatically convert it to a “_” character
outSheet.AddParameter
cellText,””
Next
‘Skip first row as we
assumed it to be a header row
For i = 2 to rowCount
outSheet.SetCurrentRow i-1
‘Re-calculate the column
count as some rows
‘Have different column
sizes
colCount =
objTable.ColumnCount (i)
For j = 2 to colCount
cellText =
objTable.GetCellData (i, j)
‘We are using index here
to avoid the problem of
‘the “_” issue if cell
text has spaces or new line chars
‘then we will get an
error. to overcome that we can also use
‘outSheet.GetParameter
(Replace (cellText,” “,”_”)).Value
outSheet.GetParameter
(j-1).value = cellText
Next
Next
8. How to get value of a parameter from any specific row in the data table
We use the ValueByRow
method to get value for any row
‘Get a value by row
DataTable.GetSheet (“SheetName”).GetParameter
(“ParameterName”).
ValueByRow (RowNumber)
9. How to execute a script for all Global Data Table iterations, when
the
script is
set to run for only one iteration:
In case we want to
manually repeat the code for each iteration, we need to write a bit
code.
‘Declare variable
Dim i, iCount
‘Get the global sheet
object
Set oGlobal =
DataTable.GlobalSheet
‘Get # of rows
iCount =
oGlobal.GetRowCount
For i = 1 to iCount
‘Set the current row
oGlobal.SetCurrentRow i
‘Execute the code to be
repeated here
Msgbox DataTable (“UserName”)
Next
10. How to get the number of columns that contain data:
To solve this problem we
need to utilize the excel formula COUNTA. We add a parameter
to the data table with the
formula and then read its value:
‘Add a
new parameter with the formula
‘For Columns 1 of data
table use A1:A65536
‘For column 2 of data
table use B1:B65536 and so on
DataTable.GlobalSheet.AddParameter
“New”,”=COUNTA(A1:A65536)”
‘Get the new value
Msgbox DataTable
(“New”)
Note: The above
code won’t work when there are no columns in the data table or all the
columns have been used
Data
table Parameterization:
1. Record new script “www.mail.yahoo.com”
2. Start recording and
provide user name and password
3. Stop recording
4. Now create 2 column in
Global data table named “user name” and
“password”
5. provide values for both
column
6. With help of data table
associated method properties the most popular is
value which is the default
data table property. Now let us see how we
access values stored in
data table.
Dim Uid, Pw
Uid =datatable.value
(”user name”, dtGlobalSheet)
Pw =datatable.value
(”password”, dtGlobalSheet)
‘After that changes it
into
Browser (“mail
yahoo”).page (“mail yahoo”).webebit (“username“ ).set “Uid”
Browser (“mail
yahoo“).page (“mail yahoo”).webebit (“password”).set “Pw”
‘In order to move your
pointer to next row, if we want to use second row just use the
bellow code
Datatable.SetNextRox
Working
with the data table objects :
Adds the specified sheet
to the run-time Data Table
AddSheet
Method
Syntax: DataTable.AddSheet
(Sheet Name)
Example:
Variable=DataTable.AddSheet
("MySheet").Add Parameter ("Time", "8:00")
DeleteSheet
Method
Deletes the specified
sheet from the run-time Data Table.
Syntax: DataTable.DeleteSheet
SheetID
Example:
DataTable.DeleteSheet "MySheet“
QuickTest Professional
Unplugged
All powers within you,
you can do it
28
Export
Method
Saves a copy of the
run-time Data Table in the specified location.
Syntax:
DataTable.Export (FileName)
Example:
DataTable.Export
("C:\flights.xls")
Uname=datatable.value
(“uname”, dtGlobalSheet)
Pw=datatable.value (“pw”,
dtGlobalSheet)
Name=”Agentname”
Dialog (“login”).winedit
(name).set Uname
Dialog (“login”).winedit
(“password”).set Pw
Datatable.export
(“c:\login.xls”)
ExportSheet
Method
Exports a specified sheet
of the run-time Data Table to the specified file.
Syntax:
DataTable.ExportSheet (FileName, DTSheet)
Example:
DataTable.ExportSheet "C:\name.xls”, 1
GetRowCount
Method
Returns the total number
of rows in the longest column in the global data sheet or in the
specified data sheet of
the run-time Data Table.
Example:
Rowcount =
DataTable.GetSheet ("MySheet").GetRowCount
GetSheet
Method
Returns the specified sheet
from the run-time Data Table.
Example:
MyParam=DataTable.GetSheet ("MySheet").Add Parameter
("Time", "8:00")
GetSheetCount
Method :
Returns the total number
of sheets in the run-time Data Table.
Import
Method :
Imports the specified
Microsoft Excel file to the run-time Data Table.
Syntax: DataTable.Import
(FileName)
Example: DataTable.Import
("C:\flights.xls")
ImportSheet
Method :
Imports a sheet of a
specified file to a specified sheet in the run-time Data Table.
Syntax: DataTable.ImportSheet
(FileName, SheetSource,SheetDest)
Example: DataTable.ImportSheet
"C:\name.xls”, 1,"name"
SetCurrentRow
Method:
Sets the specified row as
the current (active) row in the run-time Data Table.
Example: DataTable.SetCurrentRow
(2)
SetNextRow
Method:
Sets the row after the
current (active) row as the new current row in the run-time Data
Table.
SetPrevRow
Method:
Sets the row above the
current (active) row as the new current (active) row in the run-time
Data Table.
GlobalSheet
Property
Returns the Global sheet
of the run-time Data Table.
Example:
DataTable.GlobalSheet.AddParameter
"Time", "5:45"
LocalSheet
Property
Returns the current
(active) local sheet of the run-time Data Table.
Example:
MyParam=DataTable.LocalSheet.AddParameter("Time",
"5:45")
RawValue
Property
Retrieves the raw value
of the cell in the specified parameter and the current row of the
run-time
Data Table.
Syntax: DataTable.RawValue
ParameterID [, SheetID]
SheetID can be the sheet
name, index or dtLocalSheet, or
DtGlobalSheet.
QuickTest Professional
Unplugged
All powers within you,
you can do it
30
Value
Property
Retrieves or sets the
value of the cell in the specified parameter and the current row of the
run-time Data Table.
Syntax:
DataTable.Value(ParameterID
[, SheetID])
No comments:
Post a Comment