Specifying or Modifying Property Values

You can specify or modify values for properties in the test object description. You can specify a value using a constant value (either a simple value or a constant value that includes regular expressions) or you can parameterize it. You can do this for objects in the local object repository using the Object Repository window or Object Properties dialog box, and for objects in the shared object repository using the Object Repository Manager.

You can also find and replace specific object property values

Note: In some cases, the Smart Identification mechanism may enable QuickTest to identify an object, even when some of its property values change. However, if you know about property value changes for a specific object, you should try to correct the object definition so that QuickTest can identify the object from its basic object description.

Tip: You can use the Object Spy at any time to view run-time or test object properties and values of the objects in the application you are testing. You open the Object Spy by choosing Tools > Object Spy or clicking the Object Spy toolbar button

To specify a property value:

  1. Select the test object whose property value you want to specify.
  2. In the Test object details area, click in the value cell for the required property.

Tip: For an object in the local object repository, you can also select the required test object and choose Edit > Step Properties > Object Properties, and then make the following property value changes in the Object Properties dialog box.

  1. Specify the property value in one of the following ways:
    • If you want to specify a constant value, enter it in the value cell.
    • If you want to parameterize the value or specify a constant value using a regular expression, click the parameterization button in the value cell. If you specify a constant value using a regular expression, the icon is displayed next to the value.
  2. If you specified a constant value, it is shown in the Value column of the Test object details area. If you parameterized the value, the parameter name is shown with one of the following icons in the Value column

Using For Each...Next in QTP

A For Each...Next loop is similar to a For...Next loop. Instead of repeating the statements a specified number of times, a For Each...Next loop repeats a group of statements for each item in a collection of objects or for each element of an array. This is especially helpful if you don't know how many elements are in a collection.

In the following HTML code example, the contents of a Dictionary object is used to place text in several text boxes.

<HTML>
<HEAD><TITLE>Forms and Elements</TITLE></HEAD>
<SCRIPT LANGUAGE="VBScript">
<!--
Sub cmdChange_OnClick
   Dim d   'Create a variable 
   Set d = CreateObject("Scripting.Dictionary")
   d.Add "0", "Athens"   'Add some keys and items
   d.Add "1", "Belgrade"
   d.Add "2", "Cairo"
 
   For Each I in d
      Document.frmForm.Elements(I).Value = D.Item(I)
   Next
End Sub
-->
</SCRIPT>
<BODY>
<CENTER>
<FORM NAME="frmForm"
 
<Input Type = "Text"><p>
<Input Type = "Text"><p>
<Input Type = "Text"><p>
<Input Type = "Text"><p>
<Input Type = "Button" NAME="cmdChange" VALUE="Click Here"><p>
</FORM>
</CENTER>
</BODY>
</HTML>

Using For...Next in QTP

You can use For...Next statements to run a block of statements a specific number of times. For loops, use a counter variable whose value increases or decreases with each repetition of the loop.

The following example causes a procedure called MyProc to execute 50 times. The For statement specifies the counter variable x and its start and end values. The Next statement increments the counter variable by 1.

Sub DoMyProc50Times()
   Dim x
   For x = 1 To 50
      MyProc
   Next
End Sub

Using the Step keyword, you can increase or decrease the counter variable by the value you specify. In the following example, the counter variable j is incremented by 2 each time the loop repeats. When the loop is finished, the total is the sum of 2, 4, 6, 8, and 10.

Sub TwosTotal()
   Dim j, total
   For j = 2 To 10 Step 2
      total = total + j
   Next
   MsgBox "The total is " & total
End Sub

To decrease the counter variable, use a negative Step value. You must specify an end value that is less than the start value. In the following example, the counter variable myNum is decreased by 2 each time the loop repeats. When the loop is finished, total is the sum of 16, 14, 12, 10, 8, 6, 4, and 2.

Sub NewTotal()
   Dim myNum, total
   For myNum = 16 To 2 Step -2
      total = total + myNum
   Next
   MsgBox "The total is " & total
End Sub

You can exit any For...Next statement before the counter reaches its end value by using the Exit For statement. Because you usually want to exit only in certain situations, such as when an error occurs, you should use the Exit For statement in the True statement block of an If...Then...Else statement. If the condition is False, the loop runs as usual.

Using Do Loops in QTP

You can use Do...Loop statements to run a block of statements an indefinite number of times. The statements are repeated either while a condition is True or until a condition becomes True.

Repeating Statements While a Condition is True

Use the While keyword to check a condition in a Do...Loop statement. You can check the condition before you enter the loop (as shown in the following ChkFirstWhile example), or you can check it after the loop has run at least once (as shown in the ChkLastWhile example). In the ChkFirstWhile procedure, if myNum is set to 9 instead of 20, the statements inside the loop will never run. In the ChkLastWhile procedure, the statements inside the loop run only once because the condition is already False.

Sub ChkFirstWhile()
   Dim counter, myNum
   counter = 0
   myNum = 20
   Do While myNum > 10
      myNum = myNum - 1
      counter = counter + 1
   Loop
   MsgBox "The loop made " & counter & " repetitions."
End Sub
 
Sub ChkLastWhile()
   Dim counter, myNum
   counter = 0
   myNum = 9
   Do
      myNum = myNum - 1
      counter = counter + 1
   Loop While myNum > 10
   MsgBox "The loop made " & counter & " repetitions."
End Sub

Repeating a Statement Until a Condition Becomes True

There are two ways to use the Until keyword to check a condition in a Do...Loop statement. You can check the condition before you enter the loop (as shown in the following ChkFirstUntil example), or you can check it after the loop has run at least once (as shown in the ChkLastUntil example). As long as the condition is False, the looping occurs.

Sub ChkFirstUntil()
   Dim counter, myNum
   counter = 0
   myNum = 20
   Do Until myNum = 10
      myNum = myNum - 1
      counter = counter + 1
   Loop
   MsgBox "The loop made " & counter & " repetitions."
End Sub
 
Sub ChkLastUntil()
   Dim counter, myNum
   counter = 0
   myNum = 1
   Do
      myNum = myNum + 1
      counter = counter + 1
   Loop Until myNum = 10
   MsgBox "The loop made " & counter & " repetitions."
End Sub

Exiting a Do...Loop Statement from Inside the Loop

You can exit a Do...Loop by using the Exit Do statement. Because you usually want to exit only in certain situations, such as to avoid an endless loop, you should use the Exit Do statement in the True statement block of an If...Then...Else statement. If the condition is False, the loop runs as usual.

In the following example, myNum is assigned a value that creates an endless loop. The If...Then...Else statement checks for this condition, preventing the endless repetition.

Sub ExitExample()
   Dim counter, myNum
      counter = 0
      myNum = 9
      Do Until myNum = 10
         myNum = myNum - 1
         counter = counter + 1
         If myNum < 10 Then Exit Do
      Loop
      MsgBox "The loop made " & counter & " repetitions."
End Sub

Making Decisions with Select Case

The Select Case structure provides an alternative to If...Then...ElseIf for selectively executing one block of statements from among multiple blocks of statements. A Select Case statement provides capability similar to the If...Then...Else statement, but it makes code more efficient and readable.

A Select Case structure works with a single test expression that is evaluated once, at the top of the structure. The result of the expression is then compared with the values for each Case in the structure. If there is a match, the block of statements associated with that Case is executed, as in the following example.

Select Case Document.Form1.CardType.Options(SelectedIndex).Text
   Case "MasterCard"
      DisplayMCLogo
      ValidateMCAccount
   Case "Visa"
      DisplayVisaLogo
      ValidateVisaAccount
   Case "American Express"
      DisplayAMEXCOLogo
      ValidateAMEXCOAccount
   Case Else
      DisplayUnknownImage
      PromptAgain
End Select

Notice that the Select Case structure evaluates an expression once at the top of the structure. In contrast, the If...Then...ElseIf structure can evaluate a different expression for each ElseIf statement. You can replace an If...Then...ElseIf structure with a Select Case structure only if each ElseIf statement evaluates the same expression.

Making Decisions Using If...Then...Else

The If...Then...Else statement is used in QTP to evaluate whether a condition is True or False and, depending on the result, to specify one or more statements to run. Usually the condition is an expression that uses a comparison operator to compare one value or variable with another. . If...Then...Else statements can be nested to as many levels as you need.

Running Statements if a Condition is True

To run only one statement when a condition is True, use the single-line syntax for the If...Then...Else statement. The following example shows the single-line syntax. Notice that this example omits the Else keyword.

Sub FixDate()
    Dim myDate
    myDate = #2/13/95#
    If myDate < Now Then myDate = Now
End Sub

To run more than one line of code, you must use the multiple-line (or block) syntax. This syntax includes the End If statement, as shown in the following example:

Sub AlertUser(value)
   If value = 0 Then
      AlertLabel.ForeColor = vbRed
      AlertLabel.Font.Bold = True
      AlertLabel.Font.Italic = True
   End If
End Sub

Running Certain Statements if a Condition is True and Running Others if a Condition is False

You can use an If...Then...Else statement to define two blocks of executable statements: one block to run if the condition is True, the other block to run if the condition is False.

Sub AlertUser(value)
   If value = 0 Then
      AlertLabel.ForeColor = vbRed
      AlertLabel.Font.Bold = True
      AlertLabel.Font.Italic = True
   Else
      AlertLabel.Forecolor = vbBlack
      AlertLabel.Font.Bold = False
      AlertLabel.Font.Italic = False
   End If
End Sub

Copying an Object to the Local Object Repository

If you want to modify an object stored in a shared object repository, you can modify it using the Object Repository Manager, or you can modify it locally using the Object Repository window.

If you modify it using the Object Repository Manager, the changes you make will be reflected in all components that use the shared object repository. If you make a local copy of the object and modify it in the Object Repository window, the changes you make will affect only the component in which you make the change. If you later modify the same object in the shared object repository, your changes will not affect the local copy of the object in your component.

In addition, during a run session, QuickTest will use the object in the local object repository because when an component uses an object contained in both local and shared repositories, QuickTest always searches for and uses objects in the local object repository before using objects in a shared object repository.

When copying an object to the local object repository, consider the following:

  • When you copy an object to the local object repository, its parent objects are also copied to the local object repository.
  • If an object or its parent objects use unmapped repository parameters, you cannot copy the object to the local object repository. You must make sure that all repository parameters are mapped before copying an object to the local object repository.
  • If an object or its parent objects are parameterized using one or more repository parameters, the repository parameter values are converted when you copy the object to the local object repository. For example, if the repository parameter is mapped to a local parameter, the property is parameterized using a local parameter. If the value is a constant value, the property receives the same constant value.
  • If you are copying multiple objects to the local object repository, during the copy process you can choose to skip a specific object if it has unmapped repository parameters, or if it has mapped repository parameters whose values you do not want to convert. You can then continue copying the next object from your original selection.

To copy an object to the local object repository:

  1. In the Object Repository window, select an object from a shared object repository that you want to copy to the local object repository. Objects in a shared object repository are colored gray. You can select more than one object to copy, as long as the selected objects have the same parent objects.
  2. Choose Object > Copy to Local or right-click the object(s) and choose Copy to Local. The object(s) (and parent objects) are copied to the local object repository and are made editable.

Deciding Whether to Use Local or Shared Object Repositories

To choose where to save objects, you need to understand the differences between local and shared object repositories.

In general, the local object repository is easiest to use when you are creating simple record and run components, especially under the following conditions:

  • You have only one, or very few, components that correspond to a given application, interface, or set of objects.
  • You do not expect to frequently modify test object properties.

Conversely, the shared object repository is generally the preferred option when:

  • You are creating components using keyword-driven methodologies (not using record).
  • You have several components that test elements of the same application, interface, or set of objects.
  • You expect the object properties in your application to change from time to time and/or you regularly need to update or modify test object properties.
Understanding the Local Object Repository

When you use a local object repository, QuickTest uses a separate object repository for each component. (You can also use one or more shared object repositories if needed. The local object repository is fully editable from within its component.

When working with a local object repository:

  • QuickTest creates a new (empty) object repository for each component.
  • As you record operations on objects in your application, QuickTest automatically stores the information about those objects in the corresponding local object repository (if the objects do not already exist in an associated shared object repository).

    QuickTest adds all new objects to the local object repository even if one or more shared object repositories are already associated with the component. (This assumes that an object with the same name and description does not already exist in one of the associated shared object repositories).
  • If a child object is added to a local object repository, and its parents are in a shared object repository, its parents are automatically moved to the local object repository.
  • Every time you create a new component, QuickTest creates a new, corresponding local object repository and begins adding test objects to the local object repository as you record or learn objects.
  • If you learn or record on the same object in your application in two different components, the object is stored as a separate test object in each of the local object repositories.
  • When you save your component, the local object repository is automatically saved with the it. The local object repository is not accessible as a separate file (unlike the shared object repository).
Understanding the Shared Object Repository

When you use shared object repositories, QuickTest uses the shared object repositories you specified for the selected component's application area. You can use one or more shared object repositories. (You can also save some objects in a local object repository for each component if you need to access them only from the specific component.

After you begin creating your component, you can specify additional shared object repositories. You can also create new ones and associate them with your component. Before running the component, you must ensure that the object repositories being used by the component contain all the objects in your component. Otherwise, the component may fail.

You modify a shared object repository using the Object Repository Manager.

When working with a shared object repository:

  • If you record operations on an object that already exists in either the shared or local object repository, QuickTest uses the existing information and does not add the object to the object repository.
  • If a child object is added to a local object repository, and its parents are in a shared object repository, its parents are automatically moved to the local object repository.
  • QuickTest does not add an object to the shared object repository as you record operations on it. Instead, it adds new objects to the local object repository (not the shared object repository) as you learn objects or record steps on them (unless those same objects already exist in an associated shared object repository).

You can export the local objects to a shared object repository.

You can also merge the local objects directly to a shared object repository that is associated with the same component. This can reduce maintenance since you can maintain the objects in a single shared location, instead of multiple locations.