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