1. Home >
  2. Apps >
  3. Groups >

QTP Interesting and reusable Function

Question asked by Saandeep Sreerambatla in #Coffee Room on Oct 27, 2009
Saandeep Sreerambatla
Saandeep Sreerambatla 路 Oct 27, 2009
Rank A2 - PRO
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?? Posted in: #Coffee Room
Saandeep Sreerambatla
Saandeep Sreerambatla 路 Oct 27, 2009
Rank A2 - PRO
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.
Anil Jain
Anil Jain 路 Oct 27, 2009
Rank A2 - PRO
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
Saandeep Sreerambatla
Saandeep Sreerambatla 路 Oct 27, 2009
Rank A2 - PRO
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 馃榾
Saandeep Sreerambatla
Saandeep Sreerambatla 路 Oct 29, 2009
Rank A2 - PRO
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.
Saandeep Sreerambatla
Saandeep Sreerambatla 路 Oct 29, 2009
Rank A2 - PRO
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.
Saandeep Sreerambatla
Saandeep Sreerambatla 路 Nov 3, 2009
Rank A2 - PRO
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 馃榾
Anil Jain
Anil Jain 路 Nov 3, 2009
Rank A2 - PRO
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
Saandeep Sreerambatla
Saandeep Sreerambatla 路 Nov 3, 2009
Rank A2 - PRO
I guess it happens mostly with 2 since only 2 gives reminders as .5 馃榾
Anil Jain
Anil Jain 路 Nov 3, 2009
Rank A2 - PRO
Sounds like a defect in QTP or say defect in origination point VBScript.. 馃榿

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

Regards,
Abhi
Saandeep Sreerambatla
Saandeep Sreerambatla 路 Nov 4, 2009
Rank A2 - PRO
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.
Saandeep Sreerambatla
Saandeep Sreerambatla 路 Nov 4, 2009
Rank A2 - PRO
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.
Saandeep Sreerambatla
Saandeep Sreerambatla 路 Nov 4, 2009
Rank A2 - PRO
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.
Saandeep Sreerambatla
Saandeep Sreerambatla 路 Nov 10, 2009
Rank A2 - PRO
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.
Saandeep Sreerambatla
Saandeep Sreerambatla 路 Nov 10, 2009
Rank A2 - PRO
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??
Saandeep Sreerambatla
Saandeep Sreerambatla 路 Nov 11, 2009
Rank A2 - PRO
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

You must log-in or sign-up to reply to this post.

Click to Log-In or Sign-Up