.

Make your QTP scripts perform better

This post refers to a thought provoking question which was asked on Testing Tools forum. In this query jp@QTP is looking for ways to optimize QTP scripts for best performance. Everyone knows we use automated testing tools to optimize our testing process. Unless you make full use of the capability of the tool and unless a tool is used sensibly and with proper planning, it would not yield any results. Just record-and-playback is never the solution for any project. You need to go deep inside to understand the intricacies of any tool. Any software testing tool is only as good as the test engineer using it. On those thoughts, I feel this was one of the best questions asked in the forums of late.
Well, here are some of my tips and tricks to optimize the QTP scripts:

  • Launch QTP using a .vbs file and not the QTP desktop icon. You will notice a substantial increase in speed. [Refer the earlier post on How to open QTP using vbs file? ,you just need the 1st point of that post]
  • For large tests, always define variables, function in an external .vbs file and not inside a reusable action. Attach these files with your test scripts. If you define a variable or a function in an action, on every iteration of your test run, memory(RAM) will be allocated to those variables/functions and would not be released. Now as your script starts consuming more and more RAM, your System Under Test (SUT) will tend to become slower.
  • While running, QTP consumes a lot of memory by itself. It is always advisable to have lots of available RAM( much more than what is recommended by HP) and good processor speed on a system where you intend to install QTP. When you have tests (and hence QTP) running for a prolonged period if time, there are chances of memory leaks. To avoid memory leakage always restart QTP at some intervals of time. Using AOM you can automate this process. [If you want to go into details of effect of RAM on speed of computer read the post on RAM, Memory Usage thoroughly]
  • Avoid using hard coded wait(x) statement. Wait statement waits for full x seconds, even if the event has already occurred. Instead use .sync or exist statement. While using exist statement always have a value inside it.

For ex: .Exist(10) Here QTP will wait max till 10 seconds and if it finds the object at (say) 3 secs , it will resume the execution immediately thereby saving your precious time. On the other hand if you leave the parenthesis blank, QTP would wait for object synchronization timeout you have mentioned under File > Test Settings > Run Tab.

  • Make full use of what HP-QTP has provided you in the tool IDE. Use “Automatically Generate “With” statements after recording” option present under Tools > Options > General Tab. This will not only make your code look neater but also make your scripts perform better.
  • Make your own judgement whether you want to go for Descriptive Programming or Object Repository or mixed approach. Each approach has it own pros and cons that in turn is related to QTP performance.
  • Unless absolutely required, uncheck the options “Save still image capture to results” and “Save movie to results” present under Tools > Options > Run tab. These options definitely have some bearing on QTP run time performance.
  • Make the Run Mode as “fast”. This setting is present under Tool > Options > Run tab. Note: If you intend to run your scripts from QC no need to worry about this option, as the scripts WILL run in fast mode whether you want or not.
  • If you are new to automation or QTP. Read this beginner article on Automation Object Model (AOM). AOM simplifies many aspects of QTP scripting. It can help you in controlling QTP from an external file.
  • Make use of relative paths while calling reusable actions in your script. Using relative path would make your script portable and easy to manage. I will cover in detail how to’s and why’s of using relative paths in my next post.

So what do you do to optimize your scripts? Let us know through the comments below.

Have you downloaded the FREE Optimizing QTP eBook yet? Get It Now!

If you want to keep track of further articles on QTP. I recommend you to subscribe via RSS feed. You can also subscribe by Email and have new QTP articles sent directly to your inbox.

Related posts:

  1. Database Checkpoint and QTP Part2 – Using Scripts

51 comments ↓

#1 Will on 06.27.08 at 05:16

This is a good post. For a while now, I have been meaning to write a post on optimizing execution speed, and you hit on most of the points I intended to include.

One point I have a question about is the With statements. That is a feature I haven’t used and know almost nothing about. Can you explain how it improves performance?

#2 Ankur on 06.27.08 at 07:17

Thanks Will.

>>One point I have a question about is the With statements. That is a feature I haven’t used and know almost nothing about. Can you explain how it improves performance?

Here is a small experiment with windows based flight reservation sample application.
*************************************
Dim StartTime, EndTime, StartTime1, EndTime1

SystemUtil.Run “C:\Program Files\Mercury Interactive\QuickTest Professional\samples\flight\app\flight4a.exe”,”",”C:\Program Files\Mercury Interactive\QuickTest Professional\samples\flight\app\”,”open”

With Dialog(“Login”)
.WinEdit(“Agent Name:”).Set “mercury”
.WinEdit(“Agent Name:”).Type micTab
.WinEdit(“Password:”).SetSecure “4864ede3f3f8f30757cf694e3e100d29bf1ea9b9″
.WinEdit(“Password:”).Type micReturn
End With

StartTime = Timer
With Window(“Flight Reservation”)
For i=1 to 1000
.WinEdit(“Name:”).Set “Ankur”
Next
End With
EndTime = Timer
msgbox EndTime – StartTime

‘result: 41.71875

Window(“Flight Reservation”).Close

SystemUtil.Run “C:\Program Files\Mercury Interactive\QuickTest Professional\samples\flight\app\flight4a.exe”,”",”C:\Program Files\Mercury Interactive\QuickTest Professional\samples\flight\app\”,”open”

With Dialog(“Login”)
.WinEdit(“Agent Name:”).Set “mercury”
.WinEdit(“Agent Name:”).Type micTab
.WinEdit(“Password:”).SetSecure “4864ede3f3f8f30757cf694e3e100d29bf1ea9b9″
.WinEdit(“Password:”).Type micReturn
End With

StartTime1 = Timer
For i=1 to 1000
Window(“Flight Reservation”).WinEdit(“Name:”).Set “Ankur”
Next
EndTime1 = Timer

msgbox EndTime1 – StartTime1

‘result1: 45.85938
******************************

This experiment was carried out on my machine which has 2GB RAM. With 1000 iterations the diff is > 4 secs. I think the effect (performance degradation) should be much more pronounced on larger tests and if it is carried out on machines having configuration just on threshold (512MB- one which is min RAM recommended by HP).

In case of With statement, the ref to the function Window() is getting stored at one place(RAM) and it gets referenced (w/o going to OR on each iteration) while without With the function Window() and WinEdit() both are called on each iteration and probably that is responsible for the delay.

Please don’t get annoyed for my excessive usage of the term “RAM” :) I have been reading a lot on performance testing, memory leakage etc these days.

#3 Will Roden on 06.27.08 at 10:31

Now I see what you mean. I tried the same code on my laptop, and the performance difference was more pronounced. 43.26563s and 68.92188s respectively.

Then I tried it the way that I would normally handle this situation:

Set oWinEdit = Window(“Flight Reservation”).WinEdit(“Name:”)
For i=1 to 1000
oWinEdit.Set “Ankur”
Next

This took only 15.48438s, but I immediately realized it wasn’t a valid comparison. It was faster because it set an object for the WinEdit and didn’t have to find it each time. So I changed it to this:

Set oWindow = Window(“Flight Reservation”)
For i=1 to 1000
oWindow.WinEdit(“Name:”).Set “Ankur”
Next

This took 41.84375s. A slight improvement over the with statement, but close enough to not make a big difference.

#4 Ankur on 06.27.08 at 10:41

Yes, so it boils down to the fact that lesser the function calls to the Object Repository more optimized is our code.
As you rightly pointed out, With Set statement above you have reduced one more function call and hence the time diff.

#5 Jagrut on 06.29.08 at 21:11

Thanks Ankur and Will for your opinions. I am new to QTP so I may be wrong but this is only one of the few posts that discuss about optimizing QTP scripts.

I have one question. I need to understand what does ExecuteFile do internally? Does it load the specified file in memory? Is it true that larger the file more will be the memory consumption? Is there any way to unload the file from memory?

#6 swami on 07.16.08 at 14:10

Will and Ankur….
really a very good comparison….!!!!

this was really a new thing for me…

Thanks,
Swami

#7 Sunil Goswami on 09.09.08 at 08:06

Great post!

#8 Anonymous on 09.14.08 at 05:54

Hi,

If you not declared function and variables in the action where exacly you write the functions and declarethe the variables?

do you use global variables?

#9 Ankur on 09.14.08 at 06:12

@Anon: As mentioned earlier,in an external vbs file

#10 Anonymous on 12.06.08 at 09:20

i want to recommend a good piece of software that has always helped me code better with QTP .

i work on other people’s code and they dont care on script’s readability .

But i do , i shift everytime from QTP ide to VBSBeautifier , http://www.daansystems.com/vbsbeaut/ which pretty prints the code and makes it ready for the show .

Hope that helps ,
Deepan Prabhu

#11 srikanth on 12.18.08 at 05:43

1) how to count column in web table like yahoo inbox by using QTP
2) From 3rd column i want to fetch data from webtable
3) how to count read message and i want to fetch 2 mail from inbox
3) and i want to delete the same 2nd mail.

#12 Gopi on 12.29.08 at 21:26

Very useful tip! Appreciate your efforts!

#13 pranati on 01.29.09 at 02:11

How can we convert Shared repository to XML repository??Plz tel me.

#14 PRIYA on 01.31.09 at 05:24

Hi Ankur,
Can u help me to right a scrpit in QTP.

#15 How-to make your QTP scripts perform better | QAWorld on 02.07.09 at 10:56

[...] are 10 simple tips and tricks to optimize the QTP [...]

#16 Sashi on 02.13.09 at 05:54

Really very helpful and good post.
Now i have really good experience on QTP and few of the things i knowpreviously but few are really new and good one.
Hey Ankur..
I am facing a problem in dealing with ActiveX.
I am trying to automate QC but its not really working.
The script is as follows:
Browser(“Name of the browser”).Page(“name of the page”).ActiveX(“name”).WinObject(“name”).Type “name”

This code is generated when i am recording it. But when i am running it again, its not taking the values i give.

Getting an error “Object not found”
It would be really helpful if you help me on this.

#17 Marco on 02.18.09 at 12:00

Hi Ankar

Nice site and thanks for your efforts!
Just an FYI/comment – I re-ran the “With” statements test with QTP 9.5 and modified the results to go the reporter.ReportEvent instead of a msgBox and both methods (With or Without With) produce identical results.

————————
Dim StartTime, EndTime, StartTime1, EndTime1

SystemUtil.Run “C:\Program Files\HP\QuickTest Professional\samples\flight\app\flight4a.exe”,”",”C:\Program Files\HP\QuickTest Professional\samples\flight\app\”,”open”

With Dialog(“Login”)
.WinEdit(“Agent Name:”).Set “mercury”
.WinEdit(“Agent Name:”).Type micTab
.WinEdit(“Password:”).SetSecure “499af5d7e12e9715f08b3ff38567770ad5447b4b”
.WinEdit(“Password:”).Type micReturn
End With

StartTime = Timer
With Window(“Flight Reservation”)
For i=1 to 1000
.WinEdit(“Name:”).Set “Ankur”
Next
End With
EndTime = Timer

reporter.ReportEvent micPass, “Time A” , EndTime – StartTime

‘Time B 55.27734 Passed 2/18/2009 – 14:47:20

Window(“Flight Reservation”).Close

SystemUtil.Run “C:\Program Files\HP\QuickTest Professional\samples\flight\app\flight4a.exe”,”",”C:\Program Files\HP\QuickTest Professional\samples\flight\app\”,”open”

With Dialog(“Login”)
.WinEdit(“Agent Name:”).Set “mercury”
.WinEdit(“Agent Name:”).Type micTab
.WinEdit(“Password:”).SetSecure “499af5d7e12e9715f08b3ff38567770ad5447b4b”
.WinEdit(“Password:”).Type micReturn
End With

StartTime1 = Timer
For i=1 to 1000
Window(“Flight Reservation”).WinEdit(“Name:”).Set “Ankur”
Next
EndTime1 = Timer

reporter.ReportEvent micPass, “Time B” , EndTime – StartTime

‘Time B 55.27734 Passed 2/18/2009 – 14:47:20

——————-

I’m guessing object handling has been improved in 9.5?
Either way… using “With” statements provides cleaner more readable code.

Thanks
Marco

#18 Marco on 02.19.09 at 04:30

RE: QTP 9.5 “With” Statements:

oops.. I did a cut paste error in my previous post, however the times are still identical

Using With:
Time A 55.27734 Passed 2/18/2009 – 14:46:21

Not Using With:
Time B 55.27734 Passed 2/18/2009 – 14:47:20

PS – This was executed on a Dual Core E6750 @ 2.66GHz with 4Gb of RAM.

Thanks
Marco

#19 Aparna on 02.19.09 at 05:17

Hiii,

“How can i write a script to select the last row(latest tow) on the grid and right click and select the option which ever i want to”

#20 Ankur on 02.19.09 at 08:10

PS – This was executed on a Dual Core E6750 @ 2.66GHz with 4Gb of RAM.

4GB RAM. This makes all the difference.

#21 Karun Kumar on 03.09.09 at 02:04

I was helped by this site a lot in my carrier. Thank u very nuch all of you.

#22 chitra on 03.24.09 at 21:21

ya i have also tried by using with statement
but instead of using timer i have added transactions to watch difference in performance
and yes it worked

#23 LalMohan on 03.26.09 at 03:59

Good Work Ankur….Thanks for your nice cooperation to learn QTP from Basic…

#24 kanchan on 03.26.09 at 04:32

I am novice user.I started learning QTP.
I have found it a very usefull site .Thanku very much all of you..

#25 nandu on 04.01.09 at 21:49

Hi..
it’a very nice web site..
it really helps me ..
i am begginer to qtp..
already i subscribed in this web site..
but i have one doubt..
how can i post my questions..
please help me..

#26 LalMohan on 04.06.09 at 20:22

What Is MicClass And MIcFail

#27 Prasad on 04.13.09 at 13:30

Hi Ankur,
My question is related to child object. In http://newtours.demoaut.com/ site there are two edit field. Username and Password. I have written following code.
Set objDescriptive = Description.Create()
objDescriptive(“micclass”).value = “WebEdit”
Set editFieldObject = Browser(“title:=Welcome:.*”).Page(“title:=Welcome: .*”).ChildObjects(objDescriptive)
msgBox editFieldObject.Count
If editFieldObject.Count 0Then
For i =0 to editFieldObject.count -1
If editFieldObject(i).GetRoProperty(“name”) = “user.*” Then
editFieldObject(i).Set sUsername
msgBox editFieldObject.GetRoProperty(“value”)
End If
If editFieldObject(i).GetRoProperty(“name”) = “pass.*” Then
editFieldObject(i).Set sPassword
End If
Next
Else
msgBox “No edit field found”

End If

Does the above code work? If not then how can i use a generic code so that QTP will put the data on Username and Password field using child object?

#28 Vini on 04.24.09 at 06:52

Hi,
How do we convert our test cases in QC to Automated scripts in QTP? Please explain.Thanks .

#29 manjunath on 05.03.09 at 01:22

can you please let me know how to capture the data in web application and storing in the qtp excel sheet

#30 Ganesh on 06.11.09 at 04:59

Hi Ankur,

Hope you can help me out in finding a solution for the below question.

My application has several pages and each page has some common text. I wanted to perform an action only when that text is shown. I added that WebElement into the object repository and moved it under the root. I scripted such that if the WebElement object exist then pass the script othewise fail. But sometimes the script recognises the WebElement and sometimes it won’t. Not sure why? I have used regular expression for that web element. Is there any way that I can do rather than adding that web element to each and every page of the object repository?

Looking forward for your reply.

Thanks,
Ganesh

#31 Thy Nguyen on 06.11.09 at 20:29

Hi,

How ways do we get current driver of appliction in system?
Example: Get current driver (or path) of calc.exe program. Please help me. Thanks

#32 Sri on 06.21.09 at 21:43

Hi Ganesh,
make sure that you have unchecked the smart identification for that particular object in object repository and select “Different test objects descriptions” radio button in “Tools>Options>Web>Page/Frame Options”. I am sure this will solve your situation, if not let me know.
Thanks,
Sri.

#33 Prasad on 07.17.09 at 12:18

Ankur,

I am Prasad from mumbai working in Tech. ahindra.
Are u still monitor this links and forum?
I want to have a talk to you. Can you please connect me +91-9867338664.

- Regards
Prasad

#34 Rahul on 07.21.09 at 23:59

Is there an process to automate the Login functionality of my site.
I dont want to create any function, if i create that it means manual automated testing rather that automated testing

Please let me know if i can automate the process by only getting the Link ?

Thanks

#35 Sam on 09.01.09 at 23:48

@ Rahul,

U need to create an AI enabled automation testing tool.
It is not available yet. But it will be lovely to have a program that itself can create code …

#36 santhosh on 09.10.09 at 21:45

i want some qtp script examples can you please send me any one

#37 rich on 09.19.09 at 22:03

very nice place to communicate qtp tech. thank ankur and will for share

#38 prasad kumar on 09.28.09 at 04:25

sir,

i want some qtp scripts

#39 sharan on 10.23.09 at 11:46

Hi ankur, I need to use a keyword frame work using QTP, I am biginer of this QTP, could u please help me that how will I start with this framework, plz explain by taking very simple and sample scripts, so that I can understand easily.

#40 ram on 11.13.09 at 13:21

hi,sharan

if u want use keyword frame work,u have create folders like rep,rec,lib,log,env,scripts.By use all these u have to maintain run overall scripts like as setup.which automation scenario do want execute just call that module by use functions.under that module all scripts should executed and give reports into log files.
1.Generat the script by adding all the objects into object repository
2.Save the repository with .tsr extension
3.Create the function for that scipt and save .vbs extension or qfl
4.Create Xml file for standard variables (environment variables) .xml
5.recovery .qrs while running scripts get unhandeled exceptions to recover by use recovery
6.rename the all excel sheets,add modules
7.Crate a action template in main test
8.select that module and write some scenarios
9.writ steps under scenario
10.Associated recovery,rep,lib,env for that test by using navigation or scripts
11.this is for all the scripts in u r project
12.u have to run all th scripts nothing a frame
13.what are the scenario we have to execute just call key word instead of that function
14.all the scripts will be run based on calling the keyword
15.i will explained clearly latter

#41 ram on 11.13.09 at 14:20

hi ganesh

You said,Each page contain common text the we have to go for descriptive program.Just you have to take that text unique property values and write script.that script will be reusable for all pages.just copy paste that where you need.

regards
Ram.I

#42 ram on 11.13.09 at 14:40

hi vini,

if u want export all the automation scripts

1.Go to QTP/options/run/select allow the other HP products

2.Go to file/Quality center connection/connect server “http://server/qcbin” click on connect server

3.After connection create folders there only or in test plan

4. Click on save AS

5.scripts saved in your testplan with all attachments

6.You have to the variables also and associated all rep,rec,lib,env

7. Open QC and Click on Lunch.

8.Tool will be luanch with scripts

9.Click on run

10.Generate the results in defects folder

regards,
Ram.I

#43 ramu on 01.11.10 at 14:37

how to send the log file to the mail

#44 Johnny on 01.18.10 at 12:38

Hi Ankur,

I had a doubt , my scenario is .

1. I had an Existing Xml file.

2. I need to change some particular value in that file using data table .

3. I will use multiple itreations to change tha value for 10 times.

4. My doubt is how we can open an existing Xml file and how can we find the value inside it and how we can attach the particular fiel to our script?

5. so please help me out in this scenario..please mail me if any answer with you @ raj.nunna@gmail.com

#45 vijay on 02.16.10 at 12:35

Hi Anukar,
I write function for launching the web based application. It works fine when the Qtp tool position is changed(When tool is in minimized state , I changed tool to maximized ). When we cant change the state the tool rises an error like “Invalid procedure or arguments”.Please give to reply how to rectify this problem.
Function which i had written is given below
Function openapp ()
‘apath = “http://devtest.metaehr.com:8080/ehrgwt.html”

SystemUtil.Run “http://devtest.metaehr.com:8080/ehrgwt.html”,”",”http://devtest.metaehr.com:8080/”,”open”

openapp = “Web site Opened”
End Function

#46 prasanna on 02.24.10 at 05:50

Hi, I am new to QTP. I looked at all your solutions and they are really well explained and clear.
I have a question, hope you have a solution.
I am using systemutil.run to invoke an application from the server. This app displays a shockwave movie. Sometimes it takes time to display and sometimes it doesnot. if movie is displayed i capture some text and use it for a match in some other file.
now prob is , when it is displayed, it captures , test is passed.
but when it delays in displaying, text is not captured and it fails. I want it to pass. how do i make it.. tht movie is surely going to display. if not displayed it should say it doesnot exist.
how to write script for it?

#47 kiran on 02.25.10 at 20:42

Good post, it’s seems to learn easily.

#48 vikas on 03.08.10 at 08:56

hi,
i need help regarding wait statement, i want to wait 14 sec * no of iterations so if 10 iterations wait 14 * 10 how can i define it.
help is much appreciated can any one please reply me back on vicky.phd@gmail.com
Regards,
Vikas

#49 sonu on 04.04.10 at 08:59

Hi,
Iam plannning to learn QTP, can some one make me clear about how to create test scripts using Qtp. Eg: I have 5 manual testing scenarios (a) Login to viewer
(b) select the template
(c) Enter the credentials to login to particular template
(d)view the images
(e) click on close Using Qtp: Should i first record the steps from a to e and introduce the objects to object repository and then modify the script???? what should be i modifying . Please respond

#50 Sushant on 05.10.10 at 19:22

Hi Ankar,

I want to configure QTP in QC .How to proceed with that .

#51 uma on 06.23.10 at 13:11

hi thanks