Thursday, 23 June 2011

QTP: Parameterization with Excel

Dim xlApp, xlBook, xlSheet
Dim iRow, sUserName, sPassword
CONST iUserNameCol = 1 'UserName is in Column A
CONST iPasswordCol = 2 'Password is in Column B

Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.WorkBooks.Open("C:\TestFile.xls")
Set xlSheet = xlBook.WorkSheets("Sheet1")
 
'iRow = 2 because data to be driven starts from Row #2
For iRow = 2 to xlSheet.UsedRange.Rows.Count
 
   '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   
 
   '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
      Browser("title:=Find a Flight: Mercury Tours:", "index:=0").Link("text:=Home").Click
 
      'Step 4: Pass the iteration
      Reporter.ReportEvent micPass, "Iteration " & iRow-1, "UserName: " &sUserName& " is valid"
   Else
      'Step 5: Fail the iteration and return to the Home page
      Reporter.ReportEvent micFail, "Iteration " & iRow-1, "UserName: " &sUserName& " is invalid"
      Browser("title:=Sign-on: Mercury Tours", "index:=0").Link("text:=Home").Click
   End If   
 
Next
 
xlBook.Close
xlApp.Quit
Set xlSheet = Nothing
Set xlBook = Nothing
Set xlApp = Nothing

No comments:

Post a Comment