How can I import a function into my QTP test while it's running?
Asked Answered
G

1

2

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?

Ginger answered 9/11, 2012 at 0:16 Comment(0)
G
2

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
Ginger answered 9/11, 2012 at 0:16 Comment(4)
Do you know if there are negative side effects with LoadFunctionLibrary and reloading the same library multiple times (like you would do with @import or using in other libraries)? QTP10 user here, but just curious because... you know, I just want to know.Darladarlan
The only negative I am aware of relating to LoadFunctionLibrary vs ExecuteFile is that LoadFunctionLibrary doesn't support user defined classes.Gabrielagabriele
Even with Method 3, if you are working with the excels they will be renamed and you won'τ be able to find them!Snowman
The other negative that I just found out is that GetRef will not work if you are fetching any function from any imported FL via LoadFunctionLibrary. However, if you use ExecuteFile, it worksYann

© 2022 - 2024 — McMap. All rights reserved.