实现QC中生成的QTP脚本快速修改

QC和QTP之间有很多成型的接口,提供给企业级UI自动化测试以快速、便捷的开发管理方式,只不过真正使用起来可能还需要自己手工加工一下。
    笔者以自己信奉的教条为例,QC+QTP的脚本开发首先需要选择或者自己开发合适的(适应被测系统特征的)框架,不单纯依赖QC那些简单的案例管理和执行流管理 功能;然后在框架下进行测试案例开发,在QC中生成QTP测试脚本, 自动化开发人员依据测试脚本中已经生成的注释进行UI的自动化测试描述,再进行场景、数据的综合覆盖……在自动化开发人员在对QC生成的测试脚本进行修改的时候可能会觉得QC自己生成的脚本注释过于啰嗦或者不美观,自己去逐个改吧又有点费劲,感觉这个功能就成了鸡肋!
    其实,只要我们利用QTP自身提供的方法进行简单的操作,就可以做成一个快速一键编辑的简单小程序,让自动化开发更加高效,何乐而不为呢,具体方法大部分都能通过QTP的settings窗口自动生成,下面是一个简单的例子:
 
'如下三个路径中只能保留需要加载的资源,文件格式必须正确,因为函数会加载该目录下所有文件
Dim frameRoot:frameRoot = " \\XXXXX\share\TestData\EnglishBest\systemName"
Dim repattern:repattern = "Report micDone, "&AddQuoteToStr("步骤 [1-9]")&" ,"&AddQuoteToStr("End")
Public Function AddQuoteToStr (appointedText)
 AddQuoteToStr = Chr(34) & appointedText & Chr(34)
End Function
Function ReplaceReg(Str, myPattern, repStr)
 Set Regex = New RegExp
 Regex.pattern = myPattern
 Regex.Global = True
 ReplaceReg=Regex.Replace(Str, repStr)
End Function
'如下两组参数中元素个数必须相等,否则无法执行
Dim whatToRep:whatToRep = "------------------------------------>,Reporter.ReportEvent 2,End Step,Expected Results,Description,Begin Step,Start recording here..."
Dim repToWhat:repToWhat = "**********************************************************************************,Report micDone,结束步骤,预期结果,操作描述,开始步骤,脚本操作内容请如下:"
Dim whatToRepArr:whatToRepArr = Split(whatToRep,",")
Dim repToWhatArr:repToWhatArr = Split(repToWhat,",")
Set fobjShell = CreateObject("Scripting.Filesystemobject")
Set QTPApplication = CreateObject("QuickTest.Application")
QTPApplication.Launch
QTPApplication.Visible = True
'脚本运行设置
QTPApplication.Test.Settings.Run.IterationMode = "rngAll"
QTPApplication.Test.Settings.Run.ObjectSyncTimeOut = 30000
QTPApplication.Test.Settings.Run.DisableSmartIdentification = True
QTPApplication.Test.Settings.Run.OnError = "Stop" 
'参数表设置
QTPApplication.Test.Settings.Resources.DataTablePath = "<Default>"
'函数文件移除重新加载
Set AllFiles = fobjShell.GetFolder(frameRoot & "\Functions Library\FrameWork\PAFAWEB").Files
QTPApplication.Test.Settings.Resources.Libraries.RemoveAll
For Each sFile in AllFiles
 QTPApplication.Test.Settings.Resources.Libraries.Add(sFile.Path)
Next
Set AllFiles = Nothing
'场景恢复移除重新加载
Set AllFiles = fobjShell.GetFolder(frameRoot & "\Recovery Senario").Files
QTPApplication.Test.Settings.Recovery.RemoveAll
For Each sFile in AllFiles
 QTPApplication.Test.Settings.Recovery.Add(sFile.Path)
Next
Set AllFiles = Nothing
'配置文件加载
Set AllFiles = fobjShell.GetFolder(frameRoot & "\Environment").Files
For Each sFile in AllFiles
 QTPApplication.Test.Environment.LoadFromFile(sFile.Path)
Next
Set AllFiles = Nothing
'替换QC生成的指定注释内容和格式
For ainx = 1 to QTPApplication.Test.Actions.Count
 vbsContent = QTPApplication.Test.Actions.Item(ainx).GetScript
 For arr = 0 to UBound(whatToRepArr)
  vbsContent = Replace(vbsContent, whatToRepArr(arr), repToWhatArr(arr))
 Next
 vbsContent = Replace(vbsContent, "Quality Center", "Quality Center" & Chr(10))
 vbsContent = ReplaceReg(vbsContent,repattern,"")
 QTPApplication.Test.Actions.Item(ainx).SetScript(vbsContent)
Next
QTPApplication.Test.Actions.Item(1).ActionParameterDefinitions.RemoveAll
QTPApplication.Test.Actions.Item(1).ActionParameterDefinitions.Add "posSubPath","执行流文件路径区分子路径",qtParamTypeString,qtParamDirIn,inputbox("请输入默指向路径:")
'保存测试脚本
QTPApplication.Test.Save
 
    这样,一个脚本就编辑完成了,如果要实现批量修改呢?可以吧脚本放到一个测试集(执行流)里面,也可以放到一个EXCEL里面,循环去操作,这样,即便发生公函的名称被修改或者其他意外不被允许的可逆操作,也很容易批次替换修改脚本内容,不让这样机械的工作干扰自动化开发的“自动化性”甚至影响工作效率。
    附:通过QC测试集批量修改的方法如下:
  Set TDConn = CreateObject("TDApiOle80.TDConnection.1")
  Set Wshshell = CreateObject("Wscript.shell")
 
 If  TDConn.Connected = False Then
  TDConn.InitConnectionEx " http://qc.paic.com.cn/qcbin"
  TDConn.Login qcUserName,InputBox("请输入您QC用户的密码:")
  TDConn.Connect mydomain,myproject
 Else
  TDConn.Disconnect()
  TDConn.Logout()
  TDConn.ReleaseConnection()
  TDConn.InitConnectionEx " http://qc.paic.com.cn/qcbin"
  TDConn.Login qcUserName,InputBox("请输入您QC用户的密码:")
  TDConn.Connect mydomain,myproject
 End If
 
 Set tsTreeMgr = TDConn.TestSetTreeManager
 Set tsFolder = tsTreeMgr.NodeByPath("Root\" & testSetFolder)
 If  tsFolder Is Nothing Then
  Wshshell.Popup "找不到路径【" & testSetFolder & "】", 5, "运行时错误:", 0
  Set tsFolder = Nothing
  Set tsTreeMgr = Nothing
 Else
  Set TSetFact = TDConn.TestSetFactory
  Set tsFilter = TSetFact.Filter
     tsFilter.Filter("CY_USER_01") = testSetEdition
     Set tsList = tsFolder.FindTestSets("", False, tsFilter.Text)
  If  tsList Is Nothing Then
   Wshshell.Popup "找不到测试集", 5, "指定目录下没有任何测试集!", 0
   Set tsList = Nothing
   Set tsFilter = Nothing
   Set TSetFact = Nothing
   Set tsFolder = Nothing
   Set tsTreeMgr = Nothing
  Else
   For Each aTestSet In tsList
     Set TSTestFact = aTestSet.TSTestFactory
    Set TSTestsList = TSTestFact.NewList("")
    For i = 1 To TSTestsList.Count
     Set TestToMod = TSTestsList.Item(i)     
     theTestId= TestToMod.TestId
     Set MyTestFactory = TDConn.TestFactory
     Set testFilter = MyTestFactory.Filter
     testFilter.Filter("TS_TEST_ID") = theTestId
     Set testList = MyTestFactory.NewList(testFilter.text)
     Set MySpecifiedTest = testList.Item(1)
     =========================下面是修改的具体行为,不再粘贴=======================
     Next
   Next
 End If
End If

你可能感兴趣的:(function,脚本,测试,report,each,library)