28

I already have Python source files for some custom tasks. Can I create a custom library of these tasks as keywords and use in the Robot Framework?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Narendra Petkar
  • 480
  • 1
  • 4
  • 10

1 Answers1

40

Yes, you can. This is all documented fairly extensively in the Robot Framework user guide, in the section titled Creating test libraries.

You have a couple of choices. You can use your module directly, which makes every method in the module available as a keyword. This is probably not what you want since the library probably wasn't designed to be used as a collection of keywords. Your second choice is to create a new library that imports your modules, and your new library provides keywords that call the functions in the other library.

As a simple example, let's say you have a module named MyLibrary.py with the following contents:

def join_two_strings(arg1, arg2):
    return arg1 + " " + arg2

You can use this directly in a test suite as in the following example, assuming that MyLibrary.py is in the same folder as the suite, or is in a folder in your PYTHONPATH:

*** Settings ***
| Library | MyLibrary.py

*** Test Cases ***
| Example that calls a Python keyword
| | ${result}= | join two strings | hello | world
| | Should be equal | ${result} | hello world
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • Hello, in that case, where to put MyLibrary.py file? – thekucays Nov 14 '18 at 14:54
  • I put mine in a Resources subdirectory and give the relative path to MyLibrary.py in the Settings as in: *** Settings *** Library Collections Library Resources/MyLibrary.py Also note that as @Bryan showed above the Python underscores can be replaced with the more Robot Framework style spaces. – DDay Nov 30 '18 at 14:39