Forum Discussion

cunderw's avatar
cunderw
Community Hero
10 years ago
Solved

Store result of custom keyword operation in a user defined project variable?

Example:   Say I have a custom script extension / keyword test operation that takes user defined input to do some operation without the tester needing to script anything.   What I want is for...
  • HKosova's avatar
    10 years ago

    cunderw wrote:

    The only way I've found to do this is use set variable and select last operation result.


    That's the standard approach, and it's used by the built-in operations as well.

    That said, you can probably use an operation parameter for the output variable name. Say it's named OutVariableName, and should contain just the variable name, without the Project.Variables prefix.

     

    <!-- description.xml -->
    <KDTOperation Name="My Operation" Category="My Category">
      ...
      <Parameters>
    ...
    <Parameter Name="OutVariableName">The name of a project variable to save the operation result to.</Parameter> </Parameters> ... </KDTOperation>

    In the operation code, you can check if the variable is specified and exists, and if so save the operation result to it. (Note: the code is off the top of my head and untested.)

     

    // JScript
    function MyOperation_OnExecute(Param1, ..., OutVariableName){
      // Do something
      ...
      var result = true;
    
      var strVarName = Parameters.OutVariableName;
      if ((strVarName == null) || (strVarName == "")) {
        // Variable name isn't specified
        ...
      } else if (! Project.Variables.VariableExists(strVarName)) {
        // Variable doesn't exist
        ...
      } else {
        // Variable exists - save the operation result to it
        Project.Variables.VariableByName(strVarName) = result;
      }
    
      return result;
    }