Saturday 29 August 2015

Import test test data from excel and run the scripts.


 '' Above table is excel data stored in strData worksheet..
'''***********************************************
strFile= "C:\Users\Venu\Desktop\QTP_ex\ImportData.xls"
sheetNM="strData"
Dim arrColumns()
intCols=0
testCase= "Gmail Login"
intRow=0
Set Exl= CreateObject("Excel.Application")
Exl.Application.Visible = False
Exl.Workbooks.Open(strFile)

'' Search whether work sheet is available or not..
blnSheet= False
For j=1 to Exl.Worksheets.Count
    If Exl.Worksheets(j).Name=sheetNM Then
        blnSheet= True   
        Exit For
    End If
Next

'' Set the sheet reference here
Set Ws= Exl.Sheets(sheetNM)

RowCnt= Ws.UsedRange.Rows.Count
ColCnt= Ws.UsedRange.Columns.Count

For i=2 to RowCnt
    If instr(1, ws.Cells(i, 1), testCase, 1)>0 Then
        intRow=i
        Exit For
    End If
Next

If intRow=0 Then
''    "Data Not found in excel sheet.."
    ExitTest
End If

'' Get the Header values into an array
For i=1 to ColCnt
    If ws.Cells(1, i)<>"" Then
        Redim Preserve arrColumns(i)
        arrColumns(i-1)= ws.Cells(1, i)
        intCols= i     '' intCols= total number of columns in excel that we need to use as headers in datatable.
    else 
        Exit For
    End If
Next

'' Set the header values
Set thdGlobal=DataTable.GlobalSheet
For each head in arrColumns
    thdGlobal.AddParameter head, ""
Next

''First import header data from excel into DataTable 
TextFound=0
For i=intRow to RowCnt
'' some we might have more than 1 test case with same name.. so import all matching ones, run for all..
    If instr(1, ws.Cells(i, 1), testCase, 1)>0 Then
        TextFound= TextFound+1
        thdGlobal.SetCurrentRow TextFound 
        For  j=1 to intCols
            DataTable(j, "Global")= ws.Cells(i, j)
        Next
    End If
Next

Exl.Quit
Set Exl= Nothing

No comments:

Post a Comment