Including ActiveData in a Script

Before you begin, create the ActiveData asset that contains the data that you want to use or that you want to write data to.

Map the data from the ActiveData asset to the script that you want to use. Additionally, you can add rows or columns to the ActiveData asset, and add data to the ActiveData asset from the script.

  1. Record or manually script the actions that you want to test. For more information, see Recording a Script. For example, a script that records a first name, last name, and phone number in a Web application might look like the following:
    Public Sub Main()
      With _desktop.BrowserWindow("/BrowserApplication//BrowserWindow")
        .DomTextField("@id='txtFirstName'").SetText("Pat")
        .DomTextField("@id='txtLastName'").SetText("Smith")
        .DomTextField("@id='txtPhoneNumber'").SetText("555-121-3434")
        .DomButton("@id='btnAdd'").Select()
      End With
  2. Use the LoadActiveData command to specify the ActiveData asset name. Optionally, you can specify a start and end row in the ActiveData file, random rows to use, and whether data is read only.
    Workbench.LoadActiveData("activeDataName", [start row,
       end row, randomcount, readonly])
  3. To replace the text that you recorded with text from the ActiveData asset, create a declaration for each corresponding row name in the ActiveData file and then map the declaration to the appropriate code in the script. For example, to modify the script from the first step to use all the data available in the PhoneBookData ActiveData asset, you might change the script to look like the following:
    Public Sub Main()
      AddAllToPhoneBook()			'Reads all records in order
    End Sub
    
    Public Sub AddAllToPhoneBook()
      Dim data As ActiveData = Workbench.LoadActiveData( "PhoneBookData" )
      Dim row As ActiveDataRow
    		
      With _desktop.BrowserWindow("/BrowserApplication//BrowserWindow")
    			
        For Each row In data
    				
        Dim FirstName As String = row.GetString("fname")
        Dim LastName As String = row.GetString("lname")
        Dim PhoneNumber As String = row.GetString("phonenumber")
    
        .DomTextField("@id='txtFirstName'").SetText(FirstName)
        .DomTextField("@id='txtLastName'").SetText(LastName)
        .DomTextField("@id='txtPhoneNumber'").SetText(PhoneNumber)
    
        .DomButton("@id='btnAdd'").Select()
        Next
      End With
    End Sub
    When you playback the script, all the declared data from the ActiveData asset is included in the script.
  4. To add an additional column to the existing ActiveData asset, declare the ActiveData asset, and specify the ActiveData asset name and the column name. For example, to add a new column called "Occupation" to the PhoneBookData asset, type the following:
    Public Sub Main()
    
      AddColumnToDataFile( "occupation" )
    End Sub
    
    Public Sub AddColumnToDataFile( name As String )
      Dim data As ActiveData = Workbench.LoadActiveData( "PhoneBookData" )
      data.AddColumn( "Occupation", name )
      data.Save()
    End Sub
    End Module
    When you playback the script, the new column is added to the file specified by the ActiveData asset.
  5. To add an additional row to the existing ActiveData asset, declare the ActiveData asset, and specify the ActiveData asset name and the row name. For example, to add a new row of data for ID number, type the following:
    Public Sub Main()
    
      InsertNewNumberToDataFileWithOccupation( 108, "Jay", "Jones", "5551219", _ 
        "QA Analyst" )
    
    End Sub
    
    Public Sub InsertNewNumberToDataFileWithOccupation( id As Integer, _ 
        firstName As String, lastName As String, phoneNumber As String, _ 
        occupation As String )
      Dim data As ActiveData = Workbench.LoadActiveData( "PhoneBookData" )
      Dim row As ActiveDataRow = data.AddRow()
    		
      row.SetLong( "id", id )
      row.SetString( "fname", firstName )
      row.SetString( "lname", lastName )
      row.SetString( "number", phoneNumber )
      row.SetString( "occupation", occupation )
    		
      data.Save()
    		
    End Sub
    End Module
    When you playback the script, the new row is added to the file specified by the ActiveData asset.
  6. To add the value of a variable into a specific cell, you can add the following code sample to your script:
    Dim row As ActiveDataRow = data.Item(1)
    row.SetString("Columnname" , Variable)