【问题标题】:Find keyword name (or keyword name stack) in Robot Framework在 Robot Framework 中查找关键字名称(或关键字名称堆栈)
【发布时间】:2017-07-19 10:23:43
【问题描述】:

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

【问题讨论】:

  • 只是需要关键字名称堆栈,还是需要与您链接到的问题中要求的关键字关联的文件名?
  • 您打算如何使用这些信息?它是否需要被其他关键字访问?您是将其保存到文件还是数据库中?
  • 我知道这与良好的测试实践相悖,但我需要知道某些关键字是否在当前执行之前执行,如果没有执行。我试图让测试用例依赖于其他人。我知道这不是一个好习惯,但我必须这样做。

标签: python testing robotframework keyword


【解决方案1】:

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

【讨论】:

  • 我稍微修改了这个解决方案,所以它更符合我的需要,但如果没有你的帮助,我将一事无成。非常感谢! :)
猜你喜欢
  • 2019-05-26
  • 2012-03-03
  • 2016-09-17
  • 2020-08-21
  • 2020-05-11
  • 2014-05-25
  • 2015-12-04
  • 2019-04-26
  • 2021-09-05
相关资源
最近更新 更多