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
-
Create an API test.
-
From the Toolbox Pane, in the Math section, drag the Add activity, Multiply activity, and the Custom Code activity to the canvas.
Set properties for the math steps
-
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.
-
-
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.
-
Create parameters for the Custom Code activity
-
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.
-
-
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.
-
-
Click the Add button again and select Add Output Property.
-
Enter the details for the output parameter.
-
In the Name field, enter Result.
-
In the Type field select Decimal from the drop-down list.
-
-
In the Checkpoints pane, enter the value of 128.
Link the Custom Code activity to existing steps
-
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.
-
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.
Create events for the Custom Code activity
-
In the Properties pane, select the CustomCode activity from the drop-down list or by clicking the CustomCode activity in the canvas.
-
In the Properties pane, select the Events tab.
-
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.
-
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;
-
Enter a breakpoint on the last line of this method.
-
In the TestUserCode.cs file, enter the following text in the CodeActivity<number>_OnAfterExecuteStepEvent:
decimal result = CodeActivity<number>.Output.Result;
-
Enter a breakpoint on the last line of this method.
-
Save the test.
Run the test
Select Run > Run or press F5.
Check the value of the variables at the first breakpoint
-
When the test run stops at the first breakpoint, select View > Debug > Local Variables to open the Local Variables Pane.
-
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.
Add a variable to the Watch Pane
-
In the TestUsercode.cs tab, highlight the text AddResult.
-
Open the Watch pane by selecting View > Debug > Watch.
-
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.
- 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.
Check the value of the variables at the next breakpoint
-
Continue the run session by selecting Run > Continue or pressing F5.
-
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.
-
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.
-
Select Run > Continue to complete the run session and view the run results.