Creating own keywords

You can create own keywords either by grouping PaceWords (and keywords implemented in 3rd party libraries) to a new "Actionword" or by creating a full keyword library by yourself using Python.

Creating ActionWords by grouping PaceWords

Actionwords are keywords that are based on other Pacewords or keywords and do multiple actions at once. These are normally used in situations that repeat often; you notice that you need to write same set of Pacewords to many test cases.

Note that it’s advisable to keep the amount of "Actionwords" as low as possible. Typically if you have more than 100 Actionwords in one project, you will end up using a lot of effort just to maintain them.

Custom keywords are defined in a resource file. The suggested structure in Qentinel Pace is that resource files are stored in resources folder in your project.

Since we aim to keep the amount of custom keywords low, usually just one file, keywords.robot is enough. You can of course name the file anything else or have keywords for different areas of the application in different files, but we would suggest starting with a simple structure. The more files and keywords you have, the more you end up spending time finding correct place to modify if changes are needed.

# import QWeb and any other needed built-in/3rd party Robot FW libaries here
Library QWeb
Library DateTime

*** Variables *** # Define needed variables here

*** Keywords *** # Define your custom keywords here

MyNewKeyword
    [Documentation]     Logs Hello world to the results log
    ...                 This line is just an example for multiline documentation
    Log Hello world!

Creating custom keywords with Python

It’s advisable to create custom keywords in Python, if possible. Python is much more flexible language and can handle complex logic etc. much easier than plain robot framework. With Python you can utilize any Pacewords or even functions that are part of selenium.

It’s advisable to store all Python source files in libraries or libs folder in your project structure. Any python file can be imported as any other Robot Framework library and by default all methods defined in a file will be exposed as keywords.

To start with something extremely simple, we will create a Python keyword that prints "Hello from custom Python keyword" to result log.

1) Create a python file my_custom_library.py under libraries folder

2) Create the following content to the newly created Python file:

from robot.api import logger
class my_custom_library():
    def print_message(self, message):
        """Prints a custom message to log

        """
        logger.info(message)

3) Import your new library in your test suite or resource file

Library            ../libraries/my_custom_library.py

4) Call the keyword in your test case

Print message       Hello from custom Python keyword

To learn more about creating own keywords and libraries, please check our Advanced Training from training section.