DataDriven Sample

Description
This sample uses Phantom to automate data input into an application using data from an external file. The external file is read by Phantom, and the file parsed into data fields. These data fields are entered into the PhantomTarget application's Static control dialog, and the output is verified. The script cycles through the entire data file, and issues a warning if a mismatch occurs. This is a simple sample that uses a simple data format. More complex entries can be created and parsed using multiple calls to the split function or other built-in Phantom string manipulation functions.

Note that this sample requires an external data.txt file, and a modified PhantomTarget.dec declaration file (a copy of both can be downloaded via the link below).

This script illustrates the use of the following Phantom features:

- Data driven automation
- String manipulation
- User defined functions
- Exceptions
- Script flow control
- Window Declaration file usage
- File interaction

To use this sample, download the zipped file below and unzip all three files into the same directory.

[ Download Zipped datadriven.dec, data.txt, and DataDriven.psc Files ]

[ Back to Samples ]

DataDriven
# This sample script illustrates how to use Phantom to perform
# automation of data input using a data file.  The script enters a
# series of strings from an external file into the Static control
# window of the PhantomTarget application.
#
# The script requires an external text file with entries separated
# by a ':' character.  Additionally, the script uses the external
# 'datadriven.dec' file, recorded with the WinDR.exe utility.  Note
# that the .dec file entries 'PhantomTarget' and 'Static.Output'
# names were changed from the originally recorded names to be
# more meaningful.  Additionally, the Static.Output tag was changed
# to a numeric tag (5) since the text of this item will be changing.
# See the help file on Window Declarations for more information on
# numeric tags.
#
# This sample illustrates some simple string manipulation, file
# usage, arrays, exceptions, and window declaration usage.
#
# Note: The datadriven.dec and data.txt files must be in the same
# directory as this script.
#####################################################################

# Include the pre-recorded window declarations
use "datadriven.dec";

# Start the PhantomTarget application.  This path may be different
# if Phantom was installed in a different directory.
System("c:\\Program Files\\Phantom\\PhantomTarget.exe");

# Define the data file.  This assumes that the data file is in the
# same directory as the script.
string FileName = "data.txt";

# Define the tag that separates data entries in the data file
string DataSep = ":";

# Declare and define a function that will be called to enter the
# data in to the Static controls box and verify the output changes
# correctly.
function bool EnterData(string Input){
  # Invoke the Static controls window
  PhantomTarget._Controls.Stat_icText.Select();  
  # Set the text in the edit field
  Static.Edit1.SetText(Input);
  # Click the 'Set' button
  Static._Set.Click();
  # Get the value in the output static field
  string Check = Static.Output.GetText();
  # Close the dialog
  Static.OK.Click();
  # Check if the output matches the input by checking string equality
  if(Check != Input){
    # Return false if the input does not match the output
    return false;
  }else{
    # Return true if the input matches the output
    return true;
  }
}

# Open the data file
file FH = OpenFile(FileName, "r");
# Create a variable to hold each line of the file
string sLine;

# Cycle through each line of the file
while(ReadFile(FH, sLine)){
  # Use the built-in split function to separate the line into an array
  # of strings that represents the data.
  string Data = split(sLine,DataSep);
  # Cycle through the array of data
  for(int i = 0; i < length(Data); i++){
    # Call the previously defined function to enter the data into the
    # Static control window and check the output.
    if(!EnterData(Data[i])){
      # If the output did not match the input, something was wrong, so
      # throw create and throw a warning.
      exception e;
      e.SetWarning("Data Mismatch: " + Data[i]);
      e.throw();
    }
  }
}

# Close out PhantomTarget
PhantomTarget._Options.E_xit.Select();


Sample data.txt Input File
Welcome:to:Phantom!
Hello:World!
This sample shows:Data Driven:testcases.

Copyright © 2000-2009 Phantom Automated Solutions, Inc.
[ Home ] [ Contact ] [ Privacy Policy ] [System Requirements]