Adding custom code

You can extend a test by calling custom Java code.

Your code might act on one or more input values from the test and return modified values to the test. Custom code input values reside in references or field references, which must already exist in the test. References or field references that are inputs to your code must appear sequentially before your code. Verify that the test contains the references or field references needed as inputs to your code. For details on how to create references and field references, see Creating a reference or field reference.

If your custom code uses external JAR files, you will likely need to modify the Java build path: see Viewing and editing a project's Java build path. In some cases, you can avoid needing to modify the build path manually by running a test before adding your custom code to the test. This is because the first time a test runs, certain classes and libraries required for compilation are added to the build path. For example, you can import Hyades classes needed to create custom events in the execution history if the test to which you have added your custom code has been run previously. But if the test has never before been run, you get import errors because the classes are not named in the project's build path until the test has been executed.

Your code might use external resources: for example, a SQL database or a customer relationship management product. If this is the case, you must configure the custom code such that it works on every computer on which your test is deployed.

Custom code resides in the src folder of the project containing the test(s) that calls the code. By default, custom code is put in a package named test inside this folder. For example, the following figure shows the base Navigator view of two custom code classes. (The Test Navigator does not display Java source files.)

In this example, if you add these two custom code classes to your test and choose to return a value to the test (step 9), Substitute from lists these two classes in the manner shown in the following figure.

The test package also contains the generated Java code for tests in the project. But, in step 3, you can choose to put custom code in a different package (for example, custom). Separating custom from generated code is recommended, particularly if you use a source control system.

To add custom code:

  1. Open the test and select an element (test, page, or request).
  2. Click Add or Insert and select Custom Code. Add appends the custom code to the bottom of the selected element (test or test page). Insert adds the custom code above the selected page or page request.
  3. Inspect the Class name field.
    • If the code that you want to call already exists, change the class name to match its name. Clicking View Code opens the code in the Java editor.
    • If the code does not exist, change the class name to something that suggests the code's purpose. Clicking Generate Code generates a template class for logging results and opens it in the Java editor. If a class of this name already exists, you are warned that it will be overwritten and required to verify that this is what you want.
  4. Under the Arguments box, click Add. The Custom Code dialog box opens, listing all values in the test that can be used as inputs to your code (references or field references in the test that are sequentially above the code).
  5. Select all inputs needed by your code.
  6. Click OK. The window closes, the selected references are added to the Arguments box, and the Used by table is updated with this information.
  7. In the test, sequentially below your custom code, locate a value that your code returns to the test.
  8. Highlight the value: with the left button pressed, drag your mouse over it.
  9. Right-click the highlighted value, select Substitute from, and select the class name of your custom code. All custom code classes that you have added to your test are listed. After you've made your selection, the highlight color of the value to be returned to the test changes to orange and the Used by table is updated with this information.

Parent topic: Extending tests with custom Java code

Terms of use | Feedback
(C) Copyright IBM Corporation 2005. All Rights Reserved.