[英]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: 因此,我认为解决此问题的唯一方法是:
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.