昨天朴老师给我们上了一天QTP的总结,朴老师实在喜欢折腾人。还记得月初的一次模拟面试,学习了3个多月,感觉自己还是挺牛的嘛,不过朴老师的技术面试简直是把我的自信心给完全打碎了。技术牛的人,就是喜欢深究,他问一个问题,你回答上了,他就会不停的在你的回答中寻找下一个问题,直至把你问倒!
印象很深刻的一个问题,GUI功能测试和性能测试的区别:GUI是基于开发语言的,而性能测试是基于协议的。
QTP识别对象的原理:QTP要求先在仓库文件里定义仓库对象,里面存有实际对象的特征属性的值,运行的时候,QTP会根据仓库对象的特征属性描述,寻找到实际对象,然后操作实际对象。
QTP对象中有两个很重要的东西,就是Test Object(简称TO)和Runtime Object(简称RO)。根据字面意思,TO是静态的,RO是动态的,我们做自动化测试,大部分是使用动态数据,所以RO用得比较多。对于这两个概念,网上有很多资料,大家可以去搜搜看。
昨天上课主要是做计算器的自动化框架,因为是数据驱动框架,所以分三层设计:配置层,驱动层,执行层。
首先是配置层,就是读取config.txt配置文件,选取具体的数据来源(Access,SQL,Excel,File)。然后把配置文件内容传给驱动层,由驱动层选择相应的执行函数。编写完基本框架后,就是写增强型代码了。读取路径函数,自己通过Descrīption.Create()创建对象,减小对象库的空间。最后还可把代码放入lib.vbs中,在QTP加入相关vbs文件,即可直接调用相关函数,大大简化QTP中的代码行数。
经过一天的努力,竟然写出一个近200行的代码,嗯,就像老师说的,感觉很爽,呵呵~~
明天就是徐老师的LoadRunner,要养足精神,好好听课!
'-------------------------------------------------------------------
Dim dirpath
dirpath = folder_path '保存相对路径
Dim sumresult
SystemUtil.Run "calc.exe"
Call Config_Level
'-------------------------------------------------------------------
'#########################################
'# 创建对象
'#########################################
Sub Object_Create(str_x_Button)
Set Win_Calc=Descrīption.Create()
Win_Calc("regexpwndtitle").Value="计算器"
Win_Calc("regexpwndclass").Value="SciCalc"
Set Win_Calc_Btn=Descrīption.Create()
Win_Calc_Btn("text").Value=str_x_Button
Win_Calc_Btn("nativeclass").Value="Button"
Window(Win_Calc).WinButton(Win_Calc_Btn).Click
Set Win_Calc_Btn=Nothing
Set Win_Calc=Nothing
End Sub
'-------------------------------------------------------------------
'#########################################
'# 得到相对路径
'#########################################
Function folder_path()
Dim str_full_path 'as string
Dim str_testname 'as string
str_full_path = Environment("TestDir")
str_testname = Environment("TestName")
folder_path = mid(str_full_path,1,len(str_full_path) - len(str_testname) -len( "scrīpt")-1)
End Function
'-------------------------------------------------------------------
'######################################
'# 点击函数
'######################################
Function operation(num1,num2,operate)
Dim tmp1
Dim tmp2
i = 1
While i <= len(num1)
tmp1 = mid(num1,i,1)
Object_Create tmp1
i = i + 1
Wend
Object_Create operate
j = 1
While j <= len(num2)
tmp2 = mid(num2,j,1)
Object_Create tmp2
j = j + 1
Wend
Object_Create "="
End Function
'-------------------------------------------------------------------
'配置层
'读取配置文件
Sub Config_Level
Dim fsoconfig
Dim fconfig
Set fsoconfig = CreateObject("scrīpting.FileSystemObject")
Set fconfig = fsoconfig.OpenTextFile(folder_path + "\config\config.txt",1,False)
Do While fconfig.AtEndOfLine <> True
str_Config = fconfig.ReadLine
Driver_Level str_Config
Loop
fconfig.Close
Set fconfig = Nothing
Set fsoconfig = Nothing
End Sub
'-------------------------------------------------------------------
'驱动层
Sub Driver_Level(s_Config)
Dim str_file
Dim i
str_file = split(s_Config,",")
For i = lbound(str_file) to ubound(str_file)
Select Case str_file(i)
Case "auto"
Execute_AutoSource
Case "f"
Execute_FileSource
End Select
Next
End Sub
'-------------------------------------------------------------------
'执行层
'######################################
'# 使用access数据库
'######################################
Sub Execute_AccessSource
Dim Conn 'as Adodb.Connection
Dim Rst 'as Adodb.RecordSet
Set Conn = CreateObject("ADODB.Connection")
Conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + folder_path + "\data\data.mdb;Persist Security Info=False"
Set Rst = CreateObject("ADODB.RecordSet")
Rst.Open "select * from actest",Conn
Rst.MoveFirst
Do While Rst.Eof<>True
acnum1 = Rst.Fields(1)
acnum2 = Rst.Fields(2)
acoperate = Rst.Fields(3)
operation acnum1,acnum2,acoperate
Rst.MoveNext
Loop
End Sub
'######################################
'# 使用txt文件
'######################################
Sub Execute_FileSource
Dim fso
Dim f
Set fso = CreateObject("scrīpting.FileSystemObject")
Set f = fso.OpenTextFile(folder_path + "\data\data.txt",1,False)
Do While f.AtEndOfLine <> True
str_temp = f.ReadLine
temp_split = split(str_temp,",")
num1 = temp_split(0)
num2 = temp_split(2)
ōperate = temp_split(1)
operation num1,num2,operate
Loop
f.Close
Set f = Nothing
Set fso = Nothing
End Sub
'######################################
'# 自动产生随机数和操作符
'######################################
Sub Execute_AutoSource
Dim num1
Dim operate
Dim num2
Dim result
'随机数1
num1 = 10 * Rnd
'随机数2
num2 = 1000 * Rnd
'随即操作符
Randomize
operate_temp = Int((4 * Rnd) + 1)
Select Case operate_temp
Case 1
ōperate = "+"
Case 2
operate = "-"
Case 3
operate = "*"
Case 4
operate = "/"
End Select
operation num1,num2,operate
End Sub
'######################################
'# 使用Excel数据
'######################################
Sub Execute_ExcelSource
DataTable.Import(folder_path + "\data\data.xls") '把excel数据导入default.xls中临时存放
DataTable.SetCurrentRow (1)
For i_Record_Count = 1 to DataTable.GetRowCount
operation DataTable("exnum1","Global"), DataTable("exnum2","Global"), DataTable("exoperate","Global")
DataTable.SetNextRow
Next
End Sub
'-------------------------------------------------------------------
本人编码能力较差,代码是给自己留念和日后参考。 有许多漏洞,请各位见谅。