|
1 | 1 | --- |
2 | 2 | id: kane-ai-using-variables |
3 | | -title: KaneAI - Using Variables in Test Cases |
| 3 | +title: Variables in KaneAI |
4 | 4 | hide_title: false |
5 | 5 | sidebar_label: Variables |
6 | | -description: Learn how to use variables in the KaneAI test cases |
| 6 | +description: Learn how to use variables in KaneAI test cases |
7 | 7 | keywords: |
8 | 8 | - lambdatest automation |
9 | 9 | - lambdatest kaneai |
@@ -36,47 +36,126 @@ import {YOUR_LAMBDATEST_USERNAME, YOUR_LAMBDATEST_ACCESS_KEY} from "@site/src/co |
36 | 36 | },{ |
37 | 37 | "@type": "ListItem", |
38 | 38 | "position": 3, |
39 | | - "name": "KaneAI Variables", |
| 39 | + "name": "Variables", |
40 | 40 | "item": "https://www.lambdatest.com/support/docs/kane-ai-using-variables/" |
41 | 41 | }] |
42 | 42 | }) |
43 | 43 | }} |
44 | 44 | ></script> |
45 | | -The new variable definition feature in KaneAI allows users to define reusable variables, assign them values, and use those values within test cases. This feature enhances flexibility, readability, and consistency across test cases, making it easier to maintain and modify them. |
46 | 45 |
|
47 | | -## Why Use Variables? |
| 46 | +Variables are placeholders that store values, which can be reused across different parts of a test case. In automation, using variables helps make the tests more flexible, reusable, and maintainable. Instead of hardcoding values in test steps, you can define variables, assign them values, and reference them throughout your tests. This reduces redundancy and makes the tests easier to modify, especially when dealing with frequently changing parameters like URLs, credentials, or device configurations. Variables can be accessed directly from https://kaneai.lambdatest.com/variables. |
| 47 | + |
48 | 48 | Using variables enables you to: |
49 | 49 | - Avoid repetitive entries in test cases. |
50 | 50 | - Get values from within the application under test & store it in variable to validate the accuracy of data. |
51 | 51 | - Increase the reusability and adaptability of test scripts, especially useful for parameters that might change frequently, such as URLs, credentials, or environmental settings. |
52 | 52 |
|
53 | | -## Variable Syntax |
54 | | -To work with variables in KaneAI, you need to follow these syntax rules: |
| 53 | +## Types of variables |
| 54 | +- **String Variables**: Store simple text values. They can be used for things like usernames, or any textual data. String variables can be created via KaneAI or manually within the session. |
| 55 | + |
| 56 | +- **JSON Variables**: Store structured data in the form of key-value pairs. JSON variables are especially useful for passing complex objects such as API responses. JSON variables are auto-generated from API calls and DB queries used in KaneAI and cannot be created manually. |
| 57 | + |
| 58 | +## Scope of variables |
| 59 | + |
| 60 | +### Local Variables |
| 61 | +Local variables can only be defined using natural language or used as a part of an operation when an instrcution is provided by the user. They are only accessible within the specific test case where they are defined. They help ensure that values are isolated and can be used temporarily within a specific context. A local variable can be converted to a global or an environment variable within the authoring session of KaneAI. |
| 62 | + |
| 63 | +### Global Variables |
| 64 | +Global variables are accessible across multiple test cases within the entire organisation. They allow values to be reused in multiple places, enhancing consistency and reducing redundancy. Any variable which is converted from local to global has an option to persist value across sessions if the value changes within a session. More details about persist check are added below. |
55 | 65 |
|
56 | | -- Variables are defined using natural language such as "set new_variable as value". |
57 | | -- Variable usage can be done using the `{{` prefix to list all existing variables. |
58 | | -- Example: `{{username}}`, `{{phoneNumber}}`, `{{address}}` |
| 66 | +### Environment Variables |
| 67 | +Environment variables are variables that are typically set for a specific environment (e.g., staging, production). They are useful for managing values like URLs, or credentials that differ based on the environment in which the tests are being executed. Using environment variables you can execute your test on multiple environments easily. More details about environments are added below. |
59 | 68 |
|
60 | | -## Creating Variables |
61 | | -To create a variable: |
| 69 | +## How to create & edit variables |
| 70 | +Variables can be created using the following three methods: |
62 | 71 |
|
63 | | -- Navigate to the "Write a Step" text field |
64 | | -- Type your instruction in natural language such as: *Set username as John* and a variable `{{username}}` will be created with value John. |
| 72 | +### Using Slash "/" Command in authoring session |
| 73 | +You can define variables directly by using the `/` command in the step input and select "Add a variable" option. You are allowed to create _global_ and _environment_ variables using this approach and define their values. |
| 74 | +The value of variables can be edited by clicking on the variables listing icon on the top right. |
| 75 | + |
| 76 | +<img loading="lazy" src={require('../assets/images/kane-ai/knowledge-base/variables/variable-listing-in-session.jpg').default} alt="Image" className="doc_img img_center"/> |
| 77 | + |
| 78 | +### Using Natural Language in authoring session |
| 79 | +KaneAI allows you to create variables using natural language. For instance, you can write: |
65 | 80 |
|
66 | | -Example: |
67 | 81 | ```bash |
68 | 82 | Set username as John |
69 | 83 | ``` |
| 84 | +This will automatically create a _local_ variable {{username}} with the value "John". |
| 85 | +For such _local_ variables, only the scope of the variable can be changed to a global with an option to enable the persist check which ensures that the value when updated in one session or test execution gets persistes across sessions and test executions. |
| 86 | + |
| 87 | + |
| 88 | +### Via UI from Variables page |
| 89 | +By visiting the [variables page](https://kaneai.lambdatest.com/variables), you can click on "Create new" option and add relevant details and scope. You can list of all global and environment variables on this page. |
| 90 | +You can edit the session value for the variables here which will lead to updation of the value in all new test executions and sessions for that variable. |
70 | 91 |
|
71 | | -## Using Variables |
72 | | -To use a previously created variable in any subsequent step or test case, reference the variable using the same `{{` syntax. This will list down all existing variables for this test. Format: *use the value of `{{variableName}}`* |
| 92 | +## How to leverage variables during authoring |
| 93 | +Variables can be used to enhance your test cases, making them more dynamic. |
| 94 | + |
| 95 | +### Syntax for Assertions |
| 96 | +Variable usage can be done using the `{{` prefix to list all existing variables in the KaneAI session input box. When making assertions in your test case, you can use variables for inputing a value or as part of the assertion logic. |
| 97 | + |
| 98 | +<img loading="lazy" src={require('../assets/images/kane-ai/knowledge-base/variables/syntax.jpg').default} alt="Image" className="doc_img img_center"/> |
| 99 | + |
| 100 | +#### String variables example: |
73 | 101 |
|
74 | | -Example: |
75 | 102 | ```bash |
76 | | -use the value of {{phoneNumber}} in the given text field |
| 103 | +Enter {{username}} in user input field |
77 | 104 | ``` |
| 105 | +This will input the value of the username variable in the user input field. |
| 106 | + |
| 107 | +```bash |
| 108 | +assert {{username}} contains 'John' |
| 109 | +``` |
| 110 | +This will assert if the username variable contains the value "John". |
| 111 | + |
| 112 | +#### JSON variables example: |
| 113 | +JSON variables are generated from the response of an API used via the slash command or from the DB query output. We can use JSON variables to reach a particular object which might be inside the JSON hierarchy via the UI when the `{{` prefix is used in the input box inside the KaneAI session. For instance, we can assert that the email field inside the API variable response matches "john.doe@example.com". |
| 114 | + |
| 115 | +```bash |
| 116 | +assert {{api_variable.response.email}} is 'john.doe@example.com' |
| 117 | +``` |
| 118 | +Here is a video to further explain the JSON variables usage: |
| 119 | +<video class="right-side" width="100%" controls id="vid"> |
| 120 | +<source src= {require('../assets/images/kane-ai/knowledge-base/variables/json-variable.mp4').default} type="video/mp4" /> |
| 121 | +</video> |
| 122 | + |
| 123 | +## Persist Value in Variables |
| 124 | + |
| 125 | +Persist value check is a mechanism used to ensure that variables retain their values across multiple test executions. This is important for situations where a variable’s value should be preserved between test cases and test executions to ensure consistency. For example, if you fetch a value from the AUT in a test while authoring, you may want to persist it for the other test cases even during test executions. |
| 126 | + |
| 127 | +:::tip |
| 128 | +The persist check option becomes available only when you change a variable's scope from local to global. |
| 129 | +::: |
| 130 | + |
| 131 | +<img loading="lazy" src={require('../assets/images/kane-ai/knowledge-base/variables/persist-value.jpg').default} alt="Image" className="doc_img img_center"/> |
| 132 | + |
| 133 | +## Environments |
| 134 | + |
| 135 | +### What are Environments? |
| 136 | +Environments in KaneAI refer to the different configurations under which the tests are executed. Environments typically represent different stages of deployment (e.g., development, staging, production). Each environment can have its own set of variables tailored to the respective deployment scenario. |
| 137 | + |
| 138 | +<img loading="lazy" src={require('../assets/images/kane-ai/knowledge-base/variables/environment.jpg').default} alt="Image" className="doc_img img_center"/> |
| 139 | + |
| 140 | +### How to Use Environments During Test Runs |
| 141 | +Once a test case is authored using environment variables, it can then be executed in any environment as long as the variables used in the test case, exist in the requested environment. |
| 142 | + |
| 143 | +**UI**: While executing a test run or scheduling one, you can choose which environment they want to execute a test run on. All the test cases inside a test run can be executed on a single environment only at a point of time. |
| 144 | + |
| 145 | +<img loading="lazy" src={require('../assets/images/kane-ai/knowledge-base/variables/test-run-environment.jpg').default} alt="Image" className="doc_img img_center"/> |
| 146 | + |
| 147 | +**API**: You can pass environment name programmatically when initiating test runs via the API. This allows you to automate the execution of tests in different environments without manual intervention. For instance, |
| 148 | + |
| 149 | +```bash |
| 150 | +curl --location 'https://test-manager-api.lambdatest.com/api/atm/v1/hyperexecute' \ |
| 151 | +--header 'Content-Type: application/json' \ |
| 152 | +--header 'Authorization: Basic <Base64Auth>' \ |
| 153 | +--data '{ |
| 154 | + "test_run_id": "YOUR_TEST_RUN_ID", #enter test run id |
| 155 | + "concurrency": 1, |
| 156 | + "environment": "staging" #Optional |
| 157 | +``` |
| 158 | +
|
| 159 | +This will run the test case in the "staging" environment. |
| 160 | +
|
78 | 161 |
|
79 | | -## Best Practices |
80 | | -### Naming Conventions |
81 | | -- Use descriptive names for your variables |
82 | | -- Use camelCase or underscore for multi-word variable names |
|
0 commit comments