Creating User-Defined Keywords by using Python in Robot Framework

The robot framework has a lot of different libraries, it has an internal library, an external library which almost serves our purpose to some extent, but in some cases, there is need to use our own keywords, and it's our requirement to create our own keywords with robot framework, we can write a keyword with very simple python language and we can import it has a library in our test suite.

So basically we have to create a .py file and import in the project just like another library. each method in our .py file acts as a keyword, the method in the python can be of following :

  • Without arguments and without return value
  • With argument and no return value
  • Argument and return value both
Creating keyword without arguments and without return value:

Go to your project directory and create a notepad file in which I am going to write my first method which will not have any arguments and no return value. This method will create a file inside my robot project. We start the method by def and then we write the name of the method and after that, we write the actual body as shown below.

def createfile():
    file=open("C:
obot frameworkMyfile.txt","w")
    file.write(this is my first python keyword")
    file.close()

I am going to save this file in my Demo project folder my the name Myfirst.py as shown below

myfirst-keyword

We have created the method, now go to the ride editor and import this keyword, open the ride editor and create a new test suite by the name Ownkeywords and create a new test case as CreateNewFile_TC01.

Go to the test suite and import the library from where you have saved the file.

importing-myfirt-library

If it is successfully imported, it is displayed in the black color

successfully-imported-myfirst-keyword

Now go to the test case and try to access the keyword as shown below, as it does not require any arguments and doesn't return anything.

accessing-key-word-in-test-case

Creating a Keyword with Argument and No Return Value:

We are going to modify our old file Myfirst.py, go to the file location and open it with Python IDLE and edit as shown below and save it.

def createfile(mytext):
    file=open("D:
obot frameworkMyfile.txt","w")
    file.write(mytext)
    file.close()

Go to the ride editor and open the test suite Ownkeywords and delete Myfirst.py which you have imported before and then import the new edit file again. Go to the test case and delete the old keyword and try to access the new keyword, we created a keyword which requires an argument but no return value as we edited our old file which we imported before hence it is not showing anything.

accessing-keyword

But, when we run the test case it will show an error as it requires an argument.

requires-an-argument

Creating keyword with Arguments and Return value:

Go to the notepad and write the simple function as shown below

def join_two_string(arg1,arg2):
    retun arg1 + " " + arg2

And save the file as shown below

save-file-join-two-strings

Go to the ride editor and import the jointwo_strings to the Ownkeywords test suite and then go to the test case and try to access the keyword jointwo_strings.



Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions