问题
I've built many functions for my QTP 10 tests, and many of those functions rely on other, related functions. I would like to have my functions import any other functions that they require. Currently, I have to go through each of my functions and associate each of their dependencies by hand.
While I'm aware that ExecuteFile "C:\Functions\SampleFunction.vbs"
would work, the downside is that QTP is unable to display any of the code it just imported. That means that debugging the code is a nightmare, since QTP will show that yellow debugging pointer on lines that don't correspond to the code actually being run. Long story short, that approach is a mess.
Is there any other command that will import other .vbs files into QTP during runtime, so I can have the functions import the other functions they require?
回答1:
I found a post by Anish Pillai called "4 Different Ways to Associate Function Libraries to your QTP Scripts", that has some useful info. (See the original post here: http://www.automationrepository.com/2011/09/associate-function-library-to-qtp-script/ )
Method #1 is the usual way to associate functions with a test; nothing new there.
Method #2 Using AOM (Automation Object Model)
I've tried many different variations, but all of them seem to be scripts for launching a specific test from outside of QTP, not for adding a function to a running test.
Here's their code in case it proves useful:
'Open QTP
Set objQTP = CreateObject("QuickTest.Application")
objQTP.Launch
objQTP.Visible = True
'Open a test and associate a function library to the test
objQTP.Open "C:\Automation\SampleTest", False, False
Set objLib = objQTP.Test.Settings.Resources.Libraries
'If the library is not already associated with the test case, associate it..
If objLib.Find("C:\SampleFunctionLibrary.vbs") = -1 Then ' If library is not already added
objLib.Add "C:\SampleFunctionLibrary.vbs", 1 ' Associate the library to the test case
End
Method #3 Using ExecuteFile Method Has the same downfalls that I brought up in the question. Could be useful, but it's horrible for debugging within QTP 10.
Method #4 Using LoadFunctionLibrary Method This is the most promising approach. It appears to do exactly what we need it to: load vbscript function libraries while the test is running. The only catch? It appears to be QTP 11+ only. I can't vouch for this method since I don't have QTP 11, but it looks like the perfect approach.
LoadFunctionLibrary "C:\YourFunctionLibrary_1.vbs" 'Associate a single function library
LoadFunctionLibrary "C:\FuncLib_1.vbs", "C:\FuncLib_2.vbs" 'Associate more than 1 function libraries
来源:https://stackoverflow.com/questions/13300197/how-can-i-import-a-function-into-my-qtp-test-while-its-running