Showing posts with label Close all browser. Show all posts
Showing posts with label Close all browser. Show all posts

Sunday

QTP - Terminate process , Close all browser

'************************************************' Killing the process By QTP'************************************************Killprocess ="EXCEL.EXE"Killprocess2 = "calc.exe"Killprocess3 = "mspaint.exe"Set ProcessList = GetObject("winmgmts://.").InstancesOf("win32_process")For Each Process In ProcessList If Process.Name = KillProcess Then Process.Terminate End IfIf Process.Name = KillProcess2 Then Process.Terminate End ifIf Process.Name = KillProcess3 Then Process.Terminate End ifNext
'************************************************'Closing all the Browser Locally'************************************************KillprocessIE ="IEXPLORE.EXE"Set ProcessList2 = GetObject("winmgmts://.").InstancesOf("win32_process")
For Each Process In ProcessList2 If Process.Name = KillProcessIE Then Process.Terminate End IfNext
While Browser("CreationTime:=0").Exist Browser("CreationTime:=0").CloseWend
'************************************************'Closing all the Browser by WBI Object remotely'************************************************'Alternatively WMI (Window management instrumentation ) object can be Used'sComp is IP of the remote machine
SystemUtil.Run "iexplore.exe"SystemUtil.Run "iexplore.exe"sComp="shukr02-xp"Set WMIobj = GetObject("winmgmts:\\" & sComp & "\root\cimv2") ' Find All the iexplore.exe processes Set IEExp = WMIobj.ExecQuery("Select * from Win32_Process Where Name = 'IEXPLORE.EXE'")
For Each IEx in IEExp IEx.Terminate()Next'************************************************'SystemUtil Object'************************************************SystemUtil.Run "iexplore.exe"SystemUtil.Run "iexplore.exe"NumberOfProcess = SystemUtil.CloseProcessByName("iexplore.exe")
'Check If Browser Exist and if the close it'SystemUtil.Run "iexplore.exe"SystemUtil.Run "iexplore.exe"While Browser("creationtime:=0").Exist(0) Browser("creationtime:=0").CloseWend'**********************************************

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 checkingLet'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")