Table of Contents
Introduction
Robot Framework is a robust automation framework available as open-source software, renowned for its ease of use and adaptability. Although it has a large number of built-in keywords for different tasks, one of its best features is the option to generate custom or user-defined keywords, which enables users to expand its usefulness to suit their own requirements. We’ll get into the idea of user-defined keywords in Robot Framework in this blog article and see how they may really help in our test automation endeavors.
What is a user-defined or custom keyword?
User-defined keywords are unique terms that people have made up to represent a group of behaviors or acts. ‘Python’ or the unique syntax of the ‘Robot Framework’ can be used to write these keywords. They enable the abstraction of intricate step sequences into reusable parts, encouraging modular and manageable test suites. Whether you’re automating mobile apps, online applications, or APIs, user-defined keywords provide a scalable and adaptable way to create reliable test suites.
Benefits of User-Defined or custom Keyword
Reusability: By combining similar operations or features into a single, named item, user-defined keywords help to improve reusability. This makes it simple for testers to apply these keywords to different test cases, which lowers repetition and improves maintainability.
Abstraction: Testers can make their test cases easier to interpret by abstracting complicated step sequences into user-defined keywords. Testers can communicate the intended actions by using high-level, descriptive terms, rather than overwhelming test scripts with intricate implementation logic.
Modularity: By dividing test cases into more manageable chunks, user-defined keywords promote modular test design. This modular design makes maintenance and scalability easier because individual keyword changes have little effect on the entire test suite.
Customization: Testers can easily modify automation scripts to meet project needs by using user-defined keywords. User-defined keywords enable testers to tailor their automation solutions to specific tasks, such as managing unique UI components, building domain-specific functionalities, or interacting with external systems.
Parameterization: Testers can develop dynamic and flexible test cases by parameterizing user-defined keywords. Testers can use the same keyword with multiple data inputs by giving parameters to it, which allows for thorough test coverage without repeating test code.
Readability: Test scripts are easier to read and easier for team members to understand when user-defined keywords have descriptive names assigned to them. Rather than requiring lengthy comments, descriptive keywords operate as self-documenting elements by offering insights into the intended behaviors.
Teamwork: By offering a uniform means of interacting with the automation framework, user-defined keywords promote teamwork among team members. Testers can collaborate and exchange knowledge within the team by sharing and reusing custom keywords across several projects.
Centralization of Logic: By encouraging code reuse and consistency, user-defined keywords center the implementation logic inside a test suite. A keyword’s underlying logic can be updated or changed in a single place, guaranteeing that all test cases utilizing that keyword mirror the changes. This makes maintenance easier and lowers the possibility of mistakes.
Creating User-Defined Keywords
It’s simple to create user-defined keywords in Robot Framework. Testers can create their own Python libraries or use the Keywords section in Robot Framework test files to define keywords. The decision between these approaches is based on project preferences, reusability, and complexity.
Custom keyword creation using Python.
There are three types of keywords we can create; we will show you the sample code for each of them.
- No arguments, no return value
- With arguments but no return value
- With arguments and with return value
- First, create a subfolder, let’s say ‘CustomKeywords’ in your robot framework project directory.
- Create a python file in this subfolder, let’s say userkeyword.py
- Now create three python functions as per the above types. e.g. You can follow the project structure from https://docs.robotframework.org/docs/examples/project_structure
- Now go to your robot resource file and import the Python file using the relative path.
*** Settings ***
Library OperatingSystem
Library SeleniumLibrary
Library ../CustomKeywords/userkeyword.py
- Map the Python function with the ‘robot framework’ keyword.
*** Keywords ***
Print Name
print_name
- You can create different keyword for each function, or a single keyword with more than one python functions as per your need. You can also mention the standard keyword along with the user-defined keyword.
- Below is an example of where the name is passed as an argument and we are also calling standard ‘Log’ keyword
Print Name
[Arguments] ${lastname}
#print_name
print_name_arg ${lastname}
- The test script from where we are calling this keyword.
*** Test Case ***
Test User keyword
Print Name Das
- Execute the script.robot file # robot TestCasesscript.robot
- Check the log.html file.
- Now we are making z keyword with an argument and a return value.
*** Keywords ***
Print Name
[Arguments] ${lastname}
#print_name
#print_name_arg ${lastname}
#Log 'Last Name is printed'
${retval}= print_name_arg_ret ${lastname}
log ${retval}
- The log.html file after running the same script file.
You may like the test execution automation steps for TestRail and RTM: https://dasfascination.com/testrail-execution-automation/
https://dasfascination.com/rtm-test-management-automation/
Best Practices for User-Defined Keywords
To optimize the efficacy of user-defined keywords, take into account the subsequent recommended practices:
Descriptive Naming: To improve readability and clarity, give your user-defined keywords meaningful and descriptive names.
Parameterization: To make your keywords more flexible and responsive to various test conditions, parameterize them.
Encapsulation: To preserve clarity and modularity, concentrate your keywords on a particular duty or activity.
Documentation: To help other team members comprehend the use and purpose of your user-defined keywords, provide thorough documentation.
One Reply to “Custom Keywords: Unlocking the Power in Robot Framework”