http://quicktesthp.blogspot.com

QTP VBScript new series + Interview Question Bank on QTP for enrichment of Knowledge in QTP

This Site has been brought to you by HP Certified Expert of QTP.

Exciting new articles for October:

1) QTP Tip:Deselect all Radio Buttons

2) HP QTP Crypt Object

3)Adding Hyperlinks in Excel Spreadsheet

Best of Luck Friends ! ! !

Expert QTP
expert.qtp@gmail.com

All Articles are Copyright Protected. These are Free for Reading & Personal Use. Reproduction in Any Form without the Permission is Illegal & Strictly Prohibited.

Copyright © 2009 ExpertQTP

Google Search

Sunday, June 8, 2008

Learn the Basics of Closing the Processes using QTP

The article will discuss on how to close processes in QTP. First we will discuss the SystemUtil object and then later in the article few other approaches.

SystemUtil Object

SystemUtil is utility object provided by QTP. It can be used to launch or closes a process. Let's look at various methods supported by this object:

SystemUtil.Run file, [params], [dir], [op], [mode]

The Run method allows running a new process. The code below illustrates few example on how to launch a process using SystemUtil object

'Run internet explorer
SystemUtil.Run "iexplore.exe"

'Run internet explorer and open google.com
SystemUtil.Run "iexplore.exe", "http://www.google.com"

'Run a IE Process in Maximized window
Const SHOW_MAXIMIZED = 3 'Activates the window and displays it as a maximized window.
SystemUtil.Run "iexplore.exe", "http://www.google.com", , , SHOW_MAXIMIZED

SystemUtil.CloseProcessByName (
bsProcessName)

CloseProcessByName method allows closing a process by its name. The code below illustrates few examples

'Close all internet explorer windows
closedCount = SystemUtil.CloseProcessByName("iexplore.exe")

'Display # of closed windows
MsgBox closedCount

The problem with using the above method is that it will also close Quality Center (QC) window even if script was launched from QC. This method should be used in case you are not concerned about running scripts from QC.

SystemUtil.CloseProcessByWndTitle (bsTitle, [bRegExp])

CloseProcessByWndTitle method allows closing a process by its title. The title can also be supplied as a regular expression by setting the bRegExp to True

'Launch a notepad window
SystemUtil.Run "notepad.exe"

'Close the window just launched using the exact title
SystemUtil.CloseProcessByWndTitle "Untitled - Notepad"

'Launch a notepad window
SystemUtil.Run "notepad.exe"

'Close the window just launched using a pattern string
SystemUtil.CloseProcessByWndTitle ".*Notepad", True

SystemUtil.CloseDescendentProcesses

CloseDescendentProcesses can be used to close any process launched by QTP. The code below illustrates the usage

'Launch explorer
SystemUtil.Run "iexplore.exe"

'Launch excel using COM
Set oXL = CreateObject("Excel.Application")
oXL.Visible = True

'Close processes launched by QTP. This will close
'the internet explorer and Excel as well
SystemUtil.CloseDescendentProcesses

This method is best suited to be used during the end of a script to cleanup any process left open.

Closing all open Browsers

Many times it's required to close all open browsers at the start of the script. There are various ways of achieving this; one is to use the SystemUtil.CloseProcessByName method which we discussed earlier. But that approach is not generic as it won't close other browsers that QTP does support (like firefox and netscape). We can come with a generic 3 lines code which can close all supported QTP browsers

'Check the existence of a browser and close it
'until no more browsers exist
While Browser("creationtime:=0").Exist(0)

'Close the browser
Browser("creationtime:=0").Close

Wend

QTP assigns creation time to each browser based on the launch time of that browser. A browser started earlier will have a lower creationtime and a browser started at a later point of time will have a higher creationtime. So when we start closing the browser with creationtime:=0, other browser's creationtime is decreased. Which means that the creationtime:=0 browser will always exist until there is no browser open. This approach of closing browsers suffers from 2 issues

* QTP has a bug which makes it wait 10 second on Browser identified using creationtime when there is only one browser open. So above code will always take 10 secs to close the last browser
* The code does not allow to ignore any specific browser (like Quality Center)

Though there another way by which can enumerate all open browser and close them in QTP and below demonstrates the same

'Create a description for browser
Set oBrowser = Description.Create
oBrowser("micclass").Value = "Browser"

Set oPage = Description.Create
oPage("micclass").Value = "Page"

'Get all browsers
Set allBrowser = Desktop.ChildObjects(oBrowser)

Dim i, iCount

iCount = allBrowser.Count - 1

For i = 0 To iCount
'Get the page object from the browser
Set oPg = allBrowser(i).ChildObjects(oPage)(0)

'Get the URL of the
If InStr(oPg.GetROProperty("title"), "Quality Center", vbTextCompare) = 0 Then
'Close the browser
allBrowser(i).Close
End If
Next

By now you must be wondering about the line

'Get the page object from the browser
Set oPg = allBrowser(i).ChildObjects(oPage)(0)

So why didn't we use allBrowser(i).Page("micclass:=Page"), well this a Bug or Limitation in QTP which does not allow using any further Test objects on objects returned from ChildObjects. This might be because of the fact that a QTP TestObject does not have any property or method as other Test Object. Ex - a Browser supports no method or property named Page. But QTP interprets that as way of hierarchy to decide where to look for that object. Anway this is all my opinion and might not be accurate. So the only workaround of using further TestObjects is to use ChildObjects again.

Closing processes using WMI

Another way to close a process is to use Window management instrumentation (WMI)

'Name/IP of the computer
sComp = "."

'Get the WMI object
Set WMI = GetObject("winmgmts:\\" & sComp & "\root\cimv2")

'Get collection of processes for with name iexplore.exe
Set allIE = WMI.ExecQuery("Select * from Win32_Process Where Name = 'iexplore.exe'")

'Loop through each process and terminate it
For Each IE in allIE
IE.Terminate()
Next

Above code can also be used to close any process on a remote machine by changing sComp to the IP of the remote machine

Closing Internet explorer using Shell

Let's take a look at another way which is generic to VBScript and not dependent on QTP. For this we will use the COM object of "Shell.Application". This COM library provides a collection of all open windows (Explorer + Internet Explorer). We can access each windows property and close if it is iexplore.exe process. The code below demonstrates the same

'Create the shell application object
Set shlApp = CreateObject("Shell.Application")

'Get all open windows collection
Set allWins = shlApp.Windows

'Loop through each window and close if IE
For Each window In allWins
If InStr(window.fullName, "iexplore.exe") Then
'Close the IE window
window.Quit
End If
Next

The code above will only close alternate windows. This happens because we are loop through a collection of windows and closing a window in that collection reduces the collection and hence the for loop skips one window after closing a window. This issue can easily be resolved by adding the windows to be closed into an array and the closing them later

Dim windows2Close

'Initialize with negative upper bound
ReDim windows2Close( -1)

'Create the shell application object
Set shlApp = CreateObject("Shell.Application")

'Get all open windows collection
Set allWins = shlApp.Windows

'Loop through each window and close if IE
For Each window In allWins
'Close all IE windows but ignore Quality Center
If InStr(window.fullName, "iexplore.exe") And InStr(Window.LocationURL, "/qcbin/") = 0 Then

'Increase the array size by 1
ReDim Preserve windows2Close(UBound(windows2Close) + 1)

Set windows2Close(UBound(windows2Close)) = Window
End If
Next

'Loop through all array elements and close each window
For Each Window In windows2Close
Window.Quit
Next

The above code shows how to ignore few internet explorers and close rest.

Summary

In this article we explored various ways of Closing processes and browsers using QTP.

No comments:

 
Copyright © 2009 ExpertQTP