Robot Framework: Customizing your own Python library

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.