Creating and Passing Parameters Within a Script

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 script.
    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.
  2. Record or type the script that tests the application. 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
  3. Create a second Main() sub to include the parameters that you want to create and pass to child scripts. For example, the following 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"           
    
  4. Include the Main(args) command that calls the first Main() sub script and specifies the name for the parameters that you created. For example, the following code passes a string for the first name and last name in the test application, and calls 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"
                
        Main(args)
    
      End Sub
    End Module
    
  5. Modify the Sub Main() that tests the application 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 looks like the following:
    Public Sub Main(args As IDictionary(Of String, Object))
          
  6. 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"))
    The script looks like the following:
    Imports SilkTest.Ntf.XBrowser
    Public Module Main
      Dim _desktop As Desktop = Agent.Desktop
    
      Public Sub Main(args As IDictionary(Of String, Object))
      With _desktop.BrowserWindow("/BrowserApplication[1]//BrowserWindow")
        .DomButton("@id='login-form:signup'").Select()
        .DomTextField("@id='signup:fname'").SetText(args("FName"))
        .DomTextField("@id='signup:lname'").SetText(args("LName"))
        .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
    
      Public Sub Main()
        Dim args As New Dictionary(Of String, Object)
        args("FName") = "Chris"
        args("LName") = "Smith"
           
        Main(args)
    
      End Sub
    
    End Module
  7. Optional: To include a message box that returns the parameters that the script enters, add the following code:
    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.

    The script looks like the following:

    Imports SilkTest.Ntf.XBrowser
    Public Module Main
      Dim _desktop As Desktop = Agent.Desktop
    
      Public Sub Main(args As IDictionary(Of String, Object))
      With _desktop.BrowserWindow("/BrowserApplication[1]//BrowserWindow")
        .DomButton("@id='login-form:signup'").Select()
        .DomTextField("@id='signup:fname'").SetText(args("FName"))
        .DomTextField("@id='signup:lname'").SetText(args("LName"))
        .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
    
      Public Sub Main()
        Dim args As New Dictionary(Of String, Object)
        args("FName") = "Chris"
        args("LName") = "Smith"
           
        Main(args)
    
        MsgBox("Hello, " + args("FName") + "!")
        MsgBox ("Hello, " + args("FName") + " " +  args("LName") + "!")
    
      End Sub
    
    End Module
  8. Click Playback.