Adding Parameters for Flexibility: Using Property Files as a Source for Arguments


This lesson focused on using parent parameters for test arguments, but another possibility is using a Java property file. A property file is simply a text file containing a list of variables and their assigned values. You can create a single property file that holds as many test arguments as you want, then reference them using the Property Name option in the Edit Node window.

This exercise shows you how to modify your existing test to read the field arguments for the database from a property file. The remaining lessons in this tutorial use the version of the test you just finished, so be aware that you'll need to switch back to the previous version when you're done. You can copy the files from the tutorial/modular directory or create your own backup.

Reading a Single Property

If you want to read a single property or single set of properties (where a set is one value per property for a series of property names) you can do this easily by setting up your script to accept arguments and editing the JST node for that script to accept properties as parameters.

In the case of the acceptance test example, you might want to modify your test so that the script that enters a record (EnterFieldData) gets the data from a property file, instead of by inheriting parent parameters. That way, you wouldn't need to enter the field values each time you run the test--you would only need to change the property file when you wanted to use different data.

Part of the work of modifying the acceptance test is already done, because earlier in this chapter you edited EnterFieldData to accept arguments. You had also edited the EnterFieldData node of AddRecord.jst to read parent parameters. Now, you only need to create a property file and modify that node in the JST to read properties instead of parent parameters.

Creating a Property File

A property file is a text file that uses the format:

For this example, you'll create a property file, that uses the format:

  1. Create a text file that contains the text:

  2. Save the file (in the tutorial directory) as FieldData.prop.

Editing a Node to Read Properties

  1. In the Test Composer, load AddRecord.jst.

  2. Select the EnterFieldData node and click the Edit button.
    Because you edited the EnterFieldData node earlier in this chapter, it will contain a list of parent parameters, $0 through $6, one for each field of a record.

  3. Click on the first parameter ($0) in the list.

  4. In the pulldown menu to the right of the Arg # field, change Parent parameter to Property name.
    Note that the Arg # field changes to the Property name field.

  5. In the Property name field, change 0 to name and click the Update button.

  6. Repeat this process for the remaining parameters.
    Change each parameter from Parent parameter to Property name. Edit the arguemnts using the data:

    For Arg # Set property name to
    1 address1
    2 address2
    3 telephone
    4 email
    5 other

  7. Update the contents of the Comments field.
    Select and delete the existing text. Replace it with:

  8. Click Apply and click Close.

  9. In the Test Composer, click Save.

Your next step would be to edit the remaining parameterized nodes to read from the property file, but for the purposes of simplicity, this exercise focuses soley on the EnterFieldData node.

Specifying the Property File at Run Test Time

The Run Test dialog contains a field named Property File. When you run the acceptance test, be sure to enter FieldData.prop as the property file name. From the command line, use the -prop option.

You still need to pass the same test arguments described in the section, "Running a Test With Arguments." This is because. for the sake of brevity, this exercise modifies only EnterFieldData to use properties. DefineSearch, GetSearchResults, and VerifyRecord still use parent parameters. If this were a real test case, rather than an example, you would modify these three tests to use properties. Once you had done this, you would only need to pass the name of the test database for OpenFile.

Reading Multiple Properties

If, instead of reading a single property, you want to read a series of property values (for the same property name) you can do this by writing Java code that uses the JS.getProperty() method to retrieve values from the file.

In the case of the acceptance test, this would be useful if you wanted to populate the test database with multiple records, instead of using the property file to get data for just one record.

You can modify the tutorial example by:

Creating a Property File with Sets of Data

Create a property file (or, if you already have a file named FieldData.prop, edit the existing file) to use the format:

where you increment <#> for each set of records.

  1. Create a text file that contains the text:
    Where you replace "..." with five addition records (following the numbering format) containing your own data.

  2. Save the file (in the tutorial directory) as FieldData.prop.

Moving the Code that Inserts Data into a New Script

Before you change EnterFieldData.java to read properties directly from the property file, you need to save the original contents of EnterFieldData--where you actually insert the data into the record--to another file. For this example, you'll use Insert as the filename.

When you later edit EnterFieldData, you'll provide new code that calls Insert, passing the values for a single record set as parameters.

To create the Insert script:

  1. From the JavaStar main menu, choose Edit Test Script.

  2. Open the file EnterFieldData.java.

  3. Change the script name from EnterFieldData to Insert.
    This is similar to doing a "save as" operation in other applications.

    1. Click on the Find/Replace button.
      The Find/Replace dialog is displayed.

    2. In the Find field, type EnterFieldData.

    3. In the Replace field, type Insert.

    4. Click Replace All.
      JavaStar replaces all occurrences of EnterFieldData with Insert.

    5. In the Script name field (located at the top of the window) change the name to Insert.java and click the Save & Compile button.
      This saves the modified file as Insert.java, but leaves the original EnterFieldData.java file intact.

  4. Add code that calls the play() method of Add

Delete Add Node from AddRecord.jst

Now that Insert handles the add operation for each record, you need to delete the Add node from AddRecord.jst and reconnect the remaining nodes.

  1. In the Test Composer, open AddRecord.jst.

  2. Select the Add node and click the Delete button.

  3. Select the EnterFieldData node and click the Start Normal button.

  4. Click on the ClearDisplay node to complete the connection.

  5. Save the JST file.

Editing EnterFieldData to Read Properties from the File

Now you need to edit EnterFieldData so that, instead of being passed property values, it reads the properties directly from the property file.

  1. In the JavaStar Script Editor, open EnterFieldData.java again.

  2. Replace the body of the play() method with code that reads properties from the property file and calls Insert(), passing the properties as arguments.
    Here's one example of how you might edit the play() method.
    This example code:

    1. Reads the number of "records" (sets of properties corresponding to a record) in the property file.

    2. Creates a loop that repeats for the number of records to be read and:

      1. Reads each property for the current record being processed.

      2. Checks to see if any of these return null, and if so, throws an error.

      3. Creates an array containing the properties for a single record.

      4. Calls Insert() and passes the array of properties as a parameter.
        From here, the Insert() method handles inserting the data into a namedb record and adding the record to the database.

  3. Click Save & Compile.

  4. Close the Script Editor.

Clearing Parameter Settings from the JST Node

  1. From the JavaStar main menu, select Compose Test.
    The Test Composer is displayed.

  2. Open Acceptance.jst.

  3. Click on the AddRecord.jst node, then click the Edit button.
    The Edit Node dialog opens.

  4. Click on the first parameter in the list.

  5. Still in the Edit Node dialog, click the Delete button.
    This deletes $0 from the parameter list.

  6. Delete the remaining parameters $1 through $6.

  7. Delete the contents of the Comments field.

Editing the Test to Read the Properties Directly

Because your script is now reading parameters directly from the property file, you don't need to pass the data as parameters using the JST. To edit the EnterFieldData node

  1. In the Test Composer, open AddRecord.jst.

  2. Edit the EnterFieldData node to remove all parameter settings.
    You can do this either by deleting the node and recreating it (with the same connections to the other AddRecord nodes) or, to edit using the Edit node window:

    1. Select the EnterFieldData node and click the Edit button.

    2. In the Edit Node window, select a parameter from the list, then click the Delete button.

    3. Repeat Step b for each remaining parameter.

    4. Delete the contents of the Comments field.

  3. Click Apply.

  4. In the Edit Node window, click Close.

Specifying the Property File at Run Test Time

As with the case of reading a single set of properties, you need to define a property file name at run test time. In the EnterFieldData code, JS.getProperty() reads the property you specify from whatever property file you specify when you run the test.

If you're running the test using the Run Test dialog, type FieldData.prop into the Property File field. If you are running the test from the command line, use the -prop option.

Note that you still need to pass the same test arguments described in the section, "Running a Test With Arguments." This is because. for the sake of brevity, this exercise modifies only EnterFieldData to use properties. DefineSearch, GetSearchResults, and VerifyRecord still use parent parameters. If this were a real test case, rather than an example, you would modify these three tests to use properties. Once you had done this, you would only need to pass the name of the test database for OpenFile.

As the test executes, you can see that EnterFieldData types seven records into the database entry fields, and executes a button click on Add after it finishes entering each record.




Send feedback to JavaStar-feedback@suntest.com
Copyright © 1998 Sun Microsystems, Inc. 901 San Antonio Road, Palo Alto, CA 94303. All rights reserved.