Creating Custom Conditional Operations

Applies to TestComplete 14.40, last modified on April 22, 2021

This topic explains specifics of creating custom If Then operations for your keyword tests.

About Conditional Operations

Conditional operations are keyword-test operations that check some conditions during test run and then execute or do not execute child operations based on the results of the check. Typical examples of conditional operations are the If... Then, If Object and If Browser operations built into TestComplete. You can create custom conditional keyword-test operations that check specific conditions like file or folder existence or availability of windows or controls under test.

About Creating Custom Conditional Operations

Creating conditional operations is very similar to creating any other keyword test operation. You need to do the following:

  • Declare the operation and define its parameters in the description.xml file.

  • Implement script code that will handle the operation’s events and implement the operation’s functionality.

For information on creating custom keyword-test operations, see topics of the Creating Keyword Test Operations section. This topic explains specifics of creating custom conditional operations.

Declaring Conditional Operations

  • You declare conditional operations in the same manner in which you declare other custom keyword-test operations. You can find an example below. For detailed information on elements and attributes, see Structure of the Description File.

  • Important: in order for the test engine to recognize your operation as a conditional operation, the OperationType attribute of the KDTOperation element must be equal to Condition. You should also specify handlers for events that are specific to conditional operations (see below for information on these events):

    XML

    [File: description.xml]
     

    <?xml version="1.0" encoding="UTF-8"?>
    <ScriptExtensionGroup>

     
      <ScriptExtension Name="My Conditional Operation" Author="John Smith" Version="1.0" HomePage="someURL.com">
        <Script Name="myUnit1.js">
     
          <KDTOperation Name="My Conditional Operation" Category="My Category" Icon="clc-icon.bmp" OperationType="Condition">
            <Data>
              ...
            </Data>
            <Events>
              <Event Name="OnCreate" Routine="MyOp_OnCreate"/>
              <Event Name="OnSetup" Routine="MyOp_OnSetup"/>
              <Event Name="OnExecute" Routine="MyOp_OnExecute"/>
              <Event Name="CanExecuteBody" Routine="MyOp_OnCanExecuteBody"/>
            </Events>
            <Columns>
              ...
            </Columns>
          </KDTOperation>
        </Script>
        <Description>
          ...
        </Description>
      </ScriptExtension>
     
    </ScriptExtensionGroup>

  • Like any other keyword-test operation, custom conditional operations can have parameters and fields. You may want to define some parameters that will specify the condition to be checked. For instance, if you create an operation that verifies whether a file or folder exists on hard drive, create a parameter that will specify the name of the desired file or folder. For information on defining parameters and fields, see Creating Operation Parameters and Fields.

Events to Be Implemented

The functionality of custom keyword-test operations is implemented by script event handlers that are called at needed points of time. Some of the events are triggered during the test run, and their event handlers implement the operation's behavior. Some other events occur at design time, and their event handlers let users configure the operation’s parameters. Below is a description of such events.

Run-Time Events

The following events are triggered at run time. Write event handlers for them to implement the operation’s behavior:

1. OnExecute  - Occurs when the test engine starts executing your conditional operation.
Write an event handler for this event to perform initialization actions that are required for comparison.
2. CanExecuteBody  - Occurs after the OnExecute event.
Write an event handler for this event to signal to the test engine whether to run child operations or not.
3. Run child operations  - In this step, the test engine executes child operations.
No event of your conditional operation is triggered at this time.
 

Some notes on implementing event handlers:

  • The CanExecuteBody event handler should return true or false: true indicates that the test engine should run child operations; and false means the child operations should not be executed.

  • Checking conditions can take some time. It is recommended that you perform all needed checks within the OnExecute event handler, save the results to some global script variable and use the CanExecuteBody handler to return the variable’s value (that is, to return the results of the check).

Design-Time Events

Design-time events of custom loop operations do not differ from events of other operations. Here is information on some events for which you may want to create event handlers:

See Also

Creating Conditional, Loop and Group Operations
Creating Keyword Test Operations
Creating Keyword Test Operations - Basic Concepts

Highlight search results