You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
AttributeError: 'keywords' attribute is read-only and deprecated since Robot Framework 4.0. Use 'body', 'setup' or 'teardown' instead. #3868
I have been dynamically creating the test cases using the API. Below are the details for your reference.
During robot framework upgrade testing I am getting the following error.
AttributeError: 'keywords' attribute is read-only and deprecated since Robot Framework 4.0. Use 'body', 'setup' or 'teardown' instead.
Please help out to understand better on the changes in 4.0
Method Used:
class DynamicTestCases(object):
ROBOT_LISTENER_API_VERSION = 3
ROBOT_LIBRARY_SCOPE = 'TEST SUITE'
def __init__(self):
self.ROBOT_LIBRARY_LISTENER = self
def _start_suite(self, suite, result):
# save current suite so that we can modify it later
self.current_suite = suite
def add_test_case(self, name, doc, tags, kwname, *args):
"""Adds a test case to the current suite
'name' is the test case name
'kwname' is the keyword to call
'*args' are the arguments to pass to the keyword
Example:
add_test_case Example Test Case
... log hello, world WARN
"""
tc = self.current_suite.tests.create(name=name, doc=doc, tags=tags)
tc.keywords.create(name=kwname, args=args)
TC:
Create and execute test cases dynamically using test data sheet
[Arguments] ${test_data_full_path}
Set Log Level DEBUG
Log ${test_data_full_path}
Log Get the list of test cases to be executed
${test_cases_list}= API Test Cases List ${test_data_full_path}
${no_of_test_cases}= Get Length ${test_cases_list}
Log Iterate through each test case and execute the test case
FOR ${i} IN RANGE ${no_of_test_cases}
${test_name}= Get From Dictionary ${test_cases_list}[${i}] test_name
${tags}= Get From Dictionary ${test_cases_list}[${i}] test_tags
@{test_tags}= Split String ${tags} ,
${doc}= Get From Dictionary ${test_cases_list}[${i}] test_documentation
Add Test Case ${test_name} ${doc} ${test_tags} Generate and compare expected/actual results ${test_name}
END
Based on further research in case of creating the keywords in 4.0 I will have to use the below code.. Please review and confirm
tc = self.current_suite.tests.create(name=name, doc=doc, tags=tags)
tc.body.create_keyword(name=kwname, args=args)
You are right, keywords has been renamed to body as part of #3749. It's possible to still access keywords, but modifying it isn't supported at all. Because body can contain other objects than keywords, you also need to use body.create_keyword instead of body.create. setup and teardown are also nowadays directly in the suite/test/keyword object when you earlier needed to use obj.keywords.setup and obj.keywords.teardown, but this doesn't seem to affect the code above.
As you commented, it ought to be enough to simply change tc.keywords.create to tc.body.create_keyword in this case. Did that work? If you want to support also older RF versions, you can use, for example,
Thank you so much and after changing to tc.body.create_keyword .. everything works fine. The code which you share for backward compatibility is very useful, I have in corporate them in the script.
Activity
sridharvpmca commentedon Feb 27, 2021
Based on further research in case of creating the keywords in 4.0 I will have to use the below code.. Please review and confirm
tc = self.current_suite.tests.create(name=name, doc=doc, tags=tags)
tc.body.create_keyword(name=kwname, args=args)
pekkaklarck commentedon Mar 2, 2021
You are right,
keywordshas been renamed tobodyas part of #3749. It's possible to still accesskeywords, but modifying it isn't supported at all. Becausebodycan contain other objects than keywords, you also need to usebody.create_keywordinstead ofbody.create.setupandteardownare also nowadays directly in the suite/test/keyword object when you earlier needed to useobj.keywords.setupandobj.keywords.teardown, but this doesn't seem to affect the code above.As you commented, it ought to be enough to simply change
tc.keywords.createtotc.body.create_keywordin this case. Did that work? If you want to support also older RF versions, you can use, for example,sridharvpmca commentedon Mar 3, 2021
Thank you so much and after changing to tc.body.create_keyword .. everything works fine. The code which you share for backward compatibility is very useful, I have in corporate them in the script.
Test-Hassan commentedon Sep 7, 2021
Can I get the list of Keywords which are deprecated in Robot Framework V 4.0 please?