Verifying Status Code and Status Line in Robot Framework

HOME

In this article, we will discuss how to verify status code and status line in Robot Framework. 

HTTP/1.1 200 OK

RequestLibrary is a Robot Framework library aimed to provide HTTP API testing functionalities by wrapping the well-known Python Requests Library.

Prerequisite:

  1. Install Python
  2. Install PIP
  3. Install Robot Framework
  4. Install Robot framework RequestLibrary
  5. Install PyCharm IDE

Please refer to this tutorial to install Robot Framework – How to install and setup Robot Framework for Python.

Implementation Steps:

Create API tests to verify status code

*** Settings ***
Library           RequestsLibrary
Library           Collections
Library           BuiltIn

*** Variables ***
${BASE_URL}          https://reqres.in/api/users/2

*** Test Cases ***
Validate HTTP Response Details
    [Documentation]   This test case validates the HTTP response status code, line, body, headers, and content type

    # Create session
    Create Session    api    ${BASE_URL}

    # Create headers dictionary in the test case
    ${HEADERS}=       Create Dictionary      Content-Type=application/json

    # Send GET request
    ${response}=      GET On Session    api    url=${BASE_URL}    headers=${HEADERS}

    # Validate Status Code
    Should Be Equal As Numbers    ${response.status_code}    200
    Log To Console    Status Code: ${response.status_code}

   # Construct expected status line dynamically
    ${expected_status_line}=    Set Variable    HTTP/1.1 ${response.status_code} OK
    Log To Console    Expected Status Line: ${expected_status_line}

    # Validate Status Line
    ${actual_status_line}=   Evaluate    f"HTTP/1.1 ${response.status_code} OK"
    Should Be Equal    ${actual_status_line}    ${expected_status_line}
    Log To Console    Status Line: ${actual_status_line}

Create Session    api    ${BASE_URL} 
 ${HEADERS}=       Create Dictionary      Content-Type=application/json
  ${response}=      GET On Session    api    url=${BASE_URL}    headers=${HEADERS}

${response}= We are saving the response of the GET operation in the ${response} variable.

4. Asserts that the response status code is 200, indicating a successful request. We have logged the Status Code too

Should Be Equal As Numbers    ${response.status_code}    200
Log To Console    Status Code: ${response.status_code}

5. Set Variable to construct expected Status Line. It dynamically constructs the expected status line using ${response.status_code}.

${expected_status_line}=    Set Variable    HTTP/1.1 ${response.status_code} OK

${actual_status_line}=   Evaluate    f"HTTP/1.1 ${response.status_code} OK"

Execute the tests

We need the below command to run the Robot Framework script.

robot ValidateStatusCode.robot

The output of the above program is

View Report and Log

We have the test case passed. The Robot Framework generates log.html, output.xml, and report.html by default.

Let us now see the report and log details.

Report

Right-click on report.html. Select Open In->Browser->Chrome(any browser of your wish).

The Report generated by the framework is shown below:

Log

Robot Framework has multiple log levels that control what is shown in the automatically generated log file. The default Robot Framework log level is INFO.

Right-click on log.html. Select Open In->Browser->Chrome(any browser of your wish).

That’s it! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!

Additional Tutorials

How to Install Python on Windows 11
How to install and setup Robot Framework for Python
How to rerun failed tests in Robot Framework
Page Object Model in the Robot Framework
Parallel Testing in Robot Framework
How to load data from CSV files in the Robot Framework?

How to pass authorization token in header in Robot Framework

HOME

In this article, we will discuss how to pass the Authentication Token in the header. We will focus on its implementation in Robot Framework. 

Prerequisite:

  1. Install Python
  2. Install PIP
  3. Install Robot Framework
  4. Install Robot framework Selenium Library
  5. Install PyCharm IDE

Please refer to this tutorial to install Robot Framework – How to install and setup Robot Framework for Python.

RequestLibrary is a Robot Framework library aimed to provide HTTP API testing functionalities by wrapping the well-known Python Requests Library.

Implementation Steps:

Step 1.1 – Open PyCharm and create a new project. Go to File and select New Project from the main menu.

Step 1.2 – Choose the project location. Click the “Browse” button next to the Location field and specify the directory for your project.

Deselect the Create a main.py welcome script checkbox because you will create a new Python file for this tutorial.

Click on the “Create” Button.

Step 1.3 – A new dialog appears asking to open the project using any one of the given options. I have selected New Window as I like to have separate windows for each project.

Below is the image of the new project created in PyCharms.

Step 2 – Install RequestLibrary

To install RequestLibrary, you need to use the below command:

pip install robotframework-requests

Step 3 – Add robotframework-requests package to the PyCharms

Go to File->Settings ->Project:API_RobotFramework ->Python Interpreter.

Click on the “+” sign and enter robotframework-requests in the search bar. It will show a list of packages. Select the “robotframework-requests” package and click on the “Install Package”.

Once the package is installed, we will see the message that the package is installed successfully.

Once the package is installed, it can be seen under the package list as shown below:

Step 4 – Create a new directory in the new project

Right-Click on the project, select New->Directory, and provide the name as Tests

Below is the image of the new directory.

Right-click on the new directory and select New File and provide the name as AuthTokenDemo.robot as shown below:

Step 5 – Create API tests in Robot Framework

*** Settings ***
Library           RequestsLibrary
Library           Collections
Library           BuiltIn


*** Variables ***
${BASE_URL}       https://httpbin.org/basic-auth/user/pass
${AUTH_TOKEN}     Basic dXNlcjpwYXNz


*** Test Cases ***
Example API Request with Authorization Header
    [Documentation]   Example of sending a GET request with an Authorization token in the header

    Create Session    api    ${BASE_URL}

     # Create headers dictionary in the test case
    ${headers}=       Create Dictionary    Authorization=${AUTH_TOKEN}    Content-Type=application/json

   ${response}=      GET On Session    api    url=${BASE_URL}     headers=${headers}
    Log To Console    Status Code: ${response.status_code}
    Log To Console    Response Body: ${response.text}
    Should Be Equal As Numbers    ${response.status_code}    200

Create Session    api    ${BASE_URL} 
${headers}=       Create Dictionary    Authorization=${AUTH_TOKEN}    Content-Type=application/json
${response}=      GET On Session    api    url=${BASE_URL}     headers=${headers}

${response}= We are saving the response of the GET operation in the ${response} variable.

4. Asserts that the response status code is 200, indicating a successful request.

Log To Console    Status Code: ${response.status_code}
Log To Console    Response Body: ${response.text}
Should Be Equal As Numbers    ${response.status_code}    200

Step 6 – Execute the tests

We need the below command to run the Robot Framework script.

robot AuthTokenDemo.robot

The output of the above program is

Step 7 – View Report and Log

We have the test case passed. The Robot Framework generates log.html, output.xml, and report.html by default.

Let us now see the report and log details.

Report

Right-click on report.html. Select Open In->Browser->Chrome(any browser of your wish).

The Report generated by the framework is shown below:

Log

Robot Framework has multiple log levels that control what is shown in the automatically generated log file. The default Robot Framework log level is INFO.

Right-click on log.html. Select Open In->Browser->Chrome(any browser of your wish).

That’s it! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!

Additional Tutorials

How to Install Python on Windows 11
How to install and setup Robot Framework for Python
How to rerun failed tests in Robot Framework
Page Object Model in the Robot Framework
Parallel Testing in Robot Framework
How to load data from CSV files in the Robot Framework?

How to Implement Basic Auth in Robot Framework

HOME

In this article, we will discuss how to perform Basic Auth with base64. We will focus on its implementation in Robot Framework. 

Prerequisite:

  1. Install Python
  2. Install PIP
  3. Install Robot Framework
  4. Install Robot framework Selenium Library
  5. Install PyCharm IDE

Please refer to this tutorial to install Robot Framework – How to install and setup Robot Framework for Python.

RequestLibrary is a Robot Framework library aimed to provide HTTP API testing functionalities by wrapping the well-known Python Requests Library.

Implementation Steps:

Step 1.1 – Open PyCharm and create a new project. Go to File and select New Project from the main menu.

Step 1.2 – Choose the project location. Click the “Browse” button next to the Location field and specify the directory for your project.

Deselect the Create a main.py welcome script checkbox because you will create a new Python file for this tutorial.

Click on the “Create” Button.

Step 1.3 – A new dialog appears asking to open the project using any one of the given options. I have selected New Window as I like to have separate windows for each project.

Below is the image of the new project created in PyCharms.

Step 2 – Install RequestLibrary

To install RequestLibrary, you need to use the below command:

pip install robotframework-requests

Step 3 – Add robotframework-requests package to the PyCharms

Go to File->Settings ->Project:API_RobotFramework ->Python Interpreter.

Click on the “+” sign and enter robotframework-requests in the search bar. It will show a list of packages. Select the “robotframework-requests” package and click on the “Install Package”.

Once the package is installed, we will see the message that the package is installed successfully.

Once the package is installed, it can be seen under the package list as shown below:

Step 4 – Create a new directory in the new project

Right-Click on the project, select New->Directory, and provide the name as Tests

Below is the image of the new directory.

Right-click on the new directory and select New File and provide the name as AuthTokenDemo.robot as shown below:

Step 5 – Create API tests in Robot Framework

*** Settings ***
Library           RequestsLibrary
Library           Collections
Library           BuiltIn

*** Variables ***
${BASE_URL}       https://httpbin.org/basic-auth/user/pass
${USERNAME}       user
${PASSWORD}       pass
#${HEADERS}        Create Dictionary    Content-Type=application/json

**** Keywords ***
Create Basic Auth Header

    # Concatenate the username and password in "user:pass" format
    ${credentials}=    Set Variable    ${username}:${password}
    Log To Console    Credentials: ${credentials}

    # Encode the credentials using base64 encoding
    ${encoded}=    Evaluate    str(base64.b64encode('${credentials}'.encode('utf-8')), 'utf-8')    modules=base64
    Log To Console    Encoded Credentials: ${encoded}

    # Create a headers dictionary and add the Authorization header
    ${headers}=    Create Dictionary    Content-Type=application/json
    Set To Dictionary    ${headers}    Authorization=Basic ${encoded}
    RETURN    ${headers}

**** Test Cases ***
Test Preemptive Basic Auth with Custom Header
    [Documentation]   Manually setting Authorization header for preemptive Basic Authentication

    ${headers}=    Create Basic Auth Header

    Create Session    api    ${BASE_URL}    headers=${headers}
    ${response}=      GET On Session    api    url=${BASE_URL}
    Log To Console    Status Code: ${response.status_code}
    Log To Console    Response Body: ${response.text}
    Should Be Equal As Numbers    ${response.status_code}    200

${credentials}=    Set Variable    ${username}:${password}
Log To Console    Credentials: ${credentials}
${encoded}=    Evaluate    str(base64.b64encode('${credentials}'.encode('utf-8')), 'utf-8')    modules=base64
Log To Console    Encoded Credentials: ${encoded}
${headers}=    Create Dictionary    Content-Type=application/json
Set To Dictionary    ${headers}    Authorization=Basic ${encoded}
RETURN    ${headers}

${headers}=    Create Basic Auth Header
Create Session    api    ${BASE_URL}    headers=${headers}
${response}=    GET On Session    api    url=${BASE_URL}

${response}= We are saving the response of the GET operation in the ${response} variable.

4. Asserts that the response status code is 200, indicating a successful request.

 Log To Console    Status Code: ${response.status_code}
Log To Console    Response Body: ${response.text}
Should Be Equal As Numbers    ${response.status_code}    200

Step 6 – Execute the tests

We need the below command to run the Robot Framework script.

robot BasicAuthDemo.robot

The output of the above program is

Step 7 – View Report and Log

We have the test case passed. The Robot Framework generates log.html, output.xml, and report.html by default.

Let us now see the report and log details.

Report

Right-click on report.html. Select Open In->Browser->Chrome(any browser of your wish).

The Report generated by the framework is shown below:

Log

Robot Framework has multiple log levels that control what is shown in the automatically generated log file. The default Robot Framework log level is INFO.

Right-click on log.html. Select Open In->Browser->Chrome(any browser of your wish).

That’s it! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!

Additional Tutorials

How to Install Python on Windows 11
How to install and setup Robot Framework for Python
How to rerun failed tests in Robot Framework
Page Object Model in the Robot Framework
Parallel Testing in Robot Framework
How to load data from CSV files in the Robot Framework?

How to Merge Robot Framework Test Reports

HOME

In this tutorial, we will discuss the steps to combine the test reports in a single test report in the Robot Framework.

Prerequisite:

  1. Install Python
  2. Install PIP
  3. Install Robot Framework
  4. Install Robot framework Selenium Library
  5. Install PyCharm IDE

Please refer to this tutorial to install Robot Framework – How to install and setup Robot Framework for Python.

Implementation Steps

Step 1.1 – Open PyCharm and create a new project. Go to File and select New Project from the main menu.

Step 1.2 – Choose the project location. Click the “Browse” button next to the Location field and specify the directory for your project.

Deselect the Create a main.py welcome script checkbox because you will create a new Python file for this tutorial.

Click on the “Create” Button.

Step 1.3 – A new dialog appears asking to open the project using any one of the given options. I have selected New Window as I like to have separate windows for each project.

Below is the image of the new project created in PyCharms.

Step 2 – Create 3 new directories in the new project

Right-Click on the project, select New->Directory, and provide the name as Tests, Drivers, and Resources

Below is the image of the new directories.

Step 3 – Download ChromeBinaries, Geckodriver and msedgedriver binaries

Download ChromeBinaries, Geckodriver and msedgedriver binaries and place in Drivers directory. This directory contains the browser binary in it. As we are using Chrome, will keep chromedriver.exe here.

The tests are going to use the Chrome browser, Firefox and Edge browsers, so we need to download the corresponding driver binaries to open a blank browser.

https://chromedriver.chromium.org/

Releases · mozilla/geckodriver

Microsoft Edge WebDriver | Microsoft Edge Developer

I will rename chromedriver.exe to Chrome, msedgedriver.exe to Edge and geckodriver.exe to Firefox.

Step 4 – Create Test Files

This directory contains multiple test case files consisting of test steps. 

Right-click on the new directory and select New File and provide the name as LoginPageTests.robot as shown below:

Below is the code for LoginPageTests.robot

*** Settings ***
Documentation       Tests to login to Login Page
Library     SeleniumLibrary
Test Setup      Open the Browser with URL
Test Teardown   Close Browser Session
Resource       ../Resources/GenericResources.robot
Resource       ../Resources/LoginResources.robot

*** Test Cases ***

Validate Unsuccessful Login using invalid credentials

    LoginResources.Fill the login form     ${valid_username}       ${invalid_password}
    LoginResources.Verify the error message is correct


Validate successful Login

    LoginResources.Fill the login form     ${valid_username}       ${valid_password}
    DashboardResources.Verify Dashboard page opens

Step 5 – Create Resources file for each page

It maintains the files which contain page elements as well as corresponding keywords. 

Right-click on the new directory and select New File and provide the name as LoginResources.robot, DashboardResources.robot and GenericResources.robot as shown below:

GenericResources.robot contains the keywords that are common to all the tests, like the opening of the browser or closing of the browser.

*** Settings ***
Documentation    A resource file with reusable keywords and variables.
Library     SeleniumLibrary

*** Variables ***
${valid_username}     Admin
${valid_password}       admin123
${invalid_username}     1234
${invalid_password}     45678
${url}        https://opensource-demo.orangehrmlive.com/web/index.php/auth/login
${browser}      Chrome   #Default browser, if no browser provided


*** Keywords ***

Open the Browser with URL
    Open Browser   ${url}    ${browser}   executable_path=C:/Users/vibha/Documents/Automation/Python/CrossBrowser_RobotFramework/Drivers/${browser}
    Maximize Browser Window
    Set Selenium Implicit Wait    5

Close Browser Session
    Close Browser

Below is the code for LoginResources.robot

*** Settings ***
Documentation        All the page objects and keywords of landing page
Library     SeleniumLibrary

*** Variables ***
${login_error_message}      css:.oxd-alert-content--error
${dashboard_title}       css:.oxd-topbar-header-breadcrumb-module
${missing_username_error_message}    xpath://*[@class='oxd-form']/div[1]/div/span
${missing_password_error_message}   xpath://*[@class='oxd-form']/div[2]/div/span
${forgot_password_link}   xpath://div[@class='orangehrm-login-forgot']/p

*** Keywords ***

Fill the login form
    [Arguments]    ${username}      ${password}
   Input Text    css:input[name=username]   ${username}
   Input Password    css:input[name=password]   ${password}
   Click Button    css:.orangehrm-login-button


Verify the error message is correct
    Element Text Should Be    ${login_error_message}    Invalid credentials

Below is the code for DashboardResources.robot

*** Settings ***
Documentation        All the page objects and keywords of Dashboard page
Library     SeleniumLibrary

*** Variables ***
${dashboard_title}       css:.oxd-topbar-header-breadcrumb-module

*** Keywords ***

Verify Dashboard page opens
    Element Text Should Be    ${dashboard_title}      Dashboard

All the below-mentioned keywords are derived from SeleniumLibrary. The functionality of keywords mentioned above:

1. Open Browser − The keyword opens a new browser instance to the optional url.

2. Maximize Browser Window – This keyword maximizes the current browser window.

3. Set Selenium Implicit Wait – This keyword sets the implicit wait value used by Selenium.

4. Close Browser – Close the current browser.

5. Input Text − This keyword is used to type the given text in the specified textbox identified by the locator name:username.

6. Input Password – This keyword is used to type the given text in the specified password identified by the locator name:password.

The difference compared to Input Text is that this keyword does not log the given password on the INFO level.

7. Click button – This keyword is used to click the button identified by the locator. In this case, it is “Login” button.

8. Element Text Should Be – This keyword is used to verify that the current page contains the exact text identified by the locator. Here, we are checking the exact text “Invalid Credentials”.

These keywords are present in SeleniumLibrary. To know more about these keywords, please refer to this document – https://robotframework.org/SeleniumLibrary/SeleniumLibrary.html.

To run this script, go to the command line and go to directory tests.

Step 6 – Execute the tests

Run your tests separately for each browser (e.g., Chrome, Firefox, Edge), generating an individual output XML file for each. For example, use the below command to run the tests using the Chrome browser:

robot --variable browser:Chrome --output output_chrome.xml .

The output of the above program is

robot --variable browser:Firefox --output output_firefox.xml .
robot --variable browser:Edge --output output_edge.xml .

rebot --outputdir Report --report Consolidated_Report.html output_chrome.xml output_edge.xml output_firefox.xml

Step 8 – View Report and Log

We have total 6 test cases passed (2 test case for each browser).

Let us now see the report and log details.

Report

Right-click on report.html. Select Open In->Browser->Chrome(any browser of your wish).

The Report generated by the framework is shown below:

Log

The screenshots will be included in the log.html file under the specific failed test case step. Robot Framework has multiple log levels that control what is shown in the automatically generated log file. The default Robot Framework log level is INFO.

Right-click on log.html. Select Open In->Browser->Chrome(any browser of your wish).

That’s it! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!

Additional Tutorials

How to Install Python on Windows 11
How to install and setup Robot Framework for Python
How to rerun failed tests in Robot Framework
How to implement tagging in Robot Framework
 How to set variable values from Runtime command in Robot Framework
How to load data from CSV files in the Robot Framework?

Add Screenshots to Failed Tests in Robot Framework

HOME

In this tutorial, we will discuss the steps to add the screenshots to the failed tests in the Robot Framework.

Prerequisite:

  1. Install Python
  2. Install PIP
  3. Install Robot Framework
  4. Install Robot framework Selenium Library
  5. Install PyCharm IDE

Please refer to this tutorial to install Robot Framework – How to install and setup Robot Framework for Python.

Implementation Steps

Step 1.1 – Open PyCharm and create a new project. Go to File and select New Project from the main menu.

Step 1.2 – Choose the project location. Click the “Browse” button next to the Location field and specify the directory for your project.

Deselect the Create a main.py welcome script checkbox because you will create a new Python file for this tutorial.

Click on the “Create” Button.

Step 1.3 – A new dialog appears asking to open the project using any one of the given options. I have selected New Window as I like to have separate windows for each project.

Below is the image of the new project created in PyCharms.

Step 2 – Create 3 new directories in the new project

Right-Click on the project, select New->Directory, and provide the name as Tests, Drivers, and Resources

Below is the image of the new directories.

Step 3 – Download ChromeBinaries and place in Drivers directory

This directory contains the browser binary in it. As we are using Chrome, will keep chromedriver.exe here.

The tests are going to use the Chrome browser, so we need to download the ChromeBinaries to open a blank browser in Chrome.

https://chromedriver.chromium.org/

I will rename chromedriver.exe to Chrome.

Step 4 – Create Test Files

This directory contains multiple test case files consisting of test steps. 

Right-click on the new directory and select New File and provide the name as LoginPageTests.robot as shown below:

Below is the code for LoginPageTests.robot

*** Settings ***
Documentation       Tests to login to Login Page
Library     SeleniumLibrary
Test Setup      Open the Browser with URL
Test Teardown   Capture Screenshot On Failure
Suite Teardown  Close Browser Session
Resource       ../Resources/GenericResources.robot
Resource       ../Resources/LoginResources.robot

*** Test Cases ***

Validate Unsuccessful Login using invalid credentials

    LoginResources.Fill the login form     ${valid_username}       ${invalid_password}
    LoginResources.Verify the error message is correct


Validate successful Login

    LoginResources.Fill the login form     ${valid_username}       ${valid_password}
    DashboardResources.Verify Dashboard page opens


Validate Unsuccessful Login for blank username

     LoginResources.Fill the login form     ${blank_username}       ${valid_password}
     LoginResources.Verify the error message is displayed for username

Step 5 – Create Resources file for each page

It maintains the files which contain page elements as well as corresponding keywords. 

Right-click on the new directory and select New File and provide the name as LoginResources.robot, DashboardResources.robot and GenericResources.robot as shown below:

GenericResources.robot contains the keywords that are common to all the tests, like the opening of the browser or closing of the browser.

*** Settings ***
Documentation    A resource file with reusable keywords and variables.
Library     SeleniumLibrary

*** Variables ***
${valid_username}     Admin
${valid_password}       admin123
${invalid_username}     1234
${invalid_password}     45678
${blank_username}
${url}        https://opensource-demo.orangehrmlive.com/web/index.php/auth/login
${browser_name}      Chrome
${output_dir}      ./screenshots


*** Keywords ***

Open the Browser with URL
    Open Browser   ${url}    ${browser_name}   executable_path=C:/Users/vibha/Documents/Automation/Python/CrossBrowser_RobotFramework/Drivers/${browser_name}
    Maximize Browser Window
    Set Selenium Implicit Wait    5

Capture screenshot On Failure
    Run Keyword If Test Failed    Capture Page Screenshot    ${output_dir}/${TEST NAME}.png
    Close Browser

Close Browser Session
    Close Browser

Below is the code for LoginResources.robot

*** Settings ***
Documentation        All the page objects and keywords of landing page
Library     SeleniumLibrary

*** Variables ***
${login_error_message}      css:.oxd-alert-content--error
${dashboard_title}       css:.oxd-topbar-header-breadcrumb-module
${missing_username_error_message}    xpath://*[@class='oxd-form']/div[1]/div/span

*** Keywords ***

Fill the login form
    [Arguments]    ${username}      ${password}
   Input Text    css:input[name=username]   ${username}
   Input Password    css:input[name=password]   ${password}
   Click Button    css:.orangehrm-login-button


Verify the error message is correct
    Element Text Should Be    ${login_error_message}    Invalid credentials


Verify the error message is displayed for username
     Element Text Should Be    ${missing_username_error_message}      Required

Below is the code for DashboardResources.robot

*** Settings ***
Documentation        All the page objects and keywords of Dashboard page
Library     SeleniumLibrary

*** Variables ***
${dashboard_title}       css:.oxd-topbar-header-breadcrumb-module

*** Keywords ***

Verify Dashboard page opens
    Element Text Should Be    ${dashboard_title}      Dashboard

All the below-mentioned keywords are derived from SeleniumLibrary. The functionality of keywords mentioned above:

1. Open Browser − The keyword opens a new browser instance to the optional url.

2. Maximize Browser Window – This keyword maximizes the current browser window.

3. Set Selenium Implicit Wait – This keyword sets the implicit wait value used by Selenium.

5. Close Browser – Close the current browser.

6. Input Text − This keyword is used to type the given text in the specified textbox identified by the locator name:username.

7. Input Password – This keyword is used to type the given text in the specified password identified by the locator name:password.

The difference compared to Input Text is that this keyword does not log the given password on the INFO level.

8. Click button – This keyword is used to click the button identified by the locator. In this case, it is “Login” button.

9. Element Text Should Be – This keyword is used to verify that the current page contains the exact text identified by the locator. Here, we are checking the exact text “Invalid Credentials”.

These keywords are present in SeleniumLibrary. To know more about these keywords, please refer to this document – https://robotframework.org/SeleniumLibrary/SeleniumLibrary.html.

Step 6 – Execute the tests

To run this script, go to the command line and go to directory tests. We need the below command to run the Robot Framework script.

robot .

The output of the above program is

Step 8 – View Report and Log

We have 2 test cases passed and 1 failed. The Robot Framework generates log.html, output.xml, and report.html by default.

Let us now see the report and log details.

Report

Right-click on report.html. Select Open In->Browser->Chrome(any browser of your wish).

The Report generated by the framework is shown below:

Log

The screenshots will be included in the log.html file under the specific failed test case step. Robot Framework has multiple log levels that control what is shown in the automatically generated log file. The default Robot Framework log level is INFO.

Right-click on log.html. Select Open In->Browser->Chrome(any browser of your wish).

That’s it! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!

Additional Tutorials

How to Install Python on Windows 11
How to install and setup Robot Framework for Python
How to rerun failed tests in Robot Framework
How to implement tagging in Robot Framework
 How to set variable values from Runtime command in Robot Framework
How to load data from CSV files in the Robot Framework?

How to set variable values from Runtime command in Robot Framework

HOME

In this tutorial, we will set the variable values from the Run time command argument in Robot Framework using Selenium WebDriver and Python.

Variables can be changed from the command line using the –variable (-v) option or a variable file using the –variablefile (-V) option. Variables set from the command line are universally accessible for all executed test data files, and they override any variables with the same names in the Variable section and variable files imported into the test data.

Individual variables are established using the syntax –variable name:value, where name is the variable’s name without the $ symbol and value is its value. This option can be used multiple times to define multiple variables. This syntax can only be used to establish scalar variables, and they can only receive string values.

Prerequisite:

  1. Install Python
  2. Install PIP
  3. Install Robot Framework
  4. Install Robot framework Selenium Library
  5. Install PyCharm IDE

Please refer to this tutorial to install Robot Framework – How to install and setup Robot Framework for Python.

Implementation Steps:

Step 1.1 – Open PyCharm and create a new project. Go to File and select “New Project” from the main menu.

Step 1.2 – Choose the project location. Click the “Browse” button next to the Location field and specify the directory for your project.

Deselect the Create a main.py welcome script checkbox because you will create a new Python file for this tutorial.

Step 1.3 – A new dialog will appear asking to Open the project using any one of the given options. I have selected New Window as I like to have separate windows for each project.

Below is the image of the new project created in PyCharms.

Step 2 – Create a new directory in the new project

Right-Click on the project, select New->Directory and provide name as PageObject

Below is the image of the new directory.

Right-click on the new directory select New File and provide the name LoginPage.robot as shown below:

Step 3 – Download ChromeBinaries from the below location

The tests are going to use the Chrome browser, so we need to download the ChromeBinaries to open a blank browser in Chrome.

https://chromedriver.chromium.org/

The chromedriver and geckodriver are placed in a folder name drivers in the RobotFramework_Demo project. I have renamed chromedriver to chrome and geckodriver to firefox.

Step 4 – Create a simple Selenium Test

*** Settings ***
Documentation       Tests to login to Login Page
Library     SeleniumLibrary

*** Variables ***
${valid_username}     Admin
${valid_password}       admin123
${invalid_username}     1234
${invalid_password}     45678
${blank_username}
${blank_password}
${url}      https://opensource-demo.orangehrmlive.com/web/index.php/auth/login
${browser_name}      Chrome
${login_error_message}      css:.oxd-alert-content--error
${dashboard_title}       css:.oxd-topbar-header-breadcrumb-module
${missing_username_error_message}    xpath://*[@class='oxd-form']/div[1]/div/span
${missing_password_error_message}   xpath://*[@class='oxd-form']/div[2]/div/span


*** Test Cases ***

Validate Unsuccessful Login using invalid credentials
    [Tags]    SMOKE
    Open the Browser with URL
    Fill the login form     ${valid_username}       ${invalid_password}
    Verify the error message is correct
    Close Browser Session

Validate Unsuccessful Login for blank username
    [Tags]    REGRESSION
    Open the Browser with URL
    Fill the login form     ${blank_username}       ${valid_password}
    Verify the error message is displayed for username
    Close Browser Session

Validate Unsuccessful Login for blank password
    [Tags]    SMOKE     REGRESSION
    Open the Browser with URL
    Fill the login form     ${valid_username}       ${blank_password}
    Verify the error message is displayed for password
    Close Browser Session

Validate successful Login
    [Tags]    UAT
    Open the Browser with URL
    Fill the login form     ${valid_username}       ${valid_password}
    Verify Dashboard page opens
    Close Browser Session

*** Keywords ***

Open the Browser with URL
    Create Webdriver    ${browser_name}  executable_path=/Vibha_Personal/RobotFramework_Demo/drivers/${browser_name}
    Go To       ${url}
    Maximize Browser Window
    Set Selenium Implicit Wait    5

Fill the login form
   [Arguments]    ${username}      ${password}
   Input Text    css:input[name=username]   ${username}
   Input Password    css:input[name=password]   ${password}
   Click Button    css:.orangehrm-login-button

Verify the error message is correct
    Element Text Should Be    ${login_error_message}    Invalid credentials

Verify Dashboard page opens
    Element Text Should Be    ${dashboard_title}      Dashboard

Verify the error message is displayed for username
     Element Text Should Be    ${missing_username_error_message}      Required

Verify the error message is displayed for password
      Element Text Should Be    ${missing_password_error_message}      Required

Close Browser Session
    Close Browser

Step 5 – Set Variable values from the Runtime command

In the above example, we are using the Chrome browser to run the tests. I want to run the tests on the Firefox browser, but without making any changes to the existing code. How this can be achieved? We can pass variable

robot --variable browser_name:Firefox .

The output of the above program is

We have the test case passed. The Robot Framework generates log.html, output.xml, and report.html by default.

Let us now see the report and log details.

Report

Right-click on report.html. Select Open In->Browser->Firefox (any browser of your wish).

The Report generated by the framework is shown below:

Log

Robot Framework has multiple log levels that control what is shown in the automatically generated log file. The default Robot Framework log level is INFO.

Right-click on log.html. Select Open In->Browser->Firefox (any browser of your wish).

Variables with Tags

If you want to execute only test scenario tagged with UAT using firefox browser, it can be done using the below command:

robot --variable browser_name:Firefox --include UAT .

The output of the above program is

That’s it! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!

Additional Tutorials

How to Install Python on Windows 11
How to run all the tests from the folder in Robot Framework
How to rerun failed tests in Robot Framework
How to implement tagging in Robot Framework
Page Object Model in Robot Framework with Selenium and Python
How to load data from CSV files in the Robot Framework?

How to run headless tests in Robot Framework 

HOME

In this tutorial, we will run the tests in headless mode in Robot Framework.

Prerequisite:

  1. Install Python
  2. Install PIP
  3. Install Robot Framework
  4. Install Robot framework Selenium Library
  5. Install PyCharm IDE

Please refer to this tutorial to install Robot Framework – How to install and setup Robot Framework for Python.

Implementation Steps:

Step 1.1 – Open PyCharm and create a new project. Go to File and select New Project from the main menu.

Step 1.2 – Choose the project location. Click the “Browse” button next to the Location field and specify the directory for your project.

Deselect the Create a main.py welcome script checkbox because you will create a new Python file for this tutorial.

Click on the “Create” Button.

Step 1.3 – A new dialog appears asking to open the project using any one of the given options. I have selected New Window as I like to have separate windows for each project.

Below is the image of the new project created in PyCharms.

Step 2 – Create a new directory in the new project

Right-Click on the project, select New->Directory and provide name as Tests

Below is the image of the new directory.

Right-click on the new directory and select New File and provide the name as HeadlessChrome_Demo.robot and Headlessas shown below:

Step 3 – Execute tests in headless mode

We are now going to write test cases. The test case details will be as follows −

To work with the Robot Framework, we need a locator. A locator is an identifier for the textbox like id, name, class, xpath, css selector, etc.

To know more about locators, refer to these Selenium Tutorials:

 Locators in Selenium – Locate by ID, ClassName,  Name, TagName,  LinkText, PartialLinkText

Dynamic XPath  in Selenium WebDriver

CSS Selector in Selenium WebDriver

Below is an example of executing Chrome tests in headless mode.

*** Settings ***
Documentation    To validate the Login Form
Library     SeleniumLibrary

*** Test Cases ***
Validate Unsuccessful Login
    Open the Browser with URL
    Fill the login form
    verify error message is correct


*** Keywords ***
Open the Browser with URL
    Open Browser   https://opensource-demo.orangehrmlive.com/web/index.php/auth/login    headlesschrome
    Maximize Browser Window
    Set Selenium Implicit Wait    5

Fill the login form
   Input Text    css:input[name=username]   Admin
   Input Password    css:input[name=password]   Admin
   Click Button    css:.orangehrm-login-button


verify error message is correct
    ${result}=  Get Text    CSS:.oxd-alert-content-text
    Should Be Equal As Strings   ${result}  Invalid credentials

Below is an example of executing Firefox tests in headless mode.

*** Settings ***
Documentation    To validate the Login Form
Library     SeleniumLibrary

*** Test Cases ***
Validate Unsuccessful Login
    Open the Browser with URL
    Fill the login form
    verify error message is correct


*** Keywords ***
Open the Browser with URL
    Open Browser   https://opensource-demo.orangehrmlive.com/web/index.php/auth/login    headlessfirefox
    Maximize Browser Window
    Set Selenium Implicit Wait    5

Fill the login form
   Input Text    css:input[name=username]   Admin
   Input Password    css:input[name=password]   Admin
   Click Button    css:.orangehrm-login-button


verify error message is correct
    ${result}=  Get Text    CSS:.oxd-alert-content-text
    Should Be Equal As Strings   ${result}  Invalid credentials

All the below-mentioned keywords are derived from SeleniumLibrary except the last one. The functionality of keywords mentioned above:

1. Open Browser  − The keyword opens a new browser instance to the optional URL.

2. Maximize Browser Window – This keyword maximizes the current browser window.

3. Set Selenium Implicit Wait – This keyword sets the implicit wait value used by Selenium.

4. Input Text − This keyword is used to type the given text in the specified textbox identified by the locator name:username.

5. Input Password – This keyword is used to type the given text in the specified password identified by the locator name:password.

The difference compared to Input Text is that this keyword does not log the given password on the INFO level.

6. Click button – This keyword is used to click on the button with location css:.orangehrm-login-button.

7. ${result} – This is a variable that holds the text value of the error message that is located by css:.oxd-alert-content-text

8. Get Text – This keyword returns the text value of the element identified by located by css:.oxd-alert-content-text.

9. Should Be Equal As Strings – This keyword is used from builtIn keyword. This keyword returns false if objects are unequal after converting them to strings.

These keywords are present in SeleniumLibrary. To know more about these keywords, please refer to this document – https://robotframework.org/SeleniumLibrary/SeleniumLibrary.htm.

To run this script, go to the command line and go to directory tests.

Step 4 – Execute the tests

We need the below command to run the Robot Framework script.

robot .

The output of the above program is

Step 5 – View Report and Log

We have the test case passed. The Robot Framework generates log.html, output.xml, and report.html by default.

Let us now see the report and log details.

Report

Right-click on report.html. Select Open In->Browser->Chrome(any browser of your wish).

The Report generated by the framework is shown below:

Log

Robot Framework has multiple log levels that control what is shown in the automatically generated log file. The default Robot Framework log level is INFO.

Right-click on log.html. Select Open In->Browser->Chrome(any browser of your wish).

That’s it! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!

How to rerun failed tests in Robot Framework
How to use Drag and Drop in Robot Framework? 
How to set variable values from Runtime command in Robot Framework
Page Object Model in Robot Framework with Selenium and Python
Parallel Testing in Robot Framework 
Integration of Allure Report with Robot Framework 

How to use Drag and Drop in Robot Framework?

HOME

In this tutorial, we will discuss in detail how we can perform Drag and Drop actions in Robot Framework.

Prerequisite:

  1. Install Python
  2. Install PIP
  3. Install Robot Framework
  4. Install Robot framework Selenium Library
  5. Install PyCharm IDE

Please refer to this tutorial to install Robot Framework – How to install and setup Robot Framework for Python.

Implementation Steps:

Step 1.1 – Open PyCharm and create a new project. Go to File and select New Project from the main menu.

Step 1.2 – Choose the project location. Click the “Browse” button next to the Location field and specify the directory for your project.

Deselect the Create a main.py welcome script checkbox because you will create a new Python file for this tutorial.

Click on the “Create” Button.

Step 1.3 – A new dialog appears asking to open the project using any one of the given options. I have selected New Window as I like to have separate windows for each project.

Below is the image of the new project created in PyCharms.

Step 2 – Create a new directory in the new project

Right-Click on the project, select New->Directory, and provide the name as Tests

Below is the image of the new directory.

Right-click on the new directory, select New File and provide the name as Drag_And_Drop_Demo.robot as shown below:

Step 3 – Download ChromeBinaries from the below location

The tests are going to use the Chrome browser, so we need to download the ChromeBinaries to open a blank browser in Chrome.

https://chromedriver.chromium.org/

The chromedriver and geckodriver are placed in a folder named drivers in the RobotFramework_Demo project. I have renamed chromedriver to Chrome and geckodriver to Firefox.

Step 4 – Automate the drag and drop option

We are now going to write test cases. The test case details will be as follows −

  • Open the Browser with the URL
  • Verify element Text before drag
  • Drag the element and drop
  • Verify element Text after drag

To work with the Radio Button, we need a locator. A locator is an identifier for the textbox like id, name, class, xpath, css selector, etc.

To know more about locators, refer to these Selenium Tutorials:

 Locators in Selenium – Locate by ID, ClassName,  Name, TagName,  LinkText, PartialLinkText

Dynamic XPath  in Selenium WebDriver

CSS Selector in Selenium WebDriver

Let us inspect the locator of the drag and drop option.

Below is an example of a drag and drop option.

*** Settings ***
Documentation    To drag the box
Library     SeleniumLibrary

*** Test Cases ***
Verify that the user can drag and drop elements
    [documentation]  This test case verifies that a user can drag and drop an element from source to destination
    Open the Browser with URL
    Verify element Text before drag
    Drag the element and drop
    Verify element Text after drag

*** Keywords ***
Open the Browser with URL
    Create Webdriver    Chrome  executable_path=/Vibha_Personal/RobotFramework_Demo/drivers/Chrome
    Go To    https://demoqa.com/droppable
    Maximize Browser Window
    Set Selenium Implicit Wait    5

Verify element Text before drag
    Element Text Should Be      id:droppable  Drop here  timeout=5  #Before Drag and Drop

Drag the element and drop
    Drag And Drop   id:draggable   id:droppable

Verify element Text after drag
    Element Text Should Be  id:droppable  Dropped!  timeout=5  #After Drag and Drop

All the below-mentioned keywords are derived from SeleniumLibrary except the last one. The functionality of keywords mentioned above:

1. Create Webdriver − The keyword creates an instance of Selenium WebDriver.

2. Go To – This keyword navigates the current browser window to the provided URL – https://demoqa.com/droppable.

3. Maximize Browser Window – This keyword maximizes the current browser window.

4. Set Selenium Implicit Wait – This keyword sets the implicit wait value used by Selenium.

6. Drag And Drop – Drags the element identified by the locator into the target element.  The locator argument is the locator of the dragged element and the target is the locator of the target.

These keywords are present in SeleniumLibrary. To know more about these keywords, please refer to this document – https://robotframework.org/SeleniumLibrary/SeleniumLibrary.html.

Before

After

Step 5 – Automate the Drag and Drop by Offset option

Drag And Drop By Offset – Drags the element identified with the locator by xoffset/yoffset.

*** Settings ***
Documentation    To drag the box
Library     SeleniumLibrary
Test Teardown    Close Browser

*** Test Cases ***

Verify that the user can drag and drop element by offset
    [documentation]  This test case verifies that a user can drag and drop an element by offset
    Open the Browser with URL
    Verify element Text before drag
    Drag the element and drop locator by xoffset/yoffset
    Verify no change in Text


*** Keywords ***
Open the Browser with URL
    Create Webdriver    Chrome  executable_path=/Vibha_Personal/RobotFramework_Demo/drivers/Chrome
    Go To    https://demoqa.com/droppable
    Maximize Browser Window
    Set Selenium Implicit Wait    5

Verify element Text before drag
    Element Text Should Be      id:droppable  Drop here  timeout=5  #Before Drag and Drop

Drag the element and drop locator by xoffset/yoffset
     Drag And Drop By Offset  id:draggable  50  70

Verify no change in Text
     Element Text Should Be  id:droppable  Drop here  timeout=5  #After Drag and Drop

Before

After

Step 6 – Execute the tests

We need the below command to run the Robot Framework script.

 robot Drag_Drop_Demo.robot

The output of the above program is

Step 7 – View Report and Log

We have the test case passed. The Robot Framework generates log.html, output.xml, and report.html by default.

Let us now see the report and log details.

Report

Right-click on report.html. Select Open In->Browser->Chrome (any browser of your wish).

The Report generated by the framework is shown below:

Log

Robot Framework has multiple log levels that control what is shown in the automatically generated log file. The default Robot Framework log level is INFO.

Right-click on log.html. Select Open In->Browser->Chrome (any browser of your wish).

That’s it! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!

How to set variable values from Runtime command in Robot Framework
Page Object Model in Robot Framework with Selenium and Python
Parallel Testing in Robot Framework
How to run headless tests in Robot Framework 
Integration of Allure Report with Robot Framework 

How to write tests in Robot Framework in BDD Format

Last Modified Date

HOME

In this tutorial, we will run the tests in the BDD format in Robot Framework.

What is BDD?

BDD is an Agile software development process in which an application is documented and designed around the behaviour that a user expects to see when interacting with it. BDD helps to avoid bloat, excessive code, unnecessary features, and lack of focus by encouraging developers to focus only on the requested behaviours of an app or program. This methodology combines, augments, and refines test-driven development (TDD) and acceptance testing practices.

The Given-When-Then syntax is a commonly used structure for writing user stories and acceptance criteria in a behaviour-driven development (BDD). It is used to describe the desired behaviour of a system in a clear, concise, and consistent manner.

The structure is broken down into three parts:

  • Given: This section describes the initial state or context of the system. It sets the scene for the scenario being tested.
  • When: This section describes the action or event that occurs. It specifies the trigger for the scenario being tested.
  • Then: This section describes the expected outcome or result of the scenario. It defines the acceptance criteria for the scenario being tested.

Prerequisite:

  1. Install Python
  2. Install PIP
  3. Install Robot Framework
  4. Install Robot framework Selenium Library
  5. Install PyCharm IDE

Please refer to this tutorial to install Robot Framework – How to install and setup Robot Framework for Python.

Implementation Steps:

Step 1.1 – Open PyCharm and create a new project. Go to File and select New Project from the main menu.

Step 1.2 – Choose the project location. Click the “Browse” button next to the Location field and specify the directory for your project.

Deselect the Create a main.py welcome script checkbox because you will create a new Python file for this tutorial.

Click on the “Create” Button.

Step 1.3 – A new dialog appears asking to open the project using any one of the given options. I have selected New Window as I like to have separate windows for each project.

Below is the image of the new project created in PyCharms.

How to run tests in BDD format in Robot Framework?

Step 2 – Create a new directory in the new project

Right-Click on the project, select New->Directory and provide name as Tests

Below is the image of the new directory.

Right-click on the new directory and select New File and provide the name as BDD_Demo.robot as shown below:

Step 3 – Execute the tests

We are now going to write test cases. The test case details will be as follows −

To work with the Robot Framework, we need a locator. A locator is an identifier for the textbox like id, name, class, xpath, css selector, etc.

To know more about locators, refer to these Selenium Tutorials:

 Locators in Selenium – Locate by ID, ClassName,  Name, TagName,  LinkText, PartialLinkText

Dynamic XPath  in Selenium WebDriver

CSS Selector in Selenium WebDriver

Below is an example of tests in BDD format.

*** Settings ***
Documentation       Tests to login to Login Page
Library     SeleniumLibrary

*** Variables ***
${valid_username}     Admin
${valid_password}       admin123
${invalid_password}     45678
${url}      https://opensource-demo.orangehrmlive.com/web/index.php/auth/login
${browser_name}      Chrome
${login_error_message}      css:.oxd-alert-content--error
${dashboard_title}       css:.oxd-topbar-header-breadcrumb-module


*** Test Cases ***

Validate Unsuccessful Login using invalid credentials
    [Tags]    SMOKE
    Given I open the Browser with URL
    When I fill the login form     ${valid_username}       ${invalid_password}
    Then I verify the error message is correct
    And Close Browser Session

Validate successful Login
    [Tags]    UAT
    Given I open the Browser with URL
    When I fill the login form     ${valid_username}       ${valid_password}
    Then I verify Dashboard page opens
    And Close Browser Session

*** Keywords ***

Given I open the Browser with URL
    Create Webdriver    ${browser_name}  executable_path=/Vibha_Personal/RobotFramework_Demo/drivers/${browser_name}
    Go To       ${url}
    Maximize Browser Window
    Set Selenium Implicit Wait    5

When I fill the login form
   [Arguments]    ${username}      ${password}
   Input Text    css:input[name=username]   ${username}
   Input Password    css:input[name=password]   ${password}
   Click Button    css:.orangehrm-login-button

Then I verify the error message is correct
    Element Text Should Be    ${login_error_message}    Invalid credentials

Then I verify Dashboard page opens
    Element Text Should Be    ${dashboard_title}      Dashboard


And Close Browser Session
    Close Browser

All the below-mentioned keywords are derived from SeleniumLibrary except the last one. The functionality of keywords mentioned above:

1. Open Browser  − The keyword opens a new browser instance to the optional URL.

2. Maximize Browser Window – This keyword maximizes the current browser window.

3. Set Selenium Implicit Wait – This keyword sets the implicit wait value used by Selenium.

4. Input Text − This keyword is used to type the given text in the specified textbox identified by the locator name:username.

5. Input Password – This keyword is used to type the given text in the specified password identified by the locator name:password.

The difference compared to Input Text is that this keyword does not log the given password on the INFO level.

6. Click button – This keyword is used to click on the button with location css:.orangehrm-login-button.

7. ${result} – This is a variable that holds the text value of the error message that is located by css:.oxd-alert-content-text

8. Element Text Should Be  – This keyword is used to verify that the element locator contains exact the text expected.

These keywords are present in SeleniumLibrary. To know more about these keywords, please refer to this document – https://robotframework.org/SeleniumLibrary/SeleniumLibrary.htm.

To run this script, go to the command line and go to directory tests.

Step 4 – Execute the tests

We need the below command to run the Robot Framework script.

robot .

The output of the above program is

Step 5 – View Report and Log

We have the test case passed. The Robot Framework generates log.html, output.xml, and report.html by default.

Let us now see the report and log details.

Report

Right-click on report.html. Select Open In->Browser->Chrome(any browser of your wish).

The Report generated by the framework is shown below:

Log

Robot Framework has multiple log levels that control what is shown in the automatically generated log file. The default Robot Framework log level is INFO.

Right-click on log.html. Select Open In->Browser->Chrome(any browser of your wish).

That’s it! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!

What are variables in Robot Framework?

HOME

Variables are an essential component of Robot Framework and can be utilized in almost any place in test data. Simply said, if we have values that change frequently and are utilized several times in different test scenarios, adding a variable can help. So, if our value changes in the future, we can just update it in one place, and it will be reflected in all the test cases. Test data and locators are the finest use cases for variables.

In Robot Framework, there are four sorts of variables.

1. Scalar (Identifier: $) – The most common way to use variables in Robot Framework test data is using the scalar variable syntax like ${var}. When this syntax is used, the variable name is replaced with its value as-is.

2. List (Identifier: @) – If a variable value is a list or list-like, a list variable like @{EXAMPLE} is used. In this case, the list is expanded, and individual items are passed in as separate arguments.

3. Dictionary (Identifier: &) – A variable containing a Python dictionary or a dictionary-like object can be used as a dictionary variable like &{EXAMPLE}. In practice, this means that the dictionary is expanded and individual items are passed as named arguments to the keyword.

4. Environment (Identifier: %) – Robot Framework allows using environment variables in the test data using the syntax %{ENV_VAR_NAME}. They are limited to string values.
 

*** Variables ***
${STRING}           cute name                #Scalar
${INT_AS_STRING}    1                     #Scalar
${INT_AS_INT}       ${1}                    #Scalar
${FLOAT}            ${3.14}                   #Scalar
@{LIST}             one    two    three
&{DICTIONARY}       string=name    int=${1}    list=@{LIST}
${ENVIRONMENT}      %{PATH}

Let’s write a simple test using all the above identifiers:

*** Settings ***
Documentation    To validate the Login Form
Library     SeleniumLibrary
Library    Collections
Test Teardown    Close Browser Session


*** Variables ***
${valid_username}     Admin                          #Scalar
${valid_password}       admin123                   #Scalar
${url}      https://opensource-demo.orangehrmlive.com/web/index.php/auth/login        #Scalar
&{VisibleElements}  Forgot your password?=css:.orangehrm-login-forgot-header         #Dictionary


*** Test Cases ***

Validate Elements on Login Page
    Open the Browser with URL
    Verify Element on Login Page


Validate Successful Login
    Open the Browser with URL
    Fill the login form
    Verify Dashboard page opens
    Verify items in Dashboard page

Verify Environment variable

*** Keywords ***

Open the Browser with URL
    Create Webdriver    Chrome  executable_path=/Vibha_Personal/RobotFramework_Demo/drivers/chromedriver_linux64
    Go To       ${url}
    Maximize Browser Window
    Set Selenium Implicit Wait    5

Verify Element on Login Page
    Element Should Be Visible       ${VisibleElements}[Forgot your password?]

Fill the login form
   Input Text    css:input[name=username]   ${valid_username}
   Input Password    css:input[name=password]   ${valid_password}
   Click Button    css:.orangehrm-login-button

Verify Dashboard page opens
    Element Text Should Be    css:.oxd-topbar-header-breadcrumb-module   Dashboard

Verify items in Dashboard page

    @{expectedList} =      Create List      Admin   PIM   Leave     Time    Recruitment  My Info  Performance     Dashboard   Directory  Maintenance  Buzz             #List
    ${elements} =   Get Webelements    css:.oxd-main-menu-item--name
    @{actualList} =     Create List
    FOR    ${element}    IN      @{elements}
        LOG  ${element.text}
        Append To List    ${actualList}     ${element.text}
    END

    Lists Should Be Equal    ${expectedList}         ${actualList}

Close Browser Session
    Close Browser

1. ${valid_username} Admin and ${valid_password} admin123 are both scalar variables because we have the $ sign. The variables are storing the username and password values.

2. @{expectedList} is a list variable (identified by the sign @) storing 11 elements in a list (or Array).

3. &{VisibleElements} Forgot your password?=css:.orangehrm-login-forgot-header is a Dictionary variable (identified by &) storing two locators in the form of key=value.

Execute the tests

We need the below command to run the Robot Framework script.

robot Variable_Demo.robot

The output of the above program is

View Report and Log

We have the test case passed. The Robot Framework generates log.html, output.xml, and report.html by default.

Let us now see the report and log details.

Report

Right-click on report.html. Select Open In->Browser->Firefox(any browser of your wish).

Log

Robot Framework has multiple log levels that control what is shown in the automatically generated log file. The default Robot Framework log level is INFO.

Right-click on log.html. Select Open In->Browser->Firefox(any browser of your wish).

The list elements are shown below

That’s it! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!