Robot Framework - 访客界面 - 如何获取关键字的关键字子项?

Posted

技术标签:

【中文标题】Robot Framework - 访客界面 - 如何获取关键字的关键字子项?【英文标题】:Robot Framework - Visitor Interface - How do I get keyword children of keywords? 【发布时间】:2019-05-02 09:54:39 【问题描述】:

在实现 Robot 的 SuiteVisitor Interface 后,函数 def start_suite(self, suite)def start_test(self, test)def start_keyword(self, keyword) 将按预期调用。但是当我尝试列出关键字的关键字子项时,我得到空列表:

def start_suite(self, suite):
    logger.console("Event Start Suite: ".format(suite.name))
    for x in suite.tests:
        logger.console("-> Test Name: ".format(x))
        for y in x.keywords:
            logger.console("---> Keyword: ".format(y))
            for z in y.keywords:
                logger.console("-----> Child Keyword: ".format(z))

def start_test(self, test):
    logger.console("Event Start Test ".format(test.name))
    for x in test.keywords:
        logger.console("---> Keyword: ".format(x))
        for z in x.keywords:
            logger.console("-----> Child Keyword: ".format(z))    

def start_keyword(self, keyword):
    logger.console("Event Start Keyword ".format(keyword.name))
    for x in keyword.keywords:
        logger.console("-----> Child Keyword: ".format(x))

谁能解释一下原因?

我通过监听器注册访问者:

def start_suite(self, data, result):
    logger.console("Listener Start Suite")
    visitor = Visitor()
    data.visit(visitor)

我是否需要更多信息才能回答这个问题?

【问题讨论】:

你能创建完整的例子吗?目前缺少一些代码(可能是出于简洁的原因),但这使得重新创建您的情况更加耗时。为访问者、侦听器和用于启动的命令行参数提供完整的类对我来说很关键。一个简短的机器人示例也将不胜感激。 【参考方案1】:

当我运行以下设置时,自定义套件访问者会记录测试用例中定义的关键字。主要区别在于以下示例将类添加为 prerunmodifier:--prerunmodifier SoVisitor.SoVisitor

SoVisitor.robot

*** Test Cases ***
TC 1
    Custom Keyword
    Another Keyword

TC 2
    Custom Keyword
    Another Keyword


*** Keywords ***
Custom Keyword
    Sub Keyword
    No Operation

Another Keyword
    No Operation 

Sub Keyword
    No Operation 
    enter code here

SoVisitor.py

from robot.model.visitor import SuiteVisitor
from robot.api import logger

class SoVisitor(SuiteVisitor):
    def start_suite(self, suite):
        logger.console("Event Start Suite: ".format(suite.name))
        for x in suite.tests:
            logger.console("-> Test Name: ".format(x))
            for y in x.keywords:
                logger.console("---> Keyword: ".format(y))
                for z in y.keywords:
                    logger.console("-----> Child Keyword: ".format(z))

    def start_test(self, test):
        logger.console("Event Start Test ".format(test.name))
        for x in test.keywords:
            logger.console("---> Keyword: ".format(x))
            for z in x.keywords:
                logger.console("-----> Child Keyword: ".format(z))    

    def start_keyword(self, keyword):
        logger.console("Event Start Keyword ".format(keyword.name))
        for x in keyword.keywords:
            logger.console("-----> Child Keyword: ".format(x))    

RED 编辑器然后生成以下命令和控制台输出:

Command: C:\Users\anne\AppData\Local\Programs\Python\Python27\python.exe -m robot.run --listener C:\Users\anne\AppData\Local\Temp\RobotTempDir4913575656218095666\TestRunnerAgent.py:55023 -s RobotVisitor.SoVistor --prerunmodifier SoVisitor.SoVisitor C:\Users\anne\eclipse-workspace\RobotVisitor
Suite Executor: Robot Framework 3.0.4 (Python 2.7.15 on win32)
Event Start Suite: RobotVisitor
Event Start Suite: SoVistor
-> Test Name: TC 1
---> Keyword: Custom Keyword
---> Keyword: Another Keyword
-> Test Name: TC 2
---> Keyword: Custom Keyword
---> Keyword: Another Keyword
Event Start Test TC 1
---> Keyword: Custom Keyword
---> Keyword: Another Keyword
Event Start Keyword Custom Keyword
Event Start Keyword Another Keyword
Event Start Test TC 2
---> Keyword: Custom Keyword
---> Keyword: Another Keyword
Event Start Keyword Custom Keyword
Event Start Keyword Another Keyword
==============================================================================
RobotVisitor                                                                  
==============================================================================
RobotVisitor.SoVistor                                                         
==============================================================================
TC 1                                                                  | PASS |
------------------------------------------------------------------------------
TC 2                                                                  | PASS |
------------------------------------------------------------------------------
RobotVisitor.SoVistor                                                 | PASS |
2 critical tests, 2 passed, 0 failed
2 tests total, 2 passed, 0 failed
==============================================================================
RobotVisitor                                                          | PASS |
2 critical tests, 2 passed, 0 failed
2 tests total, 2 passed, 0 failed
==============================================================================
Output:  C:\Users\anne\eclipse-workspace\RobotVisitor\output.xml
Log:     C:\Users\anne\eclipse-workspace\RobotVisitor\log.html
Report:  C:\Users\anne\eclipse-workspace\RobotVisitor\report.html

【讨论】:

这是完全相同的行为,就像我的情况一样。 ""-----> Child Keyword: Sub Keyword" 从未打印过,尽管我希望也能访问子关键字列表。我期待错了吗?

以上是关于Robot Framework - 访客界面 - 如何获取关键字的关键字子项?的主要内容,如果未能解决你的问题,请参考以下文章

Robot Framework使用Phantomjs进行无界面UI自动化测试

Robot Framework与Web界面自动化测试学习笔记:如何判断单选框的选中状态

Robot Framework自动化测试第一个用例

Robot Framework应用——Mac环境下Robot Framework的安装及简单实用

Robot Framework应用——Mac环境下Robot Framework的安装及简单实用

Robot Framework - 从带有参数的其他 *.robot 脚本调用 *.robot 脚本