To use loops in Functionize, you can either record them in Architect or add them to an existing test from the Test Detail page.
Recording loops in Architect
- Open a new Architect test case and begin recording up to the point of adding in the Loop sequence.
- 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.
- Select Loops.
- Select X Times to define the number of iterations or Custom JS for the operation of the Loop.
- Click Insert.
Adding loops to an existing test from the Test Detail page
- Open the Test Detail page.
- Open the Insert Menu above the first Action of your Loop sequence.
- Click Loop.
- In the dialog box, choose Custom JS or X Times and enter the appropriate information.
- Regardless of which Loop method you select, you will then select the Loop End Action from the drop-down menu.
- Click Insert.
Examples of using loops in Functionize
- Iterating over a list of elements
For example, you could use a loop to iterate over a list of products on a website and add them to your shopping cart.
# Create a list of products to add to the cart.
products = ["Product A", "Product B", "Product C"]
# Loop over the list and add each product to the cart.
for product in products:
# Add the product to the cart.
fze.action("Add to Cart").execute(product)
- Repeating a set of actions until a condition is met
For example, you could use a loop to retry an action until it succeeds.
# Try to log in to a website.
fze.action("Login").execute("username", "password")
# If the login fails, retry it up to 3 times.
retry_count = 0
while retry_count < 3:
# Try to log in again.
fze.action("Login").execute("username", "password")
# If the login succeeds, break out of the loop.
if fze.action("IsLoggedIn").execute():
break
# Increment the retry count.
retry_count += 1
Tips for using loops in Functionize
- Use loops to automate repetitive tasks.
- Use loops to retry actions until they succeed.
- Use loops to iterate over collections of data, such as lists, arrays, and objects.
- Use loops to control the flow of your test.
To use loops, you first need to understand the different types of loops. There are two main types of loops:
- For loops:Â For loops are used to repeat a block of code a known number of times.
- While loops:Â While loops are used to repeat a block of code until a condition is met.
For loops
For loops have the following syntax:
for i in range(start, stop, step):
# Code to be repeated
i
 is a loop variable that will be incremented byÂstep
 on each iteration of the loop.start
 is the initial value of the loop variable.stop
 is the value that the loop variable will be less than or equal to on the last iteration of the loop.step
 is the amount by which the loop variable will be incremented on each iteration of the loop.
For example, the following code will print the numbers from 1 to 10:
for i in range(1, 11):
print(i)
While loops
While loops have the following syntax:
while condition:
# Code to be repeated
The condition
is evaluated at the beginning of each iteration of the loop. If the condition
is True
, the code inside the loop is executed. If the condition
is False
, the loop terminates.
For example, the following code will print the numbers from 1 to 10, but will stop if the user enters a number greater than 10:
user_input = 0
while user_input <= 10:
print(user_input)
user_input = int(input("Enter a number: "))
Nested loops
You can also nest loops, meaning that you can put one loop inside another loop. This is useful for complex tasks that require multiple levels of repetition.
For example, the following code will print a multiplication table:
for i in range(1, 11):
for j in range(1, 11):
print(i * j, end=" ")
print()
This code will print a 10×10 multiplication table, with each row representing the multiples of a different number.
Using loops in programming
Loops can be used to automate a wide variety of tasks in programming. For example, you can use loops to:
- Iterate over a list of elements
- Repeat a set of actions until a condition is met
- Control the flow of your program
Loops are an essential part of any programming language, and they can be used to write more efficient and reusable code.
Here are some examples of how loops can be used in programming:
- Iterating over a list of elements:
Suppose you have a list of names, and you want to print each name to the console. You can use a for loop to iterate over the list and print each name.
names = ["Alice", "Bob", "Carol"]
for name in names:
print(name)
- Repeating a set of actions until a condition is met:
Suppose you want to keep trying to log in to a website until you are successful. You can use a while loop to repeat the login attempt until you are logged in.
while not is_logged_in:
try_to_login()
- Controlling the flow of your program:
Suppose you want to write a program that plays a game of tic-tac-toe. You can use loops to control the flow of the game, such as by looping through the different players’ turns.
while not game_over:
current_player.take_turn()
# Check if the game is over.
if is_game_over():
break
Loops are a powerful tool that can be used to write more efficient and reusable code. By understanding how to use loops, you can improve your programming skills and write more complex and sophisticated programs.