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 the 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:Robot 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 by 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.

creating-newtest suite-ownkeywords

create-new-file-tc01

Go to the test suite and import the library from where you have saved the Myfirst 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:Robot 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

mysecond-file

Go to the ride editor and import the mysecond.py file to the Ownkeywords test suite.

importing-my-second-python-file

Create a new test case called jointwo_strings_TC03 and try to access the keyword to join two string as shown below. and pass the parameters Ngen and the second parameter Digital

join-two-string-tets-case

Now select the test case and start executing.

Installation of Selenium Library

Problem Faced:

I created a new user-defined keyword without arguments and no return value, even I had successfully imported Myfirst.py library into my new test suite Ownkeywords, but when I try to run the test case, I am getting an error as invalid mode "w" and I was unable to create myfile.txt file.

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions

Subscribe to See Videos

Subscribe to my Youtube channel for new videos : Subscribe Now