mirror of
https://github.com/httprunner/httprunner.git
synced 2026-06-10 02:09:51 +08:00
Merge pull request #1113 from lihuacai168/master
feat: support variable and function in response extract expression
This commit is contained in:
@@ -167,12 +167,21 @@ class ResponseObject(object):
|
|||||||
|
|
||||||
return check_value
|
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:
|
if not extractors:
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
extract_mapping = {}
|
extract_mapping = {}
|
||||||
for key, field in extractors.items():
|
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)
|
field_value = self._search_jmespath(field)
|
||||||
extract_mapping[key] = field_value
|
extract_mapping[key] = field_value
|
||||||
|
|
||||||
|
|||||||
@@ -195,7 +195,7 @@ class HttpRunner(object):
|
|||||||
|
|
||||||
# extract
|
# extract
|
||||||
extractors = step.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
|
step_data.export_vars = extract_mapping
|
||||||
|
|
||||||
variables_mapping = step.variables
|
variables_mapping = step.variables
|
||||||
|
|||||||
@@ -21,11 +21,26 @@ class TestResponse(unittest.TestCase):
|
|||||||
self.resp_obj = ResponseObject(resp)
|
self.resp_obj = ResponseObject(resp)
|
||||||
|
|
||||||
def test_extract(self):
|
def test_extract(self):
|
||||||
|
variables_mapping = {
|
||||||
|
'body': 'body'
|
||||||
|
}
|
||||||
|
functions_mapping = {
|
||||||
|
'get_name': lambda: 'name',
|
||||||
|
}
|
||||||
extract_mapping = self.resp_obj.extract(
|
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_1"], {"name": "Seattle", "state": "WA"})
|
||||||
self.assertEqual(extract_mapping["var_2"], "Olympia")
|
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):
|
def test_validate(self):
|
||||||
self.resp_obj.validate(
|
self.resp_obj.validate(
|
||||||
|
|||||||
Reference in New Issue
Block a user