+49 (30) 467086-20 service@microtool.de
Products » objectiF RM » 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 RM.

Example

In this example, the output should show how many test sets failed in a test set execution.

 

Create the extension schema

In the first step, you create the schema. The extension schema defines which properties can be evaluated using an extension. Depending on what you want to do, you need a schema that reads the data from objectiF RM and a schema that writes the changed data back again. If you handle input and output data in a function, you only need to create one extension schema.

  1. Create an extension schema using the context menu command of a package with Create other/ Schema.
  2. Assign a name to the sub stereotype (from package) TestManagement, since the extension function is executed later using the context menu of this package.
  3. Switch to the Schema tab and select the properties that the function requires.
    In this example, the following properties are selected: 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.
    Result of the extension schema
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.
Create the extension function
  1. Ensure that the “Generate Directory including Main File” check box is activated. Save and close the dialog.
    A directory with initial files is created under the extension function. At the same time, the Index.js file is assigned to the extension function as the initial file.
Link between extension function and index file
  1. Ensure that the “Generate Directory including Main File” check box is activated. Save and close the dialog.
    A directory with initial files is created under the extension function. At the same time, the Index.js file is assigned to the extension function as the initial file.
Write the code

In the next step, the developer writes his function in a suitable editor. If you have defined Visual Studio Code as the default program on your computer, you can open *.js files by double-clicking them. Otherwise, select Open File in VS Code from the context menu of the 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.
Write the code

In the next step, the developer writes his function in a suitable editor. If you have defined Visual Studio Code as the default program on your computer, you can open *.js files by double-clicking them. Otherwise, select Open File in VS Code from the context menu of the 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.
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 select the Check In command from the directory 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

To be able to execute the extension function, you must finally create a command that is offered to you via the context menu.

  1. Switch to the backstage menu and select Project/Extension Function commands.
  2. In the Extension Function commands view, click the Plus button at the top right to create a new command.
    Create extension function command
  3. In the following dialog, name the command and select the extension function you just created under Executed Extension Function.
  4. Under Positioning, you 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 sorted.
    Dialog of the extension function dialog
    You can also create your own group for the context menu by selecting User defined under Display in menu group.
  5. Close the dialog by clicking OK.
    New command created

Note

If you want to delete an extension function later, you must first delete the command in the Extension Function commands view.

Execute the command

You can use an extension function manually from the context menu, configure a task, or perform an action in the state machine. The command is only executed on 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 the percentage of failed tests.