Exploring the Most Used Libraries in Robot Framework: A Comprehensive Guide with Examples

Jumana Murad
6 min readMay 8, 2024

--

Welcome to a journey into the realm of Robot Framework, a versatile and powerful automation tool widely embraced by testers and developers. šŸ¤–āœØ In this comprehensive guide, we will embark on an exploration of some of the most utilized libraries within Robot Framework, uncovering their functionalities, and providing practical examples to enhance your understanding. šŸš€

Robot Frameworkā€™s extensive library ecosystem offers a wealth of tools for test automation. Whether youā€™re new to automation or a seasoned tester, libraries like BuiltIn, Collections, SeleniumLibrary, and more meet a wide range of automation needs. šŸ’” Join us as we explore these libraries and empower you to leverage Robot Framework's full potential in your projects. šŸ’ŖšŸŒŸ

Photo by Rock'n Roll Monkey on Unsplash

Built-In Library: As the name suggests, the BuiltIn library is built into the Robot Framework, providing a set of generic keywords that can be used in any testing scenario. For example, the Should Be Equal, Run Keyword If, and many more.

  • You donā€™t necessarily need to include the Library setting for the BuiltIn library explicitly in your test case settings. The BuiltIn library is automatically available in Robot Framework without the need for an explicit import.
  • Hereā€™s an example that demonstrates the use of some built-in functions provided by the BuiltIn library.

Example:

*** Variables ***
${string1} Hello
${string2} World

*** Test Cases ***
Example Built-In Functions
# Concatenate strings using the 'Catenate' keyword
${concatenated_string} Catenate ${string1} , ${string2}
Log Concatenated String: ${concatenated_string}

# Get the length of a string using the 'Length' keyword
${length} Length ${concatenated_string}
Log Length of String: ${length}

# Convert a string to uppercase using the 'Uppercase' keyword
${uppercase_string} Uppercase ${concatenated_string}
Log Uppercase String: ${uppercase_string}

# Convert a string to lowercase using the 'Lowercase' keyword
${lowercase_string} Lowercase ${concatenated_string}
Log Lowercase String: ${lowercase_string}

# Check if a string starts with a specific substring using the 'Should Start With' keyword
Should Start With ${concatenated_string} Hello

In the test case Example Built-In Functions, we use various built-in functions from the BuiltIn library:

  • Catenate: Concatenates strings together.
  • Length: Returns the length of a string.
  • Uppercase: Converts a string to uppercase.
  • Lowercase: Converts a string to lowercase.
  • Should Start With: Verifies if a string starts with a specific sub-string.

Collections: The Collections library in Robot Framework is primarily used for handling and manipulating collections of data, such as lists and dictionaries. It provides keywords that allow you to perform various operations on these data structures, such as Append To List, Remove From List, and Get From Dictionary.

  • Using Collections library to demonstrate handling and manipulating lists and dictionaries within test cases.

Example:

*** Settings ***
Library Collections

*** Variables ***
@{fruits} Apple Banana Orange
&{employee} Name=John Doe Age=30 Department=Engineering

*** Test Cases ***
Manipulate Lists and Dictionaries
# Get the length of the list using the 'Length Of List' keyword
${list_length} Length Of List ${fruits}
Log Length of List: ${list_length}

# Append an item to the list using the 'Append To List' keyword
Append To List ${fruits} Grape
Log Updated List: ${fruits}

# Remove an item from the list using the 'Remove From List' keyword
Remove From List ${fruits} Banana
Log Updated List: ${fruits}

# Get a slice of the list using the 'Get Slice' keyword
${slice_of_list} Get Slice ${fruits} 0 2
Log Slice of List: ${slice_of_list}

# Get the length of the dictionary using the 'Length Of Dictionary' keyword
${dict_length} Length Of Dictionary ${employee}
Log Length of Dictionary: ${dict_length}

# Update a key-value pair in the dictionary using the 'Set To Dictionary' keyword
Set To Dictionary ${employee} Name=Jane Doe
Log Updated Dictionary: ${employee}

# Get a value from the dictionary using the 'Get From Dictionary' keyword
${employee_name} Get From Dictionary ${employee} Name
Log Employee Name: ${employee_name}

Letā€™s explain what we did here:

  1. Import the Collections library in the settings section to access its collection-related keywords.
  2. Define variables such as lists (@{fruits}) and dictionaries (&{employee}) to hold collection data.
  3. In the test case Manipulate Lists and Dictionaries, we perform various operations using keywords from the Collections library:
  • Length Of List: Returns the length of a list.
  • Append To List: Appends an item to a list.
  • Remove From List: Removes an item from a list.
  • Get Slice: Retrieves a slice of a list based on indices.
  • Length Of Dictionary: Returns the length of a dictionary.
  • Set To Dictionary: Updates a key-value pair in a dictionary.
  • Get From Dictionary: Retrieves a value from a dictionary based on a key.

Selenium Library: The SeleniumLibrary is perhaps the most popular library for web testing in Robot Framework. It allows users to interact with web elements, perform actions like clicks and input data, and validate web page content., such asOpen Browser, Click Element, andInput Text.

Example:

*** Settings ***
Library SeleniumLibrary

*** Variables ***
${BROWSER} Chrome
${URL} https://www.example.com

*** Test Cases ***
Open Browser and Verify Title
Open Browser ${URL} ${BROWSER}
Title Should Be Example Domain
Close Browser

String Library: The String library provides keywords for handling and manipulating strings. It includes keywords such as Convert To String, Get Substring, and Replace String.

  • Using the String library to perform various string operations.

Example:

*** Settings ***
Library String

*** Test Cases ***

String Operations
${original_string} Set Variable Hello, Robot Framework!

# Get the length of the string
${length} Length ${original_string}
Should Be Equal ${length} 23

# Convert the string to uppercase
${uppercase_string} Uppercase ${original_string}
Should Be Equal ${uppercase_string} HELLO, ROBOT FRAMEWORK!

# Check if the string starts with 'Hello'
${starts_with} Starts With ${original_string} Hello
Should Be True ${starts_with}

# Check if the string ends with 'Framework!'
${ends_with} Ends With ${original_string} Framework!
Should Be True ${ends_with}

Date Time Library: The DateTime library provides keywords for handling dates and times. It includes keywords such as Get Current Date, Add Time To Date, and Subtract Time From Date.

  • This library is useful for tasks such as getting the current date and time, formatting dates, calculating date differences, and performing date comparisons.
  • Using the DateTime library in Robot Framework. We'll create a test case that calculates the number of days between two given dates and verifies if the difference meets a certain criteria.

Example:

*** Settings ***
Library DateTime

*** Test Cases ***
Calculate and Verify Date Difference
${start_date} Set Variable 2023-05-15
${end_date} Set Variable 2023-05-20
${expected_days} Set Variable 5
${actual_days} Get Date Difference ${end_date} ${start_date}
Log Actual Days Difference: ${actual_days}
Should Be Equal As Numbers ${actual_days} ${expected_days}

Requests Library: The RequestsLibrary is used for making HTTP requests. It includes keywords such as Create Session, Get Request, and Post Request.

  • Itā€™s handy for testing APIs and web services.
  • Hereā€™s an example of using the RequestsLibrary in Robot Framework to send a GET request to retrieve user details from a mock API endpoint and then validate the response status code and JSON data.

Example:

*** Settings ***
Library RequestsLibrary

*** Variables ***

${BASE_URL} https://jsonplaceholder.typicode.com

*** Test Cases ***
Get User Details
${response} Get Request ${BASE_URL}/users/1
Should Be Equal As Strings ${response.status_code} 200
${json_data} Set Variable ${response.json()}
Should Contain ${json_data}[name] Leanne Graham

Database Library: The DatabaseLibrary facilitates database testing by providing keywords to interact with databases using standard SQL queries, such as Connect To Database, Execute SQL String, and Query.

  • Letā€™s write a test case verifies the existence of a user in the database by executing an SQL query.

Example:

*** Settings ***
Library DatabaseLibrary

*** Variables ***

${DB_NAME} MyDatabase
${DB_USER} admin
${DB_PASS} password
${DB_HOST} localhost

*** Test Cases ***
Verify User Exists
Connect To Database pymysql ${DB_NAME} ${DB_USER} ${DB_PASS} ${DB_HOST}
${result} Query SELECT COUNT(*) FROM users WHERE username = 'testuser'
Should Be True ${result[0][0]} > 0
Disconnect From Database

Operating System Library: The OperatingSystem library provides keywords to interact with the underlying operating system, such as Create File, Delete File, Create Directory, and Run.

  • This library is particularly useful for performing file system operations, executing commands, and handling paths in a platform-independent manner.
  • Letā€™s create a test case that checks if a specific directory exists and creates it if it doesnā€™t.

Example:

*** Settings ***
Library OperatingSystem

*** Test Cases ***
Check and Create Directory
${directory_path} Set Variable path/to/new_directory
Run Keyword If not ${directory_exists} Create Directory ${directory_path}
${directory_exists} Directory Should Exist ${directory_path}

In conclusion, Robot Framework offers a robust ecosystem of libraries that streamline test automation and make it accessible to both beginners and experienced testers alike. By exploring some of the most used libraries like BuiltIn, Collections, SeleniumLibrary, String, DateTime, RequestsLibrary, DatabaseLibrary, and OperatingSystem, we've delved into a world of possibilities for enhancing test automation capabilities. šŸ¤–šŸš€

So, dive into the world of Robot Framework libraries, experiment with different functionalities, and unlock the full potential of test automation in your projects. Happy testing! šŸ˜ŠšŸŒŸ

Resources:

  1. Official Robot Framework Documentation: https://robotframework.org/?tab=libraries#resources šŸ“š
  2. Robot Framework User Guide: https://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html šŸ“˜
  3. Robot Framework GitHub Repository: https://github.com/robotframework/robotframework šŸ¤–

These resources provide comprehensive information, tutorials, and updates about Robot Framework. Feel free to explore them for further learning and insights. šŸ’”šŸŒ

Jumana Murad

--

--