+49 (30) 467086-20 service@microtool.de
Products » objectiF RPM » Create and Execute an Extension Function

Create and Execute an Extension Function

With the help of extension functions, teams can independently write new functions or create interfaces to other systems. If you want to use an extension function, you must write one or more functions to be executed in a code file. You also need a response file and a request file that define the structure of the input and output data, and a command that executes the extension function in objectiF RPM.

Example

In this example, the output should show how many test sets failed in a test set execution. At the same time, an extension function for a package of the TestManagement type will be implemented.

 

Create the extension function
  1. From the context menu of a package, select Create other/Extension Function.
  2. Assign a name and select the same stereotype that you selected for the extension schema. In the dialog shown below, the context stereotype chosen for the selected package is TestManagement.
Create the extension function
  1. Select the check box for Create request scheme automatically and – if you want to define input and return data seperately- select also the check box for Create response scheme automatically. Alternatively, you can also select the check box for Use request scheme.
  1. Click OK to save the changes you have made in the dialog.

    A directory with initial files is created in the package for Extension Function and, depending on what check boxes had been marked, one or two extension schemes are also created. Simultaneously, the Index.js file is assigned to the extension function as the initial file.

Extension function and schema are created
Edit the extension schema

With this second step, you will edit the schema that has been created in the previous section. An extension schema allows you to define which properties can be evaluated by an extension function. Depending on what exactly it is you want to do, you’ll need a schema that can borrow files from objectiF RPM and another schema that will return borrowed files that have been modified. If you’re handling input and output data with one function, you only need to create one extension schema.

  1. Open the extension scheme Evaluate Test Set (request) by either double clicking on it, or through it’s context menu command Edit. 
  2. In the dialog, you have the option to rename the extension scheme. The default name is automatically derived from the extension funciton.
  3. Switch to the Schema tab and select the properties that the function requires.
  4. Open the entry for TestManagement and, for the purposes of this demonstration, select the following properties: the Id in the TestSetExecutionArtifacts list, the ID and Name under TestArtifactRelationship, and the Name property under Current state.

Good to know
Once you have exported a schema, you can import it from the context menu of an extension schema using the Import JSON Schema command. You can also write your own schema and import it using this command.

  1. Confirm with OK.
Write the code

In this next step, the developer writes the function as code. If Visual Studio Code (VS Code) is installed as the default editor, the *.js files will open automatically in VS Code when double-clicked. If not, select the command Open File in VS Code in the context menu of the *.js file.

  1. In the context menu of the directory containing the code file(s), select Open directory in VS Code.
    The entire directory is checked out and opened in Visual Studio Code.
  2. Edit and save the files.
Extension function and schema are created
Code Snippet

Open the generated Index.js file and replace the function with this code:

exports.handler = async function(input) {
const failedStateName = 'failed';
const tseCount = input.testSetExecutionArtifacts.length;
const testCount = input.testSetExecutionArtifacts.reduce((sum, tse) => sum + tse.testArtifactRelationships.length, 0);
const failedTestCount = input.testSetExecutionArtifacts.reduce((sum, tse) => sum + tse.testArtifactRelationships.filter(t => t.currentState.name == failedStateName).length, 0);
if (testCount===0) {
return {
message:`Function finished successfully but nothing could be found :-(`,
};
} else {
return {
message:`In ${tseCount} Test set executions,${(failedTestCount / testCount) * 100}% of all tests have failed (${failedTestCount} of ${testCount}).`,
};
}
}
  1. Now execute the Check In command from the directory’s context menu to apply the changes.
  2. In the next dialog you will see the files that have changed. Selected files are checked in and overwritten when you click OK.
Check in files
Create a command

In order to implement extension functions, as a final step you need to create a command.

Good to know
If you want to delete an extension function at a later point, you need to delete the command in the view for Commands for Extension Functions before hand. 

  1. Switch to the backstage menu and select Project/Extension Function commands.
Create extension function command
  1. In the Extension Function commands view, click the Plus button at the top right to create a new command.
Create extension function command
  1. In the following dialog, name the command and select the extension function you just created under Executed Extension Function.
  2. In the section for Positioning, specify the context menu group in which the command is to be placed.
    If you have several commands in a group, you can use Weight to determine the position within the menu where the command is to be placed. 
Dialog of the extension function dialog

You can also create your own group for the context menu by selecting User defined in the section for Display in menu group.

  1. Close the dialog by clicking OK.
New command created
Execute the command

You can use an extension function manually from the context menu to configure a task, or perform an action in the state machine. The command is only executed within TestManagement packages.

  • In the context menu of a package, select the configured command Evaluate test sets.
Execute command

The extension function is executed and the output shows what percentage of tests failed.