Reputation: 20080
Robot Framework supports two ways to interact with the tests and modify the test structure:
I would like to prepend, to specific tests, a given keyword, or even more precisely to wrap the test in a given keyword, if that test has a specific tag. Which of the two approaches should I choose, and how I should use the API of the Body of Robot Framework to wrap it in a Wait Until Keyword Succeeds
?
Upvotes: 2
Views: 1746
Reputation: 7271
With a prerunmodifier you can modify the suite before execution, with a listener you can modify the suite during execution. As you have all the information available before execution it is suggested to use a prerunmodifier and do every modifications beforehand.
What needs to be done is creating a user keyword with the same body as the test case that needs to be wrapped. These are the steps:
test.tags
for your specific tags.robot.running.model.UserKeyword
object for the test case.test.body
to ukw.body
effectively replicating the test case as a keyword.suite.resource.keywords
). Make sure the keyword name is unique for every test case. (This entire step is that was the missing link in the previous version of the answer.)test.body
.Wait Until Keyword Succeeds
that would call the newly made user keyword to test.body
.This will handle IF/ELSE IF/ELSE
and FOR
blocks or their combinations in nested structures.
start_suite
visitor function should be implemented because access is needed to the suite.resource.keywords
object.
Visitor.py:
from robot.api import SuiteVisitor
from robot.running.model import UserKeyword
class Visitor(SuiteVisitor):
def start_suite(self, suite):
for test in suite.tests:
if 'WRAP' in test.tags: # step 1.
keyword_name = f'Wrapper Keyword For Test:{test.name}'
ukw = UserKeyword(name=keyword_name) # step 2.
ukw.body = test.body # step 3.
suite.resource.keywords.append(ukw) # step 4.
test.body.clear() # step 5.
test.body.create_keyword( # step 6.
name='Wait Until Keyword Succeeds',
args=['10 sec', '2 sec', keyword_name])
Example test and results:
*** Test Cases ***
Test A
[Tags] WRAP
KW
Log step 1
Log Many step 2 step 3
IF True
Log true
END
FOR ${i} IN RANGE 10
FOR ${j} IN RANGE 10
Log Many ${i} ${j}
END
END
Test B
[Tags] SMOKE
Log 1
Log 1
*** Keywords ***
KW
Log 1
Log 2
Upvotes: 2
Reputation: 19
I think the Listener API might be the way to go but why not use the Test and Suite Setup Keywords to be able to pull off what you are trying to do. However, in general, i.e. not just in Python or Robot Framework, it is a bad idea to have one test depend on the execution of another test. Ideally, each test should be able to run on its own, whether first or last or anywhere in the sequence so perhaps there might be a need to refactor your tests and/or Keywords to be able to achieve what you are attempting.
Upvotes: 1