Skip to main content

Testing Rules

In this section, you will learn how to add flow control conditions to optimize the testing process.

Developers can now add flow control conditions (such as loops, conditional statements, waits, and grouping) within test scenarios. This enhancement caters to the utilization of more intricate test scenario/process configurations, utilizing automated testing capabilities to address the complexities of intricate scenarios.

Group

When multiple steps in a test process are related, they can be categorized and placed into the same group. By grouping test steps, the test scene becomes more readable and operable.

Example: Categorize steps such as view details pet details, edit pet information, and view details again.

    1. Click the button "Add Step" at the bottom and select "Group".
    1. Drag the steps you want to categorize, under Grouped Steps, or add steps directly in the group.

ForEach Loop

A ForEach loop allows the repeated execution of steps based on the number of elements in a specified array, with the number of iterations matching the number of elements in the array. This loop supports extracting both the value of the current element and the index of the current iteration.

In the ForEach Loop's advanced setting, you can can also customize exception handling options for Break If and On Error. For more information, see Judgment Rules.

Example: There are two endpoints to get a list of pet information and to get individual pet details. Our requirement is to query the details of the pet that has just been added to the pet list, and we can use the ForEach loop to orchestrate this scenario in our automated test by following the actions below.

  1. The first step outside the loop is to request the Get Pet List endpoint to get the actual list data. Typically in this scenario there is an array within the endpoint covering basic information about multiple pets, such as pet IDs and names.

  2. Set up the ForEach loop, configuring the ForEach loop to use the array part of the response from the initial request. In this example, writing data.tags[*].id can extract all id values within tags as an array. Testing Process Scenario

  3. Inside the loop, add a request to the pet details endpoint, setting the request parameter ID to use the current element value from the ForEach loop. Testing Process Scenario

  4. If the list returns three pet information entries (three elements in the array), the ForEach loop will execute three times. Each execution will make an actual request with the ID value from the array returned by the initial endpoint. Testing Process Scenario

  5. After running the "test scenario", you can view the "test steps" in the generated test report. The details page will show that the actual request values in the ForEach loop match the response data from the initial endpoint, ensuring consistency in the automated testing process. Testing Process Scenario

tip

Current Loop Element

The system automatically extracts elements from the array specified in the ForEach loop and stores them in the designated variable. At the start of each iteration, this variable is updated with the current element's value from the array. If the element is an object, you can use JSONPath to extract a specific subfield, such as {{$.1.element.data.name}}.

Current Loop Index

The index of the current loop is stored in this variable. It starts at 0 and increments by 1 at the beginning of each subsequent iteration, reflecting the current index.

In addition to being visually extracted through the Dynamic Value interaction box, the above two variables also support the use of variable expressions.

For Loop

To repeat a test step, you can specify the number of loops. In the loop's advanced settings, you can also customize how exceptions are handled with options like Break If and On Error. For details on how Break If works, refer to Judgment Rules.

Example: A pet store owner logs into the pet inventory management console at the end of the business day, reviews the details of each of the 10 pets sold that day, and updates their status to sold.

    1. Click the button "Test Steps" at the bottom and select "For Loop".
    1. Enter the desired number of loops as 10.
    1. Drag the test step into the inner box of the condition or add the test step directly under the condition.

If

When the test process involves multiple conditional checks, you can control the steps by adding conditional statements (If). When the specified conditions are met, the corresponding step will be executed; otherwise, it will be skipped. For more specific details, please refer to Judgment Rules.

Example: The pet store owner determines that the pet is sold based on yesterday's sales and sets the pet's sale status to "Sold". Otherwise, query the list for sale.

    1. Click the button "Tests Step" at the bottom and select Conditional branch.
    1. In the input box behind the lf condition, fill in the variable saleStatus obtained by the request API, select the condition "Equals", and finally enter the comparison value as true. (When the sales status is judged to be true, change the pet information to "Sold")
    1. Hover over the conditional branch to stop "+ Else", click and add the "Pet List for Sale" step (otherwise, query the list of pets on sale.) )
    1. Drag the test step into the appropriate conditional branch.

Judgment Rules

When using the If statement in a test scenario, you can define conditional rules to control test execution. If the conditions are met, the corresponding step will run; otherwise, it will be skipped. Likewise, in the Break If settings for ForEach and For loops, you can set judgment rules to control the execution flow more flexibly.

RuleDescription
EqualsChecks if two values are equal.
Does not equalChecks if two values are not equal.
ExistsChecks if a field or variable exists.
Does not existChecks if a field or variable does not exist.
Less thanChecks if one value is less than another.
Less than or equalChecks if one value is less than or equal to another.
Greater thanChecks if one value is greater than another.
Greater than or equalChecks if one value is greater than or equal to another.
Matches with RegexChecks if a string matches the specified regular expression.
ContainsChecks if a string or array contains the specified value.
Does not containChecks if a string or array does not contain the specified value.
Is emptyChecks if a field, array, or variable is empty.
Is not EmptyChecks if a field, array, or variable is not empty.
In ListChecks if a value belongs to a specified List.
Not in ListChecks if a value does not belong to a specified List.

When performing conditional checks (such as greater than, greater than or equal, equals, or not equals), if the values being compared are integers or strings, the system will automatically convert strings to numbers for accurate comparison. For example, if you compare a string "18" with the number 18, the system will convert the string "18" to the number 18 before comparing.


Equals

Checks if two values are equal. If they are equal, the condition is met and the test step will be executed; otherwise, it will be skipped.

  • Example:
    • Scenario: Check if user age extracted from pre-step is equal to 18.
    • Condition Example: {{$.5.response.body.data.age}} Equals 18
    • Actual Execution Effect: If the extracted age value from pre-step is 18 (the string will be automatically converted to a number), the step will be executed, such as displaying specific information or performing certain actions.

Does not equal

Checks if two values are not equal. If they are not equal, the condition is met and the test step will be executed; otherwise, it will be skipped.

  • Example:
    • Scenario: Check if the order status extracted from pre-step is not equal to "Paid".
    • Condition Example: {{$.4.response.body.data.status}} Does not equal Paid
    • Actual Execution Effect: If the order status extracted from pre-step is another value (such as "Pending" or "Shipped"), the step will be executed.

Exists

Checks if a certain field or variable exists. If it exists, the condition is met and the step will be executed; otherwise, it will be skipped.

  • Example:
    • Scenario: Check if the email field exists in the user data extracted from pre-step.
    • Condition Example: {{$.3.response.body.data.email}} Exists
    • Actual Execution Effect: If the email field is present in the user data extracted from pre-step, the step will be executed.

Does not exist

Checks if a certain field or variable does not exist. If it does not exist, the condition is met and the step will be executed; otherwise, it will be skipped.

  • Example:
    • Scenario: Check if the phone field does not exist in the user data extracted from pre-step.
    • Condition Example: {{$.2.response.body.data.phone}} Does not exist
    • Actual Execution Effect: If the phone field is absent in the user data extracted from pre-step, the step will be executed.

Less Than

Checks if one value is less than another. If it is, the condition is met and the step will be executed; otherwise, it will be skipped.

  • Example:
    • Scenario: Check if the product stock extracted from pre-step is less than 10.
    • Condition Example: {{$.1.response.body.data.stock}} Less Than 10
    • Actual Execution Effect: If the stock value extracted from the pre-step is 8, the condition is met and the step will be executed.

Less than or equal

Checks if one value is less than or equal to another. If it is, the condition is met and the step will be executed; otherwise, it will be skipped.

  • Example:
    • Scenario: Check if the age extracted from pre-step is less than or equal to 12.
    • Condition Example: {{$.2.response.body.data.age}} Less than or equal 12
    • Actual Execution Effect: If the age value extracted from pre-step is 10, the condition is met and the step will be executed.

Greater than

Checks if one value is greater than another. If it is, the condition is met and the step will be executed; otherwise, it will be skipped.

  • Example:
    • Scenario: Check if the order amount extracted from pre-step is greater than 1000.
    • Condition Example: {{$.1.response.body.data.amount}} Greater than 1000
    • Actual Execution Effect: If the order amount value extracted from pre-step is 1105, the condition is met and the step will be executed.

Greater than or equal

Checks if one value is greater than or equal to another. If it is, the condition is met and the step will be executed; otherwise, it will be skipped.

  • Example:
    • Scenario: Check if the user points extracted from pre-step are greater than or equal to 500.
    • Condition Example: {{$.3.response.body.data.points}} Greater than or equal 500
    • Actual Execution Effect: If the points' value extracted from pre-step is 600, the condition is met and the step will be executed.

Matches with Regex

Checks if a string matches a specified regular expression. If it matches, the condition is met and the step will be executed; otherwise, it will be skipped.

  • Example:
    • Scenario: Check if the email format extracted from pre-step is correct.
    • Condition Example: {{$.2.response.body.data.email}} Matches with Regex /^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}$/i
    • Actual Execution Effect: If the extracted email format from pre-step matches the regular expression (for example, test@gmail.com), the step will be executed.
Tip

Regular expressions should be written using "literal" syntax, where the pattern is enclosed in /, with optional modifiers (e.g., g for global matching, i for case-insensitive) appended at the end. For more information on writing regular expressions, you can refer to the MDN documentation.


Contains

Checks if a string or array contains a specified value. If it does, the condition is met and the step will be executed; otherwise, it will be skipped.

  • Example:
    • Scenario: Check if the user role list extracted from pre-step contains the "admin" role.
    • Condition Example: {{$.3.response.body.data.roles}} Contains admin
    • Actual Execution Effect: If the extracted role list from pre-step contains "admin", the step will be executed.

Does not contain

Checks if a string or array does not contain a specified value. If it does not, the condition is met and the step will be executed; otherwise, it will be skipped.

  • Example:
    • Scenario: Check if the user's shopping cart extracted from pre-step does not contain a certain product.
    • Condition Example: {{$.4.response.body.data.cartItems}} Does not contain productId123
    • Actual Execution Effect: If the extracted cart array from pre-step does not contain the product productId123, the step will be executed.

Is empty

Checks if a field, array, or variable is empty. If it is, the condition is met and the step will be executed; otherwise, it will be skipped.

  • Example:
    • Scenario: Check if the remarks field extracted from pre-step is empty.
    • Condition Example: {{$.2.response.body.data.remarks}} Is empty
    • Actual Execution Effect: If the extracted remarks field from pre-step is empty, the step will be executed.

Is not Empty

Checks if a field, array, or variable is not empty. If it is not empty, the condition is met and the step will be executed; otherwise, it will be skipped.

  • Example:
    • Scenario: Check if the order remarks extracted from pre-step are filled out.
    • Condition Example: {{$.1.response.body.data.orderRemarks}} Is not Empty
    • Actual Execution Effect: If the extracted order remarks from pre-step are not empty, the step will be executed; otherwise, it will be skipped.

In List

Checks if a value belongs to a specified list. If it does, the condition is met and the step will be executed; otherwise, it will be skipped.

  • Example:
    • Scenario: Check if the product selected by the user extracted from pre-step is in the recommended product list.
    • Condition Example: {{$.3.response.body.data.productId}} In List ["prod123", "prod456", "prod789"]
    • Actual Execution Effect: If the product ID extracted from pre-step is "prod456", the step will be executed.
tip

In Apidog, when entering a list, each element should be entered separately and separated by pressing the Enter key.


Not in List

Checks if a value does not belong to a specified list. If it does not belong, the condition is met and the step will be executed; otherwise, it will be skipped.

  • Example:
    • Scenario: Check if the promo code entered by the user extracted from pre-step is not in the list of used promo codes.
    • Condition Example: {{$.4.response.body.data.promoCode}} Not in List ["usedCode1", "usedCode2"]
    • Actual Execution Effect: If the extracted promo code from pre-step is "newPromo", the condition is met and the step will be executed; otherwise, it will be skipped.

Wait

When a step in the test process needs to wait for a certain period of time after execution, such as step A needs to wait for a certain amount of time before executing step B, you can solve it by adding a waiting condition.

Example: A simulated user views pet details, views for 1000ms, and then updates the browsing status in the pet details.

    1. Click the button "Tests Step" at the bottom and select "Wait".
    1. Enter the time to wait 1000 (in milliseconds).