If you have been working on the other lessons in this tutorial, you've spent a lot of time on OpenFile
. This lesson has a different application, but still has a file to open, so you're going to spend more time; however, you can build on what you've learned and use methods in the API to make your file handling even more rigorous.
You are going to work with these methods in this part of the lesson:
JSComponent.relativefile()
and JSComponent.choosefile()
, which simulate file dialogs
JS.note()
, which adds messages to the test results file
JS.getProperty()
and JS.setProperty()
, which share data between scripts
Start by recording a script that opens a file.
openAction
. tutorial/API/TCTester/newfilew
newfilew
." This is pretty much the same as you did before. You edited that script to replace the hard-coded file name with an argument. That is better, but you can do better still. Here are some of the problems that remain with a file-opening script, even if you make the file name a variable:
GUINotFoundException
, which may disguise a very different problem.
Next, you will examine each of these in detail and see solutions available to lessen the problems with scripts that address files.
Two things are being tested in the openAction
script:
For better reuse, use two scripts.
The first, VerifyTestFile
:
The second, openAction
:
If VerifyTestFile
fails, it means that test file wasn't set up properly. If openAction
fails, the application has a problem opening the file. This makes error assignment and debugging much clearer than leaving it all in one script. VerifyTestFile
becomes very reusable for any application using that file.
Examine your openAction
script by viewing it in the script editor.
tutorial/API/TCTester/Tests/openAction.java
play()
method. Notice the call to the file dialog simulation:JS.frame("Temperature Converter Test Input")
.dialog("java.awt.FileDialog", "Open")
.relativefile("..\\", "newfilew");
In a previous exercise, "Adding Parameters for Flexibility", you replaced the file name "newfilew"
with an argument that is passed to the test at run time. While this improves the script and makes it more flexible, please note that the directory, the first argument to the JSComponent.relativefile()
method, remains hard-coded. This string is dependent on two things, each of which makes this script fragile and less portable:
To correct this, make the directory, as well as the file, an argument. It would be preferable to specify the path as absolute, rather than relative. You'll do this in a few moments.
Should the openAction
script fail to open a test file that is known to exist, it is most likely a problem in the application code. The proper behavior is to note that error against the application.
However, as the code uses a variable label to show that the file has been open, the verify on this label will fail with a GUINotFoundException
.
The ideal correction to this is to change the application code itself to use a setName()
for this component. Then, if JavaStar is instructed to use component names, the text of the label will not become the actual name of the component.
However, the TCTester
application does not use setName()
. We can compensate by catching the GUINotFoundException
within the script, and posting an error to the log by using JS.check()
.
try {
JS.frame("Temperature Converter Test Input")
.member("java.awt.Label", filename)
.verify(this,filename, "label should reflect file name");
} catch (GUINotFoundException oops) {
JS.check(false, "File label not correct");
}
Now let's actually create the VerifyTestFile
and openAction
scripts.
Create a template script and edit it to code the required instructions.
VerifyTestFile
. tutorial/API/TCTester/Tests/VerifyTestFile.java
import java.io.*;
play()
method.String fileDir, fileName;
try {
// find the arguments
fileDir=args[0];
fileName=args[1];
// make sure they are not empty
if (fileDir.length() == 0 || fileName.length() == 0) {
JS.note("Empty argument found");
JS.note("Directory: " + fileDir);
JS.note("File Name: " + fileName);
throw new IOException();
}
} catch (Exception oops) {
JS.note ("Unable to resolve file name arguments");
throw oops;
}
JS.note()
is the API method that allows you to post a string to the test result file. Here you use it to add a better explanation for what caused the exception.
// verify the file is ok
TestCaseFile tf = new TestCaseFile(fileDir,fileName);
if (!tf.fileExists()) {
JS.note ("Could not find " +fileDir + fileName);
throw new IOException();
}
// get the absolute path
String absFileDir = tf.getParent();
String fileSize = String.valueOf(tf.getSize());
JS.setProperty(
String keyname,
String value)
to update the file. Here is the code that sets the values in the property file and notes to the log what those values are.
JS.setProperty("FILEDIR", absFileDir);
JS.setProperty("FILENAME", fileName);
JS.setProperty("FILESIZE", fileSize);
JS.note("Properties set: ");
JS.note ("FILEDIR: " + absFileDir);
JS.note ("FILENAME: " + fileName);
JS.note ("FILESIZE: " + fileSize);
tutorial/API/TCTester/Tests/openAction.java
play()
method. All your updates will go within the brackets of this method.public void play(String[] args) throws Throwable {
//YOU ADDRESS THE API HERE
play()
method. // Code to get the properties we need
String filedir, filename;
filedir=JS.getProperty("FILEDIR");
filename=JS.getProperty("FILENAME");
relativefile()
and correct it. choosefile()
that uses an absolute path. Change to this method, and use the values from the properties for the input parameters.
// change from relativefile to choosefile,
// from literals to properties
TCTesterDeclarations.Main.tCTestCaseBuilder()
.dialog("java.awt.FileDialog", "Open")
.choosefile(filedir, filename);
JS.frame("Temperature Converter Test Input")
.member("java.awt.Label", filename)
.verify(this,filename, "label should reflect file name");
try {
JS.frame("Temperature Converter Test Input")
.member("java.awt.Label", filename)
.verify(this,filename, "label should reflect file name");
} catch (GUINotFoundException oops) {
JS.check(false, "File label not correct");
}
Now that you have built the scripts, you can build a test that contains them.
By creating a test that contains the pair, you have the effect of a single script similar to the OpenFile
you created for the Name
application. This test can be used within other tests, and the properties it sets can be addressed by the other scripts.
If you are not familiar with using the test composer, please see "Composing Tests" in the JavaStar User's Guide.
OpenTCFile
as the jst name.VerifyTestFile
and openAction
. VerifyTestFile
. Use the Edit button. VerifyTestFile
and openAction
.Now you are ready to test your test.
tutorial/API/TCTester/Tests/OpenTCFile.jst
/tutorial/API/TCTester
for the directory, and fileOfThree
for the file name. Adjust the directory path as needed to be correct for your platform. At this point, you have learned to build a robust file-handling test.
Send feedback to
JavaStar-feedback@suntest.com
Copyright © 1998
Sun Microsystems, Inc. 901 San Antonio Road, Palo Alto, CA 94303.
All rights reserved.