Tuesday, October 5, 2010

VBScript to capture a screenshot in QTP and insert it in the test results

Example Usage:

Call CaptureScreen("Pass", "The test passed")

----------------------------------------------------------------------------------


Public Sub CaptureScreen(Result, Details)
'********************************************
' Author : Chinmay Mudholkar
' Purpose : Taking a screenshot of the current screen and inserting it in the test results with the specified result type
' Inputs : Result: The result type
'  Details: The details about the screenshot/report
' Returns : None
'********************************************

'Capture the screenshot in a temp file first, do everything else later
Desktop.CaptureBitmap Environment.Value("TestDir") & "\temp.bmp", True

Dim imgname, imgpath, fso

If Details = "" Or IsEmpty(Details) Then
Details = Result
End If

imgname = "temp.bmp"
imgpath = Environment.Value("TestDir") & "\" & imgname

Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(imgpath) Then
If UCase(Result) = "PASS" Then
Reporter.ReportEvent micPass, "Captured Screen", Details, imgpath
ElseIf UCase(Result) = "FAIL" Then
Reporter.ReportEvent micFail, "Captured Screen", Details, imgpath
Else
Reporter.ReportEvent micDone, "Captured Screen", Details, imgpath
End If

fso.DeleteFile imgpath, True
End If
End Sub

----------------------------------------------------------------------------------

VBScript to run an MS SQL Job

Example Usage:

Call runSQLJob("My Job")

------------------------------------------------------------


Public Sub runSQLJob(JobName)
'********************************************
' Author : Chinmay Mudholkar
' Purpose : Runs the specified SQL job.  Returns after the job execution is complete.
' Inputs : JobName: Name of the SQL job to run
' Returns : None
'********************************************

Dim jst, myCon, ObjSQL
jst = "SELECT ISNULL((SELECT CAST(J.JOB_ID AS VARCHAR(36)) FROM msdb..sysjobs j INNER JOIN msdb..sysjobactivity ja ON j.job_id = ja.job_id WHERE j.name = '" & JobName & "' AND ja.stop_execution_date IS NOT NULL AND ja.start_execution_date = (SELECT MAX(jain.start_execution_date) FROM msdb..sysjobactivity jain WHERE jain.job_id = j.job_id GROUP BY jain.job_id)), 0)"
Set myCon=Createobject("ADODB.Connection")
myCon.ConnectionString = "your connection string here"
ObjSQL = "EXEC msdb.dbo.sp_start_job @job_name = '" & JobName & "'"
myCon.Open()
myCon.Execute(ObjSQL)
Do
Wait(2) 'Wait in the beginning for the job to start and then before querying job status everytime.
Dim rs
Set rs = CreateObject("adodb.recordset")
rs.open jst, myCon, 3, 3
If Len(rs.fields(0))>1 Then
Exit Do
End If
Set rs = Nothing
Loop
myCon.Close()
End Sub

------------------------------------------------------------

Generate a random alphabet string using VBScript

Usage Example:

MsgBox getRandomString(15)
' This would return a random string of 15 characters.

-------------------------------------------------------------

Public Function getRandomString(ofLength)
'********************************************
' Author : Chinmay Mudholkar
' Purpose : Getting a random alphabet string of a specified length
' Inputs : ofLength: The length of the random string required
' Returns : A random string of the specified length
'********************************************

If Not IsNumeric(ofLength) Or IsEmpty(ofLength) Or ofLength = "" Then
getRandomString = ""
Exit Function
End If

Dim retval, i
retval = ""
For i = 1 To ofLength
retval = retval & Chr(Int(26*Rnd+97))
Next
getRandomString = retval
End Function

-------------------------------------------------------------

VBScript to verify that the specified object exists on the current page

Public Function verifyExistence(ObjectType, ObjectName, ExpStatus)
'********************************************
' Author : Chinmay Mudholkar
' Purpose : Verify that the specified object exists on the current page
' Inputs : ObjectType: The type of object based upon its micClass
'  ObjectName: The HTML name of that object
'  ExpStatus: The expected status of the object
' Returns : Boolean value.  The current status of the object.
'********************************************

Dim Status

Select Case ObjectType
  Case "Link"
Status = Browser("title:=.*").Page("title:=.*").Link("name:=" & ObjectName, "Index:=0").Exist(5)
Case "Image"
Status = Browser("title:=.*").Page("title:=.*").Image("alt:=" & ObjectName).Exist(5)
Case "WebElement"
Status = Browser("title:=.*").Page("title:=.*").WebElement("innertext:=" & ObjectName & ".*","index:=0").Exist(5)
Case "WebList"
Status = Browser("title:=.*").Page("title:=.*").WebList("name:=" & ObjectName).Exist(5)
Case "WebTable"
Status = Browser("title:=.*").Page("title:=.*").WebTable("innertext:=" & ObjectName & ".*","index:=0").Exist(8)
Case "Browser"
Status = Browser("CreationTime:=" & ObjectName).Exist(5)
Case "WebEdit"
Status = Browser("title:=.*").Page("title:=.*").WebEdit("name:=" & ObjectName).Exist(5)
Case "WebButton"
Status = Browser("title:=.*").Page("title:=.*").Webbutton("name:=" & ObjectName,"index:=0").Exist(5)
Case "WebRadioGroup"
Status = Browser("title:=.*").Page("title:=.*").WebRadioGroup("name:=" & ObjectName).Exist(5)
Case "WebCheckBox"
Status = Browser("title:=.*").Page("title:=.*").WebCheckBox("name:=" & ObjectName).Exist(5)
Case Else
Status = False
End Select

If CBool(Status) = CBool(ExpStatus) Then
verifyExistence = True
Else
verifyExistence = False
End If

Set Status = Nothing

End Function

This VBScript function will give you the current date in the required format

Usage Examples:

MsgBox getDatestamp("MM/DD/YYYY")
' Returns 10/05/2010

MsgBox getDatestamp("DD-MM-YY")
' Returns 05-10-10

MsgBox getDatestamp("DD.MM.YYYY")
' Returns 05.10.2010

MsgBox getDatestamp("DD")
' Returns 05


------------------------------------------------------------------------------------------

Public Function getDatestamp(inFormat)
'********************************************
' Author : Chinmay Mudholkar
' Purpose : Getting the current date stamp
' Inputs : inFormat: The format that the date is required in. (eg: DD-MM-YY)
' Returns : The current date in the format required.
'********************************************

Dim retval, dd
retval = UCase(inFormat)

dd = Right("00" & Day(Date), 2)
mm = Right("00" & Month(Date), 2)

If InStr(retval, "DD") Then
retval = Replace(retval, "DD", dd)
End If

If InStr(retval, "MM") Then
retval = Replace(retval, "MM", mm)
End If

While InStr(retval, "YY")
If InStr(retval, "YYYY") Then
retval = Replace(retval, "YYYY", Year(Date))
ElseIf InStr(inFormat, "YY") Then
retval = Replace(retval, "YY", Right(Year(Date), 2))
End If
Wend

getDatestamp = retval
End Function

------------------------------------------------------------------------------------------

Introduction

Hi,

My name is Chinmay Mudholkar. I am a test automation engineer from Pune, India. You will find useful scripts for QTP on my blog.