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

Wednesday, 15 June 2011

Friday, 3 June 2011

How to count the number of link in a website using QTP?

To count the number of links in a web page, We can use ChildObjects to get the link collection, then retrieve the properties for example number of links or any other objects on page.

Dim oDesc, oAllLinks, iNumberOfLinks
set oDesc = Description.Create()
oDesc("micclass").Value = "Link"
Set oAllLinks = Browser("creationtime:=0").
Page("micclass:=Page").ChildObjects(oDesc)
iNumberOfLinks = aAllLinks.Count
msgbox iNumberOfLinks

How To Validate the All links in a website through QTP?


‘SCENARIO NAME :Check all links
‘DESCRIPTION :This code Check all links in any web page
‘PRECONDITIONS :This Assume that when click on Link the new page will open on current window but not in new window
‘AUTHOR :Mohan kumar
‘***********************************************************************

Set oDesc = Description.Create()
‘ Retrieve HTML tag
oDesc(”html tag”).Value = “A”
Set rc = Browser(”title:=.*”).Page(”title:=.*”).ChildObjects(oDesc)
num = rc.Count() ’get the number of link in a page
For i=0 to num-1
Set rc = Browser(”title:=.*”).Page(”title:=.*”).ChildObjects(oDesc)
ref = rc(i).GetROProperty(”href”) ’get the “href”propety of the i th link
Browser(”title:=.*”).Page(”title:=.*”).link(”text:=.*”,”index:=”&i).click ’click on i th link
Browser(”title:=.*”).sync
title1=Browser(”title:=.*”).getRoproperty(”title”) ’get the tile of the target page
MsgBox title1
Browser(”title:=.*”).navigate(ref)’Navigates to the url taken from “href” property
Browser(”title:=.*”).sync
title2=Browser(”title:=.*”).getRoproperty(”title”)’get the tile of the tNavigated page
MsgBox title2
If title1=title2 Then’condition to check for the targetted page and Navigated page
Reporter.ReportEvent 0, “Navigated To Correct Page”,”"&title1′Reports if correct
else
Reporter.ReportEvent 1,”"&title1,”"&title2
End If
Browser(”title:=.*”).back’Navigates back to main page
Browser(”title:=.*”).sync

How To Copy an excel sheet data's to another excel ?

Following is the code to copy the conntents of a sheet in one excel to another excel sheet

Set objExcel = CreateObject(“Excel.Application”)
objExcel.Visible = True
Set objWorkbook1= objExcel.Workbooks.Open(“C:\Documents and Settings\mohan.kakarla\Desktop\1.xls”)
Set objWorkbook2= objExcel.Workbooks.Open(“C:\Documents and Settings\mohan.kakarla\Desktop\2.xls”)
objWorkbook1.Worksheets(“Sheet1″).UsedRange.Copy
objWorkbook2.Worksheets(“Sheet1″).Range(“A1″).PasteSpecial Paste =xlValues
objWorkbook1.save
objWorkbook2.save
objWorkbook1.close
objWorkbook2.close
set objExcel=nothing