现在的位置: 首页 > 综合 > 正文

How to close all browsers from QTP?

2013年10月14日 ⁄ 综合 ⁄ 共 1791字 ⁄ 字号 评论关闭

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 Tip1The
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")

抱歉!评论已关闭.