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

标签 python testing robotframework keyword

我一直在努力解决与这个问题相同的问题: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

关于python - 在 Robot Framework 中查找关键字名称(或关键字名称堆栈),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42508905/

相关文章:

java - Jacoco 插件不报告重命名的集成测试类的覆盖率

testing - 即使断言失败,测试也没有失败

testing - 如何在 AngularJs 中使用私有(private)方法编写可测试的 Controller ?

python - 以编程方式使用 Gmail 的最佳方式是什么?

python - 使用 Flask WTForms validate_on_submit 总是返回 false

python - 如何从 Microsoft Powerpoint 打开嵌入的 '.py' 文件?

robotframework - 机器人框架如何在数据驱动测试中为报告输出的每个测试用例设置自己的名称

ios - 无法使用 iOS 应用程序的 Appium Inspector 定位元素

docker - 机器人框架-如何查看在Shell中执行的命令的输出?

javascript - 带有 Python 表条目的简单用户 Web 界面