问题
After implementing Robot's SuiteVisitor Interface the functions def start_suite(self, suite)
, def start_test(self, test)
and def start_keyword(self, keyword)
are being called as expected. But when I try to list the keyword children of keywords, I get empty lists:
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))
Can someone explain why?
I register the visitor via the listener:
def start_suite(self, data, result):
logger.console("Listener Start Suite")
visitor = Visitor()
data.visit(visitor)
Is there more information needed from my side to be able to answer this question?
回答1:
When I run the following setup the custom Suite Visitor logs the keywords defined in the test case. The main difference is that the below example adds the class as a 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))
The RED editor then generates the following command and console output:
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
来源:https://stackoverflow.com/questions/53554210/robot-framework-visitor-interface-how-do-i-get-keyword-children-of-keywords