多测师是一家拥有先进的教学理念,强大的师资团队,业内好评甚多的接口自动化测试培训机构!

17727591462

联系电话

您现在所在位置:接口自动化测试培训 > 新闻资讯

QTP中的数据库、文件操作-自动化测试

更新时间:2022-05-31 09:24:23 作者:多测师 浏览:161

  1.datatable

  QTP本身程序就给我们提供了这么一个数据表,我们可以把测试数据或测试用例填入这个数据表中。

  如:设计用例

  username  passwd

  case1  mercury mercury

  case2 xxxxxxx xxxxxx

  录制脚本

  For i=1 to Datatable.GetRowCount

  Dialog("Login").WinEdit("Agent Name:").SetDataTable("username", dtGlobalSheet)

  Dialog("Login").WinEdit("Password:").SetDataTable("passwd", dtGlobalSheet)

  Dialog("Login").WinButton("OK").Click

  datatable.GlobalSheet.SetNextRow

  Next

  本例是验证一个登录系统,通过DataTable不同的用例设计,驱动起这段脚本,达到测试的效果.当然上面的例子中还少一个很重要的步骤,那就是结果比较.如果不能进行结果比较的自动化测试不能够称为自动化测试。当然我们这里主要讲的是数据驱动,所以不在对上面的例子进行补充。

QTP中的数据库、文件操作-自动化测试

  2.文本文件

  我们可以把文本文件当成数据文件,通过对文本文件的读写操作,来实现数据驱动。

  例:文本文件内的内容

  mercury,mercuy

  读文件的代码

  Function writeorderno(orderno)

  Dim fso, myfile,username,passwd

  Set fso=CreateObject("scrīpting.FileSystemObject")

  Set myfile=fso.openTextFile("C:\testing.txt",1,false)

  tmp=split(myfile.readline,",")

  username=tmp(0)

  passwd=tmp(1)

  myfile.close

  End Function

  写文本文件的代码

  Function writeorderno(orderno)

  Dim fso, myfile

  Set fso=CreateObject("scrīpting.FileSystemObject")

  Set myfile=fso.openTextFile("C:\result1.txt",8,false)

  myfile.writeline orderno

  myfile.close

  End Function

  3.EXCEL文件

  我们可以把EXCEL文件当成数据文件,通过对EXCEL文件的读写操作,来实现数据驱动。

  可以把EXCEL文件当作对象的方式来完成写的操作

  Dim Excel,ExcelSheet

  Set Excel=CreateObject("Excel.Application")

  Set ExcelSheet=CreateObject("Excel.Sheet")

  ExcelSheet.Application.visible=true

  ExcelSheet.ActiveSheet.Cells(1,1).value=1

  ExcelSheet.ActiveSheet.Cells(1,2).value=2

  ExcelSheet.ActiveSheet.Cells(1,3).value=3

  Excel.Save "C:\test.xls"

  Set ExcelSheet=Nothing

  用ADO的方式连接EXCEL文件来做读的操作

  Dim conn,input,filename

  filename="D:\公基本情况(tb_gsgk)-标准格式.xls" '

  Set conn= createobject("ADODB.Connection")

  conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source="&filename&";Extended Properties='Excel 8.0;hdr=yes'"

  Set input= createobject("ADODB.Recordset")

  input.Open "select * from [公基本情况-标准格式$] " ,conn,2,2

  input.close

  Set input=nothing

  4.数据库

  可以利用设计数据表,把测试数据和测试用例放在数据表里,用ADO或者其他任何可以访问连接数据库的方式连接数据库,来实现数据驱动

  Dim res,cmd,sql

  Set Res=createobject("adodb.recordset")

  Set Cmd=createobject("adodb.command")

  Cmd.activeconnection="Provider=SQLOLEDB.1;Password=111111;Persist Security Info=True;User ID=sa;Initial Catalog=xhq;Data Source=192.168.191.142"  '这句话是连接数据库的数据源,要做修改

  Cmd.CommandType = 1

  sql="selec t * from 表 where name=username"

  Cmd.CommandText = sql

  Set res = Cmd.Execute()

  Set res = nothing

  Set cmd.ActiveConnection = nothing

  Set Cmd= nothing

  以上内容为大家介绍了自动化测试中的QTP中的数据库、文件操作,本文由多测师亲自撰写,希望对大家有所帮助。了解更多自动化测试相关知识:https://www.aichudan.com/xwzx/

联系电话

17727591462

返回顶部