专注于自动化测试,性能测试.......

发布新日志

  • 使用VBS脚本从Exce中读取测试数据(原创)

    2009-09-24 21:39:34

      这是以前为了在测试工具中方便的读取Excel数据写的脚本(一个轻量级框架的一部分)。总体思路是:利用Excel的com接口,把数据读取到字典(键值对,也就是测试字段对应测试数据)中,然后在通过键值对的key把测试数据读取出来。(这种方法虽然比较简陋,但是胜在简单易用)。下面就具体说明一下:

     首先,我们先看一下Excel中的测试数据格式,

    Field   DataType     DataValue     DataValue 
    姓名     string           姓名1      姓名2
    性别     string           男          女
    年龄       int            34         45

    第一列 字段名称

    第二列 数据类型

    第三列及之后 测试数据

    具体代码如下:

    Dim objexcel 'excel对象
    Dim objworkbook 'excel的workbook对象
    Dim objsheet 'sheet对象
    Dim objDictionary '字典对象

    '=========================
    '获取sheet对象
    'sheetname: sheet名称
    'path: 文件路径
    '=========================
    Function Getexcelobject(sheetname,filepath)

      Set bjexcel = CreateObject("excel.application")
      Set bjworkbook = objexcel.Workbooks.Open(filepath)
      Set bjsheet = objworkbook.Sheets(sheetname)
    End Function

    '========================
    '关闭excel
    '========================

    Function ExcelColse()
      objworkbook.Close
    End Function 

    '========================
    '获取测试字段与默认测试数据的键对数组
    '========================

    Function GetDictionary()
      Set bjDictionary = CreateObject("Scripting.Dictionary")
      x=2
      Do While Not (objsheet.cells(x,1).value ="")
      objDictionary.Add CStr(objsheet.cells(x,1)),CStr(objsheet.cells(x,3))
      x=x+1
      Loop 
    End Function

    '========================
    '获取测试字段与指定测试数据的键对数组
    '========================

    Function GetSpecDictionary(y)
      Set bjDictionary = CreateObject("Scripting.Dictionary")
      x=2
      Do While Not (objsheet.cells(x,1).value ="")
      objDictionary.Add CStr(objsheet.cells(x,1)),CStr(objsheet.cells(x,y))
      x=x+1
      Loop 
    End Function

    '=========================
    '获取默认测试数据值
    'field:AUT字段
    '=========================
    Function Getvalue(field)
      GetDictionary()
      Getvalue = objDictionary(field)
    End Function 

    '=========================
    '获取测试数据的数量
    '=========================
    Function Getvaluenumber()
      GetDictionary()
      Getvaluenumber = objDictionary.Count
    End Function 


    '========================
    '获取指定列数据,field为字段名,colnumber为列值
    '========================
    Function Getspecvalue(field,colnumber)
      GetSpecDictionary(colnumber)
      Getspecvalue = objDictionary(field)
    End Function


    如果想读取默认测试数据,也就是读取第三列,需如下进行:

    Call Getexcelobject("sheet1","E:\TestData\查询.xls")
    WScript.Echo Getvalue("姓名") 
     

    如果想读取任意列的测试数据,需:

    Call Getexcelobject("sheet1","E:\TestData\查询.xls")
    WScript.Echo Getspecvalue("姓名",4) 




  • 在记录集中插入记录

    2009-04-04 13:14:50

    '在记录集中插入记录

    '建立连接对象
    Set bjconn = CreateObject("adodb.connection")

    '建立记录集对象
    Set bjrecordset =CreateObject("adodb.recordset")

    objconn.Open _
    "Provider = SQLOLEDB;Data Source=.;Trusted_Connection =yes;"&_
    "Initial Catalog =Cwxt;User Id =sa;password=1;"
    objrecordset.Open "select * from tbl_user",objconn,3,3

    objrecordset.AddNew()
    objrecordset.Fields(0).Value ="1"
    objrecordset.Fields(1).Value ="22"
    objrecordset.Fields(2).Value ="33"
    objrecordset.Update()

    objconn.Close
    objrecordset.Close

  • 复制Excel某列数据到文本文件中

    2009-04-01 00:51:24

     

    Set bjexcel = CreateObject("excel.application")
    objexcel.Visible = True
    Set bjwork = objexcel.Workbooks.Open("d:\1.xls")
    Set bjsheet = objwork.worksheets(1)
    '行
    i=1
    '列
    n=3
    Do While True
        strValue = objsheet.Cells(i,n)
        If strValue = "" Then
            Exit Do
        End If
        strText = strText & strValue & vbCrLf
        i = i + 1
    Loop

    objExcel.Quit

    Set bjFSO = CreateObject("Scripting.FileSystemObject")
    Set bjFile = objFSO.CreateTextFile("d:\ExcelData.txt")

    objFile.Write strText
    objFile.Close

  • 判断文件/文件夹是否存在

    2008-08-26 22:13:29

     在日常测试中,可能需要判断生成的文书或者文件夹是否正确存在,而手工去一一验证则工作量比较大而且枯燥。利用脚本实现就比较方便了。具体如下:

    以下是具体的实现代码:
    Set ōbjFSO = CreateObject("scrīpting.FileSystemObject")
    If objFSO.FolderExists("C:\scrīpts") Then
        Wscrīpt.Echo "The folder exists."
    Else
        Wscrīpt.Echo "The folder does not exist."
    End If
     
    判断是否存在指定文件
    Set ōbjFSO = CreateObject("scrīpting.FileSystemObject")
    If objFSO.FileExists("C:\scrīpts.txt") Then
        Wscrīpt.Echo "The file exists."
    Else
        Wscrīpt.Echo "The filedoes not exist."
    End If
    而如果文件夹在远程计算机上,则可以利用以下语句实现:
    strComputer = "atl-ws-01"//计算机名
    Set ōbjWMIService = GetObject _
        ("winmgmts:\\" & strComputer & "\root\cimv2")
    Set colFolders = objWMIService.ExecQuery _
        ("Select * From Win32_Directory Where " & _
            "Name = 'C:\\scrīpts'")
    Wscrīpt.Echo colFolders.Count
    如果不知道文件生成的路径,则可以:
    strComputer = "atl-ws-01"
    Set ōbjWMIService = GetObject _
        ("winmgmts:\\" & strComputer & "\root\cimv2")
    Set colFolders = objWMIService.ExecQuery _
        ("Select * From Win32_Directory Where " & _
            "FileName = 'scrīpts'")
    Wscrīpt.Echo colFolders.Count
  • 检查当前使用用户是否为管理员

    2008-08-25 22:59:18

    Set ōbjNetwork = CreateObject("Wscrīpt.Network")
    strComputer = objNetwork.ComputerName
    strUser = objNetwork.UserName

    Set ōbjGroup = GetObject("WinNT://" & strComputer & "/Administrators")
    For Each objUser in objGroup.Members
      If objUser.Name = strUser Then
      Wscrīpt.Echo strUser & " is a local administrator."
      End If
    Next

    那么,这段脚本做了些什么?好,首先我们创建了 Wscrīpt Network 对象的一个实例;我们可以使用该对象获得计算机及登录用户的名称。

    牢牢掌握了这些名称之后,我们使用 WinNT 提供者绑定到所讨论计算机上的 Administrators 组。然后,我们进入一个 For Each 循环,遍历所有的组成员(Members 属性以数组形式保存,这就是我们为什么需要 For Each 循环的原因)。对于找到的每一位成员,我们检查成员的登录名(objUser.Name)是否等于登录用户的名称(保存在变量 strUser 中)。

    以及该成员的名称是否与登录用户的名称相匹配?如果都满足条件,那么意味着登录用户肯定是一位本地管理员;否则,他(她)不是 Administrators 组的成员。在示例脚本中,我们仅仅显示了用户是否为一名本地管理员;在实际的登录脚本中,您可以更进一步,执行一些要求管理员权限的任务。

Open Toolbar