February 06, 2008
Tips4 - To Access a Excel by without pre-defined DSN
In Silktest, there is no built-in functions provided to access data from Excel. But we can access Excel data, by treating Excel as Database. By using below code snippet, you can access the Excel data without creating any DSN. You can directly pass Excel filename and worksheet name.
Code
[+] public void ProcessExcelData(STRING sExcelFile, String sExcelSheet)
[ ] // Purpose: Drives test suite based on excel sheet(testcase) information.
[ ] // Executes for given worksheet.
[ ] //SQL declaration
[ ] HANDLE hDB
[ ] HANDLE hSQL
[ ] List of STRING glsData //To get testdata
[ ]
[ ] Print ("Test data:{sExcelFile} & Worksheet: {sExcelSheet} ")
[ ]
[-] do
[ ] hDB = DB_Connect ("DRIVER=Microsoft Excel Driver (*.xls);DRIVERID=790;FIRSTROWHASNAMES=1;READONLY=FALSE;DBQ={sExcelFile}")
[ ]
[ ] //execute a SQL statement
[ ] hSQL = DB_ExecuteSQL (hDB, "SELECT * from [{sExcelSheet}$]")
[ ] //while there are still rows to retrieve
[-] while DB_FetchNext (hSQL, glsData)
[ ] GetListData (glsData) //Get all data without null
[ ]
[ ]
[ ] //clean up the query
[ ] DB_FinishSql (hSQL)
[ ]
[ ] //disconnect from the database
[ ] DB_Disconnect (hDB)
[ ]
[-] except
[ ] ExceptLog()
[ ] Print ("Excelsheet {sExcelFile} couldn't be accessed by SilkTest.")
[ ]
[ ]
0 comments:
Post a Comment