mirror of
https://github.com/httprunner/httprunner.git
synced 2026-05-07 05:32:43 +08:00
feat: response extract expresion support variable and function
This commit is contained in:
@@ -167,12 +167,21 @@ class ResponseObject(object):
|
||||
|
||||
return check_value
|
||||
|
||||
def extract(self, extractors: Dict[Text, Text]) -> Dict[Text, Any]:
|
||||
def extract(self,
|
||||
extractors: Dict[Text, Text],
|
||||
variables_mapping: VariablesMapping = None,
|
||||
functions_mapping: FunctionsMapping = None,
|
||||
) -> Dict[Text, Any]:
|
||||
if not extractors:
|
||||
return {}
|
||||
|
||||
extract_mapping = {}
|
||||
for key, field in extractors.items():
|
||||
if '$' in field:
|
||||
# field contains variable or function
|
||||
field = parse_data(
|
||||
field, variables_mapping, functions_mapping
|
||||
)
|
||||
field_value = self._search_jmespath(field)
|
||||
extract_mapping[key] = field_value
|
||||
|
||||
|
||||
@@ -195,7 +195,7 @@ class HttpRunner(object):
|
||||
|
||||
# extract
|
||||
extractors = step.extract
|
||||
extract_mapping = resp_obj.extract(extractors)
|
||||
extract_mapping = resp_obj.extract(extractors, step.variables, self.__project_meta.functions)
|
||||
step_data.export_vars = extract_mapping
|
||||
|
||||
variables_mapping = step.variables
|
||||
|
||||
@@ -21,11 +21,26 @@ class TestResponse(unittest.TestCase):
|
||||
self.resp_obj = ResponseObject(resp)
|
||||
|
||||
def test_extract(self):
|
||||
variables_mapping = {
|
||||
'body': 'body'
|
||||
}
|
||||
functions_mapping = {
|
||||
'get_name': lambda: 'name',
|
||||
}
|
||||
extract_mapping = self.resp_obj.extract(
|
||||
{"var_1": "body.json.locations[0]", "var_2": "body.json.locations[3].name"}
|
||||
{
|
||||
"var_1": "body.json.locations[0]",
|
||||
"var_2": "body.json.locations[3].name",
|
||||
"var_3": "$body.json.locations[3].name",
|
||||
"var_4": "$body.json.locations[3].${get_name()}",
|
||||
},
|
||||
variables_mapping=variables_mapping,
|
||||
functions_mapping=functions_mapping,
|
||||
)
|
||||
self.assertEqual(extract_mapping["var_1"], {"name": "Seattle", "state": "WA"})
|
||||
self.assertEqual(extract_mapping["var_2"], "Olympia")
|
||||
self.assertEqual(extract_mapping["var_3"], "Olympia")
|
||||
self.assertEqual(extract_mapping["var_4"], "Olympia")
|
||||
|
||||
def test_validate(self):
|
||||
self.resp_obj.validate(
|
||||
|
||||
Reference in New Issue
Block a user