Writing Your First Code
Robot Framework is a versatile and powerful automation framework that uses plain text syntax. It is designed to be easy to read and write, making it accessible to both technical and non-technical users. In this guide, we’ll cover the basics of Robot Framework syntax to get you started.
In this guide, we will primarily focus on testing, specifically Test Cases and Test Suites. The same principles also apply when discussing RPA Tasks.
File name
You can name your files anything you like, as long as they have the .robot extension. For example: 'MyFirstTest.robot'.
Sections
A Robot Framework test suite consists of multiple test cases organized within a single file. The basic structure of a test suite file includes the following sections:
- Settings
- Test Cases
Although there are additional sections available, the Test Cases section is mandatory for running a test.
Settings
The Settings section is where you define the libraries, resource files, and other settings needed for your test suite.
*** Settings ***
Library String
The libraries contain the keywords (functions/steps) that Robot Framework uses to execute tests. Each library includes documentation of the keywords it provides. To use these, importing the library is necessary.
Test Cases or Tasks
The Test Cases or Tasks section is where you define your individual test cases. Each test case/task is a sequence of keywords. These keywords determine the steps or actions that your test or task performs. There is no limit to the number of steps a testcase can have, but there are good practices and it is good to create conventions with your co-workers to ensure readability.
- Tests
- Tasks
*** Test Cases ***
Create A Random String
Log To Console We are going to generate a random string
Generate Random String 10
Log To Console We finished generating a random string
*** Tasks ***
Create A Random String
Log To Console We are going to generate a random string
Generate Random String 10
Log To Console We finished generating a random string
Indentation
In the provided examples, you can see that Robot Framework code is separated by spaces. To correctly differentiate elements, Robot Framework requires at least two spaces. Here are a few examples based on the code we've seen so far:
*** Settings ***
Library String
- The
Library
keyword in the Settings section is unindented. This indicates thatLibrary
is the setting being declared. - Between
Library
andString
, there are several spaces. This indicates thatString
is the name of the library needed for our tests.
*** Test Cases ***
Create A Random String
Log To Console We are going to generate a random string
Generate Random String 10
Log To Console We finished generating a random string
Create A Random String
in the Test Cases section is unindented, indicating that it is a test case name.- The lines below
Create A Random String
are indented, showing that these are keywords used in the test case. - Several spaces between
Generate Random String
and10
indicate that10
is an argument for the keyword, specifying that our random string will be 10 characters long. - The
Log To Console
keywords are followed by a few spaces and a sentence. The spaces ensure that the following sentence is the text we want to log to our console.
You can see that spacing and indentation are extremely important in Robot Framework. They are necessary to distinguish between a keyword and its arguments, as well as to differentiate between test names and the keywords within the tests. Remember, Robot Framework needs at least two spaces to correctly differentiate elements. More spaces are also allowed. For more details on spacing conventions, refer to the Style Guide.