QTP Descriptive Programming - How to close all Browsers?

QTP Descriptive Programming - How to close all Browsers?

This is a very practical task - How to close all browsers from QTP?
You may need to close browsers before or during the QTP script execution.

You can be surprised, but QTP script, that closes all browsers, will contain 3 lines only:

Well, how does this code work?

To answer, I have to tell about QuickTest Professional Descriptive Programming.
Descriptive Programming (DP) is a working with object, which are not described in QTP Object Repository (OR).
All objects properties are calculated dynamically during the QTP test execution.

Please, check this code:
While Browser("CreationTime:=0").Exist
    Browser("CreationTime:=0").Close
Wend
"CreationTime" is a QTP browser property, which indicates the order in which browsers were opened.

QTP Tip1: The first browser that opens receives the value "CreationTime" = 0, the second browser receives "CreationTime" = 1, and so on...

QTP Tip2: To specify value of property in Descriptive Programming, please use ":=" operator.
For example, "Title:=My application".

Since we cannot know in advance - what browser will be selected and closed, we use its "age", i.e. "CreationTime" property. We decide, which browser should be closed, during the QTP script execution only.


Let's return to above QTP script...
  • Browser("CreationTime:=0").Exist - it checks whether the first opened browser exists or not
  • If the browser exists, we close it - Browser("CreationTime:=0").Close - and repeat checking

Let's continue сomplicating our task:
How to close browsers by mask?

For example, we have to close all browsers navigated to any Google's sites (URL contains 'google.com').

In this case, QTP script will look like:
(Click the image to enlarge it)

Using this QTP code, you can close dynamically browsers by mask.

Please, note several useful QTP tips.

QTP Tip3
: To get browser's URL, use: GetROProperty("URL") function.
For example, this line returns URL of the first opened browser:
Browser("CreationTime:=" & CreationTime).GetROProperty("URL")

QTP Tip2: Analogously, to get browser's Title, use: GetROProperty("Title") function.
Browser("CreationTime:=" & CreationTime).GetROProperty("Title")


Summary:
I've shown and explained:
  • Simple concepts of QTP Descriptive Programming
  • How to close all browsers from QTP test
  • How to close browsers by mask from QTP test

Related articles:


I hope, these simple scripts will be usefull for you. Please, let me know.
Do you have suggestions/materials/questions on QTP? Please, post your comments on my blog.

--
Dmitry Motevich

16 comments:

Anonymous said...

Hai,
I am Saritha , Thanks for your valid info..
i am new to QTP,trying these tips in QTP8.2. and with IE6.1
Browser("CreationTime:=0").exit
is showing problems.
CreationTime property is not available by default ??we need to specify in the smart identification option???

Thanks in advance

Dmitry Motevich said...

To Saritha,
I would have helped you, if you had provided the detailed info.
Why didn't you explain the phrase "is showing problems"? Which problems?

Anonymous said...

Hi Dmitry

You are simply superb.
I think u are the best teacher I have ever seen in my life.

I am a certified QTP specialist. Even then after seeing your videos I found your methods better than mine.

I appreciate it .Keep up the good work. People get a lot of help from your videos.
God bless the good soul like you

Dmitry Motevich said...

2Anonymous,
Thank you for your words, I'm glad :)
Actually, I do not think that I'm the best teacher :)
There are a lot smarter persons...

Anonymous said...

Hey Dmitry,

This code works well and closes all instances of IE. But what question arises in my mind is - when I SPY on IE Browser I can't see any property called "CreationTime"; so how does it work?

Regards,
Ashwini

Dmitry Motevich said...

Ashwini Khanna (from September 1),

Yes, QTP Spy does not show CreationTime property. Actually, I cannot explain its behaviour :) At least, it's illogical.

What about the second part of your qestion - "how does it work".
There is a clear answer in QTP Help. I've just copied a sentence:

CreationTime value indicates the order in which the browser was opened relative to other open browsers with an otherwise identical description. The first browser that opens receives the value CreationTime = 0.

For example, if you record a component on three otherwise identical browsers that are opened at 9:01 pm, 9:03 pm, and 9:05 pm, QuickTest assigns the CreationTime values, as follows: CreationTime = 0 to the 9:01 am browser, CreationTime = 1 to the 9:03 am browser, and CreationTime = 2 to the 9:06 am browser.

Anonymous said...

Great way to teach your favourite subjects .Simply superb.by the way what is mangazey ?


vasu from India

swami said...

Hi this is raj ,i want write the descriptive program whenever our project not yet ready(we dot know the properties of object)....plz give me answer

Dmitry Motevich said...

@ranganadh,
I wish you good luck.
P.S. please use correct English.

Anonymous said...

Hi,

Good work.

I found an issue with using
Browser("CreationTime:=" & CreationTime).GetROProperty("Title")

A synchronisation issue,...
it will show url if I run it without any synchronisation or delay.. shows "{the url} - Windows Internet Explorer"

When I put in a delay, it shows the "{the title} - Windows Internet Explorer"

Only tried this on one web page...

Cheers

Anonymous said...

As far as I understand, this wouldn't close the browsers that have open dialog windows. It could have been easy to just close any existing child dialog of current browser using Description objects, but dialogs are children of Page object, not directly the browser. So resulting code with possibility of dialogs would be a bit more complicated.

Anonymous said...

Hi

Thanks for your tips regarding closing browsers.

The script you gave me closes only 8 browsers.

Is there a way to close all open browsers (irrespective of how many are opened?)?.

Here you go with the script that i used:

Dim URL,CreationTime,Mask, i

For i = 0 to 15
If (Browser("CreationTime:="&i).exist) then
Browser("CreationTime:="&i).close
else
exittest
end if
Next


Thanks in advance

Anonymous said...

Hi,

your code needed one updation, Check the below code:

Dim URL,CreationTime,Mask, i

For i = 0 to 15
If (Browser("CreationTime:=0").exist) then
Browser("CreationTime:=0").close
else
exittest
end if
Next


In order to close all the browsers, you need to set the Creationtime = 0, becasue after 8 browsers the value of i is 8 & Browsers left are 7, so the creation time (0 - 7) doesn't match with those browsers.

Regards,
Gaurav

Anonymous said...

Thanks for Easy explanation.
Mark.

Bala said...

Hi Dmitry,

How can i close the borwser,i mean would like to close the(4 browsers opened)last browser first and then 3rd browserlike this?

Need urgent reply,

Thanks,
Venkat

Unknown said...

Browser("title:=.*").page("title:=.*").Link("html tag:=A", "text:=Matters")
Is it a valid syntax? When i try to use this syntax it is unable to idenbtifythe browser. I used this syntax bcuz after login it may launch the user in any of the tabs butlater it should click on Matters. Can any one help on this