Thursday 29 May 2008

Test suite driver script

As a follow-up to this week posts I thought I should share my automation test driver script. This contains my test case driver logic. All administration of the tests are handled via Web pages and the data is in an SQL Server database. The script is in the Script.mts file in a QTP Test and can be used in QTP (obvious) and for non-GUI tests via vbs standalone as shown in yesterdays post.




'**************************************
'*** test suite driver script ***
'**************************************

'Get and set test run options for QTP
If intTestRunOptionID = "" Then intTestRunOptionID = 1

'Execute main
Main intTestRunOptionID

Sub Main (intTestRunOptionID)

Set objTestRun = GetTestRunOptions (intTestRunOptionID)

'Check Global run status and stop test if requested
If objTestRun("TEST_RUN") = "True" Then
'Start test and report
ResetTestRun intTestRunOptionID, "TestStart"
Else
If objTestRun("TEST_RUN_TOOL") = "QuickTestPro" Then CloseAllBrowsers
Exit Sub
End If

'*** Handle Result Records ****
Select Case objTestRun("TEST_RESULT_OPTION")
Case "New"
'Create new result set
intTestRunResultID = CreateAndUpdateNewResult (intTestRunOptionID, _
"TestRun_" & Now(), objTestRun("REPORT_TO_NET") )
'Create summary result
CreateSummaryResult intTestRunResultID, intTestRunOptionID
'Create result rows
CreateResultRows intTestRunResultID, intTestRunOptionID
Case "Resume"
intTestRunResultID = objTestRun("TEST_RUN_RESULT_ID")
End Select

'Initialize
blnAllTestRunsExecuted = False

Do Until blnAllTestRunsExecuted

'Get and set global test run data
Set objTCGlobalParameter = GetGlobalTestParameters (intTestRunResultID)
If objTCGlobalParameter("TEST_RUN_RESULT_SUMMARY_ID") = "" Then
blnAllTestRunsExecuted = True
End If

If blnAllTestRunsExecuted Then
objTCGlobalParameter("TEST_RUN_STATE") = "finished"
Exit Do
Else
objTCGlobalParameter("TEST_RUN_STATE") = "started"
End If

'Initialize
blnAllTestCasesExecuted = False

'*** Open application and/or get version***
If objTestRun("TEST_RUN_TOOL") = "QuickTestPro" Then
objTCGlobalParameter("Version") = Trim(OpenApplication _
(objTCGlobalParameter("TEST_ENVIRONMENT_ALIAS"), "TestHost", "MyAUT", "Start"))
Else
objTCGlobalParameter("Version") = GetAUTVersion _
(objTCGlobalParameter("TEST_ENVIRONMENT_ALIAS"))
End If
objTCGlobalParameter("Release") = GetRelease (objTCGlobalParameter("Version"))

'Update summary with version
UpdateSummary objTCGlobalParameter("TEST_RUN_RESULT_SUMMARY_ID"), "TEST_RUN_VERSION", _
objTCGlobalParameter("Version")

If objTestRun("EMAIL_OPTION") = "True" Then
objTestRun("EMAIL_SUBJECT") = GetTestSuiteSummary (objTCGlobalParameter)
objTestRun("HTML_BODY") = GetTestCaseResultRowsHTML(objTCGlobalParameter)
SendEmail objTestRun
End If

'*** Execute Test Cases ***
Do Until blnAllTestCasesExecuted
'Check Global run status and stop test if requested
Set objTestRun = GetTestRunOptions (intTestRunOptionID)
If objTestRun("TEST_RUN") = "True" Then
'Continue
Else
If objTestRun("TEST_RUN_TOOL") = "QuickTestPro" Then
CloseAllBrowsers
End If
objTCGlobalParameter("TEST_RUN_STATE") = "stopped"
If objTestRun("EMAIL_OPTION") = "True" Then
objTestRun("EMAIL_SUBJECT") = GetTestSuiteSummary (objTCGlobalParameter)
objTestRun("HTML_BODY") = GetTestCaseResultRowsHTML(objTCGlobalParameter)
SendEmail objTestRun
End If
'Reset TestRun option
ResetTestRun intTestRunOptionID, "TestStop"
Exit Sub
End If
'Get and set test scenario data
Set objCurrentTestProperties = GetCurrentTestProperties _
(intTestRunResultID, objTCGlobalParameter("TEST_SUITE_ID"))
If objCurrentTestProperties("TEST_RUN_RESULT_ROW_ID") = "" Then
blnAllTestCasesExecuted = True
End If
If blnAllTestCasesExecuted Then
Exit Do
End If
'Get reference test data
Set objReferenceTestData = GetTestReferenceData _
(objCurrentTestProperties("TEST_RUN_RESULT_ROW_ID"))
'Get test case parameters
Set objTCParameter = GetTestCaseParameters _
(objCurrentTestProperties("TEST_CASE_ID"))

'Merge all parameters into one Dictionary
Set objTCParameter = MergeDictionaries (objTCParameter, objTCGlobalParameter)
Set objTCParameter = MergeDictionaries (objTestRun, objTCParameter)
Set objTCParameter = MergeDictionaries (objTCParameter, objCurrentTestProperties)
Set objTCParameter = MergeDictionaries (objTCParameter, objReferenceTestData)

'Clean up
Set objCurrentTestProperties = Nothing
Set objReferenceTestData = Nothing

'Report test case start
UpdateTestCaseStart objTCParameter
If objTCParameter("TEST_RUN_TOOL") = "QuickTestPro" Then
'Initalize report dictionary
Set dicMetaDescription = CreateObject("Scripting.Dictionary")
dicMetaDescription("Status") = micDone 'Default value
dicMetaDescription("PlainTextNodeName") = _
"Test Case#" & objTCParameter("TEST_SCENARIO_ID") & "." & objTCParameter("TEST_CASE_ID") & " - " & _
objTCParameter("TEST_CASE_DESCRIPTION")
dicMetaDescription("StepHtmlInfo") = objTCParameter("TEST_CASE_CATEGORY_NAME") & _
"<BR><BR>" & objTCParameter("TEST_CASE_DESCRIPTION")
'Some backdoor settings
dicMetaDescription("DllIconIndex") = 206
dicMetaDescription("DllIconSelIndex") = 206
dicMetaDescription("DllPAth") = _
"C:\Program Files\Mercury Interactive\QuickTest Professional\bin\ContextManager.dll"
intContext = Reporter.LogEvent("User", dicMetaDescription, Reporter.GetContext)
'Set test case node as a parent
Reporter.SetContext intContext
End If
'Log all input parameters
If objTCParameter("TEST_RUN_TOOL") = "QuickTestPro" Then
strTestCaseParameters = ""
allKeys = ArraySort_asc(allKeys)'Sort
For i = 0 to UBound(allKeys)
strTestCaseParameters = strTestCaseParameters & "<TR><TD><B>" & _
allKeys(i) & "</B></TD><TD> " & objTCParameter(allKeys(i)) & "</TD></TR>"
Next
dicMetaDescription("PlainTextNodeName") = "Input parameters"
dicMetaDescription("StepHtmlInfo") = "<DIV align=left><TABLE STYLE=""font-size: 8pt"">" & _
strTestCaseParameters & "</TABLE></DIV>"
Reporter.LogEvent "User", dicMetaDescription, Reporter.GetContext
End If

'**********************************************************
'****** Execute test case and store output in object ******
Set objTCData = ExecuteTestCase (objTCParameter)'**********
'**********************************************************
'**********************************************************

'Save output values
objTCData("TEST_RUN_RESULT_ROW_ID") = objTCParameter("TEST_RUN_RESULT_ROW_ID")
UpdateTestCaseOutParameters objTCData

'Log all output parameters and test result
If objTCParameter("TEST_RUN_TOOL") = "QuickTestPro" Then
allKeysOut = objTCData.Keys 'Get all the keys into an array
allValuesOut = objTCData.Items 'Get all the items into an array
strTestCaseParameters = ""
allKeysOut = ArraySort_asc(allKeysOut)'Sort
For i = 0 to UBound(allKeysOut)
If allValuesOut(i) <> "" Then'Log parameters with values only
strTestCaseParameters = strTestCaseParameters & "<TR><TD><B>" & allKeysOut(i) & _
"</B></TD><TD> " & objTCData.Item(allKeysOut(i)) & "</TD></TR>"
End If
Next
'Set test case result
Select Case objTCData("TEST_CASE_RESULT")
Case "Passed"
dicMetaDescription("Status") = micPass
Case "Failed"
dicMetaDescription("Status") = micFail
Case "Warning"
dicMetaDescription("Status") = micWarning
Case Else
dicMetaDescription("Status") = micDone
End Select
dicMetaDescription("PlainTextNodeName") = "Output parameters"
dicMetaDescription("StepHtmlInfo") = "<DIV align=left><TABLE STYLE=""font-size: 8pt"">" & _
strTestCaseParameters & "</TABLE></DIV>"
Reporter.LogEvent "User", dicMetaDescription, Reporter.GetContext
End If

'Merge data
Set objTCParameter = MergeDictionaries (objTCParameter, objTCData)

'Report test case result
UpdateTestCaseStop objTCParameter
StoreTestCaseResult objTCParameter

'In case of failure
If objTCParameter("TEST_CASE_RESULT") = "Failed" Then
'Set UI type
If objTCParameter("TEST_CASE_GROUP_ID") = "5" Then
objTCParameter("TEST_INTERFACE") = "API"
End If
Set objError = CreateErrorFiles (objTCParameter)
Set objTCParameter = MergeDictionaries (objTCParameter, objError)
If objTestRun("EMAIL_OPTION") = "True" Then
objTestRun("EMAIL_SUBJECT") = "Test case ID:" & objTCParameter("TEST_CASE_ID") & " - " & _
objTCParameter("TEST_CASE_DESCRIPTION") & " failed"
objTestRun("HTML_BODY") = GetTestCaseErrorHTML(objTCParameter)
SendEmail objTestRun
End If
ElseIf objTCParameter("TEST_CASE_RESULT") = "Warning" Then
If objTestRun("TEST_RUN_TOOL") = "QuickTestPro" Then CloseAllBrowsers
objTCGlobalParameter("TEST_RUN_STATE") = "stopped due to Warning"
If objTestRun("EMAIL_OPTION") = "True" Then
objTestRun("EMAIL_SUBJECT") = GetTestSuiteSummary (objTCGlobalParameter)
objTestRun("HTML_BODY") = GetTestCaseResultRowsHTML(objTCGlobalParameter)
SendEmail objTestRun
End If
'ExitTest
Exit Sub
End If

'Reset QTP report node
If objTestRun("TEST_RUN_TOOL") = "QuickTestPro" Then Reporter.UnSetContext

'Reset test data
Set objTCData = Nothing

'Reset test case data
Set objTCParameter = Nothing

Loop 'Get next test case

'Close all browsers
If objTestRun("TEST_RUN_TOOL") = "QuickTestPro" Then
CloseAllBrowsers
End If

objTCGlobalParameter("TEST_RUN_STATE") = "finished"
If objTestRun("EMAIL_OPTION") = "True" Then
objTestRun("EMAIL_SUBJECT") = GetTestSuiteSummary (objTCGlobalParameter)
objTestRun("HTML_BODY") = GetTestCaseResultRowsHTML (objTCGlobalParameter)
SendEmail objTestRun
End If

'Update Test summary
UpdateSummary objTCGlobalParameter("TEST_RUN_RESULT_SUMMARY_ID"), "TEST_RUN_STATUS", "Complete"
UpdateSummary objTCGlobalParameter("TEST_RUN_RESULT_SUMMARY_ID"), "TEST_RUN_STOP", Cstr(Now())

'Clean
Set objTCGlobalParameter = Nothing

Loop 'Get next test run

'Reset TestRun option
ResetTestRun intTestRunOptionID, "TestDone"

End Sub

'***********
'*** END ***
'***********

2 comments:

Stefan Thelenius said...

From mail:

"When I run TestSuiteDriver.vbs I got this error ('Type mismatch...'). Stefan I would like to use your information but I do not know how.

If you have a time please help me with right direction.

Thank you Valeria"

Hi,

The driver script calls a lot of functions which is not included in the download files. The reason for that is that they may contain sensitive data and I have not made deidentified copies of them (yet). I published my Driver script so that people could get some ideas how to implement test driver logic.

/Stefan

Software quality assessment said...

Thanks for sharing the whole code. But if you could also share some explanation with the code that will make the script more easy to understand.