在Robot Framework中查找关键字名称(或关键字名称堆栈)

问题描述:

我一直在努力解决与此问题相同的问题:Robot Framework location and name of keyword - 我需要找到一个关键字名称堆栈(我对关键字文件名不感兴趣现在)。
似乎作者找到了解决方案。
不幸的是,我不能应用它,因为在我的Robot Framework(3.0.2)版本中,_ExecutionContext没有字段或属性关键字,所以在我的情况下,行EXECUTION_CONTEXTS.current.keywords[-1].name
引发了一个异常。感谢您的任何帮助!在Robot Framework中查找关键字名称(或关键字名称堆栈)

+0

你_just_需要一个关键字名称堆栈,或者你需要与关键字相关联的文件名正如您链接到的问题中所要求的那样? –

+0

你打算如何使用这些信息?它是否需要被其他关键字访问?你是否将它保存到文件或数据库? –

+0

我知道这与测试的良好实践是相反的,但我需要知道某些关键字是否在当前执行之前执行,如果不是,则执行它们。我试图让测试用例依赖于其他人。我知道这不是一个好习惯,但我必须这样做。 – daburu

对您的问题最简单的解决方案可能是结合keyword library and listener into a single module。监听者可以跟踪已调用的关键字,并且该库可以提供访问该关键字列表的关键字。

下面是一个非常基本的例子。没有错误检查,并且需要精确匹配,但它说明了总体思路。

首先,自定义库:

from robot.libraries.BuiltIn import BuiltIn 

class CustomLibrary(object): 
    ROBOT_LISTENER_API_VERSION = 2 
    ROBOT_LIBRARY_SCOPE = "GLOBAL" 

    def __init__(self): 
     self.ROBOT_LIBRARY_LISTENER = self 
     self.keywords = [] 

    # this defines a keyword named "Require Keyword" 
    def require_keyword(self, kwname): 
     if kwname not in self.keywords: 
      raise Exception("keyword '%s' hasn't been run" % kwname) 

    # this defines the "start keyword" listener method. 
    # the leading underscore prevents it from being treated 
    # as a keyword 
    def _start_keyword(self, name, attrs): 
     self.keywords.append(name) 

接下来,使用它的一个例子:

*** Settings *** 
Library CustomLibrary.py 

*** Keywords *** 
Example keyword 
    pass 

*** Test Cases *** 
Example test case 
    log hello, world! 

    # this will pass, since we called the log keyword 
    require keyword BuiltIn.Log 

    # this will fail because we haven't called the example keyword 
    require keyword Example keyword 
+0

我稍微修改了这个解决方案,所以它更适合我的需求,但如果没有您的帮助,我将无法获得。非常感谢你! :) – daburu