Showing posts with label QTP Codes Samples. Show all posts
Showing posts with label QTP Codes Samples. Show all posts

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.

Saturday, January 9, 2010

Working With Screen Capture in QTP

Converting PNG to JPG format

I have seen one question in Advanced QTP forum, that is the user needed to capture screenshot in JPG format instead of PNG.
QTP cannot capture snapshot(s) as JPG format, but we have an option to convert it using DOT Net Factory. I hope this code will be useful for all QTP users. So I have posted this in my blog.

Here is the code to convert from PNG to JPG

sImagePath="C:\Documents and Settings\TEMP\Desktop\cap.png"'Make sure to mention your destination path for snapshot

Desktop.CaptureBitmap sImagePath'Capturing as PNG format

Call ConvertJPG(sImagePath)

Function ConvertJPG(sBmpPath)

Set oBmp=DotNetFactory.CreateInstance("System.Drawing.Bitmap","System.Drawing",sBmpPath)
Set oDir=DotNetFactory.CreateInstance("System.IO.Path","mscorlib")
Set oFile=DotNetFactory.CreateInstance("System.IO.File","mscorlib")

oBmp(sBmpPath)

sNewFile = oDir.GetDirectoryName(sBmpPath)
sNewFile = sNewFile&"\" & oDir.GetFileNameWithoutExtension(sBmpPath)
sNewFile = sNewFile&"." &"JPG"
oBmp.Save(sNewFile)'Saving as JPG format

oBmp.Dispose()
oFile.Delete(sBmpPath)'Deleting the previous PNG file.

Set oDir=Nothing
Set oFile=Nothing
Set oBmp=Nothing
End Function