Resource file in Robot Framework#
| Author | Juha-Matti Hietala |
| Last updated by | Alena Galysheva |
| Last Updated | 27.03.2025 |
Requirements#
- Robot Framework and SeleniumLibrary installed
- Basic knowledge of Robot Framework
This is a short guide for the usage of a resource file in robot framework scripts. Using a resource file is a simple task, but the earlier it is taken to use, more possible frustration it saves.
Resource file#
Resource file is a .resource file that is used as a common resource for different robot framework scripts. Reasons to use a resource file is to have commonly shared variables in one file. If for some reason a variable changes name, id, class or something similar, the change is needed to be made only in one file that the others use as a resource.
Simple demonstration#
You have two different test cases that both use some of the same elements from the website:
Test case 1
*** Settings ***
Documentation Without Resource File
Library SeleniumLibrary
*** Variables ***
${BASE_URL} https://fip-AAA-BBB-CCC-DDD.kaj.poutavm.fi/en/ # page to be tested
${SIGNIN_BUTTON} class=user-info
${SIGNIN_SUBMIT} id=sumbit-login
${LOGIN_EMAIL} id=field-email
${LOGIN_PASSWORD} id=field-password
${SIGN_OUT} //div[@class='user-info']/a/i[@class='material-icons']
*** Test Cases ***
Sign In And Out
Open Browser To Homepage
Click Sign In
Type In Username correct_username
Type In Password correct_password
Submit Login
Click Log Out
*** Keywords ***
Open Browser To Homepage
Open Browser ${BASE_URL} firefox
Click Sign In
Click Element ${SIGNIN_BUTTON}
Type In Username
[Arguments] ${username}
Input Text ${LOGIN_EMAIL} ${username}
Type In Password
[Arguments] ${password}
Input Text ${LOGIN_PASSWORD} ${password}
Submit Login
Click Element ${SIGNIN_SUBMIT}
Click Log Out
Click Element ${SIGN_OUT}
[Teardown] Close Browser
Test case 2
*** Settings ***
Documentation Without Resource File
Library SeleniumLibrary
*** Variables ***
${BASE_URL} https://fip-AAA-BBB-CCC-DDD.kaj.poutavm.fi/en/ # page to be tested
# SIGN IN
${SIGNIN_BUTTON} class=user-info
${SIGNIN_SUBMIT} id=sumbit-login
${LOGIN_EMAIL} id=field-email
${LOGIN_PASSWORD} id=field-password
${LANGUAGE_DROP} id=_desktop_language_selector
${LANGUAGE_ENGLISH} //div[@id='_desktop_language_selector']/div/div/ul[contains(., 'English')]
# ADD TO CART
${CLOTHES_TOP} id=category-3
${TSHIRT_HUMMINGBIRD} //img[@alt = 'Hummingbird printed t-shirt']
${ADD_CART} //div[@class='add']/button
${ADD_SUCCESSFULL} //div[@class='modal-header']/h4[text()='Product successfully added to your shopping cart']
*** Test Cases ***
Sign In And Add Item To Cart
Open Browser To Homepage
Click Sign In
Type In Username correct_username
Type In Password correct_password
Submit Login
Click Language Drop Down
Click Language To English
Click Clothes Link
Click Hummingbird Tshirt
Add Hummingbird Tshirt To Cart
Confirm Shirt Was Added
[Teardown] Close Browser
*** Keywords ***
Open Browser To Homepage
Open Browser ${BASE_URL} firefox
Click Sign In
Click Element ${SIGNIN_BUTTON}
Type In Username
[Arguments] ${username}
Input Text ${LOGIN_EMAIL} ${username}
Type In Password
[Arguments] ${password}
Input Text ${LOGIN_PASSWORD} ${password}
Submit Login
Click Element ${SIGNIN_SUBMIT}
Click Language Drop Down
Click Element ${LANGUAGE_DROP}
Click Language To English
Click Element ${LANGUAGE_ENGLISH}
Click Clothes Link
Click Element ${CLOTHES_TOP}
Click Hummingbird Tshirt
Click Element ${TSHIRT_HUMMINGBIRD}
Add Hummingbird Tshirt To Cart
Click Element ${ADD_CART}
Confirm Shirt Was Added
Wait Until Page Contains Element ${ADD_SUCCESSFULL}
Wait Until Element Contains ${ADD_SUCCESSFULL} Product successfully added to your shopping cart
Element Should Contain ${ADD_SUCCESSFULL} Product successfully added to your shopping cart
First one is a simple sign in and sign out test, the second sign in and add an item to shopping cart. These both share the steps for signing in. As you can imagine there can be a large number of scripts in your testing environment which all have some test case(s) that share this sign in step, and if done like the two examples above, have the variables determined inside all files separately. Now imagine the typo in the sign in button's id (id=submit-login), is fixed to id=submit-login. This would be a pretty frustrating situation if there were a large number of test files, going through them one by one and fixing this one typo on each. So instead of getting into a situation like this and pulling your hair out, you can use the resource file for shared variables.
Creating and using a resource file#
To use a resource file simply create a empty .resource file, for example resources.resource
resources.resource:
*** Settings ***
Documentation Resource file
Library SeleniumLibrary
*** Variables ***
*** Test Cases ***
*** Keywords ***
Add the variables used between different scripts, in this case I add all variables except base url from the Test case 2, as it covers all variables from both files.
*** Settings ***
Documentation Resource file
Library SeleniumLibrary
*** Variables ***
# SIGN IN
${SIGNIN_BUTTON} class=user-info
${SIGNIN_SUBMIT} id=submit-login
${LOGIN_EMAIL} id=field-email
${LOGIN_PASSWORD} id=field-password
# ADD TO CART
${CLOTHES_TOP} id=category-3
${TSHIRT_HUMMINGBIRD} //img[@alt = 'Hummingbird printed t-shirt']
${ADD_CART} //div[@class='add']/button
${ADD_SUCCESSFULL} //div[@class='modal-header']/h4[text()='Product successfully added to your shopping cart']
*** Keywords ***
To use the resource file simply declare it as a resource in the *** Settings *** portion of the script.
For example in Test case 2:
*** Settings ***
Documentation With Resource File
Library SeleniumLibrary
Resource path_to/resources.resource
*** Variables ***
${BASE_URL} https://fip-AAA-BBB-CCC-DDD.kaj.poutavm.fi/en/
*** Test Cases ***
Sign In And Add Item To Cart
Open Browser To Homepage
Click Sign In
Type In Username correct_username
Type In Password correct_password
Submit Login
Click Language Drop Down
Click Language To English
Click Clothes Link
Click Hummingbird Tshirt
Add Hummingbird Tshirt To Cart
Confirm Shirt Was Added
[Teardown] Close Browser
*** Keywords ***
Open Browser To Homepage
Open Browser ${BASE_URL} firefox
Click Sign In
Click Element ${SIGNIN_BUTTON}
Type In Username
[Arguments] ${username}
Input Text ${LOGIN_EMAIL} ${username}
Type In Password
[Arguments] ${password}
Input Text ${LOGIN_PASSWORD} ${password}
Submit Login
Click Element ${SIGNIN_SUBMIT}
Click Language Drop Down
Click Element ${LANGUAGE_DROP}
Click Language To English
Click Element ${LANGUAGE_ENGLISH}
Click Clothes Link
Click Element ${CLOTHES_TOP}
Click Hummingbird Tshirt
Click Element ${TSHIRT_HUMMINGBIRD}
Add Hummingbird Tshirt To Cart
Click Element ${ADD_CART}
Confirm Shirt Was Added
Wait Until Page Contains Element ${ADD_SUCCESSFULL}
Wait Until Element Contains ${ADD_SUCCESSFULL} Product successfully added to your shopping cart
Element Should Contain ${ADD_SUCCESSFULL} Product successfully added to your shopping cart
Now all the variables in the resources.resource file are recognized in the scripts that use it as a resource, so changes to the variables need to be made only in the resource file, like in this example the ${SIGNIN_SUBMIT} id=submit-login variable's typo correction, saving time and hairs.
Notice that everything from the resource file is carried over, so if you have created keywords in there, they will be recognized and you can run them as test cases. Test it around if you find it useful.