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 a 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 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")

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

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


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


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

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.


Creating a Keyword with Argument and No Return Value

We are going to modify our old file, 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")

Go to the ride editor and open the test suite Ownkeywords and delete 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.

But, when we run the test case it will show an error as it 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

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

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


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 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.

0 results
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions