Loops and Conditionals

Thorough testing often involves repeating certain tasks on your site to confirm consistency, accuracy, and functionality. Functionize offers Page Objects as one way to store common testing tasks such as flows for logging in, running a search, or navigating to a secondary location in the site.

Another option which offers more precision and flexibility is using Loops and Conditionals in your test cases. Common concepts in software development ("do...while", "do...until", and "if/else/else if" statements), Loops and Conditionals are methods to repeat certain actions to test a task more completely. Loops repeat for a specified number of times or until a certain condition is met, while Conditionals will execute or skip a selection of actions based on whether a given condition is met or unmet.

Use Case

Your online retail site's shopping cart had a bug early in development where the number of items added didn't match the display on screen. As part of your test suite you want to ensure this bug isn't reintroduced at any point. Below we will record a test in Architect to validate that as we add items to the cart, the display number increases accordingly. We'll use a Loop to add multiple items and check the display on each iteration.

An advanced use of Loops in this test would be to add a custom JavaScript Action within the Loop to verify that a projected value for the cart's total dollar amount matches the actual dollar amount once the item has been added to the cart. 

Items of Note

Once your test containing Loops is saved and executed, the Loop sequences are displayed in Read-Only format in the Browser Results tab of the Test Detail page and in Slider View.

Actions within Loops may only be modified from the Actions tab of the Test Detail page. This includes Adding, Deleting, and Editing Actions within a Loop.

Best practice for adding new Loops is from the Actions tab as well although they may be added from the Slider View or Browser Results tab. As the Actions tab is the only place to add, edit or delete the individual Actions within the Loop, or delete the Loop entirely, it's the ideal location to add a new Loop to your test.

Incorporating Loops in Architect

This example demonstrates the shopping cart use case mentioned above verifying that the cart's item count increases as the items are added.

  1. Open a new Architect test case and begin recording up to the point of adding in the Loop sequence.
  2. Record the Actions for the Loop sequence. Once you reach the end of the sequence you plan to Loop, click the plus sign icon to open the Actions panel.
  3. Select Loops.
  4. Select X Times or Custom JS for the operation of the Loop. X Times runs the loop for the specified number of iterations, and Custom JS can be used to set a specific condition for exiting the loop such as "run while this statement is true". For this example, X Times is selected and 10 is entered.
  5. Select the Actions of the Loop. There are multiple selection methods outlined in the Loops panel. For this example, the white dot next to the first and last Actions are selected.
  6. Click Save. Architect will display the Loop Actions grouped together. 
  7. For this example, the verification of the number displayed on the cart should increase as we add items. We'll open the Verify Action and change the text value from "1" to a custom expression of "{{fze.iteration}}" and save the update. This will now validate the display number against the current loop iteration being performed.
    Why is this included in the test? Perhaps the mismatch bug happens only when a large number of items have been added to the cart or maybe it appears randomly. Validating within each iteration can help track down unpredictable or unforeseen behaviors of your site.
  8. Record any other Actions needed in the test. For this example we've added a final verification that the total dollar amount displayed for the cart is equal to the total number of iterations adding the item multiplied by the price of the item being added.
  9. Save the test.

Viewing Completed Loops 

As noted above, the Browser Results tab and the Slider View will display Actions within a Loop in Read-Only format.

By default, all Loops seen on the Browser Results tab and Slider View will display the last iteration with the number of the iteration and the pass/fail status displayed on each Action. 

Browser Results tab

Slider View

Editing Loops

As noted above, the Actions tab in the Test Detail page is the only place to edit, add, or delete Actions within a Loop.

Edit within a Loop

Insert an Action within a Loop

Delete an Action within a Loop

Adding Loops to an Existing Test

As noted above, Loops may be added from the Browser Results tab or the Slider View. However, since they cannot be edited from either of those locations, it's best practice to add new Loops from the Actions tab as with Editing or Deleting Loops.

  1. Open the Actions tab of the Test Detail page.
  2. Open the Insert Menu above the first Action of your Loop sequence. Click Loop.
  3. In the dialog box, choose Custom JS or X Times and enter your JavaScript statement set to return a boolean true/false value, or the number of iterations.
  4. Select the Target of the Loop from the drop-down menu. The Target is the last Action included within the Loop.
  5. Click Insert.

Deleting Loops and Conditionals

The Loop and Conditional Actions can be deleted from the test to eliminate a Loop or Conditional sequence. Deleting the Action will return the included Actions to their usual state. 

NOTE: Deleting the Loop or Conditional Action does not delete each Action that had been part of the Loop or Conditional sequence.

  1. Open the Actions tab of the Test Detail page and find the Loop to be deleted.
  2. Open the Action Settings for the Loop Action.
  3. Click Delete. Confirm Deletion.

Viewing, Adding, Editing, and Deleting Conditionals

Conditionals are very similar to Loops with some important differences. Instead of repeating a sequence while or until a certain condition is met, or repeating for a fixed number of iterations, Conditionals evaluate whether the custom JS expression is true or false. The Actions within the Conditional will only be executed if that expression evaluates to true. 

NOTE: Conditionals may be edited, added, and deleted from the Browser Results tab, the Actions tab, or the Slider View whereas Loops must be altered on the Actions tab.

View Conditional result

Edit Conditional

Add Conditional

Delete Conditional