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 the tester to be able to also store the result of the operation in a specified project variable so they can do a checkpoint, etc... with it.

 

The only way I've found to do this is use set variable and select last operation result. While this works, it's not ideal.

 

Any help or information would be much appreciated.


  • 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;
    }

     

6 Replies

  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)

    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;
    }

     

    • cunderw's avatar
      cunderw
      Community Hero

      This works great if I hard code strVarName to a string of the variable name. How ever doing:

       

      var strVarName = Parameters.OutVariableName;

      Returns empty even after setting it in the paremeters edit dialog that appears after my user form.

       

      Are there any additional steps that are required to access the parameter inside the script besides declaring it in the description.xml and the function itself?

       

      EDIT: Was able to get this work using Data field and adding it to the form as opposed to the parameter.

      • HKosova's avatar
        HKosova
        SmartBear Alumni (Retired)

        Does it work if you use just OutVariableName instead of Parameters.OutVariableName? Parameter values should be available as the OnExecute function parameters.