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 in the Keyword View as you would in other steps.

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 in the Keyword View:

  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 from the Keyword View:

    • If...Then

    • While...Wend

    • For...Next

    • Do...While

    • Do...Until

    In addition, if you want to use the Elself...Then or Else statements, switch to the Editor to add these statements from the Edit > Code Snippet menu command.

    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:

    • 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.

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

    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

Back to top

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.

  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 from the Keyword View:

    While...Wend. Performs a series of statements as long as a specified condition is True.

    For...Next. Uses a counter to perform a group of statements a specified number of times.

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

    Do...Until. Performs a series of statements indefinitely, until a specified condition becomes True.

  3. In the Value column, enter a required condition.

  4. To complete the loop statement:

    • 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 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

Back to top

See also: