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.
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.
A property file is a text file that uses the format:
<property name> = <value>
For this example, you'll create a property file, that uses the format:
name = <name value for first record>
address1 = <address1 value for first record>
address2 = <address2 value for first record>
telephone = <telephone value for first record>
email = <email value for first record>
other = <other value for first record>
name=Elmo
address1=45 Sesame St.
address2=Anytown USA
telephone=555-2857
email=tickleme@elmo.com
other=none
tutorial
directory) as FieldData.prop.
AddRecord.jst
.EnterFieldData
node and click the Edit button.0
to name
and click the Update button.
For Arg # | Set property name to |
---|---|
1 | address1 |
2 | address2 |
3 | telephone |
4 | |
5 | other |
Read properties from FieldData.prop
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.
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.
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:
Create a property file (or, if you already have a file named FieldData.prop
, edit the existing file) to use the format:
total = <total number of records>
name_<#> = <name value for first record>
address1_<#> = <address1 value for first record>
address2_<#> = <address2 value for first record>
telephone_<#> = <telephone value for first record>
email_<#> = <email value for first record>
other_<#> = <other value for first record>
where you increment <#> for each set of records.
total = 7
name_0=Elmo
address1_0=45 Sesame St.
address2_0=Anytown USA
telephone_0=555-2857
email_0=tickleme@elmo.com
other_0=none
...
name_6=Susan
address1_6=101 Learning Lane
address2_6=Anytown USA
telephone_6=555-0277
email_6=susan@sesameSt.com
other_6=Educator
tutorial
directory) as FieldData.prop.
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:
EnterFieldData.java
.EnterFieldData
to Insert
.EnterFieldData
.Insert
.EnterFieldData
with Insert
.
Insert.java
and click the Save & Compile button.Insert.java
, but leaves the original EnterFieldData.java
file intact.
Add a = new Add();
a.play(new String[1]);
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.
AddRecord.jst
.Add
node and click the Delete button.EnterFieldData
node and click the Start Normal button.ClearDisplay
node to complete the connection.Now you need to edit EnterFieldData so that, instead of being passed property values, it reads the properties directly from the property file.
EnterFieldData.java
again.play()
method with code that reads properties from the property file and calls Insert()
, passing the properties as arguments.play()
method.
public void play(String[] args) throws Throwable {
Insert o = new Insert();
int total = 0;
total = Integer.parseInt(JS.getProperty("total"));
// read total # of records
for(int i = 0; i < total; i++) {
String name = JS.getProperty("name_" + i);
String address1 = JS.getProperty("address1_" + i);
String address2 = JS.getProperty("address2_" + i);
String telephone = JS.getProperty("telephone_" + i);
String email = JS.getProperty("email_" + i);
String other = JS.getProperty("other_" + i);
if (name == null || address1 == null || address2 == null||
telephone == null|| email == null|| other == null)
throw new NoSuchFieldException("Invalid Key Value");
// construct args
String[] record = new String[6];
record[0] = name;
record[1] = address1;
record[2] = address2;
record[3] = telephone;
record[4] = email;
record[5] = other;
// call play method of insert with args
o.play(record);
}
}
Insert()
and passes the array of properties as a parameter.
namedb
record and adding the record to the database.
Acceptance.jst
.AddRecord.jst
node, then click the Edit button.$0
from the parameter list.
$1
through $6
.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
AddRecord.jst
.EnterFieldData
node and click the Edit button.
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.