View Feed
group-icon
Coffee Room
Discuss anything here - everything that you wish to discuss with fellow engineers.
12933 Members
Join this group to post and comment.

QTP Interesting and reusable Function

Guys,

I started this thread where in we can include interesting and reusable functions in this thread.

There are few people working on QTP in our forum , however interesting functions can attract people.

What you say??
I will go with the first one..

To take a screenshot of the application.

For getting this I have to work for 15 days 😀

There is a procedure called CaptureBitmap

You can also use Browser.CaptureBitmap.

You can also use Browser("browsername").captureBitmap.

You can save the image in .bmp and .png formats.

Let me know if you have any questions 😀

I will give a proper function for this tomorrow.
Thread Appreciated.

I would appeciate you more, for the functions and keeping this thread alive (I would try to help you regularly). Just one query, in my knowledge using inbuilt capturebitmap method we can save screenshots only in .BMP format. Are you sure we can save in .PNG as well??

-CB
Yes we can..

I have used this now
desktop.CaptureBitmap("C:\image.bmp")
and
desktop.CaptureBitmap("C:\image.png")

The first one took around 3.51MB where as the second one took only 250KB 😀
This is an old question I had asked.

How to keep the system unlocked when the script is executing in QTP.

I have got the idea to run a scheduled task when ever we run the script.

Just keep these lines of code in a text file and save as .vbs

Set WSHShell = WScript.CreateObject("WScript.Shell")
WSHShell.SendKeys "^%{F2}"

Set the timing in the scehduled tasks accordingly.

Source SQA forums.
This function kills the processes in taskmanager.

problem:I have been lot of excel sheets and I am getting some errors like The sheet is already open.
It is used by another program etc.

So this function kills the process in task manager such that it closes all open excels.

et objWMIService = GetObject("winmgmts:\\.\root\CIMV2")
Set colProcess = objWMIService.ExecQuery ("Select * from Win32_Process Where Name = 'EXCEL.exe")
For Each objProcess in colProcess
objProcess.Terminate()
Next
Set objWMIService = Nothing
Set colProcess = Nothing



In place of EXCEL.exe what ever exe you place it gets closed.

So use this if it is required for sure.
I have a question guys,

Actually we had a very big discussion at our office regarding the Round function in QTP.

What it does ??

msgbox Round(pi,2)

displays 3.14.

So we tried the following

Round(3/2)
Round(5/2)
Round(7/2)
Round(9/2)

The results are
2
2
4
4

My question is if 1.5 == 2 then why 2.5 also ==2??

same way 3.5==4 and 4.5==4 ??

This was the question we got in mind when we are working on Round function today 😀

My observation is it happens mostly for /2 only i mean divided by 2 only.

Didnt do any analysis but its interesting 😀
English-Scared
I have a question guys,

Actually we had a very big discussion at our office regarding the Round function in QTP.

What it does ??

msgbox Round(pi,2)

displays 3.14.

So we tried the following

Round(3/2)
Round(5/2)
Round(7/2)
Round(9/2)

The results are
2
2
4
4

My question is if 1.5 == 2 then why 2.5 also ==2??

same way 3.5==4 and 4.5==4 ??

This was the question we got in mind when we are working on Round function today 😀

My observation is it happens mostly for /2 only i mean divided by 2 only.

Didnt do any analysis but its interesting 😀

😁 Interesting

The answer I will explain is even more interesting.

In the scenarios you explained, where you have round off upto no decimal, in this case if final answer is decimal(.)5, QTP has a tendency to show move answer towards even number. Check my hypothesis 😁

In all other scenarios, if decimal part is lesser then .5 it will move towards prior number and if it is greater than .5 it will move towards higher number.

Do let me know if I sounds convincing.

-CB
I guess it happens mostly with 2 since only 2 gives reminders as .5 😀
Sounds like a defect in QTP or say defect in origination point VBScript.. 😁

-CB
abhikansh.jain
abhikansh.jain • Nov 4, 2009
Hello,
Can anybody please tell me how can I capture multiple screeshots and save them in same excel file?

Regards,
Abhi
I have few questions multiple screenshots in the sense is it your Application flow?

Then you can do one thing, take screenshots of the application as mentioned in the post 4.

Save all the screenshots in a specific folder.

Then open excel and run a macro to include the screen shots in the specific cells giving the path you saved them earlier.
Descriptive programming::

I think many people use this in many instances.

There are two types of descriptive programming--
1. Static
2. Dynamic

Static one::

for example if you take the Browser("Google")

Then Browser("name:=Google") is the static approach.

Using Dynamic is just achieving the same using functions::

For example we write a createBrowser function to create a browser of any name.

Public Function CreateBrowser(bname)

Dim temp
set Temp("micclass").value = "Browser"
set Temp("name").value = bname
set CreateBrowser = temp

End function

Then you can call the function..

set temp= CreateBrowser ("Google")
set brvar = Browser(temp)
Then directly you can use as brvar.Close

This is just an example you can write a set of functions for most used objects in the application thus automation becomes simple.
One more interesting feature here is--

If you write the same function for page.

Then using
Set temp = CreatePage("google")
set pagevar = brvar.page(temp)

Here using the variable pagevar.close we are eliminating lot of typing.

I mean pagevar.close is equal to Browser().Page().Close.

So if we create same for webelements . webbuttons etc accessing them in the script will be easy.
Atlast I have figured out how to send a mail using outlook without using Clickyes software or CDO objects.

Will post a function tomorrow, but the idea is to use the UI of the outlook.

Earlier scenarios which we discussed sends the mail without using the UI , thus causing the error to appear.

Now if we use the front end UI of outlook we can send the mail.
I got one scenario where I have to automate Multiple browsers of same application.

For example: Consider the gmail login page with the user Id and password fields.
Now I have to open a browser and enter the details and have to login.

The next scenario is to open gmail again and enter the same details again!!

The problem arises that QTP cannot differentiate between the two browsers which is old which is new..

I have one method using complete descriptive for the new browser , my problem will be solved.

The other method is to use creationtime which is not working properly.

So guys any ideas??
This is the method to send email through outlook.

Took from Tarun's Book QTP Unplugged

There are other ways as well but this is the easier one 😀

Const olmailItem = 0
Set olapp = CreateObject("outlook.Application")
Set Newmail = olapp.CreateItem(olmailItem)
Newmail.to = xxx@yyy.com
Newmail.Subject = "Test"
Newmail.Body = "Body"
Newmail.Attachments.Add("Attachmentpath")
Newmail.Display
emailcaption = Newmail.GetInspector().Caption
Set omailWindow = Description.Create
omailWindow("title").value = emailcaption
omailWindow("title").RegularExpression = False
Window(omailWindow).Type micAltDwn+"s"+micAltUp
sTitle = "title:=*Spelling.*"
If Window(omailWindow).Window(sTitle).Exist(2) Then
Window(omailWindow).Window(sTitle).Close
End if

sTitle = "title:=Microsoft Office Outlook"
If Window(omailWindow).Window(sTitle).Exist(2) Then
Window(omailWindow).Window(sTitle).winbutton("text=&Yes").Click
End If

Share this content on your social channels -