top of page

Robot Framework Crash Course: Writing Your First Test Case

Writer's picture: Ajit SinghAjit Singh
  • Open source test automation framework.

  • Can be used for testing different types of application embedded ,web based RPA(Robotic Process Automation).

  • Keyword driven(human readable keyword) approach helps to make tests reusable and more modular easy to understand.

  • Data driven approach helps to tests different types and sets of data.

Installation (Windows)

Pre-requisite Install Python pip(Already Included in the Python standard library since Python 3.4)

Installing Robot Framework using pip(Python path is added to environment variable)

Run the below commands in command prompt

pip install robotframework

Check the installation

robot --version

More Platform Installation

Writing Your First Test Case


Before writing your first test case, let us understand some key concepts required to write the tests . There are different section in robot framework and it is identified by a header row.


Settings:


This section is meant for importing libraries, variable files, resource files and also to add the meta data for the test cases & test suites can be added. Settings Section header is defined as below. Number of asterisks can be very long but at least one should be in the beginning.


*** Settings ***
# Import libraries
Library SeleniumLibrary

Variable:


This section is meant for defining the variable which is going to be used by test cases. Variable is used to store the data that is common to all the test cases and keywords in the same file.

*** Variables ***
${URL} https://reqres.in/

Keywords:


This section is meant for defining user-defined keywords that the test case will use. User-defined keywords are reusable blocks of code that can be called to perform some common behavior in the test cases. There are different types of keyword Library , User-defined and Resource keywords.


  • Library Keywords are provided by Robot Framework libraries like Open Browser.

  • User-defined keywords are defined by the user. These can be used to encapsulate common functionality like verifying the title of a web page.

  • Resource keywords are same as user-defined keywords, but they are defined in a separate file and it is imported into other test cases and keywords.


*** Keywords ***
Open Browser To Verify Title
    [Arguments]    ${url}   ${browser}    ${expectedTitle}
    Open Browser    ${url}    ${browser}
    ${TITLE}=      Get Title
    Should Contain    ${TITLE}   ${expectedTitle}

Test Cases:


The Test Cases section is meant for writing the actual steps of your test case. You can use keywords, variables, and other test cases in this Test Cases section.


*** Test Cases ***
Verify Title
    Open Browser To Verify Title    ${URL}  Chrome  Reqres
    Close Browser

Adding Simple Test Case



Create a File with file_name.robot extension. This file will contains your test case.


Install the selenium library

pip install robotframework-seleniumlibrary

Test Case:

A Test Case named "Verify Title" which opens the specified URL and verifies the title is defined. We also have defined a user-defined keyword "Open Browser To Verify Title" that accepts three arguments: url, browser and title. This keyword calls the "Open Browser" keyword to open the browser in the specified browser and then followed by "Get Title" keyword to get the title of the website and compares with the specified title using "Should Contains" keyword. The user-defined keyword is called in the test case, and the browser is closed after verification.


*** Settings ***
# importing the selenium library
Library     SeleniumLibrary

*** Variables ***
# defining the url variable
${URL}      https://reqres.in/


*** Test Cases ***
Verify Title
    Open Browser To Verify Title    ${URL}  Chrome  Reqres
    Close Browser


*** Keywords ***
Open Browser To Verify Title
    [Arguments]    ${url}   ${browser}    ${expectedTitle}
    Open Browser    ${url}    ${browser}
    ${TITLE}=      Get Title
    Should Contain    ${TITLE}   ${expectedTitle}

Run the below command to execute the test

robot file_name.robot





13 views0 comments

Comments


bottom of page