Robot Framework

How to create User Defined Keywords in Robot Framework

User Defined Keywords in Robot Framework

With the help of user Defined keywords, we can group our test steps under a single keyword. So instead of writing multiple lines repeatedly, we can just write the keyword once. It also helps in making the test cases more readable.

The different ways in which we can create user-defined keywords are as follows:

  • User Defined Keywords with Arguments
  • User Defined Keywords without Arguments
  • User Defined Keywords with Arguments and Return Value

In this blog, we will discuss-

  • Project setup for User Defined Keywords.
  • Create a Keyword in the keywords section without arguments and use it in the test case section.
  • Create a Keyword in the keywords section with arguments and use it in the test case section.
  • Creating a Keyword in the keywords section with arguments and return value and using it in the test case section.

Project setup for User Defined Keywords-

Firstly, we must install all prerequisite libraries in our project (for more info about prerequisites you can visit- https://www.neovasolutions.com/2022/07/21/robot-framework-automation-using-selenium-and-python/ )

To ensure that the required libraries are installed, use pip list command, or manually navigate to:

File > settings > project: robot automation> python interpreter

Project Setup

After verifying the prerequisites, create a file named ‘UserKeywords

— Create the file under the ‘Testcases’ directory with .robot extension >Enter

File Name

Given below is an example for a better understanding of how we can create keywords in our test.

Testcase:

Step1: Visit https://www.google.com/

Step2: Search Product

Step3: Get the title of the webpage

Step4: Close the browser

*** Settings ***
Library SeleniumLibrary
*** Variables ***
${url}  https://www.google.com/
${browser}  chrome
${SearchBar}    xpath://input[@title='Search']

*** Test Cases ***
Verify Search Page
    ${Title}=   Search Product     ${SearchBar}  ProductName
    log to console    ${Title}
    close browser
*** Keywords ***
#creating simple user defined keywords without arguments
Start Test
    open browser    ${url}  ${browser}
    maximize browser window
#creating user defined keywords with arguments and return value
Search Product
    [Arguments]   ${search_bar}  ${product_name}
    input text    ${search_bar}  ${product_name}
#get the title and store it in variable
    ${title}=   get title
    [Return]    ${title}

***Settings*** – To work with robot framework keywords we need to import   Library SeleniumLibrary in the settings section because all the keywords are built in the selenium library. Similarly, we can import other libraries, resource files, and variable files as per our project requirement in this section.

***Variables*** – This section contains the defined variables that are used              commonly, here we declared ${url}, ${browser}, and ${SearchBar} variables for browser, URL, and search field so that we can reuse these variables. In the same way, we can create more variables according to the project requirement.

User Defined Keywords code

Code explanation:

Let us understand the code by diving into various parts:

  • In this test case, we first created a keyword named ‘Start Test’ in the keywords section and mentioned it in the test cases section. The Start Test Keyword will open the https://demo.automationtesting.in/Register.htmlURL in the chrome browser and will maximize the browser window.
Part 1
*** Test Cases ***
Verify Search Page
    Start Test

*** Keywords ***
#creating simple user defined keywords without arguments

Start Test
    open browser    ${url}  ${browser}
    maximize browser window

Here, the Start Test Keyword does not require any arguments, it is a keyword without an argument.

  • Next, we created a keyword named ‘Search Product’ and passed parameters to it, to pass a parameter to the keyword we added an argument that accepts two arguments ${search_bar} and ${product_name}, accordingly, the input text keyword will use these arguments to search the product. In the test cases section, we used the keyword and passed two parameters to it in order to search for a product.
Part 2
*** Test Cases ***
Verify Search Page
    Start Test
    Search Product     ${SearchBar}  ProductName
*** Keywords ***
#creating simple user defined keywords without arguments
Start Test
    open browser    ${url}  ${browser}
    maximize browser window
#creating user defined keywords with arguments 
Search Product
    [Arguments]   ${search_bar}  ${product_name}
    input text    ${search_bar}  ${product_name}

In this part, the Search Product keyword takes two parameters, it is a keyword with an argument.

  •  Lastly, we used get title keyword to get the title of the page and stored it in a variable, after which we printed the title that has been captured by using log to console keyword and ended the session by close browser keyword.
Part 3
*** Test Cases ***
Verify Search Page
    Start Test
    ${Title}=   Search Product     ${SearchBar}  ProductName
    log to console    ${Title}
    close browser
*** Keywords ***
#creating simple user defined keywords without arguments
Start Test
    open browser    ${url}  ${browser}
    maximize browser window
#creating user defined keywords with arguments
Search Product
    [Arguments]   ${search_bar}  ${product_name}
    input text    ${search_bar}  ${product_name}
#get the title and store it in variable
    ${title}=   get title
    [Return]    ${title}

Here, the Search Product keyword takes two parameters and returns a value, it is a keyword with an argument and returns the value.

Execution:

To execute the test case we need to use the command below:

Execution command

After execution, we should get the result as a pass.

Pass result

Reports:

After every execution, the robot framework creates detailed HTML reports.

We get three files – log.html, output.xml, and report.html.

Log.html:

User Defined Keywords Log

 Report.html:

User Defined Keywords report

Read our next blog on “How to create Variables in Robot Framework”.