Don't think you are, know you are

博客园 首页 新随笔 管理

 

QC 运行 QTP 脚本是不带参数的,因此当我们构建自己的框架,需要灵活配置一些东西的时候,需要在QTP中读取外部文件,清除和准备数据时需要操作数据库。

下面是简单实现代码,适用于10.0.

 

1,读取文本文件

 

加一个配置文件存放测试 Url 和数据库连接字符串

 

Dim fso, SettingFile,TestUrl,DBConnectionString
Set fso=CreateObject("scripting.FileSystemObject")  
Set SettingFile = fso.openTextFile("C:\AutoTestSettingFile.txt",1,false)
TestUrl=SettingFile.ReadLine
DBConnectionString=SettingFile.ReadLine
SettingFile.Close

 

2,

查询SQL数据库

Dim cnn,rst
Set cnn = createObject("adodb.connection")
Set rst = createObject("adodb.recordset")
sql1="SELECT  * FROM Student"
cnn.Open "provider=SQLOLEDB;User ID=sa;PWD=infoshare;Initial Catalog=SL_LINQDemo;Data Source=CSICNSQL03;database=SL_LINQDemo"        
res.Open sql1,cnn
res.MoveFirst

While  NOT res.EOF

msgbox res.Fields("StudentName")

res.MoveNext

Wend

 

执行删除,构建测试环境

Dim cnn,rst
Set cnn = createObject("adodb.connection")
Set rst = createObject("adodb.recordset")
' CLEAR CALENDAR AND EVENTS
sql1 ="DELETE FROM dbo.event WHERE convert(varchar,event_start_datetime,101)='05/03/2010'"
sql2 ="DELETE FROM dbo.calendar_track_date WHERE convert(varchar,track_date,101)='05/03/2010'"
sql3 ="DELETE FROM dbo.calendar_track_detail WHERE convert(varchar,track_date,101)='05/03/2010'"
'CLEAR CASE
sql4 = "DELETE FROM  dbo.case_info WHERE case_name LIKE 'CSITESTTEAM%'"
cnn.Open DBConnectionString        
rst.Open sql1,cnn,3,2
rst.Open sql2,cnn,3,2
rst.Open sql3,cnn,3,2
rst.Open sql4,cnn,3,2
cnn.close 

 

 

3,如何读取Excel (陈能技博客转)

所在机器必须安装Excel

 

arrRange = ReadFile("c:\1.xls","Sheet1")
'Msgbox UBound (arrRange,1)
'Msgbox UBound (arrRange,2)
' 读取其中一个单元格的数据
Msgbox arrRange(1,1)

Function ReadFile(sFileName,sSheetName)
       Dim oExcel
       Dim oSheet
       Dim oRange
       Dim arrRange
 
       On Error Resume Next
           ' 创建Excel应用程序对象
              Set oExcel = CreateObject("Excel.Application")
              If err.Number <> 0 Then
                     MsgBox "未能初始化Excel" & vbCrLf & _
                                 "请确保Excel已安装", vbCritical
                     Exit Function
              End If
       On Error Goto 0
                    
       On Error Resume Next
                     ' 打开Excel文件
                     oExcel.Workbooks.Open(sFileName)
                  If err.Number <> 0 Then
                            MsgBox "未能加载Excel文件" & vbCrLf & _
                                   "请确保Excel文件路径正确或格式正确", vbCritical
                            Exit Function
                     End If
       On Error Goto 0
      
       ' 获取表格的使用范围
       Set oSheet = oExcel.Worksheets(sSheetName).UsedRange
       ' 获取从A列到Z列,从第1行到第1000行的范围i中的所有值
       Set oRange = oSheet.Range("A1:Z1000")
       '把Excel数据转换到数组
       arrRange = oRange.Value
       ' 关闭工作簿
       oExcel.WorkBooks.Item(1).Close
       ' 退出Excel
       oExcel.Quit
       Set oExcel = Nothing
    ' 返回包含Excel数据的数组
     ReadFile = arrRange
End Function

 

posted on 2010-06-08 16:43  炭炭  阅读(1747)  评论(0编辑  收藏  举报