Friday, January 15, 2010

Balloon Tool Tip Trick in QTP Instead Of Automatic Timeout Message Box

If you are using automatic timeout msgbox in your QTP Script(s), then please switch over to Balloon Tool Tip. It is much better than VB script or WSH timeout message boxes.

For instance: If you want to know the current iteration number while running your scripts, then you may use automatic timeout msgbox using the below code.

Start_Time=Time

Set WshShell = CreateObject("WScript.Shell")
wshShell.Popup "Current Iteration No. "&Environment.Value("TestIteration"), 5 'Next step will not run until the time-out expires

SystemUtil.Run "Iexplore.Exe","
http://www.google.co.in"
Browser("name:=Google").Page("title:=Google").WebEdit("name:=q").Set "QTP"
Browser("name:=Google").Page("title:=Google").WebButton("name:=Google Search").Click


End_Time=Time
Msgbox Hour(End_Time-Start_Time) & ":" & Minute(End_Time-Start_Time) & ":" & Second(End_Time-Start_Time)


The above code has one drawback. When the msgbox is invoked, next step will not run until the specified msgbox's time-out expires.

Try the same scenario using Balloon Tool Tip code. The current iteration number will be displayed in System Tray and the script will not be stopped to display the tool tip.

icon_Path="C:\Documents and Settings\Desktop\6.ico" 'Choose any Icon file path from your PC. Make sure the file format should be .Ico.
Set oNtfy=DotNetFactory.CreateInstance("System.Windows.Forms.NotifyIcon","System.Windows.Forms")
Set oToolTip =DotNetFactory.CreateInstance("System.Windows.Forms.ToolTipIcon","System.Windows.Forms")
Set oIcon=DotNetFactory.CreateInstance("System.Drawing.Icon","System.Drawing",icon_Path)


Function fn_BalloonToolTip(vIterationNo)
oNtfy.Icon=oIcon
oNtfy.BalloonTipIcon=oToolTip.Info
oNtfy.Visible=True

oNtfy.Text = "QTP Info." 'Balloon Tool Tip Text Name
oNtfy.BalloonTipTitle = "Current Iteration No" 'Balloon Tool Tip Title
oNtfy.BalloonTipText="Current Iteration No. "&vIterationNo
oNtfy.ShowBalloonTip(5000) 'This line displays current iteration No in system Tray
End Function

Function fn_Kill_Balloon 'This function kills all the objects.
oNtfy.Dispose() 'After calling this method you will not see the balloon icon in system tray
Set oIcon =Nothing
Set oTool =Nothing
Set oNtfy=Nothing
End Function


Start_Time=Time
Call fn_BalloonToolTip(Environment.Value("TestIteration")) 'Calling balloon tool tip function


SystemUtil.Run "Iexplore.Exe","
http://www.google.co.in"
Browser("name:=Google").Page("title:=Google").WebEdit("name:=q").Set "QTP"
Browser("name:=Google").Page("title:=Google").WebButton("name:=Google Search").Click


Call fn_Kill_Balloon'Calling kill function

End_Time=Time

Msgbox Hour(End_Time-Start_Time) & ":" & Minute(End_Time-Start_Time) & ":" & Second(End_Time-Start_Time)

After invoking the fn_BalloonToolTip function, you will see the Balloon Tool Tip in your system tray as same as below,




In previous WScript code, the QTP script is waited until the msgbox timeout expires. Here QTP script will not wait for Balloon Tool Tip to be closed. The tool tip will remain in the system tray unless/until you call the fn_Kill_Balloon

I hope this technique will be useful for others also.

Please let me know if you have any questions in this.

12 comments:

  1. I tried using this code, and I was able to get the icon, but the balloon itself did not show? why is this?

    ReplyDelete
  2. Hi, could you send the code which you tried, please?

    E-mail: asiqahamed@yahoo.com

    Thanks,
    Asiq

    ReplyDelete
  3. Nice one!

    First time it didn't work well.
    I have to do some changes as per my scripts.
    Now it working fine.

    It's cool...

    shrikalpesh@gmail.com

    ReplyDelete
  4. I could not get it to work. I copied the exact code on here and modified the Icon path to my icon location. Is there something on this script that needs to be changed before it can work properly?

    ReplyDelete
  5. hi,

    I'm able to get the tooltip and is being closed after 5secs, but the icon in tray is not getting removed, so whenever i run everytime new icon is adding, please let me know

    ReplyDelete
  6. Are you calling fn_Kill_Balloon function in your code?

    ReplyDelete
  7. Thanks,
    I have a question, if QTP stop suddently, so fn_Kill_Ballon can not be called, how we remove system tray.

    ReplyDelete
  8. Working fine for Me ... I have a question ... like to know how to remove the icons manually from the System tray if the script is terminated before calling fn_Kill_Ballon function. do we need to run the "fn_Kill_Ballon" function each and every time when we call "fn_BalloonToolTip" function?

    ReplyDelete
  9. Hi, this function is working fine if I run using QTP.
    I kept the same code in .vbs file and tried executing it by double clicking the .vbs (out side to QTP env). But, got an error as "Object Required: 'DotNetFactory'"... would like to implement this functionality in .vbs ... pls guide me in that regards.

    ReplyDelete
  10. How to run this function? Its not working for me..

    ReplyDelete