Custom Test Types Interfaces

Remote Agent Example


Option Explicit
'These variables are required by the interface:

Private Status As String ' The current testing status
Private Descr As String ' The current testing description
' Connection Parameters
Private ServerName As String ' The server URL
Private ProjectName As String ' The current project’s name
Private DomainName As String ' The Quality Center domain name
Private UserName As String ' The current user name
Private SysUserName As String ' The workstation system user
Private Password As String ' The current user’s password
' Current Test
Private TestPath As String ' The current test’s path
Private TestName As String ' The current test’s name
Private TestID As Integer ' The current test’s ID
' Current Test Set
Private TestSetID As String ' The current test set’s ID
Private TestSetInfo As String ' The current test set’s info
' Current Test Instance
Private TestInstID As Long ' The test instance's ID
Private TestInstName As String ' The test instance's name
Private TestInstDescr As String ' The test instance's descriptor

Private S_OK As Long ' Return value if the run is OK
Private S_FALSE As Long ' Return value if the run failed
Private END_OF_TEST As Long ' Return value for end of test

Private LogFileDir As String
Private LogFilePath As String

'
'*******************************************************************
Private Sub Class_Initialize()

    ' Initialization method of some class members.
    S_OK = 0
    S_FALSE = 1
    END_OF_TEST = 4
    LogFileDir = "C:\Temp"
    LogFilePath = LogFileDir & "\MyOTARunAgent.log"

    Log "Initialize " & CStr(Now)

End Sub

Public Function is_host_ready(Descr As String) As Long

' Quality Center calls this method to check if the
' host is ready.
' You can use this sub-routine to initialize the return value variables.
' In this example, is_host_ready always reports that
' the host is ready, and changes the description to
' "Ready"

    Descr = "Ready"

    is_host_ready = S_OK

End Function

Public Function set_value(ByVal name As String, _
    ByVal Value As String) As Long

    'Quality Center uses this method to set the
    'variables declared.

    Select Case name
    ' Connection Parameters
    Case "TDAPI_host_name"
        ServerName = Value
    Case "project_name"
        ProjectName = Value
    Case "domain_name"
        DomainName = Value
    Case "user_name"
        UserName = Value
    Case "sys_user_name"
        SysUserName = Value
    Case "password"
        Password = Value
    ' Current Test
    Case "test_path"
        TestPath = Value
    Case "test_name"
        TestName = Value
    Case "test_id"
        TestID = Val(Value)
    ' Current Test Set
    Case "test_set_id"
        TestSetID = Val(Value)
    Case "test_set"
        TestSetInfo = Value
    ' Current Test Instance
    Case "testcycle_id_integer"
        TestInstID = Val(Value)
    Case "tstest_name"
        TestInstName = Value
    Case "tstest_name"
        TestInstDescr = Value
    End Select

    set_value = S_OK

    On Error Resume Next

         Log "set_value: " _
             & " name = " & name & ", value = " & Value

End Function

Public Function get_status(StatusDescription As String, _
    CurrentStatus As String) As Long

    ' Quality Center calls this method to check the
    ' status and description of the run.

    StatusDescription = Descr

    CurrentStatus = Status

    get_status = S_OK

End Function

Public Function run() As Long

' Quality Center calls this method to run the test
' and update the test status and description.
' In this example, batch.bat is downloaded
' and run.

    On Error GoTo runErr

    Log "Start run " & CStr(Now)

    Dim td As New TDAPIOLELib.TDConnection
    Dim TestSetFact As TestSetFactory
    Dim thisTestSet As TestSet
    Dim TSTestFact As TSTestFactory
    Dim thisTSTest As TSTest
    Dim RunFact As RunFactory
    Dim theRun As TDAPIOLELib.run

    'These variables are needed to get
    ' batch.bat for this example:
    Dim TestFact As TDAPIOLELib.TestFactory
    Dim theTest As TDAPIOLELib.Test
    Dim LocalScriptPath As String
    Dim ExtStorage As TDAPIOLELib.ExtendedStorage

'Connect to the project
    td.InitConnectionEx ServerName
    td.ConnectProjectEx DomainName, ProjectName, UserName, Password

'Get a local copy of batch.bat

    Set TestSetFact = td.TestSetFactory
    Set thisTestSet = TestSetFact(TestSetID)
    Set TSTestFact = thisTestSet.TSTestFactory

    Log "TSTestFactory acquired"

    'Get the TSTest object
    Set thisTSTest = TSTestFact(TestInstID)

    Log "TSTest ID = " & thisTSTest.ID

    'Get the correct version of the test from the TSTest
    Set theTest = thisTSTest.Test

    Log "The test name = " & theTest.name

    ' Get the file
    Set ExtStorage = theTest.ExtendedStorage
    LocalScriptPath = ExtStorage.Load("-r batch.bat", True)

    ' Add the file name to the path returned from Load
    ' so it directly refers to the file.
    LocalScriptPath = LocalScriptPath & "\batch.bat"
    Log "LocalScriptPath = " & LocalScriptPath

    ' Update the status variables for use in get_status
    Status = "RUNNING"
    Descr = "Running..."

    Log "Running..."

    ' Run the application and update the status
    Dim rc
    rc = Shell(LocalScriptPath)
    Status = "END_OF_TEST"
    Descr = "Completed"

    Log "Command executed"

    ' Record the run in the project
    ' Get the run factory for the TSTest
    Set RunFact = thisTSTest.RunFactory

    Log "RunFactory acquired"

    'Test run name (ID)
    Dim runName As String
    runName = "New Run"

    ' Create the new Run
    Set theRun = RunFact.AddItem(runName)

    'User who ran the test (from set_value)
    theRun("RN_TESTER_NAME") = UserName

    Log "Created new Run"

    ' Mark the run Passed
    theRun.Status = "Passed"
    theRun.Post

    Log "Run posted"

    run = S_OK

Exit Function

runErr:

Dim msg$

    On Error Resume Next

    msg = "Error in run: " & Err.Description & "; " & Err.Source

    Log msg

    On Error GoTo runErr

    Resume Next

End Function

Private Sub Log(msg As String)

    If Dir(LogFileDir, vbDirectory) = "" Then
        MkDir LogFileDir
    End If

    Dim FN As Long
    FN = FreeFile
    Open LogFilePath For Append As FN
    Print #FN, msg & vbCrLf
    Close #FN

End Sub


© 1992 - 2012 Hewlett-Packard Development Company, L.P.