Conditional and loop statements

Relevant for: GUI actions, scripted GUI components and keyword GUI components

Use conditional statements and loop statements to add decision making and iterations to your tests. Add them to your tests and components as you would add other steps.

Versions 26.3 and later: Python code snippets are available in the menu.

Version 26.1: Enter conditional and loop statements in your Python scripts manually.

Editor and Keyword View

You can add conditional and loop statements in the Editor or Keyword View.

The Keyword View is available for VBScript only.

The tasks described on this page provide details for adding steps in the Keyword View, but you can type or record the same steps in the Editor.

Conditional statements

Conditional statements perform a step or a series of steps based on specific conditions. If a condition is not fulfilled, the next Elseif condition or Else statement is examined.

To add a conditional statement:

  1. Select the step before which you want to add the conditional statement.

  2. Select Edit > Code Snippet, and select the statement you want to add.

    The following conditional statements are available:

    VBScript Python View

    If...Then

    if Keyword View and Editor
    Else else Editor only
    Elself...Then elif Editor only

    Each part of the conditional statement is added as a separate step.

    For example, select If to add an If statement. After defining the details of the If statement, add a Then statement as a separate step.

  3. In the row for the new conditional statement, complete the step details in the Editor, or build it in the Keyword View, as follows:

    • Click in the Item cell, and select the object on which you want to perform the conditional statement.

    • Click in the Operation cell, and select the operation you want to perform.

    • If needed, click in the Value cell and enter the required condition.

  4. Add the second part of your conditional statement, for example a Then statement.

    Type the step in the Editor or build it in the Keyword View, as follows:

    1. Right-click the new conditional statement and select Insert New Step After Block.

    2. Set the values for the new step in the Operation and Value columns.

    You can also record steps. After adding a conditional statement, all recorded steps are automatically inserted within the conditional statement block.

    Example:

  5. If the conditional statement replaces the statement just before it, delete the row immediately above the new statement.

    Example:

  6. Complete a statement with an Else statement, or by nesting an additional level in your statement.

    Select the new statement, and then select Edit > Code Snippet, and select the new statement you want to add.

    Example: The statements below check that the Username edit box exists in the Advantage Online Shopping site.

    If the edit box exists, Then a user name is entered; Else a message is sent to the Run Results.

    The same example is displayed in the Editor as follows:

    If Browser("Advantage Shopping").Page("Advantage Shopping").WebEdit("username").Exist  Then
    	Browser("Advantage Shopping").Page("Advantage Shopping").WebEdit("username").Set DataTable("p_UserName", dtGlobalSheet)
    Else 
    Reporter.ReportEvent micFail, "UserName Check", "The User Name field does not exist."
    End If

Loop statements

Use loop statements to run a group of steps repeatedly, while or until a condition is true, or a specific number of times without any conditions.

To add a loop statement:

  1. Select the step before which you want to add the loop statement.

  2. Select Edit > Code Snippet, and select the statement you want to add.

    The following loop statements are available:

    While...Wend (VBScript) / while (Python) . Performs a series of statements as long as a specified condition is True.

    For...Next (VBScript) / for (Python) . Uses a counter to perform a group of statements a specified number of times.

    Do...While (VBScript only). Performs a series of statements indefinitely, as long as a specified condition is True.

    Do...Until (VBScript only). Performs a series of statements indefinitely, until a specified condition becomes True.

  3. Enter a required condition by typing it in the Editor, or in the Keyword View in the Value column.

  4. To complete the loop statement, do one of the following:

    • Type a step in the Editor, within the loop statement structure.

    • Select the loop statement step and record a new step to add it to your loop statement.

    • Select the loop statement step and right click and then select Insert New Step.

Example: The following example counts the number of items in a list and then selects them one by one.

After each of the items has been selected, the test continues.

The same VBScript example is displayed in the Editor as follows:

itemsCount = Browser("Welcome").Page("Find a Flight").WebList("toDay").GetROProperty("items count")
For i = 1 To ItemsCount-1
	ItemName = Browser("Welcome").Page("Find a Flight").WebList("toDay").GetItem(i)
	Browser("Welcome").Page("Find a Flight").WebList("toDay").Select ItemName
Next

See also