Writing test cases using the robot framework often requires developing your own keywords, and some keywords need to be implemented by writing Python code yourself. In RF, you need to define the Python library yourself. This process is actually not complicated, this article to introduce the next.
1, in the Python installation directory under the Lib\site-packages directory to create a new directory, directory name is the name of the library, such as Newlibrary
2, in the new Newlibrary directory to create a python file, any valid file name, the extension is py. such as: myclass.py
The contents are as follows
class MyClass (object): def __init__ (self): Pass def printmsg (self,msg): Print " "+msg
The above code defines a Python class, which, in addition to the constructor method _init_, defines a printmsg method, which is actually a keyword in RF that can be used directly in RF. Below we introduce.
3. Create the __init__.py file under the new Newlibrary directory, the file name must be fixed. The function of this file is to expose the keyword class so that RF can know.
The contents are as follows:
from Import MyClass class newlibrary (MyClass): ' GLOBAL '
4, through the previous three steps, one of the simplest custom Newlibrary library is created, in the RF tool can be used
1) To import the Newlibrary library first
2) use keyword printmsg. It is necessary to note that when referenced in RF, it can be written as printmsg or print Msg. The general habit is to separate the words with spaces. Because the RF keyword is not case sensitive.
Test Cases * * *SUCCESSSD print msg God printmsg ss printmsg yy
5. Add New Keywords
Adding new keywords is simple, just add a new method to the MyClass class. Such as:
class MyClass (object): def __init__ (self): Pass def printmsg (self,msg): Print " +msg def join_info ( self,a,b):Print a+b
The Join_info method is defined here. When used in RF, it can be written as join info separated by a space.
The simplest custom modules are described above, although they are simple, but can actually meet the needs of most of their own development.
Robot Framework: Customizing your own Python library