pyresttest
pyresttest copied to clipboard
Create an extractor extension for json arrays
Hi @svanoort
Im trying to write a custom extractor for json arrays. Essentially I have a list of objects and I would like to extract the UUID given a name for an object
Context.. My http response looks something like this. List of roles..
[
{
"name": "Administrator",
"id": "0b22fcc5-b8bb-4ea9-8282-ef2f2811d6dd",
"created": "2016-06-21T14:50:58.000+0000",
"updated": "2016-06-21T14:50:58.000+0000"
},
{
"name": "Manager",
"id": "79e38b24-1bbd-48ee-95e6-4e5afa636f10",
"created": "2016-06-21T14:50:58.000+0000",
"updated": "2016-06-21T14:50:58.000+0000"
},
{
"name": "Employee",
"id": "bd4b0348-7ddd-431b-b16a-16408c306d3a",
"created": "2016-06-21T14:50:58.000+0000",
"updated": "2016-06-21T14:50:58.000+0000"
}
]
Here is my extractor code in its own file "extractor_custom.py"
import pyresttest.validators as validators
class RoleExtractorFromListWithRoleName(validators.AbstractExtractor):
validators.AbstractExtractor.is_header_extractor = False
## Parse method
@staticmethod
def parse(config):
print "*** In Parse ... ***"
extractor = RoleExtractorFromListWithOrgIdAndRoleName()
extractor.config = config
extractor.roles = config.get('roles')
extractor.roleName = config.get('roleName')
return extractor
## Method to extract role id from a list of roles based on Role Name
@classmethod
def extract_role_id_with_name(self, config):
print "*** In Extraction Fn ... ***"
self = self.parse(config)
if not self.roles:
print "\n*** No Roles to iterate through ***\n"
return None
for role in self.roles:
print "*************"
current_role_name = role.get('name').lower()
if self.roleName.lower() in current_role_name:
print current_role_name
return role.get('id')
print "*************\n"
return None
Ive registered it as an "EXTRACTOR"
EXTRACTORS = {
'extract_role_id' : mashery_extractors.RoleExtractorFromListWithOrgIdAndRoleName.extract_role_id_with_name
}
The problem arises when i try to plug it in a yaml test file. Sample Test case
- test:
- name: "Get List of Roles"
- headers: { template: { "Content-Type": "application/json", "Authorization": "Bearer $access_token" } }
- url: { template: "https://api.$domain/$objectId/roles" }
- method: "GET"
- expected_status: [200]
- extract_binds:
- 'adminRoleId': { 'extract_role_id': { jsonpath_mini: '.', roles: '.', roleName: "Adminstrator" } }
- validators:
- compare: { header: 'content-type', comparator: 'contains', expected: 'json' }
Here's the error I get when i try to run that test
*** In Extraction Fn ... ***
*** In Parse ... ***
config == {'roleName': 'Adminstrator', 'jsonpath_mini': '.', 'roles': '.'}
extractor == .
*************
role ==> .
Traceback (most recent call last):
File "/Users/pshokeen/Library/Python/2.7/bin/pyresttest", line 4, in <module>
resttest.command_line_run(sys.argv[1:])
File "/Users/pshokeen/Library/Python/2.7/lib/python/site-packages/pyresttest/resttest.py", line 913, in command_line_run
main(args)
File "/Users/pshokeen/Library/Python/2.7/lib/python/site-packages/pyresttest/resttest.py", line 826, in main
working_directory=os.path.dirname(test_file), vars=my_vars)
File "/Users/pshokeen/Library/Python/2.7/lib/python/site-packages/pyresttest/resttest.py", line 213, in parse_testsets
base_url, import_test_structure, test_files, vars=vars)
File "/Users/pshokeen/Library/Python/2.7/lib/python/site-packages/pyresttest/resttest.py", line 224, in parse_testsets
mytest = Test.parse_test(base_url, child)
File "/Users/pshokeen/Library/Python/2.7/lib/python/site-packages/pyresttest/tests.py", line 377, in parse_test
extractor_type, extractor_config)
File "/Users/pshokeen/Library/Python/2.7/lib/python/site-packages/pyresttest/validators.py", line 492, in parse_extractor
parsed = parse(config)
File "/Users/pshokeen/code/acceptance-testing/mashery_extractors.py", line 39, in extract_role_id_with_name
current_role_name = role.get('name').lower()
AttributeError: 'str' object has no attribute 'get'