Robot Framework 位置和关键字名称 [英] Robot Framework location and name of keyword

查看:38
本文介绍了Robot Framework 位置和关键字名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个带有 0 参数函数的 Python 库,我的自定义机器人框架关键字可以调用该函数.它需要知道定义关键字的文件的绝对路径,以及关键字的名称.我知道如何使用 robot.libraries.BuiltIn 库和 ${SUITE_SOURCE}${TEST NAME} 对测试用例做类似的事情> 变量,但我找不到任何与自定义关键字类似的内容.我不在乎答案有多复杂,也许我必须深入了解 Robot Framework 的内部类并以某种方式访问​​该数据.有没有办法做到这一点?

I want to create a python library with a 0 argument function that my custom Robot Framework keywords can call. 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. 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. Is there any way to do this?

推荐答案

感谢 janne 我找到了解决方案.

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 是调用的关键字栈,最早的在前,最近的在最后,所以 EXECUTION_CONTEXTS.current.keywords[-1] 得到最后一个关键字,也就是调用这个函数的关键字.

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.get_handler(name).libname 获取定义关键字 name 的库的名称.对于用户定义的关键字,它是文件名(不是完整路径)减去扩展名.

EXECUTION_CONTEXTS.current.get_handler(name).libname gets the name of the library in which the keyword name is defined. 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 是一个包含所有资源的字典,其中key是绝对路径.因为文件路径是键,所以我必须搜索键,使得值的名称是定义关键字的资源的名称 (libname)

EXECUTION_CONTEXTS.current.namespace._kw_store.resources is a dictionary of all included resources, where the key is the absolute path. 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)

这篇关于Robot Framework 位置和关键字名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆