Monday, August 30, 2010

Handling Pop-ups in QTP

Pop-up handling in QTP

If pop-up windows/dialogs still interrupt your testing scripts, then below you can see 3 different ways to avoid pop-ups or block pop-ups.

1. Using Registry
2. Using Menu Option
3. Using Recovery Scenario

[Avoiding pop-ups in the first place is the better way, options 1 & 2 above]

1.
Using registry to turn off/on pop-up blocker:

To check whether pop-up blocker is enabled or disabled:

Set WshShell = CreateObject("WScript.Shell")
a=WshShell.RegRead("HKCU\Software\Microsoft\Internet Explorer\New
Windows\PopupMgr")
If (a=1) Then
Msgbox "Popup blocker is ENABLED."
Else
Msgbox "Popup blocker is DISABLED."
End If

To enable or disable pop-up blocker:

Set WshShell = CreateObject("WScript.Shell")
'Disable the IE pop-up blocker
WshShell.RegWrite "HKCU\Software\Microsoft\Internet Explorer\New Windows\PopupMgr", "0", "REG_DWORD"
'Enable the IE pop-up blocker - in other words BLOCK Pop-ups
WshShell.RegWrite "HKCU\Software\Microsoft\Internet Explorer\New Windows\PopupMgr", "1", "REG_DWORD"

Understanding some of the methods used above:

You create a WshShell object whenever you want to run a program locally, manipulate the contents of the registry, create a shortcut, or access a system folder.

RegWrite Method:
Creates a new key, adds another value-name to an existing key (and assigns it a value), or changes the value of an existing value-name.

Syntax:
object.RegWrite(strName, anyValue [,strType])

object:
WshShell object.

strName:
String value indicating the key-name, value-name, or value you want to create, add, or change.

anyValue:
The name of the new key you want to create, the name of the value you want to add to an existing key, or the new value you want to assign to an existing value-name.

strType:
Optional. String value indicating the value's data type.

RegRead Method:
Returns the value of a key or value-name from the registry.

Syntax:
object.RegRead(strName)

object:
WshShell object.

strName:
String value indicating the key or value-name whose value you want.

2.
You can also turn it off/on from menu options before starting:
(Tools (menu)-> Internet Options, Privacy Tab, Turn on Pop-up Blocker check-box)

Below is the recorded script (using IE 8 & QTP 10) to turn on pop-up blocker. Similarly record for turning off the pop-up.

1. SystemUtil.Run "C:\Program Files\Internet Explorer\iexplore.exe","","C:\Documents and Settings\sachin","open"
2. Browser("Browser").WinToolbar("ToolbarWindow32").Press "&Tools"
3. Browser("Browser").WinMenu("ContextMenu").Select "Internet Options"
4. Browser("Browser").Dialog("Internet Options").WinTab("SysTabControl32").Select "Security"
5. Browser("Browser").Dialog("Internet Options").WinTab("SysTabControl32").Select "Privacy"
6. Browser("Browser").Dialog("Internet Options").WinCheckBox("Turn on Pop-up Blocker").Set "ON"
7. Browser("Browser").Dialog("Internet Options").WinButton("OK").Click
8. Browser("Browser").CloseAllTabs


Below is the descriptive programming script (using IE 8 and QTP 10) to turn on pop-up blocker. Similarly use the script for turning off the pop-up by just changing the 5th line.

1. SystemUtil.Run "C:\Program Files\Internet Explorer\iexplore.exe","","C:\Documents and Settings\sachin","open"
2. Browser("title:=about:blank").WinToolbar("nativeclass:=ToolbarWindow32", "height:=19","windowextendedstyle:=128").Press "&Tools"
3. Browser("title:=about:blank").WinMenu("menuobjtype:=3").Select "Internet Options"
4. Browser("title:=about:blank").Dialog("text:=Internet Options", "nativeclass:=#32770").WinTab("nativeclass:=SysTabControl32").Select "Privacy"
5. Browser("title:=about:blank").Dialog("text:=Internet Options", "nativeclass:=#32770").WinCheckBox("text:=Turn on Pop-up &Blocker").Set "ON"
6. Browser("title:=about:blank").Dialog("text:=Internet Options", "nativeclass:=#32770").WinButton("text:=OK").Click
7. Browser("title:=about:blank").CloseAllTabs


3.
You can also use recovery scenarios for this:

Recovery scenarios are intended for use only with events that you cannot predict in advance, or for events that you cannot otherwise synchronize with a specific step in your component.

QTP Recovery Scenario