1. 程式人生 > >robot framework 自定義Library

robot framework 自定義Library

1. 首先在..\Python27\Lib\site-packages 目錄下建立MyLibrary 目錄,用於放自定義的library庫。

2.在MyLibrary 資料夾下新建mylibrary.py檔案建立自己的關鍵字,如下所示:

class MyLibrary(object):

 
  def test1(self):
        """test 1"""
        print "test1"
    def test2(self):
        """test 2"""
        print "test1"
    def test3(self):
        """test 3"""
        print "test3"

這裡我們定義了三個關鍵字test1,test2,test3

2.要想在robot framework 啟動後加載這些關鍵字,還需要在MyLibrary 目錄下建立__init__.py 檔案,並且它不是空的。

# mylibrary 即建立的py檔名,MyLibrary即關鍵字Class名

from mylibrary import MyLibrary
__version__ = '0.1'
class MyLibrary(MyLibrary):
    """
    this is comment about mylibrary
    """
    ROBOT_LIBRARY_SCOPE = 'GLOBAL'


這個檔案中其實有用的資訊就三行,但必不可少。robot framwork 在啟動時會載入這個檔案,因為
在這個檔案裡指明瞭有個mylibrary 檔案下面有個MyLibrary類。從而載入類裡的方法(test1(),test2等)。

這樣在robot framework RIDE中就可以像新增普通庫一樣,新增自定義的MyLibrary這個庫了