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:
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
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
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.
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.
But, when we run the test case it will show an error as it requires an argument.
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 mysecond.py 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.
I created a new user-defined keyword without arguments and no return value, even I had successfully imported