Debug an API user code file - Exercise

Relevant for: User code files

In this exercise, you create and debug an API user code file to practice using some of UFT One's debugging capabilities for API tests.

Note: For a task related to this scenario, see Debug a test, component, function library, or user code file.

Create test steps

  1. Create an API test.

  2. From the Toolbox Pane, in the Math section, drag the Add activity, Multiply activity, and the Custom Code activity to the canvas.

Back to top

Set properties for the math steps

  1. In the Input/Checkpoints tab , in the Input pane, enter the values for the Add operation.

    • In the Value column for A, enter 10.

    • In the Value column for B, enter 6.

  2. In the Input/Checkpoints tab, enter the values for the Multiply operation.

    • In the Value column for A, enter 2.

    • In the Value column for B, enter 4.

Back to top

Create parameters for the Custom Code activity

  1. In the Edit Input/Output Property/Parameter Dialog Box (API Testing), enter the details for the input parameter.

    • In the Name field, enter AddResult.

    • In the Type field, select String from the drop-down list (if it is not already selected).

    A new input property called AddResult appears in the Input pane inside the Input/Checkpoints tab.

  2. Create another input parameter called MulResult:

    • In the Name field, enter MulResult.

    • In the Type field, select String from the drop-down list (if it is not already selected).

    A new input property called MulResult appears in the Input pane inside the Input/Checkpoints tab.

  3. Click the Add button again and select Add Output Property.

  4. Enter the details for the output parameter.

    • In the Name field, enter Result.

    • In the Type field select Decimal from the drop-down list.

  5. In the Checkpoints pane, enter the value of 128.

Back to top

Link the Custom Code activity to existing steps

  1. In the Select Link Source Dialog Box (API Testing), select the AddActivity step. In the right pane, select Result and click OK.

    The link source Step.OutputProperties.AddActivity<number>.Result appears in the AddResult row.

  2. In the dialog, select the Multiply step. In the right pane, select Result and click OK.

    The link source Step.OutputProperties.MultiplyActivity<number>.Result appears in the MulResult.

Back to top

Create events for the Custom Code activity

  1. In the Properties pane, select the CustomCode activity from the drop-down list or by clicking the CustomCode activity in the canvas.

  2. In the Properties pane, select the Events tab.

  3. In the Events Tab (Properties Pane - API Testing), create a default handler for ExecuteEvent and AfterExecuteStepEvent. Two events, CodeActivity<number>_OnExecuteEvent and CodeActivity<number>_OnAfterExecuteEvent are added to the TestUserCode.cs file.

  4. In the TestUserCode.cs file, enter the following text in the CodeActivity_OnExecuteEvent:

    decimal AddResult = AddActivity.Result;
    decimal MulResult = MultiplyActivity.Result;
    CodeActivity<number>.Output.Result = AddResult*MulResult;
  5. Enter a breakpoint on the last line of this method.

  6. In the TestUserCode.cs file, enter the following text in the CodeActivity<number>_OnAfterExecuteStepEvent:

    decimal result = CodeActivity<number>.Output.Result;
  7. Enter a breakpoint on the last line of this method.

  8. Save the test.

Back to top

Run the test

Select Run > Run or press F5.

Back to top

Check the value of the variables at the first breakpoint

  1. When the test run stops at the first breakpoint, select View > Debug > Local Variables to open the Local Variables Pane.

  2. In the Local Variables pane, see the current values of the Add and Multiply activity. The current values should be 16 for the AddActivity and 8 for the MultiplyActivity.

    You can expand the notes on various rows to see the variable values of the different items used in your test run.

Back to top

Add a variable to the Watch Pane

  1. In the TestUsercode.cs tab, highlight the text AddResult.

  2. Open the Watch pane by selecting View > Debug > Watch.

  3. In the Watch Pane, click the Add New Watch Expression button and enter Add Result.

    A line with the expression AddResult, with a value of 16, and type Decimal appears.

  4. Click the Add New Watch Expression button again to add the variable MulResult to the Watch pane. The pane should display the expression MulResult, with a value of 8, and type Decimal.

Back to top

Check the value of the variables at the next breakpoint

  1. Continue the run session by selecting Run > Continue or pressing F5.

  2. When the run session pauses at the next breakpoint, highlight the text CodeActivity<number>.Output.Result and add it to the Watch pane.

    The pane displays that the value of this variable is 128.

    Note that the AddResult and MulResult values, which you added in the previous step to the Watch pane, are undefined with a type Incorrect Expression. This is because these values are present and relevant to the current event.

  3. Click the Local Variables tab. Note that the line Result displays a value of 128, since the custom code entered earlier noted that Result is equal to CodeActivity6.Output.Result, which was equal to AddResult*MulResult.

    In addition, if you hover over the variable names in the Editor in the paused run session, an expandable tooltip displaying the current value of the variable and its properties can be viewed.

  4. Select Run > Continue to complete the run session and view the run results.

Back to top