February 15, 2024 No Comments

Introduction

Robot Framework is an open-source test automation framework, widely used for acceptance testing and robotic process automation (RPA). It provides a simple, easy-to-use syntax that allows both technical and non-technical users to create test cases and automation scripts. The framework is written in Python but is designed to be language-agnostic, allowing users to implement test cases and keywords in various programming languages. Its capabilities can be extended by libraries implemented with Python, Java, or many other programming languages.

Installing Robot Framework

  • First, let’s create a virtual environment for the project. To create a virtual environment, open the terminal at your desired location and run the below command.

				
					virtualenv env_robot
				
			
  • Now run the following command to activate the virtual environment.

				
					source env_robot/bin/activate
				
			

(Note: These commands are for Ubuntu.)

  • Execute the following command in the terminal to install Robot Framework.

				
					pip install robotframework
				
			

Executing the Hello World program with Robot Framework

Here’s a simple “hello world” example written in the Robot Framework syntax and saved in a file named app.robot.

				
					*** Test Cases *** 
Testcase1 
   log    Hello World!
				
			

The Test Cases header marks the beginning of the test cases section. Following that, Testcase1 serves as the user-defined name for the test case, describing its purpose. The keyword log is utilized to print messages (e.g., ‘Hello World!’) to the console or log file during the execution of the test case.

Space is used in the Robot Framework to keep test cases, keywords, and the overall test suite structured clearly, making it easier to read and maintain.

Run the test suite using the command line below.

				
					robot app.robot
				
			

You can specify an output directory for the report by using the --outputdir.

				
					robot –outputdir results <file_name> 
				
			

Example: robot –outputdir result app.robot

This command will place the generated report in the results directory.

Simple calculator test

Here is an example illustrating terminal output, log file entries, and a report file that displays both successful and unsuccessful outcomes.

				
					*** Settings ***
Documentation    Simple Calculator Test
Library          OperatingSystem
Library          Collections


*** Test Cases ***
Addition Test
   [Documentation]    Test the addition operation of the calculator
   ${expression}    Set Variable    2 + 3
   ${result}    Evaluate    ${expression}
   Should Be Equal As Numbers    ${result}    5


Subtraction Test
   [Documentation]    Test the subtraction operation of the calculator
   ${expression}    Set Variable    5 - 2
   ${result}    Evaluate    ${expression}
   Should Be Equal As Numbers    ${result}    3


Multiplication Test
   [Documentation]    Test the multiplication operation of the calculator
   ${expression}    Set Variable    4 * 3
   ${result}    Evaluate    ${expression}
   Should Be Equal As Numbers    ${result}    12


Division Test
   [Documentation]  Test the division operation of the calculator
   ${expression}   Set Variable    25 / 5
   ${result}    Evaluate    ${expression}
   Should Be Equal As Numbers   ${result}    5


Module Test
   [Documentation]  Test the Module operation of the calculator
   ${expression}   Set Variable    50 % 5
   ${result}    Evaluate    ${expression}
   Should Be Equal As Numbers    ${result}    0
				
			

Note: The dollar sign ($) is used to represent variables. These variables are defined and given values using a specific syntax.

After running the above code, the following output will appear in the terminal.

Now, replace the multiplication test case with the following test case.

				
					Multiplication Test
  [Documentation]    Test the multiplication operation of the calculator
  ${expression}    Set Variable    4 * 3
  ${result}    Evaluate    ${expression}
  Should Be Equal As Numbers    ${result}    14
				
			

After updating the code as instructed and running it, you’ll see this output in the terminal.

Log file

Log files are like comprehensive summaries of test activities, presented in HTML format. They organize information systematically, providing an overview of the entire test, specific test cases, and details about the actions performed. While log files contain a wealth of details, reports offer a simpler and more general understanding of the overall situation.

An example of a log file with keyword details visible

The log displays various details about the test suite, such as the number of steps, the total number of items, and the total number of passed or failed items. Additionally, a table shows the results of each step in the test suite.

Report File

The report files provide a webpage summary of test outcomes, displaying numbers and details about test groups. The report includes individual test listings. When both reports and logs are generated, the report has clickable links to the log file for more detailed information. A green background indicates successful tests, red signifies issues in any test, and yellow indicates that all tests were skipped.

An example report file of successful test execution

An example report file of failed test execution

Looping and Conditional Statements in Robot Framework

The following script demonstrates the application of a for loop and an if condition to illustrate a specific use case.

				
					*** Test Cases ***
Example Test Case
   [Documentation]    Example of for loop and if condition
   [Tags]    example
   ${numbers}=    Set Variable    1    2    3    4    5  # Or you can use "Evaluate    [1, 2, 3, 4, 5]"
   ${sum}=    Set Variable    0
   FOR    ${number}    IN    @{numbers}
       Log    Number: ${number}
       ${sum}=    Evaluate    ${sum} + ${number}
   END
   Log    Total Sum: ${sum}
   IF    ${sum} > 10
       Log    Sum is greater than 10
   ELSE
       Log    Sum is not greater than 10
   END
				
			

In the script, we began with a test case titled Example Test Case. Then, we created a list called “numbers” and assigned some values to it. After that, we calculated the sum of these numbers and stored the result in a variable named “sum“. Using a for loop, we computed the sum of all the numbers in the list. Later, we checked if the sum exceeded 10 using an if condition. The log statements in the script will be printed in a log file, which you can view using the log.html file.

You’ll see the below output in the terminal.

Writing Content In Text Files Using Robot Framework

Here’s a Robot Framework script for writing and appending content to a text file.

				
					*** Settings ***
Library   OperatingSystem


*** Test Cases ***
Write Content to Text File
   [Documentation]    Writes content to a text file
   [Tags]    file_operations
   ${file_path}=    Set Variable    file1.txt
   ${content}=    Set Variable    This is content to be written to the file.
   Create File   ${file_path}    ${content}
   ${written_content}=    Get File    ${file_path}
   Log    ${written_content}


Append content to Text File
   [Documentation]    Appends content to a text file
   [Tags]    file_operations
   ${file_path}=    Set Variable    file2.txt
   ${content_to_append}=    Set Variable    This is content to be appended to the file.
   Append To File    ${file_path}    ${content_to_append}
   ${appended_content}=    Get File    ${file_path}
   Log    ${appended_content}
				
			
  • The first test case Write Content to Text File writes content to a text file.

  • The second test case Append Content to Text File appends content to the text file.

  • You can use your file paths instead of file1.txt and file2.txt.

  • You can expect to see an output similar to the image provided above in your terminal.

  • After executing the robot file you can find the files file1.txt and file2.txt in your current working directory.

Verifying the output

  • File1.txt

  • File2.txt

Here is the GitHub link containing the code mentioned above. 

Conclusion

Robot Framework stands out as a user-friendly yet robust solution for automating tests and streamlining tasks in software development and quality assurance. Its straightforward syntax, language-agnostic design, and extensive library support make it accessible to a wide range of users, from technical experts to non-programmers. With its ability to generate detailed reports and log files, Robot Framework facilitates efficient test management and analysis, empowering teams to enhance productivity and deliver high-quality software products effectively.

Write a comment

Your email address will not be published. Required fields are marked *

Want to talk to an Expert Developer?

Our experts in Generative AI, Python Programming, and Chatbot Development can help you build innovative solutions and scale your business faster.