简体   繁体   English

Robot Framework的位置和关键字名称

[英]Robot Framework location and name of keyword

I want to create a python library with a 0 argument function that my custom Robot Framework keywords can call. 我想使用自定义Robot Framework关键字可以调用的参数函数0创建python库。 It needs to know the absolute path of the file where the keyword is defined, and the name of the keyword. 它需要知道定义关键字的文件的绝对路径,以及关键字的名称。 I know how to do something similar with test cases using the robot.libraries.BuiltIn library and the ${SUITE_SOURCE} and ${TEST NAME} variables, but I can't find anything similar for custom keywords. 我知道如何使用robot.libraries.BuiltIn库以及${SUITE_SOURCE}${TEST NAME}变量对测试用例进行类似的robot.libraries.BuiltIn ,但是对于自定义关键字我找不到类似的东西。 I don't care how complicated the answer is, maybe I have to dig into the guts of Robot Framework's internal classes and access that data somehow. 我不在乎答案有多复杂,也许我必须深入研究Robot Framework内部类的内胆并以某种方式访问​​该数据。 Is there any way to do this? 有什么办法吗?

Thanks to janne I was able to find the solution. 多亏了珍妮,我才能找到解决方案。

from robot.running.context import EXECUTION_CONTEXTS

def resource_locator():
    name      = EXECUTION_CONTEXTS.current.keywords[-1].name
    libname   = EXECUTION_CONTEXTS.current.get_handler(name).libname
    resources = EXECUTION_CONTEXTS.current.namespace._kw_store.resources
    path = ""
    for key in resources._keys:
        if resources[key].name == libname:
            path = key
            break
    return {'name': name, 'path': path}

EXECUTION_CONTEXTS.current.keywords is the stack of keywords called, with the earliest first and the most recent last, so EXECUTION_CONTEXTS.current.keywords[-1] gets the last keyword, which is the keyword that called this function. EXECUTION_CONTEXTS.current.keywords是调用的关键字堆栈,最早的是最早的,最近的是最后一个,因此EXECUTION_CONTEXTS.current.keywords[-1]获得最后一个关键字,这是调用此函数的关键字。

EXECUTION_CONTEXTS.current.get_handler(name).libname gets the name of the library in which the keyword name is defined. EXECUTION_CONTEXTS.current.get_handler(name).libname获取定义关键字name的库的name In the case of user defined keywords, it is the filename (not the full path) minus the extension. 对于用户定义的关键字,它是文件名(不是完整路径)减去扩展名。

EXECUTION_CONTEXTS.current.namespace._kw_store.resources is a dictionary of all included resources, where the key is the absolute path. EXECUTION_CONTEXTS.current.namespace._kw_store.resources是所有包含的资源的字典,其中键是绝对路径。 Because the file path is the key, I have to search for the key such that the value's name is the name of the resource in which the keyword is defined ( libname ) 因为文件路径是键,所以我必须搜索键,使得值的名称是在其中定义了关键字的资源的名称( libname

I took a relatively quick look through the sources, and it seems that the execution context does have any reference to currently executing keyword. 我比较快速地浏览了源代码,似乎执行上下文确实对当前正在执行的关键字有任何引用。 So, the only way I can think of resolving this is: 因此,我认为解决此问题的唯一方法是:

  1. Your library needs also to be a listener, since listeners get events when a keyword is started 您的库也必须是一个侦听器,因为侦听器在启动关键字时会获取事件
  2. You need to go through robot.libraries.BuiltIn.EXECUTION_CONTEXT._kw_store.resources to find out which resource file contains the keyword currently executing. 您需要浏览robot.libraries.BuiltIn.EXECUTION_CONTEXT._kw_store.resources以找出哪个资源文件包含当前正在执行的关键字。

I did not do a POC, so I am not sure whether this actually doable, bu that's the solution that comes to my mind currently. 我没有进行POC,所以我不确定这是否切实可行,但是这就是我目前想到的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在机器人框架中找不到名称为“foo”的关键字错误 - No keyword with name 'foo' found error in robot framework Robot Framework中的关键字是什么? - What is a keyword in Robot Framework? 机器人框架中的 IF ELSE [关键字作为条件] - IF ELSE in robot framework [Keyword as a condition] 在关键字中运行关键字的机器人框架 - Robot Framework running a Keyword within a keyword 在机器人框架中申请循环后,错误如下-关键字名称不能为空 - After applying for loop in robot framework , Error came as follows- Keyword name cannot be empty 机器人框架 - “未找到名称为‘运行进程’的关键字”,同时包含进程库 - Robot Framework - “No keyword with name 'Run Process' found” while Process library included 导入具有相关功能的python模块后,robot框架出现“没有带名称的关键字”错误 - robot framework gives "no keyword with name" error after importing the python module which has the associated function 找不到名称为“获取 Json 值”的关键字或我在机器人框架中的 HttpLibrary.HTTP 中使用的任何关键字 - No Keyword with name 'Get Json Value' Found Error or any Keywords that I use in HttpLibrary.HTTP in Robot Framework 机器人框架-查询返回的关键字类型 - Robot Framework - query returned keyword types Robot FrameWork ::在关键字结束之前返回一个值 - Robot FrameWork:: Return a Value before Keyword ends
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM