.

Quiz: A message box that will close automatically

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 yourselfqtp_msgbox 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.

If you want to keep track of further articles on QTP. I recommend you to subscribe via RSS feed. You can also subscribe by Email and have new QTP articles sent directly to your inbox.

Related posts:

  1. How to include an Action template in new action automatically?
  2. How to delete cookies through a script?
  3. HP QTP Quiz: Passing by value vs Passing by reference
  4. Quiz: Find the difference between these VBScript codes?
  5. Can we run multiple QTP instances on one machine?

22 comments ↓

#1 Alex on 11.12.08 at 3:38 pm

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.

#2 Percy Bell on 11.12.08 at 6:42 pm

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”

#3 Madhi on 11.12.08 at 8:57 pm

Set sh = CreateObject(”WScript.Shell”)
sh.Popup “This msgbox will close in 10 secs”,10, “This is name of window”

#4 Joy on 11.12.08 at 10:40 pm

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

#5 MAYURBIDKAR on 11.13.08 at 4:07 am

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

#6 Anonymous on 11.13.08 at 3:06 pm

' 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

#7 Anonymous on 11.13.08 at 3:09 pm

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.

#8 Alex on 11.19.08 at 7:21 pm

@ 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!

#9 boris79 on 11.28.08 at 5:22 am

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

#10 Anurag on 12.10.08 at 9:03 am

Plz provide the solution Ankur

#11 Srikanth on 12.10.08 at 8:38 pm

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

#12 Chaitra Jayadev on 12.10.08 at 9:18 pm

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.

#13 Sudhan on 12.11.08 at 12:22 am

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….

#14 Sree on 12.15.08 at 10:26 pm

Hi,

a=”sree is a good boy”
Set sh = CreateObject(”WScript.Shell”)
sh.Popup a,2, “”

#15 Siva Phaneendra K on 12.27.08 at 7:37 am

‘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

#16 Paul Grossman on 01.28.09 at 12:22 pm

Yikes!

Many good solutions, but none are functions.

#17 Shobhit Kaul on 02.25.09 at 2:18 am

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

#18 kumar on 02.26.09 at 1:03 am

hi alex,

thank you for your solution. really it is use full for me.

#19 Jitu on 03.03.09 at 9:47 am

Hello Alex,
The kind of solution you have proposed is really good and we should try using it instead of MsgBox & Popup method.

#20 Subashini on 05.08.09 at 6:48 am

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

#21 shobhit kaul on 05.11.09 at 9:15 pm

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

#22 Paresh on 06.01.09 at 4:51 am

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!!

Leave a Comment