Step-by-Step Guide to Robot Framework for Automation with Examples
Keshab Fouzdar
Full Stack QA Engineer | ISTQB? Certified | AWS Certified Cloud Practitioner | Experian PowerCurve Collection Developer | Banking Domain | Selenium, RestAssured, Playwright, Codecept.js, Cypress
Robot Framework is a generic open-source automation framework used for test automation. It has simple plain text syntax and utilizes keywords to abstract away the complexities, making it easy to use even for those with minimal programming knowledge. This guide will walk you through setting up Robot Framework and writing your first automation script with code snippets.
Prerequisites
Before you begin, ensure you have the following installed:
Step 1: Setting Up Your Environment
bash
pip install robotframework
2. Install SeleniumLibrary (for web testing):
bash
pip install robotframework-seleniumlibrary
3. Install a web driver (e.g., ChromeDriver for Chrome): Download the appropriate driver from ChromeDriver and ensure it's in your system PATH.
Step 2: Creating Your First Robot Framework Test
bash
mkdir robotframework-automation
cd robotframework-automation
2. Create a test suite file, e.g., example_test.robot:
bash
touch example_test.robot
3. Open 'example_test.robot' and add the following code:
robot
*** Settings ***
Library SeleniumLibrary
*** Variables ***
${URL} https://example.com
${BROWSER} chrome
*** Test Cases ***
Open Browser and Take Screenshot
Open Browser ${URL} ${BROWSER}
Capture Page Screenshot
Close Browser
This script does the following:
Step 3: Running Your Test
To execute your Robot Framework test, run the following command in your terminal:
bash
robot example_test.robot
You should see a browser window open, navigate to the specified URL, and then close after a screenshot is taken. The results will be stored in an output folder created in your project directory.
Step 4: Interacting with Web Elements
Robot Framework allows you to interact with web elements easily using keywords. Let's extend our script to perform a search on a website.
robot
*** Settings ***
Library SeleniumLibrary
*** Variables ***
${URL} https://duckduckgo.com
${BROWSER} chrome
${SEARCH_TERM} Robot Framework
*** Test Cases ***
Search in DuckDuckGo
Open Browser ${URL} ${BROWSER}
Input Text name=q ${SEARCH_TERM}
Click Button xpath=//input[@type="submit"]
Wait Until Page Contains ${SEARCH_TERM}
Capture Page Screenshot
Close Browser
Step 5: Utilizing Keywords
Keywords help in abstracting and reusing code. You can define your own keywords to make the tests more readable and maintainable.
robot
*** Settings ***
Library SeleniumLibrary
*** Variables ***
${URL} https://duckduckgo.com
${BROWSER} chrome
${SEARCH_TERM} Robot Framework
*** Keywords ***
Open DuckDuckGo And Search
[Arguments] ${term}
Open Browser ${URL} ${BROWSER}
Input Text name=q ${term}
Click Button xpath=//input[@type="submit"]
Wait Until Page Contains ${term}
*** Test Cases ***
Search in DuckDuckGo
Open DuckDuckGo And Search ${SEARCH_TERM}
Capture Page Screenshot
Close Browser
Step 6: Running Tests with Assertions
To perform assertions, you can use built-in keywords like Should Be Equal or Should Contain. Let's add an assertion to verify the search results.
robot
*** Settings ***
Library SeleniumLibrary
*** Variables ***
${URL} https://duckduckgo.com
${BROWSER} chrome
${SEARCH_TERM} Robot Framework
*** Keywords ***
Open DuckDuckGo And Search
[Arguments] ${term}
Open Browser ${URL} ${BROWSER}
Input Text name=q ${term}
Click Button xpath=//input[@type="submit"]
Wait Until Page Contains ${term}
*** Test Cases ***
Search in DuckDuckGo
Open DuckDuckGo And Search ${SEARCH_TERM}
Page Should Contain ${SEARCH_TERM}
Capture Page Screenshot
Close Browser
Conclusion
Congratulations! You've now set up Robot Framework, written your first automation script, interacted with web elements, and performed assertions. Robot Framework's simple syntax and powerful libraries make it an excellent choice for automating and testing web applications.
Continue exploring Robot Framework’s documentation and features to build more advanced and robust automation scripts. Happy testing!