Creating and Passing Parameters Between Scripts

You can create and pass parameters between a parent and child script or using a single script. The advantage of using a parent and child scenario is that multiple child scripts can use the parent script. For example, if the parent script creates a random number, you might want to use that functionality in several child scripts.

  1. Create a parent script that includes the parameters that you want to pass.
    1. Choose File > New.
      The New Asset dialog box opens.
    2. Select .NET Script from the asset types list, and then type a name for the script in the Asset name text box.

      For example, you might name the script "NameParameters."

    3. Click OK to save the script. Silk Test Workbench saves the script as an asset and displays the script template.
    4. Modify the Main() sub to include the parameters that you want to create and pass to child scripts. For example, the following parent script passes a string for the first name and last name in the test application.
      Public Sub Main()
                  Dim args As New Dictionary(Of String, Object)
                  args("FName") = "Chris"
                  args("LName") = "Smith"           
      
    5. Include the RunScript command that calls the child script and specifies the name for the parameters that you created. For example, the following parent script passes a string for the first name and last name in the test application, and calls the child script named childscript with the parameter called args.
      Public Module Main
            Dim _desktop As Desktop = Agent.Desktop
      
            Public Sub Main()
                  Dim args As New Dictionary(Of String, Object)
                  args("FName") = "Chris"
                  args("LName") = "Smith"
                  
                  Workbench.RunScript("childscript", args)
      
            End Sub
      End Module
      
  2. Optional: To include a message box that returns the parameters that the script enters, add the following code to the parent script:
    MsgBox("Hello, " + args("FName") + "!")
    MsgBox ("Hello, " + args("FName") + " " +  args("LName") + "!")
    
    A message box opens when the script plays back and shows the values that the test enters.
  3. Create a child script that calls the parameters from the parent script. For example, to work with the first name and last name in the sample Web application Sign Up form, record a script for the Sign Up form that includes a first name and last name. The script might look like the following example:
    Imports SilkTest.Ntf.XBrowser
    Public Module Main
    	Dim _desktop As Desktop = Agent.Desktop
    
    	Public Sub Main()
    		With _desktop.BrowserWindow("/BrowserApplication[1]//BrowserWindow")
    			.DomButton("@id='login-form:signup'").Select()
    			.DomTextField("@id='signup:fname'").SetText("first")
    			.DomTextField("@id='signup:lname'").SetText("last")
    	  .DomElement("@src='http://extjs.com/s.gif'").DomClick(MouseButton.Left, 
         New Point(8, 16))
    			.DomButton("@textContents='Today'").Select()
    			.DomTextField("@id='signup:email'").SetText("test@test1.com")
    			.DomTextField("@id='signup:street'").SetText("123 street rd")
    			.DomTextField("@id='signup:city'").SetText("Marlton")
    			.DomListBox("@id='signup:state'").Select("Massachusetts")
    			.DomTextField("@id='signup:zip'").SetText("09876")
    			.DomTextField("@id='signup:password'").SetText("test")
    			.DomButton("@id='signup:signup'").Select()
    			.DomButton("@id='signup:continue'").Select()
    		End With
    		
    	End Sub
    End Module
  4. Modify the Sub Main() to take one parameter that is an IDictionary(Of String, Object) that you want to set and pass to parent scripts. The IDictionary class is provided by .NET. You can review the documentation for the class at http://msdn.microsoft.com/en-us/library/s4ys34ea.aspx.
    The revised Sub Main () code will look like the following:
    Public Sub Main(args As IDictionary(Of String, Object))
  5. Modify the controls that you want to use parameters.
    For example, to modify the first and last name in the Web application recording, replace the following code:
    .DomTextField("@id='signup:fname'").SetText("first")
    .DomTextField("@id='signup:lname'").SetText("last")
    with:
    .DomTextField("@id='signup:fname'").SetText(args("FName"))
    .DomTextField("@id='signup:lname'").SetText(args("LName"))
  6. From the parent script, click Playback.
    Note: If you run a script with parameters from the child script, it fails since no parameters are specified. However, you can add a second Main() sub with no parameters, and pass in default parameter values to the other Main() to run from a single script directly.