用户关键字的robot.running.model.Keyword 对象children 属性在prerunmodifier start_suite 函数中返回空列表 [英] User keyword's robot.running.model.Keyword object children attribute returns empty list in prerunmodifier start_suite function

查看:41
本文介绍了用户关键字的robot.running.model.Keyword 对象children 属性在prerunmodifier start_suite 函数中返回空列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的 prerunmodifier,它实现了 start_suite 函数,它从 suite 变量中获取套件设置关键字并打印其属性.该对象是 robot.running.model.Keyword 类的实例,这里是 doc 用于该类.name、关键字typeidparent 属性正确,但timeoutdoctagschildren 属性不返回任何内容.keywordsmessages 属性也是如此.下面是我的简化代码示例和输出.

I have a simple prerunmodifier that implements the start_suite function in which it gets the suite setup keyword from the suite variable and prints its attributes. The object is an instance of robot.running.model.Keyword class, here is the doc for that class. The name, keyword type, id and parent attributes are correct but the timeout, doc, tags, children attributes return nothing. It is the same with keywords and messages attributes. Below is my simplified code example and the output.

我希望有以下孩子:Log、Log Many、No Operation.是否有可能在这样的 prerunmodifier 中获取这些关键字的名称(和参数)?我正在使用机器人框架==3.1.2.

I would expect the following children: Log, Log Many, No Operation. Is it possible to get the name (and arguments) of these keywords in a prerunmodifier like this? I am using robotframework==3.1.2.

这是套件文件(test.robot):

This is the suite file (test.robot):

*** Settings ***
Suite Setup    Custom Suite Setup Keyword

*** Test Cases ***
A test
    No Operation
    
*** Keywords ***
Custom Suite Setup Keyword
    [Timeout]    2 min
    [Documentation]    It is a keyword doc.
    [Tags]    1TAG    2TAG
    Log    1st child
    Log Many    2nd    child
    No Operation
    [Teardown]    My Keyword Teardown
    
My Keyword Teardown
    Log     teardown

这是预运行修改器(modifier.py):

This is the prerunmodifier (modifier.py):

from robot.api import SuiteVisitor
from robot.libraries.BuiltIn import BuiltIn

class MyModifier(SuiteVisitor):

    def __init__(self):
        self._BuiltIn = BuiltIn()
        
    
    def start_suite(self, suite):
        self._BuiltIn.log_to_console(f'suite keywords - {suite.keywords}')
        self._BuiltIn.log_to_console(f'class          - {type(suite.keywords.setup)}')
        self._BuiltIn.log_to_console(f'name           - {suite.keywords.setup.name}')
        self._BuiltIn.log_to_console(f'id             - {suite.keywords.setup.id}')
        self._BuiltIn.log_to_console(f'parent(suite)  - {suite.keywords.setup.parent}')
        self._BuiltIn.log_to_console(f'timeout        - {suite.keywords.setup.timeout}')
        self._BuiltIn.log_to_console(f'type           - {suite.keywords.setup.type}')
        self._BuiltIn.log_to_console(f'doc            - {suite.keywords.setup.doc}')
        self._BuiltIn.log_to_console(f'tags           - {suite.keywords.setup.tags}')
        self._BuiltIn.log_to_console(f'children       - {suite.keywords.setup.children}')

这是输出:

prompt# robot --prerunmodifier modifier.MyModifier --pythonpath ./ test.robot
suite keywords - [Custom Suite Setup Keyword]
class          - <class 'robot.running.model.Keyword'>
name           - Custom Suite Setup Keyword
id             - s1-k1
parent(suite)  - Test
timeout        - None
type           - setup
doc            -
tags           - []
children       - []
==============================================================================
Test
==============================================================================
A test                                                                | PASS |
------------------------------------------------------------------------------
Test                                                                  | PASS |
1 critical test, 1 passed, 0 failed
1 test total, 1 passed, 0 failed
==============================================================================

推荐答案

使用 Robot Framework 4.0,如果用作套件设置的关键字在套件本身内实现,也就是套件拥有该关键字,则是可能的.机器人.running.model.TestSuite resource 属性的文档说:

With Robot Framework 4.0 it is possible if the keyword that is used as a suite setup is implemented within the suite itself, aka if the suite owns the keyword. The robot.running.model.TestSuite resource attribute's doc says:

ResourceFile 实例包含套件拥有的导入、变量和关键字.当从文件系统解析数据时,这些数据来自创建套件的同一个测试用例文件.

ResourceFile instance containing imports, variables and keywords the suite owns. When data is parsed from the file system, this data comes from the same test case file that creates the suite.

因此子关键字及其参数可以在 suite.resource.keywords 对象列表中找到.

So the children keywords and their args can be found in suite.resource.keywords object list.

from robot.api import SuiteVisitor

class Visitor(SuiteVisitor):
    
    def start_suite(self, suite):
        for keyword in suite.resource.keywords:
            if suite.setup.name == keyword.name:
                for item in keyword.body:
                    print(f'{item.name} - {item.args} - {item.type}')

                if keyword.teardown.name:
                    print(f'{keyword.teardown.name} - {keyword.teardown.args} - {keyword.teardown.type}')

打印:

Log - ('1st child',) - KEYWORD
Log Many - ('2nd', 'child') - KEYWORD
No Operation - () - KEYWORD
My Keyword Teardown - () - TEARDOWN

同样,如果 user 关键字是在导入的资源文件中实现而不是在套件文件本身中实现,这将不起作用.

Again, this does not work if the user keyword is implemented in an imported resource file and not in the suite file itself.

这篇关于用户关键字的robot.running.model.Keyword 对象children 属性在prerunmodifier start_suite 函数中返回空列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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