Академический Документы
Профессиональный Документы
Культура Документы
If you were to parameterize the above process, instead of ending your script at the
confirmation page, you would end it at the Find Flight page. This is because, when the
next iteration executes, it is already at the correct page which can enable the script to start
all over again at the expected point. In other words, our process should look like below:
Below, I have created 2 columns in the Local (Action1) DataTable- UserName &
Password. You can create columns by simply double-clicking the Column heading:
Create Columns in DataTable
Below is the syntax to retrieve data from the DataTable’s local sheet:
Therefore, we can now create 2 statements using the syntax above to retrieve our user
name and password:
DataTable("UserName", dtLocalSheet)
DataTable("Password", dtLocalSheet)
Before we create our script, we must first tell QTP to run the test from all rows in the
Local DataTable. In Keyword view, right-click Action1 then click Action Call Properties:
Right-Click Action1
In the Run Tab of Action Call Properties window, select “Run on all rows”. A Dialog
will popup mentioning what we discussed in the beginning of this post, about ending our
iteration where the first step in our process begins.
1. Launch application
2. Enter UserName/Password from DataTable and click Login
3. Verify if the Find Flights page appears
4. If Find Flights page appears, iteration passed.
5. Return to Home Page and start Step #2
'Step 1
If DataTable.LocalSheet.GetCurrentRow = 1 Then
SystemUtil.Run "iexplore.exe", "http://newtours.demoaut.com"
End If
After executing the above code, or manually launching the application, execute the below
steps:
'Parameterization Block:
With Browser("title:=Welcome: Mercury Tours", "index:=0")
'Step 2
'Parameter 1: UserName
.WebEdit("name:=userName").Set Datatable("UserName",
dtLocalSheet)
'Parameter 2: Password
.WebEdit("name:=password").Set Datatable("Password",
dtLocalSheet)
.Image("name:=login").Click
End With
'Step 3
'If Find a Flight page appears, go back to Home
If Browser("title:=Find a Flight: Mercury Tours:",
"index:=0").Exist(10) Then
'Step 4
Reporter.ReportEvent micPass, "Iteration " &
DataTable.LocalSheet.GetCurrentRow, _
"UserName: " & Datatable("UserName", dtLocalSheet) & "
is valid"
Browser("title:=Find a Flight: Mercury Tours:",
"index:=0").Link("text:=Home").Click
Else
'Step 5
'Else, fail the iteration and return to the Home page
Reporter.ReportEvent micFail, "Iteration " &
DataTable.LocalSheet.GetCurrentRow, _
"UserName: " & Datatable("UserName", dtLocalSheet) & "
is invalid"
Browser("title:=Sign-on: Mercury Tours",
"index:=0").Link("text:=Home").Click
End If
Once the above script ends its execution, your test results should look like this:
Execution Results
This topic covered a very high-level process of parameterization, but I hope it gives you a
good idea how this process can be enhanced and adapted to your application. The next
topic in Parameterization will cover Excel Spreadsheets and a few tricks that we can use
for successfully driving different sets of data to our AUT.
In the previous article, we parameterized a test for a Login process with the help of
QTP’s Local DataTable. This topic covers the same concept with the help of an (external)
Excel file. The process will remain the same, but how we extract data differs. If you are
looking to understand Parameterization concepts and need a quick introduction, I would
recommend you to read the following article: Introduction to Parameterization with
QTP’s Local DataTable
Before we start the process of extracting data from Excel, and using the extracted data in
our automated process, we must first understand the Excel Automation object. Before
that even, we must understand the CreateObject function. The CreateObject function
creates and returns a reference to an Automation object1. In other words, usage of
CreateObject in terms of Excel will return the reference of Excel’s Automation object.
This reference will enable us to use Excel’s methods for data retrieval. Below is the
syntax of CreateObject:
Set Var = CreateObject(servername.typename [, location])
The Set keyword is used here because we have to bind out variable to an object. The Set
statement assigns an object reference to a variable or property, or associates a procedure
reference with an event2. We can use the syntax above in terms of Excel and create a
reference object that will hold methods of the Excel application:
Next, we will create a reference to the Excel WorkBook, which is the file that we will
open to retrieve data from. The workbook we’re working with is located in the root
folder: C: and its name is TestFile.xls. Thus, we will substitute the
pathNamefileName in our code below:
Next, let’s create a reference to the Excel Spreadsheet (Sheet1, Sheet2, Sheet3..) which
contains the data we need to drive our parameterized script. Our data is contained in
Sheet1 of our workbook, thus, we will use it in our code below:
Below is a snapshot of the spreadsheet, with headings in Row 1 and data starting from
Row 2:
To retrieve the number of rows and columns used in the table, we will use the code
below:
'Total Rows
iRows = xlSheet.UsedRange.Rows.Count
'Total Columns
iCols = xlSheet.UsedRange.Columns.Count
Lastly, we will retrieve the data from Excel:
'First UserName is stored in the first column (Column A), second Row
(A2) of the spreadsheet
sUserName = xlSheet.Rows(2).Columns(1).Value
'First Password is stored in the second column (Column B), second Row
(B2) of the spreadsheet
sPassword = xlSheet.Rows(2).Columns(2).Value
I think we have covered the concepts needed to parameterize our script directly from an
Excel file. Now, let’s use the same example we did in the previous article to parameterize
the Login process for Mercury/HP’s demo AUT.
1. Launch application
2. Enter UserName/Password from DataTable and click Login
3. Verify if the Find Flights page appears
4. If Find Flights page appears, iteration passed.
5. Return to Home Page and start Step #2
'Step 1: Launch Application
SystemUtil.Run "iexplore.exe", "http://newtours.demoaut.com"
'Step 2: Enter UserName/Password from DataTable and click Login
'Retrieve UserName and Password from "iRow" rows and columns A & B
sUserName = xlSheet.Rows(iRow).Columns(iUserNameCol).Value
sPassword = xlSheet.Rows(iRow).Columns(iPasswordCol).Value
'Parameterization Block:
With Browser("title:=Welcome: Mercury Tours", "index:=0")
.WebEdit("name:=userName").Set sUserName 'Parameter 1:
UserName
.WebEdit("name:=password").Set sPassword 'Parameter 2:
Password
.Image("name:=login").Click
End With
'Step 3: Verify if the Find Flights page appears
Steps 1-5
We can now combine all the 5 steps into a single code block:
'Steps 2-5
'Retrieve UserName and Password from "iRow" rows and columns A & B
sUserName = xlSheet.Rows(iRow).Columns(iUserNameCol).Value
sPassword = xlSheet.Rows(iRow).Columns(iPasswordCol).Value
'Parameterization Block:
With Browser("title:=Welcome: Mercury Tours", "index:=0")
'Step 2
.WebEdit("name:=userName").Set sUserName 'Parameter 1: UserName
.WebEdit("name:=password").Set sPassword 'Parameter 2: Password
.Image("name:=login").Click
End With
xlBook.Close
xlApp.Quit
Set xlSheet = Nothing
Set xlBook = Nothing
Set xlApp = Nothing
When the above code executes, the follow results will be populated in QTP:
I hope this article will help automation developers create parameterization with Excel.
The concepts covered in this article are quite basic, but they should serve as a baseline to
creating parameterization through Excel which can give users more control over how
they data-drive their tests. In the coming articles, I will show how Excel can be used as a
data-source with performance that QTP’s DataTable just doesn’t provide.
QTP: Working With Multiple Windows Applications
After writing the article Working with Multiple Browser Applications with QTP, I
thought, there are several instances where automation developers have to work with
applications containing multiple windows, in a Standard Windows Environment. This
technique uses a similar methodology as demonstrated in the article for Web-based apps,
but the crux of this technique differs. It can be used by automation developers testing
most types of windows applications, and it contains concepts that are showcased in my
generic automation framework RelevantCodes[1]One, which will be released in the
coming few weeks.
With the help of this technique, regardless of how many windows applications are opened
through QTP, we would never have to keep track of the Window’s title or any of its
recognition properties (unless we want to). In other words, regardless of a window’s
dynamic nature, this concept will enable you to give the window a name of your choice,
and use that name to identify the window, instead of keeping track of changes in its
properties.
• LaunchAdd: Launch a new window and retain its reference throughout the test
cycle
• AddNew: Automatically add a new open window to the collection without you
having to specify its properties (see list of dependencies)
• AddCustom: Adds a custom window by specifying its properties as an array
• All identifiers stay intact until the test finishes.
This concept contains 3 main methods, as described above. You can download the Class
clsWindow for complete documentation, but for a quick overview, please continue
reading.
First, and the one that may be used the most often is a method called AddCustom. It
enables users to add a window of their choice to the collection, with the name they want
to specify. From that point onwards, regardless of the number of changes that particular
window goes through, it will not be required to update its properties. The name you give
the window can be used throughout the test cycle. Below is the code snippet for
AddCustom:
Me.sName = sName
Me.arrPropertyValue = arrPropertyValue
'Retrieve the handle of the specified window
lngHwnd = GetCustomWindowHwnd()
Me.sFilePath = sFilePath
Me.sName = sName
'If the handle is valid, then add it to the global collection object
If lngHwnd <> -1 Then
colObject.Add sName, Window("hwnd:=" & lngHwnd)
If Not oDict.Exists(lngHwnd) Then
oDict.Add lngHwnd, lngHwnd
End If
End If
End Sub
sFilePath = Me.sFilePath
sName = Me.sName
GetRecentlyOpenWindowHwnd = -1
On Error Goto 0
End Function
Lastly, and a tricky one is AddNew. This will automatically add any new open window
after LaunchAdd has been executed. Like the other 2 methods, this method will also
enable preservation of a window’s property until the end of the test.
'Loop for all objects in the collection and find a one that does
'not exist in the global dictionary: colWindows
For x = 0 to oParent.Count - 1
If Not oDict.Exists(oParent(x).GetROProperty("hwnd")) Then
lngHwnd = oParent(x).GetROProperty("hwnd")
oDict.Add lngHwnd, lngHwnd
colObject.Add sName, Window("hwnd:=" & lngHwnd)
Exit Do
End If
Next
Wait(1)
iTimeElapsed = iTimeElapsed + 1
Loop Until iTimeElapsed = 5 'This timeout can be minized to 1, if
desired
End Sub