Forum Discussion

johny2010_1's avatar
johny2010_1
Contributor
16 years ago

How tu run scripts properly ? Help for beginner

I am new to TestComplete and I want to make tests with scripts.

I found this piece of code from a web page but it doesn't work.



Anybody give me idea ? It gives double usage error when I want to run, but i couldn't find where the problems occurs



Here is the address of the web page I take the code and and the code itself. Please somebody help..



http://www.bughuntress.com/portfolio/automated-testing/test-complete.html



Sub SingleUserMethod

    Dim HTTPTask

    Dim Users(3)  'The array of virtual users

    Dim Indx


    'The name of the server where the Web application is installed

    TargetServer = "http://www.thetistech.com"

 

    'This variable will contain a task

        'TaskName = LoadTesting.HTTPTaskName(1)

    Set HTTPTask = LoadTesting.HTTPTask("ThetisTask1")


    'Change the name of the target server

    HTTPTask.TargetServer = "http://www.thetistech.com"


    'Create three virtual users

    For Indx = 0 To 2

        'Create a virtual user

        Set Users(Indx) = LoadTesting.CreateVirtualUser("VirtualUserName" + CStr(Indx + 1))

        'Specify a task

        Users(Indx).Task = HTTPTask

        'Execute the task of the current virtual user

        Users(Indx).Run("VirtualUser"+CStr(Indx + 1))

        Sys.Delay(10)

    Next

    'To view the results of the script execution, see the statistics at

    'http://localhost/loadtesting/LoadTestingDemo.dll?action=report

    'The results contain statistics of three virtual users.

End Sub


Sub UserGroupMethod

    Dim HTTPTask

    Dim Users (3)

    Dim UserGroupVar, Indx


    'This variable will contain a task

    Set HTTPTask = LoadTesting.HTTPTask("ThetisTask1")


    'Change the name of the target server

    HTTPTask.TargetServer = "http://www.thetistech.com/"


    'Create a virtual user group 

    Set UserGroupVar = LoadTesting.CreateVirtualUserGroup("httpUsers")


    'Create 3 virtual users that will belong to a given group

    For Indx = 0 To 2

        'Create a virtual user

        Set Users(Indx) = LoadTesting.CreateVirtualUser("VirtualUserName" + CStr(Indx + 1))

        'Specify a task

        Users(Indx).Task = HTTPTask

        'Specify a group

        Users(Indx).Group = UserGroupVar

    Next

    'Execute the tasks of all virtual users that belong to the given group

    UserGroupVar.Run("UserGroup")

    'The results contain statistics of three virtual users.    

End Sub


Sub ChangeContentDisposition

    Dim HTTPTask, VirtualUser, requestPhoto, requestAddress, i, Cnt, CntItem


    ' Create a virtual user that will use the PostMethod task

    Set HTTPTask = LoadTesting.HTTPTask("PostMethod")

    Set VirtualUser = LoadTesting.CreateVirtualUser("VirtualUser1")

    VirtualUser.Task = HTTPTask


    ' Obtain requests that contain the photo and address variables

    Set requestPhoto = VirtualUser.Task.Connection(0).Request(3)

    Set requestAddress = VirtualUser.Task.Connection(0).Request(4)


    ' Checks if the request holds the ContentDisposition structure

    i = InStr(requestPhoto.RequestHeader, "multipart/form-data")

    If i > 0  Then

        ' Sends the values of all variables transmitted in the current request to the Log

        Set Cnt = requestPhoto.ContentDisposition

        Log.CreateNode("requestPhoto")

        For i = 0 To Cnt.Count - 1

            Log.Message Cnt.Item(i).Header,_

                    Cnt.Item(i).DispositionType&Chr(13)&chr(10)&Cnt.Item(i).Data

        Next

        Log.CloseNode


        ' Modifies the photo field by loading a new image from a file

        Set CntItem = Cnt.Item("photo")

        CntItem.LoadFromFile(Options.Project.Directory&_

                                    "..\..\..\..\Load Testing\images\img0.jpg")

     End If


    i =InStr(requestAddress.RequestHeader, "multipart/form-data")

    If i > 0 Then

        ' Sends the values of all variables transmitted in the current request to the Log

        Set Cnt = requestAddress.ContentDisposition

        Log.CreateNode("requestAddress")

        For i = 0 To Cnt.Count - 1

            Log.Message Cnt.Item(i).Header,_

                    Cnt.Item(i).DispositionType&Chr(13)&chr(10)&Cnt.Item(i).Data

        Next

        Log.CloseNode


        ' Modifies the address field

        Set CntItem = Cnt.Item("address")

        CntItem.Data = "NewAddress"&VarToStr(i)

    End If


    ' Runs the task

    VirtualUser.Run("ChangeContentDisposition")

    ' To view the results of the script execution, see the statistics at

    ' http://localhost/loadtesting/ltorders.dll?action=report

    ' The results should contain new values for the address and photo fields

End Sub


Sub ChangeRequestVariables

    Dim VirtualUsers(3), VirtualUserGroup, i, HTTPTask, request


    ' Creates a group that consists of three virtual users that use the GetMethod task

    Set VirtualUserGroup = LoadTesting.CreateVirtualUserGroup("UserGroup1")


    For i = 0 To 2

        ' Creates a virtual user and specifies a task and a group for it

        Set HTTPTask = LoadTesting.HTTPTask("GetMethod")

        Set VirtualUsers(i) = LoadTesting.CreateVirtualUser("VirtualUser"&CStr(i+1))

        VirtualUsers(i).Task = HTTPTask

        VirtualUsers(i).Group = VirtualUserGroup


        ' Modifies the "address" variable in the request

        Set request = HTTPTask.Connection(0).Request(5)

        request.Variables.Item("address") = "New+Address+"&CStr(i)

    Next


    ' Runs the task

    VirtualUserGroup.Run("ChangeRequestVariables")

    ' To view the results of the script execution, see the statistics at

    ' http://localhost/loadtesting/ltorders.dll?action=report

    ' The results must contain each user's address

End Sub


Sub OnRequestEvent

    Dim HTTPTask, VirtualUser, request


    ' Creates a virtual user that will use the GetMethod task

    Set HTTPTask = LoadTesting.HTTPTask("GetMethod")

    Set VirtualUser = LoadTesting.CreateVirtualUser("VirtualUser1")

    VirtualUser.Task = HTTPTask


    ' Set the RequestEventTrigger property to true for a specific request

    ' to enable TestComplete to generate the OnLoadTestingRequest event

    Set request = VirtualUser.Task.Connection(0).Request(5)

    request.RequestEventTrigger = True


    ' Runs the task

    VirtualUser.Run("OnRequestEvent")

    ' To view the results of the script execution, see the statistics at

    ' http://localhost/loadtesting/ltorders.dll?action=report

    ' The address field should be changed in the results

End Sub


Sub ProjectEvents1_OnLoadTestingRequest(Sender, User, Request)

    ' Checks the request ID of the incoming event

    If Request.RequestID = 6 Then

        ' Changes the variable value

        Log.Message "Old Value of Address", Request.Variables.Item("address")

        Request.Variables.Item("address") = "NewAddress"

    End If

End Sub


'Executes tasks on several 

'work stations simultaneously using the Remote Agent Service (RAS). 


'The RAS function creates a group that consists of six virtual users. Each 

'virtual user gets a task, name, group and work station on which it will "work".

'Permissions to process events are set in this function as well. 


'The ProjectEvents1_OnLoadTestingRequest method processes events that are

'generated when the LoadTesting plug-in is functioning.


Sub RAS

    Dim HTTPTask, TargetServe, Users(6), UserGroupVar, Station1, Station2, i


    TargetServer = "http://www.thetistech.com"   


    'This variable will contain a task

    Set HTTPTask = LoadTesting.HTTPTask("ThetisTask2")


    'Change the name of the target server

    HTTPTask.TargetServer = TargetServer 


    'Create a virtual user group 

    Set UserGroupVar = LoadTesting.CreateVirtualUserGroup("httpUsers")


    'Get workstations

    Set Station1 = LoadTesting.Stations.ItemByAlias("Master")

    'In the HTTP Load Testing panel specify the name of the work station for 

    'the Slave alias. 

    Set Station2 = LoadTesting.Stations.ItemByAlias("Slave")


    'Create 6 virtual users that will belong to a given group

    For i = 0 To 5 

        'Create a virtual user

        Set Users(i) = LoadTesting.CreateVirtualUser("VirtualUserName"&CStr(i + 1))


        'Specify a task

        Users(i).Task = HTTPTask

        

        'Specify a group

        Users(i).Group = UserGroupVar



        'Assign users to work stations

        If i < 3 Then

            Users(i).Station = Station1

        Else

            Users(i).Station = Station2

        End If



        'Set the RequestEventTrigger property to True for a specific request

        'to enable to generate the OnLoadTestingRequest event

        Users(i).Task.Connection(0).Request(0).RequestEventTrigger  = True

        Users(i).Task.Connection(10).Request(0).RequestEventTrigger = True

    Next



    'Execute the tasks of all virtual users listed in the given group

    UserGroupVar.Run("UserGroup")

End Sub



'The OnLoadTestingRequest event handler

Sub ProjectEvents1_OnLoadTestingRequest(Sender, User, Request)

    'The virtual user starts working. Information about this is posted to the log

    If Request.RequestID = 1 Then

        Log.Message User.Name&" started working...", User.Station.Name

    End If



    'The virtual user stops working. Information about this is posted to the log

    If Request.RequestID = 21 Then

        Log.Message User.Name&" stopped working...", User.Station.Name

    End If

End Sub

1 Reply