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

Find keyword name (or keyword name stack) in Robot Framework

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

解决您的问题的最简单方法可能是组合 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