Variables are an integral feature of Robot Framework, and they can be used in most places in test data. In simple terms, if we have values that are constantly changing and are used multiple times in different test cases, creating a variable helps. So in case if our value changes in the future, we can just update the value in one place and that will be reflected in all the test cases. The best use case for using variables would be test data and locators.
Primarily there are 4 types of variables in Robot Framework –
1. Scalar (Identifier: $) – The most common way to use variables in Robot Framework test data is using the scalar variable syntax like ${var}. When this syntax is used, the variable name is replaced with its value as-is.
2. List (Identifier: @) – If a variable value is a list or list-like, a list variable like @{EXAMPLE} is used. In this case, the list is expanded and individual items are passed in as separate arguments.
3. Dictionary (Identifier: &) – A variable containing a Python dictionary or a dictionary-like object can be used as a dictionary variable like &{EXAMPLE}. In practice, this means that the dictionary is expanded and individual items are passed as named arguments to the keyword.
4. Environment (Identifier: %) – Robot Framework allows using environment variables in the test data using the syntax %{ENV_VAR_NAME}. They are limited to string values.
Let’s write a simple test using all the above identifiers –
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | *** Settings *** Documentation Login Functionality Library SeleniumLibrary *** Variables *** ${UserNameHRM} Admin #Scalar ${PasswordHRM} admin123 #Scalar @{CredentialsTheInternetHerokuApp} tomsmith SuperSecretPassword! #List &{VisibleElements} OrangeHRM=id:welcome InternetHerokuApp=css:[href="/logout"] #Dictionary *** Test Cases *** Verify Successful Login to OrangeHRM [documentation] This test case verifies that the user is able to successfully log in to OrangeHRM [tags] Smoke Open Browser https://opensource-demo.orangehrmlive.com/ Chrome Wait Until Element Is Visible id:txtUsername timeout=5 Input Text id:txtUsername ${UserNameHRM} Input Password id:txtPassword ${PasswordHRM} Click Element id:btnLogin Element Should Be Visible ${VisibleElements}[OrangeHRM] timeout=5 Close Browser Verify Successful Login to the-internet.herokuapp [documentation] This test case verifies that user is able to successfully Login to the-internet.herokuapp [tags] Smoke Open Browser https://the-internet.herokuapp.com/login Chrome Wait Until Element Is Visible id:username timeout=5 Input Text id:username ${CredentialsTheInternetHerokuApp}[0] Input Password id:password ${CredentialsTheInternetHerokuApp}[1] Click Element css:button[type="submit"] Element Should Be Visible ${VisibleElements}[InternetHerokuApp] timeout=5 Close Browser Verify Environment variable Should Contain %{PATH} /Library/Frameworks/Python.framework/ #Environment *** Keywords *** |
Let’s understand the above script step by step.
1 2 3 4 5 | *** Variables *** ${UserNameHRM} Admin #Scalar ${PasswordHRM} admin123 #Scalar @{CredentialsTheInternetHerokuApp} tomsmith SuperSecretPassword! #List &{VisibleElements} OrangeHRM=id:welcome InternetHerokuApp=css:[href="/logout"] #Dictionary |
1. ${UserNameHRM} Admin and ${PasswordHRM} admin123 are both scalar variables because we have the $ sign. The variables are storing the username and password values.
2. @{CredentialsTheInternetHerokuApp} tomsmith SuperSecretPassword! is a list variable(identified by the sign @) storing username and password in a list(or Array).
3. &{VisibleElements} OrangeHRM=id:welcome InternetHerokuApp=css:[href=”/logout”] is a Dictionary variable(identified by &) storing two locators in the form of key=value.
1 2 3 4 5 6 7 8 9 10 | Verify Successful Login to OrangeHRM [documentation] This test case verifies that the user is able to successfully log in to OrangeHRM [tags] Smoke Open Browser https://opensource-demo.orangehrmlive.com/ Chrome Wait Until Element Is Visible id:txtUsername timeout=5 Input Text id:txtUsername ${UserNameHRM} Input Password id:txtPassword ${PasswordHRM} Click Element id:btnLogin Element Should Be Visible ${VisibleElements}[OrangeHRM] timeout=5 Close Browser |
In the above snippet as you can see instead of writing the username and password, we have used the corresponding scalar variables – ${UserNameHRM}, ${PasswordHRM}. Also after log in, we are identifying the presence of an element and for that, we are getting the value from the dictionary variable – ${VisibleElements}[OrangeHRM]. Since the dictionary variable has two values, we would be specifying the key of the value that we want, hence [OrangeHRM]; which fetches the value id:welcome.
1 2 3 4 5 6 7 8 9 10 | Verify Successful Login to the-internet.herokuapp [documentation] This test case verifies that user is able to successfully Login to the-internet.herokuapp [tags] Smoke Open Browser https://the-internet.herokuapp.com/login Chrome Wait Until Element Is Visible id:username timeout=5 Input Text id:username ${CredentialsTheInternetHerokuApp}[0] Input Password id:password ${CredentialsTheInternetHerokuApp}[1] Click Element css:button[type="submit"] Element Should Be Visible ${VisibleElements}[InternetHerokuApp] timeout=5 Close Browser |
In the above code snippet instead of writing the username and password, we have used the ${CredentialsTheInternetHerokuApp}[0] which gets the first value of the list – tomsmith and ${CredentialsTheInternetHerokuApp}[1] which gets the second value of the list – SuperSecretPassword!. Then after log in, we are identifying the presence of an element and for that, we are getting the value from the dictionary variable – ${VisibleElements}[InternetHerokuApp]. Since the dictionary variable has two values, we would be specifying the key of the value that we want, hence [InternetHerokuApp]; which fetches the value css:[href=”/logout”].
1 2 | Verify Environment variable Should Contain %{PATH} /Library/Frameworks/Python.framework/ #Environment |
Robot Framework allows using environment variables in the test data using the syntax %{ENV_VAR_NAME}. They are limited to string values. In the above snippet, we are verifying that the path variable of the system contains the string /Library/Frameworks/Python.framework/. This is the output of the path variable of my system –
Upon execution we should get the result as PASS
Do check out 🙂
Github:Â https://github.com/alapanme/Robot-Framework
All Robot Framework Articles: https://testersdock.com/robot-framework-tutorial/