Hybrid Keywords Sample

In robot framework, there are two ways you can control keywords in a library, one is hybrid keywords and another is dynamic keywords.

Hybrid Keywords



http://robotframework.googlecode.com/hg/doc/userguide/RobotFrameworkUserGuide.html#hybrid-library-api

 

from somewhere import external_keyword

  class HybridExample:
 
    def get_keyword_names(self):
        return ['my_keyword', 'external_keyword']
 
    def my_keyword(self, arg):
        print "My Keyword called with '%s'" % arg
 
    def __getattr__(self, name):
        if name == 'external_keyword':
            return external_keyword
        raise AttributeError("Non-existing attribute '%s'" % name)

 

 

QAAPCLIKeywords

def get_keyword_names(self):

         apis = []      

         #Get methods list of this class.

         own_methods = inspect.getmembers(RWQAAPCLIKeywords, predicate=inspect.ismethod)

         for mtd_name, mtd in own_methods:

                   if not mtd_name.startswith('_') and mtd_name != 'get_keyword_names':

                            apis.append(mtd_name)       

 

         #Adding keywords from _self methods.

         apis.extend(self._self.list_methods())       

 

         return apis

 

def __getattr__(self, name, *args):

         #When call keywords in _self, find method and return.

         if name in self._self.methods:

                   func = self._self.find_method(name)

                   return func

Base.py

from inspect import ismethod, stack

class Base(object):

    def __init__(self):

        self.ignore_mtds = []

        self.methods = self.list_methods()

 

    def list_methods(self):

        apis = list(set(dir(self)))

        apis = [x for x in apis if (not x.startswith('_')) and \

                                (ismethod(getattr(self, x)))]

        

        ignore = ['list_methods', 'find_method']

        if self.ignore_mtds: ignore.extend(self.ignore_mtds)

       

        return list(set(apis) - set(ignore))

 

    def find_method(self, name):

        if name in self.methods:

            return getattr(self, name)

        

        return None

發佈了52 篇原創文章 · 獲贊 2 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章