Debug a function - Exercise

Relevant for: GUI actions. scripted GUI components, and function libraries

In this exercise, you create and debug an action or a function, to practice using some of UFT's debugging capabilities for GUI tests.

Suppose you create an action or a function that defines variables that are used in other parts of your test or function library. You can add breakpoints to the action or function to see how the value of the variables changes as the test or function library runs. To see how the test or function library handles the new value, you can also change the value of one of the variables during a breakpoint.

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

Create a new action or function

  1. Create or open a function library.

  2. Create a new function called SetVariables.

    Enter the VBScript code in the Editor of your action or the function library, as follows:

    Function SetVariables
    
    Dim a
    a="hello"
    b="me"
    MsgBox a
    
    EndFunction

Back to top

Associate the function library with a test)

  1. Bring the function library into focus.

  2. Right-click the function library document tab and select Associate Library '<Function Library Name>' with '<Test Name>'. UFT associates the function library with your test or application area.

Back to top

Add a call to the function in your test

Add a call to the function by typing the following in the Editor:

SetVariables 

Back to top

Add breakpoints

Click in the left margin of the lines containing the text b="me" and MsgBox a.

Back to top

Begin running the test or component

Run the test. The test stops at the first breakpoint, before executing that step (line of script).

Back to top

Check the value of the variables in the debug panes

  1. Select View > Debug > Watch to open the Watch pane.

  2. In the Editor of your test action or in the function library, highlight the variable a and select Run > Add to Watch.

    UFT adds the variable a to the Watch pane. The Value column indicates that the value of a is currently "hello", because the breakpoint stopped after the value of variable a was initiated. The Type column indicates that a is a String variable.

  3. In the Editor displaying your test action or in the function library, highlight the variable b and select Run > Add to Watch.

    UFT adds the variable b to the Debug Watch pane. The Value column indicates <Variable is undefined: 'b'> (and the Type column displays Incorrect expression), because the test or component stopped before variable b was declared.

  4. Select View > Debug > Local Variables to open the Local Variables pane.

    Only variable a is displayed (with the value "hello"), because a is the only variable that was initiated up to this point.

    Variable b is not displayed because the test or component stopped before variable b was declared.

Back to top

Check the value of the variables at the next breakpoint

Click the Run button to continue running the test.

The test stops at the next breakpoint. Note that the values of variables a and b were both updated in the Watch and Local Variables panes.

Back to top

Modify the value of a variable using the Console pane

  1. Select View > Debug > Console.

  2. At the command prompt at the bottom of the pane, enter:if b="me" then a="b is me" else a="b is you" end if. Then press Enter on the keyboard.

  3. Click the Local Variables pane to verify that the value of variable a was updated according to the command you entered and now displays the value: "b is me"

  4. Click the Run button to continue running the test.

    The message box that opens displays "b is me" (which is the modified value of a). This indicates that you successfully modified the values of both a and b using the Debug Console pane.

  5. Click OK to close the message box.

Back to top

Repeat a command from the command history

  1. Remove the first breakpoint and run the test or component again.

    When the test at the breakpoint (before displaying the message box), modify the value of variable b in the Console pane by running the command b="not me".

  2. In the Console pane, highlight the command line that reads if b="me" then a="b is me" else a="b is you". Then right-click and select Copy.

  3. In the command prompt, right-click and select Paste.

  4. Press Enter to run the command, and then click the Run button to complete the test or component run.

    A message box saying "b is you" opens. Click OK to close the message box.

Back to top