今天学了下数据驱动测试,运行了Excel作数据源的计算器的简单例子,代码如下(只是实现了读Excel的功能,具体的比较过程没有写)。
SystemUtil.Run "calc","","",""
'Do While F.AtEndOfLine <> True
btn_str=ReadExcel("E:\qtpPractice\test.xlsx","Sheet1")
''The time li data goes
For I=1 to UBound(btn_str)
''The time li data column
For J=1 to UBound(btn_str,1)
Print btn_str(I,J)
Next
Next
Function ReadExcel(sFileName,sSheetName)
Dim Excel
Dim Range
Dim arrRange
'open excel and Appointed sheet
On Error Resume Next
Set Excel = CreateObject("Excel.Application")
Excel.Workbooks.Open(sFileName)
Set Range=Excel.Worksheets (sSheetName).UsedRange
If Err.Number <> 0 Then
ReadExcel = Array("Error")
Exit Function
End If
On Error Goto 0
''change excel data to Two dimensions several or array
arrRange = Range.Value
Excel.Workbooks.Item(1).Close
Set Range = Nothing
Excel.Quit
Set Excel = Nothing
''returen two dimensions several
ReadExcel = arrRange
End Function
note:关于错误处理多说两句,On Error Resume Next
On Error Goto 0
能够在自动化过程中对异常自动处理,并继续下面的测试,如果没有这句,异常出现,很可能测试就中止了,很可能跑了一天的用例都白跑了。
note2:函数中使用了Excel的COM接口进行编程,通过Excel接口访问Sheet页中的单元格,把单元格的数据转存储到一个二维数组中。
note3:脚本中本来也是可以用环境变量来获取测试脚本所在的路径的,不知为什么这里没通过,就直接用了路径E:\qtpPractice\test.xlsx,这个以后有机会再研究