Convert TruClient actions and functions to code

TruClient provides a hybrid solution that enables users to create both non-coded and coded actions and functions. This enables you to take advantage of fast script development available with TruClient when you create a script. You can then convert selected actions and functions to code, making it easier to add complex logic and customized functions to the script.

Note:

  • Code conversion and replay of converted code (provided as tech preview) will be deprecated in a coming version.
  • This feature is available in TruClient Standalone and TruClient in VuGen, and is supported in TruClient Web scripts only.

Workflow: Convert TruClient actions and functions to code

This section describes the end-to-end workflow for converting TruClient actions and functions to code.

Step 1: Convert a TruClient action to code (for functions, see Convert functions and function libraries to code)

Step 2: Replay and enhance the converted script

Step 3: Debug the coded script action

Back to top

Benefits of converting TruClient actions and functions to code

Benefits of converting actions and functions to code include:

  • Ability to create complex scripts. Converting TruClient actions and functions to code makes it easier to add complex logic to your script. Although TruClient has a rich set of built-in functions, in some cases it is necessary to implement custom functions. For example, if you want to programmatically change the object description. After converting a TruClient action or function to code, you can customize the action or function and add complex logic to improve your load testing abilities.

  • Faster processing. While TruClient provides you with the ability to quickly and easily record complex business processes, the more complex an application's client logic is, the more resources are required to process the script during replay. When a script has a large number of steps, the user interface can get busy and this can affect TruClient performance. When you convert actions or functions to code, the script is saved as a text file—thus accelerating script processing.

  • Extensive debugging capabilities. TruClient provides a wide range of tools for debugging coded actions and functions. They include the ability to debug coded syntax, set and pause on lines with breakpoints, highlight replay line, show the error icon if replay error occurs, and display the Coded and Model Inspector Panels on step replay.

  • Simplifies script development. Speeds up script development by enabling you to combine model and scripting modes for the best of both worlds. If you are unsure how to do something in TruClient code, do it first in the TruClient model, and then convert it to code.

  • Easy to use. You can select actions in your TruClient script and convert them into working JavaScript code—with just two clicks. After recording the action and enhancing the model with transactions, control flows, parameter substitution and anything else that can be done in TruClient, just click Convert to Code.

  • Ability to convert existing TruClient scripts. The coded feature enables you to convert any of your existing TruClient scripts to code.

Back to top

Step 1: Convert a TruClient action to code

  1. Open a TruClient script.

    You can use an existing script, record a new TruClient script, or start from an empty action. If you use an empty script, skip to step 3.

  2. Replay the script (optional).

    We recommend replaying the script and correcting any errors that occur during the process. When you replay the script before conversion, TruClient sets an automatic end event for the browser being recorded, and this becomes the default end event for all browsers after conversion.

    Note:  

    • If you convert the script to code without first replaying the script, steps will not have automatic end events.

    • If you manually changed an end event before conversion, TruClient automatically generates an end event for all the browsers.

  3. Configure the script conversion settings.

    1. Click the Convert to Code button to open the TruClient Coded Conversion Settings dialog box.

    2. Select the scope of the script to convert:

      • A single action. Converts only the currently selected action to code.

      • All functions in a single library. Converts a single non-coded function library to code. Select the library to convert from the dropdown list. For details, see Convert functions and function libraries to code.

      • A single function. Converts a single non-coded function to code. Select the function to convert from the dropdown list. For details, see Convert functions and function libraries to code.

      • Entire script. Converts all non-coded actions and function libraries in the script to code.

    3. Select the object identification method:

      • TruClient managed object repository. TruClient continues to manage the object repository after conversion (this option supports all object identification methods). This is the default option.

      • User managed object repository. Enables you to create objects and manage the object repository.

        If you selected this option, configure the following conversion settings:

        UI Element Description
        Select default identification method

        Select the object identification method to use: XPath (default), JavaScript, or Descriptors.

        Note: The Automatic object identification method is not supported.

        Keep predefined identification for non-Automatic methods If selected, retains all predefined identification methods, except for Automatic, which is replaced by the default identification method selected above. Clear this option to replace all predefined identification methods with the default identification method.
        Include other object identification method as comments If selected, includes the other object identifiers as comments in the code.

        Include objects roles as a comment

        If selected, includes all object roles as a comment in the code.
    4. Configure the step conversion options.

      • Add a comment before each step. Adds a comment describing each step to the code.

      • Include all step arguments (Optional Step). Includes step arguments with their default values in the code.

        Note: Choosing this option results in a significantly longer coded script.

      • Include conversion warnings. Includes warning comments as a result of converting actions to code. This can include warnings for changing the end event in a transaction, or for using a different step.

    5. Click Convert to convert the selected action, all actions, function library, or selected function to code.

      Actions that have been converted to code are indicated as such in the action selector .

      After conversion, objects created by TruClient are shown as:

      TCS.object.tcManaged.<object name>.operation(arguments)

      User created objects are shown as:

      TCS.object.userManaged.*.operation(arguments)

    Note: You can roll back the conversion at any time by clicking . When you undo the conversion, you lose all changes made to the code, and return to the original model of the action in the script.

Back to top

Step 2: Replay and enhance the converted script

You can insert JavaScript code into preexisting steps in the arguments fields of most steps. This allows you to perform multiple customizations to enhance your load testing abilities.

  1. Replay the script (optional).

    We recommend replaying the script and correcting any errors before inserting code.

    1. Select the down arrow next to the replay button , and select Replay step by step to view the replay more slowly and in a controlled manner. Repeat this procedure after each step to continue the step by step replay.

      You can view script action details in the status area.

      Note: When replaying a coded script step by step, TruClient only pauses on steps that begin with TCS.

      Example: The replay pauses only on the navigate and click functions, and not on the x variable definition and increasing it by 1.
      TCS.browser.navigate({
      	"Location": TCS.argType.JSArg("\"http://12.34.56.789/\"")
        });
      var x = 1;
      x = x+ 1;
      TCS.object.tcManaged.allkindofCGIsObject.click({
      	"X Coordinate": 108,
      	"Y Coordinate": 8
        });
    2. Identify the End Event for each step (optional).

      If you converted the script without replaying it first, no end events are defined in the step arguments. To add end events manually, enter the end event argument in the format:

      "End Event": TCS.argType.endEvent.<select end event>

      For the list of end events, see TCS.endEvent.<end event type>.

      You can also change end events that were automatically generated or selected before converting the script, by selecting a different event.

  1. Enhance the coded script.

    You can insert JavaScript code into preexisting steps in the converted script, or create new coded steps using TruClient coded functions. This enables you to perform multiple customizations.

    To add additional objects to the script, click , and select:

    • Add TruClient managed object. Enables you to add additional TruClient managed objects to the script. To do so, select a new object in the application browser window. The object details are displayed in the Create Object window. After you select an object, you can use it by name.

      After pasting the managed object, you can modify the coded name, name, and selected identification method.

    • Generate object code. Enables you to add additional user-defined objects to the script.

      To generate code for a new object, select the object in the application browser window. TruClient provides you with the code options to copy into the script in the Object Details window.

      Edit the object code as required, and then click to copy the code, and paste it into the script. For the list of TruClient coded objects, see Object.

      Tip:  

      • Click Detach Coded Window to open the code editor in a separate expanded window, making it easier to edit code and work with two screens.

      • Press Ctrl + F, and use the search bar to find steps, functions, and transactions in coded scripts without needing to navigate through long scripts. For details, see Search bar.

      • You can also use a coded function library as a global function library by exporting and importing coded function libraries to and from a script.

Back to top

Step 3: Debug the coded script action

The following table lists the actions available to debug the coded script.

Note: For coded functions, see Actions available for Coded functions.

Action How to
Run step by step Use the Replay step by step option (available from the Replay button dropdown menu) to aid debugging of the code. Replay is stopped at each line that starts with a TCS. statement.
Use event handlers

Click to add event handlers to help your test recover smoothly. For details, see Event handlers.

Update objects

To make changes to an object, right-click an object, and select Update Object.

You can change the coded name, name, and identification method for the object.

You can also click to regenerate the code for the selected object.

Make a step optional To make a step optional, add the argument "Optional Step": true to the step.
Insert breakpoints

To insert a toggle breakpoint, right-click the relevant step, and select Toggle Breakpoint.

Note: Breakpoints only work in Interactive mode, and they only stop on TruClient APIs—not on custom logic.

Use Inspector Panel

Use the Inspector Panel, displayed at the bottom of the TruClient Development window, to do a detailed inspection of variable values in the code at a specific point in your script. The panel is displayed automatically during replay when it is triggered by one of the following:

  • A TCS.utils.breakpoint() line
  • Toggle breakpoint
  • Using step by step replay

The panel displays user-defined variables in the code. You can add an expression defined in the code, and view detailed information for that expression, including adding and removing variables.

  • The first time the Inspector Panel is displayed, there is no expression set. After an expression is added, it is saved for each time the panel is displayed.
  • If you add a variable to the code, it is defined at the next breakpoint.
  • The Inspector Panel does not show parameters. However, you can add a parameter, as follows:

    1. Add the line var paramVal = TCS.parameters.get("NewParam1");
    2. Add paramVal to the Inspector Panel.

For more details on using the Inspector Panel, see Insert toggle breakpoints.

Note: The variables defined in the Evaluate JavaScript code step are not supported.

Insert a wait step

Use the TCS.utils.wait function to insert a wait step. This delays the next step from starting for the specified time.

Identify objects

Use the highlighter to check if an object is visible in the application at any time.

  • For TruClient managed objects, right-click the object name (a line starting with TCS.object.tcManaged), and select Highlight.

  • For user managed objects, right-click the coded object (a line starting with "JavaScript", "XPath", or "Descriptors"), and select Highlight.

Generate snapshots

Use the TCS.utils.snapshot function to generate a snapshot during replay.

Edit general settings

Click to open the TruClient General Settings, and modify the settings that apply to the script. For details, see Configure TruClient general settings.

Display log messages

Click to display log messages in the User Output window while debugging the script (in the lower left side of the Development window). This window shows TCS.log messages.

 

Check code for syntax errors

Click to check for exceptions in the code.

Back to top

Convert functions and function libraries to code

  1. Open a TruClient script.

  2. Click the Convert to Code button to open the TruClient Coded Conversion Settings dialog box.

  3. Select the scope of the functions to convert:

    • All functions in a single library. Converts a single non-coded library to code. Select the library to convert from the dropdown list.

    • A single function. Converts a single non-coded function to code. Select the function to convert from the dropdown list.

  4. Click Convert to convert the selected function or function library to code. The converted function or function library is displayed in the Function Libraries tab.

    You can also use a coded function library as a global function library by exporting and importing coded function libraries to and from a script. For details, see TruClient functions and function libraries.

Actions available for Coded functions

The following table lists the actions available for coded functions.

Action How to
Check for syntax errors

Click Check code for syntax errors to check for exceptions in the coded function.

Edit code in expanded window Click Detach Coded Window to open the code editor in a separate expanded window, making it easier to edit code and work with two screens.
Revert coded library to original model-based library Click Undo Conversion to roll back the conversion. If you undo the conversion, the function libraries revert back to the original model-based structure, and any changes in the coded function libraries will be lost.
Add TruClient managed objects to the function

Click Add TruClient managed objects to add additional TruClient managed objects to the function. To do so, select a new object in the application browser window. The object details are displayed in the Create Object window. After you select an object, you can use it by name.

After pasting the managed object, you can modify the coded name, name, and selected identification method.

Add user-defined objects to the function

Click Generate object code to add additional user-defined objects to the function.

To generate code for a new object, select the object in the application browser window. TruClient provides you with the code options to copy into the function in the Object Details window.

Edit the object code as required, and then click to copy the code, and paste it into the function. For the list of TruClient coded objects, see Object.

For details on using other debugging capabilities on coded functions, such as setting breakpoints, pausing on lines with breakpoints, highlighting replay line, showing error icon if replay error occurs, and displaying the Coded and Model Inspector Panels on step replay, see Step 3: Debug the coded script action.

Back to top

Notes and limitations

  • The Automatic identification method is not supported for user managed object repositories.

  • Related objects in a script, and transactions with a non-default End Event cannot be converted to code.

  • Any change to the End Event in a grouped step is ignored when a script action is converted to coded.

  • Action on error in the Interactive Options tab of General Settings is not supported.

  • If you are using ECMAScript 6, you can run the script on IE, but it fails to verify the code.

  • Modal dialogs such as window.alert, window.prompt, and window.confirm are not supported.

Back to top

See also: