InterviewSolution
| 1. |
What are the different types of variables in the context of the robot framework? |
|
Answer» Variables are a key component of Robot Framework, and they can be utilized in ALMOST every test data scenario. They're most typically used as arguments for KEYWORDS in the Test Case and KEYWORD sections, although they're also allowed in all settings. A variable cannot be used to specify a standard keyword name, but the BuiltIn keyword Run Keyword can be used to achieve the same result. Following are the different types of variables:
The scalar variable syntax like ${var} is the most popular way to use variables in Robot Framework test data. The variable name is substituted with its value as-is when this syntax is used. Variable values are often strings, but variables can also hold any object, such as numbers, lists, dictionaries, or even bespoke objects. Example: *** Test Cases ***Constants Log Hello Log Hello, world!!Variables Log ${GREET} Log ${GREET}, ${NAME}!!The use of scalar variables is demonstrated in the example above. Both the above test cases are similar if the variables ${GREET} and ${NAME} are available and assigned to the strings Hello and world, respectively. When a scalar variable is used without any text or other variables around it, like in ${GREET} above, the variable is substituted with its value, which can be any object. If the variable isn't used alone, as in ${GREER}, ${NAME}!!, its value is transformed to a string before being concatenated with the other data.
When a variable, such as ${EXAMPLE}, is used as a scalar, its value is utilized exactly as it is. If a variable's value is a list or looks like a list, it can also be used as a list variable, such as @{EXAMPLE}. The list is enlarged in this scenario, and each item is sent in as a distinct argument. *** Test Cases ***Constants Login user passwordList Variable Login @{CREDENTIALS}In the above example, the variable @{CREDENTIALS} has values [‘user’, ‘password’]. Robot Framework keeps all of its variables in one internal storage location and allows them to be used as scalars, lists, or dictionaries. If you want to use a variable as a list, its value must be a Python list or a list-like object. Strings are not allowed to be used as lists in Robot Framework, although other iterable objects like tuples and dictionaries are.
A dictionary variable is a variable containing a Python dictionary or a dictionary-like object. The syntax for a dictionary variable is &{EXAMPLE}. In PRACTICE, this means the dictionary is enlarged, and individual items are supplied to the keyword as named arguments. For example, *** Test Cases ***Constants Login user=user1 password=password1Dict Variable Login &{CREDENTIALS}In the above example, assuming that the variable &{CREDENTIALS} has value {‘user’: ‘user1’, ‘password’: ‘password1’}, the two test cases are equivalent.
The %{ENV_VAR_NAME} syntax in Robot Framework allows you to use environment variables in your test data. They're only allowed to use string values. By separating the variable name and the default value with an equal sign, such as %{ENV_VAR_NAME = default_value}, you can give a default value that will be used if the environment variable does not exist. Environment variables set in the operating system before the test execution are available during it, and the keywords Set Environment Variable and Delete Environment Variable, both available in the OperatingSystem library, can be used to create new ones or delete existing ones. Because environment variables are GLOBAL, environment variables defined in one test case can be reused in subsequent test cases. Changes to environment variables, on the other hand, are ineffective after the test has been completed. |
|