Header Ads

How to work with Data Table in QTP

Well in the last post we have learnt about the function concept of QTP. Today i will cover few more about datatable.

1. Mostly we will be using MS-Excel for QTP. There are two methods to invoke one excel file to your test.
1.1. Save your test to a specific folder.
1.1.1. Open that folder via window explorer.
1.1.2. Find Default.xls
1.1.3 Rename it as Default-old.xls
1.1.4 Now rename your datasheet as "Default.xls"
1.1.5 Paste it here.
1.1.6. Reopen your script viz test.

you can find the excel is populated in the datatable area with exact sheetnames given in that excel.

2. Otherwise you can dynamically add excel file through code.
I would say this is very good method. Every time you invoke a excelfile that will load into your primary memory . So it is going to consume the primary memoory. It is better to load files whenever required.

The few lines are enough to start with

datatable.AddSheet("Sam")
datatable.ImportSheet "H:\Automation\sam.xls",1,"Sam"
msgbox datatable.GetSheet("Sam").GetRowCount 

The first line will add a sheet.(i prefer not to touch the Global or the sheet1)
The secondline will import the sheet to the QTP datatable in the sheet called Sam.1 signifies it will import the first sheet to sam.

Now you need to know how many rows are there in the datatable.

Now we need to read the rows one by one...for this we need to select a particular row first.
the sysntax is
datatable.SetCurrentRow(i)
i is the looping variable

datatable.AddSheet("Sam")
datatable.ImportSheet "H:\Automation\Sam.xls",1,"Sam"
s= datatable.GetSheet("Sam").GetRowCount

For i=1 to s
   datatable.SetCurrentRow(i)

Next

All right now we need to catch the values specified in the sheet.

The syntax is ...
datatable(" Colomn Name"

like-
datatable.AddSheet("Sam")
datatable.ImportSheet "H:\Automation\Sam.xls",1,"Sam"
s= datatable.GetSheet("Sam").GetRowCount

For i=1 to 5
    datatable.SetCurrentRow(i)
msgbox datatable("Name")
Next

so it will give me all the names specified in the sheet under "Name" colomn


Remember one thing always...
Datatable starts with 1 if you are using excel.
But Array starts with 0
Powered by Blogger.