While debugging your scripts I am sure you must be using a lot of “msgbox” functions. Won’t it be good if this message box closes automatically so you need to have to pain yourself to reach that mouse.
Can you design a function which will close the message box automatically after x seconds. If yes, write your script in comments below.
Check out this space tomorrow for an answer.
@ Alex
Print is AWESOME! That’s even better than the Msgbox and doesn’t seem to noticeably slow the script at all!
Thank you for posting about it.
msgbox_time=”10″
try this this will be useful for message box automatically closes
Set objshell = createobject(“Wscript.shell”)
rmensaje = objshell.popup(“mensaje”,3,”Mensaje “,64)
Hi,
While i creating the text area checkpoint,it is showing that Can not create text area checkpoint like that …can u explain how can i recover this
excellent answers
Sanu wins!
Took 18 months, and his coding style is a bit rough around the edges, but I declare Sanu the first person to fufill the requirement of a function that creates a self-closing dialogbox, and with an icon indicater no less!
Extra points for creativity Sanu!
Public Function SelfCloseMsgbox(title,content)
Dim x,y
Set x = CreateObject(“WScript.Shell”)
y = x.popup(content,3,title,4+32)
Set x = Nothing
Set y = Nothing
End Function
Call SelfCloseMsgbox(“QTP”,”QTP is wonderful..!!!”)
I need to close QTP Automatically after executing n (may be n=5) number of scripts OR if it find ideal for 5 to 10 seconds after execution. plz help
Is there any code using Vb script
REM use this function as a reusable fucntion.
REM instead of msgbox call printmsg.
REM while you dont want msgs to be displayed just comment one line as shown below.
REM If the code is in debug mode, just enable the line to view msgbox with value
Function Printmsg(printparam)
‘msgbox(printparam)
End Function
Hi Alex,
I have used the print before, but I had one question. How do I store the values from the print log to the txt file ( or any file)?
This code can be used in the start up script to kill the excel process:
Dim strComputer,objWMIService,colProcessList
strComputer = “.”
Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\cimv2”)
Set colProcessList = objWMIService.ExecQuery (“Select * from Win32_Process Where Name = ‘EXCEL.EXE'”)
For Each objProcess in colProcessList
objProcess.Terminate()
Next
msgbox “Testing”
wait(5) ‘ system to wait for 5 secs
dialog(“nativeclass:=#32770”).winbutton(“text:=OK”).click ‘to click on OK Button
isn’t this enogh ?
This will append all ur result in one file path u will give in result_file_path:
Const ForAppending = 8
Dim result_file_path
result_file_path = “C:\Documents and Settings\bh001kh\Desktop\Text Result\Check_services.txt”
‘***************************************************************************************************************************************
Set oFSO = CreateObject(“Scripting.FileSystemObject”)
Set oCheckServices = oFSO.OpenTextFile(result_file_path, ForAppending, True)
‘***************************************************************************************************************************************
sLine = space(15) & “msg whatever u want to add”
oCheckServices.WriteLine(sLine)
oCheckServices.WriteLine(vbcrlf)
OK idea provided by you is good. i will implement the
But why do you want to Close QTP after running on single testcase?? Why cann’t use use Batch Runner Tool??
Thanks Paresh
Code provided by you is working fine from VbScript file. but i want to close QTP (not from any external files) after execution of test done. This is very much helpful when we are executing single test unattentively. i will try the function of Killprocess by Name and let you know the results.
If you are starting QTP using VB Script, then it is easy to Close the QTP again.
Below Code will help you to Start/Stop QTP
‘*****************************
Dim App
StartQTP
StopQTP
‘ Start QTP
Function StartQTP ()
‘ Launch QuickTest Professional and make it visible.
Set App = CreateObject(“QuickTest.Application”)
App.Launch
App.Visible = True
End Function
‘ Stop QTP
Function StopQTP ()
App.Quit
End Function
‘*****************************
Make a Note that, You can be able to Close QTP using VB Script if you have started QTP using the same Script so you can use the same Object to Close it.
If you are starting QTP outside of any other Script then you have to Kill QTP Process.
To KILL QTP Process, you can use below CODE.
‘*****************************
Call KillProcessByName(“QTPro”)
‘ Kill Process By Name
Public Sub KillProcessByName(ByVal psProcessName As String)
‘* Iterate through all running processes to locate the specified process and kill it.
For Each pProcess As Process In Process.GetProcessesByName(psProcessName)
‘* Kill the process
If Not pProcess.HasExited Then Call pProcess.Kill()
‘* Wait while the process completes it’s exit.
Do While Not pProcess.HasExited
Call System.Windows.Forms.Application.DoEvents()
Loop
Next ‘pProcess
End Sub
‘*****************************
i want to close QTP after execution of test. (Self Close of QTP) Could you provide idea about on this.
Well I prefer to use Debug Windows to check values for each variables – using message box is not a Good style. We can easily use Debug Windows.
I don’t understand why people are using MessageBox for checking the value.
Recommend not to use Message Box. Still If you want to use MessageBox, and if you forgot everytime to remove it then – I will try to find out the solution. Thanks!!
Hi Suba,
Well what sort of external data base table do you have ? I hope it must be in xls.
so if you want to export your run time table results into external xls sheet then you have to use :
Datatable.Export(“c:\external.xls”)
and replica of your run time table will be formed at c:\external.xls
Thanks
Shobhit Kaul
Capgemini India
Hi Ankur
Need your help in QTP.Could you please let me know how do I export the values of my global sheet to an external database Table.
Thank you
Suba
Hello Alex,
The kind of solution you have proposed is really good and we should try using it instead of MsgBox & Popup method.
hi alex,
thank you for your solution. really it is use full for me.
Hi , i dont think any of these answers can be used to directly close message box window during run of qtp script.The one method explained above is an alternative to it (using popup) ,but none was the exact way to kill message box.Please if some one have answer for this then mail me at kaul.shobhit@gmail.com
Yikes!
Many good solutions, but none are functions.
‘This script will show you the demonstration – closing a ‘dialog box automatically.
Set abc = CreateObject(“WScript.Shell”)
For i = 1 To 10
abc.Popup i,1,”Warning”
Next
Hi,
a=”sree is a good boy”
Set sh = CreateObject(“WScript.Shell”)
sh.Popup a,2, “”
Hi…
Ima new to ths QTP… I was aware about QTP technically, but donno how to write scripts…so can u send some notes for that… it will b very much useful 4 me….
I would rather prefer to use a function for recovery scenario object and call that in d script and ask it to wait for 10 seconds.
Hi Madhi ,
Thank’s yaar this is working Thank’s but this is a solution when we use Popup Msgbox but when iam using Print in my If else then my print window is not supporting this script. can u privide me a solution for print as well
Plz provide the solution Ankur
I would try a solution with a recovery scenario… Maybe use the same window title all the time so that only the right popup will be closed automatically
@ Anonymous
That’s exactly why I no longer debug with a msgbox.
I know I’ve not answered the question directly, but I have offered an alternative that will allow your test to run faster (no stopping for the msgbox, no waiting for the msgbox to timeout and ‘autoclick’, just straight logging in a background window.
Instead of: msgbox “My Message”
use: Print “My Message”
I bet you’ll never go back to msgbox once you try it!
none of these answers are direct to the question – MSGBOX is problematic because script (and VB6 executables) STOP and wait for a response. How to “kill” a message box was the challenge.
' my answer – based on:
' WatchProc.vbs
' Author: Robert Smith Jr
' Original author's description:
' Script which will shell a process (like another script) and wait for it
' to terminate. It will kill the shelled process if it does not complete
' in x seconds. Useful for environments where machines may not be
' consistently available.
' – Watch (via event subscription) a shelled process
' and cancel it if it is still running after MAXTIME
' Writes a log file to watchproc.log
' robsmith@cs.cmu.edu
'
' Script:
Const MaxTime=10
Dim wshShell
Dim processid
Dim DoLoop
Dim FSO
Dim outfil
Dim svc
Dim rsink
Dim pathrun
Dim progname
Dim params
Dim blnDebug
Dim blnError
blnDebug = True
blnError = False
Set FSO = CreateObject("Scripting.FilesystemObject")
If blnDebug Then Set outfil = FSO.OpenTextFile("watchproc.log",8,TRUE)
If blnDebug Then outfil.write vbcrlf & Now() & "WatchProc.vbs started …" & VbCrLf
' Init WMI
If blnDebug Then outfil.write Now() & " Binding to WMI and creating SINK…" & VbCrLf
Set wshShell=CreateObject("WScript.Shell")
Set svc = GetObject("WINMGMTS:{impersonationLevel=impersonate,(Security)}!\\.\ROOT\CIMV2")
' Create the Event Notification Sink
Set rsink = CreateObject("WbemScripting.SWbemSink")
WScript.ConnectObject rsink, "EVENTSINK_"
If blnDebug Then outfil.write Now() & " WMI bind / create SINK Status = " & Err.Number & VbCrLf
pathrun = "C:\"
progname = "msgbox.vbs"
params = " "
If blnDebug Then outfil.write "Default arguments: " & pathname & " " & progname & vbcrlf
DoLoop = True
Call Launch( "wscript " & progname, pathrun, blnDebug, rsink, svc, blnError )
If Not blnError Then
If blnDebug Then outfil.write Now() & "Launching wscript " & sname & vbcrlf
count = 0
Do While DoLoop
count = count + 1
wscript.sleep 1000
If count > MaxTime Then
' If the user has killed the msgbox before this does?
Call KillPid ( processid, blnDebug, rsink, DoLoop )
End If
Loop
If blnDebug Then
outfil.write Now() & "It took " & count & " seconds to finish "
Set outfil = Nothing
End If
End If
WScript.quit
'**************************************************************
Sub Launch( progname, pathrun, blnDebug, rsink, svc, blnError )
Dim params
Dim oClass
err.clear
Set oClass = svc.Get( "Win32_Process" )
If err = 0 Then
oClass.Create progname, pathrun, params, processid
If blnDebug Then outfil.write prog & " PID:" & processid & " created …" & vbcrlf
Call WatchPid( processid, blnDebug, svc, rsink )
blnError = False
Else
MsgBox "ERROR: " & err.number & " " & err.description
blnError = True
End If
End Sub
'***************************************************************
Sub KillPid( id, blnDebug, rsink, DoLoop )
Dim colproc
Dim strQuery
Dim proc
On Error Resume Next
strQuery = "Select * from Win32_Process where processid=" & id
Set colproc = svc.ExecQuery( strQuery )
If colproc.count <> 0 Then
For Each proc In colproc
proc.terminate()
If blnDebug Then outfil.write "Process " & id & " Timed Out " & Now() & vbcrlf
rsink.Cancel()
DoLoop = False
Next
End If
End Sub
'*****************************************************************
Sub WatchPid( pid, blnDebug, svc, rsink )
Dim strQuery
strQuery = "SELECT * FROM __InstanceOperationEvent " & _
"WITHIN 5 WHERE TargetInstance ISA 'Win32_Process' AND TargetInstance.ProcessId='" & pid & "'"
svc.ExecNotificationQueryAsync rsink, strQuery
End Sub
'******************************************************************
Sub EVENTSINK_OnObjectReady( objObject, objAsyncContext, rsink, DoLoop, blnDebug )
If objObject.Path_.Class = "__InstanceDeletionEvent" Then
If blnDebug Then outfil.write "Process " & objObject.TargetInstance.ProcessId & " Finished " & now() & vbcrlf
rsink.Cancel()
DoLoop = False
WScript.Quit
End If
End Sub
I leave indentation up to you
The called messagebox vbs is obvious
I also leave up to you the problem of forcing the called messagebox to the front and setfocus
Hi,
Here is Code which closes dialog box automatically, no need to click on Ok button
—
smsg = “User Message”
Set oWScript = CreateObject”Wscript.Shell”)
For i = 1 to 1
oWscript.popup smsg ,1
Next
Set oWScript = nothing
Mayur
set a = createobject(“wscript.shell”)
msg=”Messagebox will close automatically””””””Message you want to display
tme=”5″””””””””timeout
title=”testing””””title of window
a.popup msg,tme,title
Set sh = CreateObject(“WScript.Shell”)
sh.Popup “This msgbox will close in 10 secs”,10, “This is name of window”
You would just create a WScript Shell object and use the Popup method on it.
Set WshShell = CreateObject(“Wscript.Shell”)
WshShell.Popup “Wait 5 seconds”, 5, “Title”
I’ve recently been introduced to the QTP “Print” function.
Rather than use an application modal message box, I’ve found it much easier to use Print “my message”
This runs in a background window, keeping a running log of my test execution much better than a message box that disappears (losing the information) when I click the OK button.