Actions vs Functions in QTP 9.0
When deciding whether a piece of code should go in a reusable action or a function, we usually favored functions in QTP 8.2. When Mercury introduced QuickTest 9, they made the argument for functions even more compelling with the Multi Document Interface. Now we can open as many function libraries as we want, but we are still limited to one test. So, if you want to edit a reusable action in another test, you have to close your current test, open the second test, make your changes, save and close the second test then reopen your original test.
In addition to that new argument for functions in QTP 9, the same old arguments from 8.2 still apply. I’ll sum up a few of the key differences between reusable actions and functions here.
Passing Data
Actions - can only accept primative data types as parameters (strings, integers, etc)
Functions - can accept arrays, dictionary objects and test objects (i.e. Pages, Frames, WebRadioGroups, etc.)
Resource Usage
Actions - For each action, there’s a folder, three separate vbs files, a local Object Repository, a subfolder containing snapshots, an Excel spreadsheet, and a few seconds of load time.
Functions - There’s the code contained in the Function, and that’s all
API
Actions - You cannot insert calls to Existing Actions through the QTP API, you can only do it through the interface
Functions - You can attach Function Libraries to Test Scripts through the QTP API
Return Values
Actions - Return values are difficult to understand and read in the syntax
Functions - Return values work like they do in other languages (i.e. as lvalues)
UPDATE:
In comments, Jugular Bean points out a significant downside to functions that I neglected above. You cannot change the number of parameters that a function accepts without affecting all calls to that function. That is a problem that used to drive me crazy. It seems like every time I put a new function in a library, the next test I write needs that function with one extra option. Now that I use the getopts function to define the parameters in my functions, I can easily add new optional parameters without messing up other tests that already call the function.
Tags: Actions, featured, Functions, Object Repository, QTP 9, QuickTest Pro, Software-Testing


April 3rd, 2006 at 9:52 am
Hi, as a frequent reader I find your site very interesting, and helpful as a QTP user!!!
Few thing you forgot to mention in this post, when using functions you there isn’t hierarchical arrangement of the test results.
And more important, what do you recommend to use actions or functions???
Reply to DOA
April 3rd, 2006 at 12:05 pm
That’s an interesting point about the hierarchical arrangement of test result, and it represents how a methodology for one team may not work with another.
Our test scripts are still divided into actions, and in these actions, our functions put Reporter.ReportEvent calls all over the test results. You can’t see that we’re in a function when you view the results, but the point is that the event is still there, still registered under the test action. It may not be as granular as you need, but it’s worked well for us.
We use actions to organize our test scripts into test cases. We use functions for just about everything else. Before the MDI in QTP 9, we used External Actions all over the place, even for the smallest modular pieces of functionality. We’ve spent the last couple weeks converting the most critical code from actions to functions, improving the parameter interface, and reporting more events to the results.
So, the short answer is, I strongly recommend functions over actions for small bits of modular functionality. We use functions now about 90% of the time.
Reply to Marcus
April 3rd, 2006 at 11:37 pm
For doing some logical task a function is useful. But for modularising the workflow of the app one is testing, I prefer Actions.
2 Useful features of actions when compared to functions
- Number of parameters can be increased at any time without affecting already existing action calls.
- Can return more than 1 output parameter
It’s unfortunate that they have so many disadvantages, otherwise I’d use them over functions anyday.
Reply to Jugular Bean
April 4th, 2006 at 4:16 am
One more issue that may be acute. while exploring this matter I found out that it is not possible to use “Step-into” in debugging mode when using library functions, into the library functions, which means the function code is like a black box while debugging. this can be one hack of a lack.
Reply to DOA
April 4th, 2006 at 1:59 pm
As of version 9, they’ve fixed the black-box problem wrt functions. You can now debug and step through and develop and open as many as you want. It’s awesome, and probably the best feature of version 9. We’d never use them as much as we do if they hadn’t fixed that part of it.
Besides the black-box problem, Jugular Bean’s objections were the original reason we didn’t use functions more. I hated the signature-change implications implicit in functions until Will developed the GetOpts method. Since then it hasn’t troubled us. I hate that we’ve done so much work to avoid using their built-in mechanisms, but here we are.
As to the other point, about multiple output parameters, we tend to approach functions like you do in most other programming languages, where it’s habitual to desire only one output parameter. If we need more than one, we use one of those cool hash-like structures like we use to pass arguments.
The fact that you can’t insert actions into tests via the automation API was also one of the main reasons we switched. We’re trying to produce code that generates QTP code, and without the ability to insert references to actions, the only thing you can do with them is to have them already inserted into a template test script, then do a save as, then overwrite the mts file, and execute. With function libraries, you just add the libraries, insert the code, and you’re done. There is no test template, and you can be much more assured that it will work without broken links.
Dynamic QTP code generation is just about the best thing we’ve done with our approach, and one that I’ll describe in-depth someday, when it’s a little more baked.
Reply to Marcus
June 3rd, 2006 at 3:42 am
I would like know how one can start authoring scripts after collecting objects in object respository via learn mode.
I would like to know the most productive way to do it.
Reply to shyam
June 23rd, 2006 at 3:24 am
Can anybody tell me Object Repository is a Data file or a link file
Reply to Prashant
August 18th, 2006 at 6:46 am
Don’t we lose the convenience of accessing a Local Data Sheet and a local object repository?
Reply to Ajay Dass
August 21st, 2006 at 2:38 pm
Not at all… if you’re writing a function that you know will access a particular column in your local data sheet, you can call the DataTable method exactly as you normally would. These functions act as though they are just plopped down in the code you’re staring at in your test script anyway, as though they were more of a macro than an encapsulated function.
Same with the local object repository - just make the call to it and the function will refer to the same OR that is local to the calling script.
So, yeah, it works that way, but the way we write functions is much more generic than that: if we need access to a data table item or a specific object in the OR, we just pass it as a parameter to the function. That way it’s guaranteed to work the same way no matter what script calls it.
Hope this helps.
Reply to Marcus
October 3rd, 2006 at 2:13 am Hi,everybody can u any body explian about functions.what is the importance of functions in QTP
Reply to praveen
August 6th, 2008 at 12:45 am
Hi , can u tel me wat is QTP API and were it is used and its purpose
Reply to Jessie
November 5th, 2006 at 3:31 pm
Hi everybody,
first i would like to comment it’s a great site !!
regarding “Actions vs Functions in QTP 9.0” ,one of the biggest use of actions is images checkpoints which you can’t do it with any function (as far as i know…)
can any one comment on that ?
10x
Reply to Yaniv
February 23rd, 2007 at 1:14 am
Hi Yaniv
we cannot use checkpoints in functions, however we can call a resuable action from a function which has images checkpoints.
Pankaj
Reply to pankaj
March 25th, 2007 at 1:18 am
what is subfunction
Reply to ravindra
March 25th, 2007 at 1:22 am
it is not function in function.
Reply to ravindra
April 13th, 2007 at 9:11 am
how to access the datatables information through the script in QTP
Reply to Anusha
April 15th, 2007 at 5:41 am
hi can anybody provide me all generic functions in VB for QTP script.
Regards,
Krishna
Reply to krishna
April 30th, 2007 at 11:14 pm
to use the datatable in the scripts. below is the syntax:
Dialog.Winedit. set datatable.value(”A”,”Global”)
A is equal to the column name in the excel and
Global is spreadsheet name
Fill in all possible values in column A which will be used for the column given next to “Winedit”
Reply to teja
June 12th, 2007 at 6:32 am
Hi All,
Please help me in knowing the use Data Driven Testing. Also, I would like to know the procedure to be followed to do the same.
Thanks n Regards
Archana
Reply to Archana
June 19th, 2007 at 10:00 am
how to write functions and how to call functions
in QTP
Reply to kumar
August 24th, 2007 at 3:29 am
it is very useful&fentastic.
Reply to srinu
February 10th, 2008 at 1:03 pm
Is it a good idea to have checkpoints in Functions? If we do have the checkpoints then it is not possible to view the properties of the checkpoint in the function whereas you would be able to do that in a action
Reply to Prajakti
March 25th, 2008 at 1:03 am
Hi anusha,
it’s very simple….If u r data in global(or action) sheet which is provided by default by QTP ….u can use this datatable(”A”,1).
A—-> Column Name
1—-> Sheet Number (ID)[global=1,action=2…
suppose If u r data available in .xl sheet which was saved some where else in u r machine..then u need to import that particular file at run time and apply above formul to retrive values.
to import file: datatable.import(”pathof the file”)
Reply to seetha
March 27th, 2008 at 5:43 am
hi can any one send scripts with examples…
and also AOM, FSO concepts,
thanking u all
Reply to kumar
April 8th, 2008 at 6:55 pm
hi,
could you please any body please let me know regardig below question.
when i am writing the descriptive programming on web applications i am not able to identify the object proeprties even using Object Spy also in order to capture the object.for ex take gmail how can we write the discriptive programming to identify the compose link and body box.
please any body can give answer to me
thanks in anticipation.
warm regards,
chandra
Reply to chandra mohan reddy
May 30th, 2008 at 7:25 am
Can any one tell me if we can call a function in QTP through descriptive programming?
Reply to Anuj Sharma
August 12th, 2008 at 10:59 am
I know, basic in qtp, But I want to improve, what books I should read and write the code
Please some body refer any books to get knowledge in advance
Reply to vreddy