在QTP Test中利用vbs和cmd实现重新启动QTP

关于qtp如何自启动,这个问题之前在iQuickTest上面问过zzxxbb123,不过他给的答案比较概括一点。所以自己就想办法弄的具体了点。以下是之前写的文章,也是最早在51autotest上面放的。链接

http://www.51autotest.com/home.php?mod=space&uid=2535&do=blog&id=145

连续长时间执行过QTP或使用QC连续长时间调用QTP的朋友应该会知道,QTP在长时间执行过程中会产生内存泄露。而QTP自身也有一个工具Remote Agent来实现每执行N次来重新启动测试工具释放内存。

      但是,不知道是不是我用破解版的原因,如果使用RunTest来调用QC执行QTP时,这个设置的Remote Agent会使得测试失败,并卡死在某一步。所以在这里需要进行手动清理QTP长时间运行过程中产生的内存泄露。
      在这里,我所知的清理内存有两种方式,一种是利用一些内存清理工具,一种是通过脚本控制QTP重新启动。
      首先说说内存清理工具。今天在群里下到了一个兄弟共享的内存清理工具memempty.exe,它可以进行定时清理。但是由于QTP执行每个Test的时间都不相同,并且QTP在执行过程中,这个工具也无法有效的清理干净内存部分。对这些不是很懂,点下就行了,免得出丑。
       使用脚本控制,这里我用的是创建外部vbs并通过cmd命令来执行的方式实现的,脚本如下:
dim restartcode
set fso = CreateObject("Scripting.FileSystemObject")
set file = fso.CreateTextFile("D:\RestartQTP.vbs")
restartcode = "'浣跨敤涓€涓瓑寰呮椂闂翠娇寰梣tp鑳借繑鍥瀜c涓€涓墽琛屾垚鍔熺殑缁撴灉"&vbnewline
restartcode = restartcode &"WScript.Sleep 3000"&vbnewline
restartcode = restartcode &"'浣跨敤AOM鏉ユ帶鍒秖tp鐨勯噸鍚搷浣�"&vbnewline
restartcode = restartcode &"Dim qtp"&vbnewline
restartcode = restartcode &"Set qtp = CreateObject(""QuickTest.Application"")"&vbnewline
restartcode = restartcode &"qtp.Quit"&vbnewline
restartcode = restartcode &"WScript.sleep 2000"&vbnewline
restartcode = restartcode &"qtp.Launch"&vbnewline
restartcode = restartcode &"qtp.visible = true"&vbnewline
restartcode = restartcode &"set qtp = nothing"&vbnewline
restartcode = restartcode &"set fso = CreateObject(""Scripting.FileSystemObject"")"&vbnewline
restartcode = restartcode &"fso.deletefile ""D:\RestartQTP.vbs"""&vbnewline
restartcode = restartcode &"set fso = nothing"&vbnewline

file.write restartcode
set fso = nothing
set WshShell = CreateObject("WScript.Shell")
WshShell.run"cmd.exe /c D:\RestartQTP.vbs"
set WshShell = nothing

这样就可以实现重新启动QTP了,不过在执行下一个test之前需要一个等待时间,这个在调用脚本中很简单,就不罗嗦了
原文地址:https://www.cnblogs.com/SilenceCity/p/2730255.html