Advanced Keyword Development in Robot Framework

Robot Framework is a popular open-source test automation framework used for acceptance testing and acceptance test-driven development (ATDD). The framework provides a simple, yet powerful syntax for writing tests, which are organized as keywords. The use of keywords makes the tests easy to read, write and understand.

In this blog, we will explore advanced keyword development in Robot Framework. We will learn about the different types of keywords, how to create them, and how to make use of the advanced features to make our tests more powerful and efficient.

Creating Keywords

To create a keyword, you first need to define the keyword in a test library file. The keyword is defined using a specific syntax, and it is then made available to the tests.

The syntax for defining a keyword is as follows:

*** Keywords ***
Keyword Name
    [Arguments]
    [Keyword Implementation]

The keyword name is the name that you give to the keyword. The arguments are the parameters that are passed to the keyword when it is called, and the keyword implementation is the code that is executed when the keyword is called.

Types of Keywords

In Robot Framework, there are two types of keywords:

  1. User-defined keywords
  2. Built-in keywords

User-defined keywords are created by the user and are specific to the tests being executed. These keywords can be written in either Python or Java and can be reused across multiple tests.

*** Keywords ***
My Custom Keyword
    [Arguments]    ${arg1}    ${arg2}
    Log    The value of arg1 is ${arg1}
    Log    The value of arg2 is ${arg2}

In the example above, a user-defined keyword named My Custom Keyword is created. This keyword takes two arguments, arg1 and arg2. The Log keyword is then used to log the values of the two arguments.

To use this keyword in a test case, you would simply call it by its name:

*** Test Cases ***
My Test Case
  My Custom Keyword      10      20


In the above test case, the My Custom Keyword keyword is called with the arguments 10 and 20. The keyword will then log the values of these arguments as specified in the keyword definition.

Built-in keywords are provided by the framework itself and can be used for various purposes, such as navigating a web page, interacting with elements on the page, and performing actions.

*** Test Cases ***
My Test Case
    ${variable}=    Set Variable         10
    Run Keyword If  ${variable} == 10     Log to console    The variable is equal to 10

In the example above, the Run Keyword If keyword is used to conditionally execute the Log keyword. The Set Variable keyword is used to set the value of the ${variable} variable to 10. The Run Keyword If keyword is then used to check if the value of ${variable} is equal to 10. If it is, the Log keyword will be executed and the message “The variable is equal to 10” will be logged.The Run Keyword If keyword is just one example of the many built-in keywords that Robot Framework provides. Other built-in keywords include Log, Sleep, Wait Until Keyword Succeeds, Should Be Equal, and many more. These keywords can be used to perform a wide range of actions and verification in your test cases.

Steps involved in developing advanced keywords in the Robot Framework.

Step 1: Defining Keywords

The first step in developing advanced keywords in the Robot Framework is to define the keywords themselves. Keywords can be defined in the *** Keywords *** section of a test file. For example:

*** Keywords ***
Add Numbers
    [Arguments] ${a} ${b}
    ${c}= Add ${a} ${b}
    [Return] ${c}

Multiply Numbers
    [Arguments] ${a} ${b}
    ${c}= Multiply ${a} ${b}
    [Return] ${c}

In this example, the Add Numbers and Multiply Numbers keywords are defined. The [Arguments] section specifies the arguments required by the keyword, while the [Return] section specifies the return value of the keyword.

Step 2: Using Built-in Keywords

The Robot Framework provides a number of built-in keywords that can be used to develop advanced keywords. For example, you can use the Run Keyword And Return Status keyword to run another keyword and return its status, or the Run Keyword And Ignore Error keyword to run another keyword and ignore any errors that may occur.

*** Keywords ***
Add Numbers And Check Status
    [Arguments] ${a} ${b}
    ${status} ${result}= Run Keyword And Return Status Add Numbers ${a} ${b}
    Log ${status}
    [Return] ${result}

Multiply Numbers And Ignore Error
    [Arguments] ${a} ${b}
    ${result}= Run Keyword And Ignore Error Multiply Numbers ${a} ${b}
    [Return] ${result}

In this example, the Add Numbers And Check Status keyword runs the Add Numbers keyword and returns its status, while the Multiply Numbers And Ignore Error keyword runs the Multiply Numbers keyword and ignores any errors that may occur.

Step 3: Using Variables

Variables can be used to store data that can be used by keywords. For example:

*** Keywords ***
Add Numbers
    [Arguments] ${a} ${b}
    ${c}= Add ${a} ${b}
    [Return] ${c}

Multiply Numbers
    [Arguments] ${a} ${b}
    ${c}= Multiply ${a} ${b}
    [Return] ${c}

Calculate
    [Arguments] ${operation} ${a} ${b}
    ${result}= Evaluate ${operation} ${a} ${b}
    [Return] ${result}

Evaluate
    [Arguments] ${operation} ${a} ${b}
    ${result}= ${operation} ${a}

Robot Framework provides several advanced features that can be used to make the tests more powerful and efficient.

  1. Return Values: Keywords can return values, which can be used by other keywords or by the test itself.

Example: 

*** Test Cases ***
Example Test Case
    ${result}=    Add Numbers    2    3
    Should Be Equal    ${result}    5

In this example, the Add Numbers keyword returns a value of 5, which is then stored in the variable ${result}. The subsequent Should Be Equal keyword compares the value stored in ${result} to 5, and the test case is considered passed if they are equal. If they are not equal, the test case would fail.

  1. Loop Keywords: Keywords can be used within loops to repeat actions multiple times.

Example:

*** Test Cases ***
Example Test Case

    :FOR    ${item}    IN    1    2    3

    \    Log    ${item}

    \    Should Be Equal As Strings    ${item}    ${item}

    :END

In this example, the :FOR keyword is used to iterate over a list of values (1, 2, and 3). On each iteration, the Log keyword is used to log the current value of ${item}, and the Should Be Equal As Strings keyword is used to compare ${item} to itself to demonstrate that the loop is working correctly. The :END keyword is used to end the loop.

  1. Keyword Arguments: Keywords can accept arguments, which can be used to pass values to the keyword.

Example:

*** Keywords ***
Add Numbers
    [Arguments]    ${a}    ${b}
    ${c}=    Add    ${a}    ${b}
    [Return]    ${c}

*** Test Cases ***
Example Test Case
    ${result}=    Add Numbers    2    3
    Should Be Equal    ${result}    5

In this example, the Add Numbers keyword takes two arguments, ${a} and ${b}. These arguments are used to perform a calculation (${c}=Add ${a} ${b}), and the result is returned using the [Return] keyword. In the test case, the Add Numbers keyword is called with the arguments 2 and 3, and the result is stored in the ${result} variable. The subsequent Should Be Equal keyword then compares the value stored in ${result} to 5 to verify that the calculation was performed correctly.

  1. Keyword Calls: Keywords can be called within other keywords to perform actions.

Example: 

*** Keywords ***
Add Numbers
    [Arguments]    ${a}    ${b}
    ${c}=    Add    ${a}    ${b}
    [Return]    ${c}

Multiply Numbers
    [Arguments]    ${a}    ${b}
    ${result}=    Add Numbers    ${a}    ${b}
    ${c}=    Multiply    ${result}    ${b}
    [Return]    ${c}

*** Test Cases ***

Example Test Case
    ${result}=    Multiply Numbers    2    3
    Should Be Equal    ${result}    6

In this example, the Add Numbers keyword takes two arguments, ${a} and ${b}, performs a calculation, and returns the result. The Multiply Numbers keyword takes two arguments, ${a} and ${b}, calls the Add Numbers keyword to perform the calculation, and then performs a second calculation (${c}=Multiply ${result} ${b}) using the result. The final result is returned using the [Return] keyword. In the test case, the Multiply Numbers keyword is called with the arguments 2 and 3, and the result is stored in the ${result} variable. The subsequent Should Be Equal keyword then compares the value stored in ${result} to 6 to verify that the calculations were performed correctly.

  1. Dynamic Keyword Calls: Keywords can be called dynamically based on the values of variables or other conditions.

Example:

*** Keywords ***
Add Numbers
    [Arguments]    ${a}    ${b}
    ${c}=    Add    ${a}    ${b}
    [Return]    ${c}

Multiply Numbers
    [Arguments]    ${a}    ${b}
    ${result}=    Add Numbers    ${a}    ${b}
    ${c}=    Multiply    ${result}    ${b}
    [Return]    ${c}

*** Test Cases ***
Example Test Case
    ${operation}=    Set Variable    Multiply Numbers
    ${result}=    Evaluate    ${operation}    2    3
    Should Be Equal    ${result}    6

In this example, the Add Numbers and Multiply Numbers keywords are defined as in the previous example. In the test case, the Set Variable keyword is used to store the keyword name (Multiply Numbers) in a variable (${operation}). The Evaluate keyword is then used to dynamically call the keyword, passing the keyword name (stored in ${operation}) and the arguments 2 and 3 as arguments. The result is stored in the ${result} variable, and the subsequent Should Be Equal keyword then compares the value stored in ${result} to 6 to verify that the calculations were performed correctly.

  1. Multiple Implementations: Keywords can have multiple implementations, which can be selected based on the values of variables or other conditions. You can also call it overloading.

Example:

*** Keywords ***
Add Numbers
    [Arguments]    ${a}    ${b}
    ${c}=    Add    ${a}    ${b}
    [Return]    ${c}

Add Numbers
    [Arguments]    ${a}    ${b}    ${c}
    ${result}=    Add    ${a}    ${b}
    ${result}=    Add    ${result}    ${c}
    [Return]    ${result}

*** Test Cases ***
Example Test Case 1
    ${result}=    Add Numbers    2    3
    Should Be Equal    ${result}    5

Example Test Case 2
    ${result}=    Add Numbers    2    3    4
    Should Be Equal    ${result}    9

In this example, two implementations of the Add Numbers keyword are defined. The first implementation takes two arguments, ${a} and ${b}, performs a calculation, and returns the result. The second implementation takes three arguments, ${a}, ${b}, and ${c}, performs two calculations, and returns the final result. In the two test cases, each implementation of the Add Numbers keyword is called with different arguments, and the results are stored in the ${result} variable. The subsequent Should Be Equal keywords then compare the values stored in ${result} to the expected values to verify that the calculations were performed correctly.

  1. Keyword Documentation: Keywords can be documented to provide information about what they do and how they are used.

Example:

*** Keywords ***
Add Numbers
    [Documentation]
    This keyword takes two numbers as arguments and returns their sum.
    [Arguments]    ${a}    ${b}
    ${c}=    Add    ${a}    ${b}
    [Return]    ${c}

Multiply Numbers
    [Documentation]
    This keyword takes two numbers as arguments and returns their product.
    [Arguments]    ${a}    ${b}
    ${c}=    Multiply    ${a}    ${b}
    [Return]    ${c}

In this example, the Add Numbers and Multiply Numbers keywords are defined with documentation. The [Documentation] section provides a brief description of each keyword’s purpose and arguments. The keywords themselves are defined as in previous examples. The documentation can be viewed in the Robot Framework log file or in tools such as the Robot Framework Ride. This documentation makes it easier for other users to understand the purpose and usage of each keyword.

Conclusion

In conclusion, advanced keyword development in Robot Framework is an essential aspect of test automation. By using the advanced features of the framework, you can create tests that are more powerful, efficient, and maintainable. Whether you are a beginner or an experienced test automation engineer, Robot Framework is a great tool to help you achieve your testing goals.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.