Logic | Zoho QEngine Help

Logic

This list of tasks help create robust and flexible automation scripts that can adapt to various conditions and handle exceptions effectively. Logic tasks include the following:
  1. Execute JS
  2. Conditional statements - IF, ELSE IF, and ELSE
  3. Set Variable
  4. Call Function
  5. Finally
  6. Continue On Error

1. Execute JS  

The task executes Javascript code on the currently selected window or frame within a webpage. This is particularly valuable for performing advanced validations, or custom actions that cannot be achieved using other standard tasks inside Zoho QEngine. For example, this task can be used to manipulate dynamically changing elements on a webpage, such as custom drop-downs.

Syntax 
executeJS(<script>);

 

Return Type
STRING

 

Parameter

Data Type

Description

<script>

STRING

This contains the JavaScript code snippet. It should be written within (" ") and must adhere to JavaScript syntax rules.


NotesNote: There won't be a screenshot preview for this task, as the execution takes place directly in the browser console. 



2. Conditional statements  - IF, Else IF, Else

Zoho QEngine offers tasks that help us write conditional statements that are essential to implement decision logic, allowing different code blocks to be executed based on varying conditions or criteria. The following are the conditional statement variants to build your business logic: 
  • IF
  • Else IF
  • Else

NotesNote: Conditional Statements examine specified criteria, and act in one way if the criteria are met, or in another way if the criteria are not met. The criteria is mandatory for all conditional statements except the "else" conditional statement. The "else" conditional statement does not accept any criteria.

IF 

Validates given criterion and performs specified actions if the criterion is met. The "If" conditional statement can be followed by "else if" and "else" conditional statements(not mandatory). If the criterion is not met, the actions are skipped, and the program moves on to the next task.

Syntax  
if(<variable> <operator> <expression>)
{
<Actions>
}

 

 

Parameter

Description

<variable>

Variable that stores the value that needs to be compared to a specified value.

<operator>

Operator to compare the variable's value against the specified value.

<expression>

It may directly be a value, or an expression evaluating to the required value, which needs to be compared with the specified value stored in the variable.

<Actions>

Set of conditions to be executed when the specified criteria are met. This can also be other Zoho QEngine tasks.


Else IF 

The "else if" conditional statement is always preceded by an "if" conditional statement. It executes given statements when the preceding "if" conditional statement fails and the "else if" criterion is met. If the "else if" criterion is also not met, the actions are skipped, and the program moves on to the next task.

 

Syntax 

else if(<variable> <operator> <expression>)
{
<Actions>
}


Parameter

Description

<variable>

Variable that stores the value that needs to be compared to a specified value.

<operator>

Operator to compare the variable's value against the specified value.

<expression>

It may directly be a value, or an expression evaluating to the required value, which needs to be compared with the specified value stored in the variable.

<Actions>

Set of conditions to be executed when the specified criteria are met. This can also be other Zoho QEngine tasks.

 

Else 

The "else" conditional statement is always preceded by an "if" or an "else if" conditional statement. It executes given statements when the preceding "if" and "else if" conditional statements fail. The "else" statement, if present, must always be the last in a set of conditional statements, i.e., it must not be followed by the "if" or the "else if" condition in that set of statements.


Syntax
else
{
<actions>
}


Example: A user wants to test a form. The following script checks if the entered name and email are valid.
  1. openURL("www.zwatch.com" , "same window"); // Navigates to the URL. Learn more about openURL task
  2. enteredname = getText(ui.zwatch.username_field);
  3. enteredemail = getText(ui.zwatch.email_field);
  4. validuname = "Chris Morgan";
  5. validemail = "Chrismorgan@gmail.com";
  6.  
  7. if(enteredname == validname && enteredemail==validemail) // Validates the entered email and name in the login form matches with the actual.
  8. {
  9. info("Entered name and email matched the valid username and email.");
  10. }
  11. else if(enteredname == validname && enteredemail != validemail)
  12. {
  13. info("Email is incorrect");
  14. }
  15. else if(enteredname != validname && enteredemail == validemail)
  16. {
  17. info("Name is incorrect");
  18. }
  19. else
  20. {
  21. info("Both name and email are incorrect.");
  22. }

 

where:

 

"www.zwatch.com"

URL of the website that needs to be opened.

"same window"

This represents that the URL needs to be opened in the same window.

enteredname

The local variable that stores the entered user name from the name field in the login form.

ui.zwatch.name_field

The recorded element for the name field of the login form.

enteredemail

The local variable that stores the entered email from the email field in the login form.

ui.zwatch.email_field

The recorded element for the email field of the login form.

validname

The local variable that stores the actual name to login.

"Chris Morgan"

The valid name saved to perform the validation.

validemail

The local variable that stores the actual email to login.

"Chrismorgan@gmail.com"

The valid password saved to perform the validation.

"Entered name and email matched the valid name and email."

The message displayed when the name and email entered in the login form matches with the actual.

"Email is incorrect"

The message displayed when the email which is entered in the login form doesn't match with the actual.

"Name is incorrect"

The message displayed when the name which is entered in the login form doesn't match with the actual.

"Both name and email are incorrect."

The message displayed when the name and email entered in the login form doesn't match with the actual.

3. Set Variable 

The Set Variable task assigns a given expression or value to a local variable, which is accessible only within the respective test case. A local variable declared in this way ensures that its scope is limited to the test case where it is defined.
 
Syntax 
<variable> = <expression>;

 

Parameter

Description

<variable>

Variable which will hold the given value.

<expression>

Value to be assigned to the variable.

 

Example 1: The following script assigns the value 0 to variable - temp:
 
  1. temp = 0;
 
Example 2: A user wants to check the add to cart functionality of a website. The following script verifies whether a product is added to the cart.
 
  1. cart_count = getCount(ui.zwatch.cart); // Stores the count fetched from the cart. Learn more about the Get Count task
  2.  
  3. // Check whether the selected product is added to the cart.
  4. if(cart_count == 1)
  5. {
  6. info(cart_count +" "+ "product has been added to the cart."); // Here, the + operator is used to concatenate the variable value and the string text.
  7. }
  8. else
  9. {
  10. info("Failed to add the product to the cart.");
  11. }

where:

 

cart_count

The local variable that stores the count of products added to the cart.

ui.zwatch.cart

The recorded element for the cart of the e-commerce website.

cart_count +" "+ "product has been successfully added to the cart."

The concatenated expression that has to be displayed if the product is added to the cart.

"Failed to add the product to the cart."

The message that has to be displayed if the product isn't added to the cart.

4. Call Function  

Functions created within a project are accessible across all the test cases within the project. This task invokes a custom function defined inside the respective project.

 

Syntax 

<module_linkname>.<function_linkname>(<parameters>);
 
(or)
 
<variable> = <module_linkname>.<function_linkname>(<parameters>);

 

 

Parameter

Description

<module_linkname>

Link name of the module in which the function has been defined.

<function_linkname>

Name of the function to be invoked.

<parameters>

The arguments that are passed to the function at the time of invocation. An argument is a variable declared while defining a function.

Notes
Note:
  • The usage of a parameter depends on whether arguments are specified while defining the function.

  • A parameter can be a variable or an expression that evaluates to a value.

  • The number of parameters and their data types must match the arguments specified in the function definition. Any mismatch will result in an error, and the script will not be saved.

 

Example: A user wants to test the add to cart functionality of an e-commerce website. The following script calls a user defined function "zaccounts.login()" to login to an e-commerce website. After logging in, the user wants to add a product to the cart.
 
  1. zaccounts.login(); // The function to login to the website is invoked here.
  2. openURL("https://zwatch.com","same window"); // Navigates to the URL. Learn more about openURL task
  3. click(ui.zwatch.zwatch_Daytona);
  4. click(ui.zwatch.add_to_cart_button); // Learn more about Click task
  5. wait(5);

 

where:

 

zaccounts.login();

The login function called inside the test script.

"https://zwatch.com"

URL of the website that needs to be opened.

"same window"

This represents that the URL needs to be opened in the same window.

ui.zwatch.zwatch_Daytona

The recorded element for the selected product.

ui.zwatch.add_to_cart_button

The recorded element for the "Add To Cart" button.

5

The time it waits for the webpage to add the product to the cart.

 

5. Finally  

The task executes the script block regardless of whether the preceding test steps succeed or encounter exceptions. This script block will be executed as a final step, providing a way to ensure that certain actions are taken irrespective of the test case outcome. For example, cleanup operations that have to be performed irrespective of the test case outcome.
 

Syntax 

finally
{
<action>
}

 

Parameter

Description

<action>

Custom logic or function to be executed irrespective of the test case results. This can also be Zoho QEngine tasks.

 

Notes
Note:
  • More than one "Finally" block cannot be added in test cases.
  • The "Finally" block can't be used inside functions.

6. Continue On Error  

The task executes the enclosed script blocks irrespective of the error that occurs during test case execution. For example, it is used to prevent a single error from interrupting the entire test case workflow.

 

Syntax 

continueOnError
{
<code>
}

 

 

Parameter

Description

<code>

Custom logic or functions to be enclosed to prevent it from test case termination.