Using the JavaStar API: Verifying Menu Components


After you have completed the project settings, create a script to test that the menu components are properly enabled. To do this, create a template for the script, then fill in the proper code using some of the API commands.

Let's see what happens when you try to select a menu component from the "Record/Playback" window.

  1. Click on Create a Test Script, and on Start to begin the application.
    If your project settings are correct, the application, TCTestCaseBuilder, should be in place.

  2. Click on Interact.

  3. From the application, pull down the File Menu. The New, Open, and Exit menu items are enabled.

  4. Click Inspect to view the JavaStar code for these components.
    The "Inspect" window does not show these components. It remains on the main application.

  5. Click Quit to close the Record/Playback window.

Java 1.1.x AWT MenuComponents are specific to a platform. If JavaStar tried to address these components directly through the GUI itself, the resulting scripts would be platform dependent. Robust scripts should run on all platforms.

Because of this, there needs to be another means to address the Menu Components. There is: using the JavaStar API. You can use methods in the JSMenuComponent class provided with JavaStar to get the actual MenuItem, then use the MenuItem's methods to do the verification.

This is a common use for the API: obtaining the component itself, then checking information about the component that is not available to you from the verification function within the "Record/Playback" window.

Using the API to Obtain a Component

Although our example uses a menu component, parallel commands exist for a regular component. The menu class is JSMenuComponent; a regular component is JSComponent.

You can use the declarations generated for this application. To learn more about generating declarations, see "Generating Declarations".

Here is an example of locating a menu component:

This is not the AWT MenuItem, though. Use the getValidUnique() method of JSMenuComponent (or JSComponent) to extract the actual component from the JavaStar wrapper. As getValidUnique() returns a generic Component, you must downcast the component to the proper class.

Once you have the MenuItem, you can use its methods to verify. However, you'll need a means to report the results of that verification to the test.

Using an Internal Verification

You can make output assertions from within a script using the JS static method, JS.check(). JS.check() takes two parameters: a boolean that is the condition you want to check, and a String that represents the purpose of the check.

The initial condition should be presented such that if it is true, the check will pass. For example, you assert that if the New menu item is enabled, this check should pass. You do this in code by writing:

Now that you've got the idea of what is needed, let's review the mechanics of getting this code into the script.

Step 1: Creating the Base Script

You could code the entire script by hand, but it would be subject to errors of omission for the items JavaStar provides that you shouldn't have to think too much about. It's easier to use the record features of JavaStar to generate a base script.

If you were really verifying the state of the application at start-up, you'd probably do the verifications of all buttons and text fields directly from the Record/Playback window, then edit the script to add the menu verifications. Feel free to do that if you have the time. Our directions are just for how to do the menu verification.

  1. Click on Create a Test Script, and Start the application.

  2. Record the script. Name it VerifyFileNotOpen.

  3. Immediately Stop the recording.

  4. Quit the "Record/Playback" window.

This created a script with an empty play() method, except for obtaining a component. Now you need to edit that script to put in the code to verify the menu item.

Step 2: Edit and Test the Script

Use the Script Editor within JavaStar to enter the required code. See "Editing Tests" in the JavaStar User's Guide for more information on using this editor.

  1. Click Edit Test Script, then browse to locate this script:

  2. Locate the play() method.

  3. Add the code to verify that the New menu item is enabled:

  4. If you want, delete the unnecessary declaration for the button.

  5. Click Save and compile.

  6. Run the script (you can do this from the editor by clicking Run Script).
    It should run successfully with one passed check.

  7. Click Quit to close the Record/Playback window and Close to close the editor.

Now that you've got a start on using the API, try a lesson that is a bit more complicated.




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