How to use extract_json_path_value method in gabbi

Best Python code snippet using gabbi_python

jsonhandler.py

Source:jsonhandler.py Github

copy

Full Screen

...39 content_type == 'application/json'40 and 'stream=' not in parameters)41 @classmethod42 def replacer(cls, response_data, match):43 return cls.extract_json_path_value(response_data, match)44 @staticmethod45 def dumps(data, pretty=False, test=None):46 if pretty:47 return json.dumps(data, indent=2, separators=(',', ': '))48 else:49 return json.dumps(data)50 @staticmethod51 def loads(data):52 try:53 return json.loads(data)54 except ValueError as exc:55 raise GabbiDataLoadError('unable to parse data') from exc56 @staticmethod57 def load_data_file(test, file_path):58 info = test.load_data_file(file_path)59 info = six.text_type(info, 'UTF-8')60 return json.loads(info)61 @staticmethod62 def extract_json_path_value(data, path):63 """Extract the value at JSON Path path from the data.64 The input data is a Python datastructure, not a JSON string.65 """66 path_expr = json_parser.parse(path)67 matches = [match.value for match in path_expr.find(data)]68 if matches:69 if len(matches) > 1:70 return matches71 else:72 return matches[0]73 else:74 raise ValueError(75 "JSONPath '%s' failed to match on data: '%s'" % (path, data))76 def action(self, test, path, value=None):77 """Test json_paths against json data."""78 # Do template expansion in the left hand side.79 lhs_path = test.replace_template(path)80 rhs_path = rhs_match = None81 try:82 lhs_match = self.extract_json_path_value(83 test.response_data, lhs_path)84 except AttributeError:85 raise AssertionError('unable to extract JSON from test results')86 except ValueError:87 raise AssertionError('left hand side json path %s cannot match '88 '%s' % (path, test.response_data))89 # read data from disk if the value starts with '<@'90 if isinstance(value, six.string_types) and value.startswith('<@'):91 # Do template expansion in the rhs if rhs_path is provided.92 if ':' in value:93 value, rhs_path = value.split(':$', 1)94 rhs_path = test.replace_template('$' + rhs_path)95 value = self.load_data_file(test, value.replace('<@', '', 1))96 if rhs_path:97 try:98 rhs_match = self.extract_json_path_value(value, rhs_path)99 except AttributeError:100 raise AssertionError('unable to extract JSON from data on '101 'disk')102 except ValueError:103 raise AssertionError('right hand side json path %s cannot '104 'match %s' % (rhs_path, value))105 # If expected is a string, check to see if it is a regex.106 is_regex = (isinstance(value, six.string_types) and107 value.startswith('/') and108 value.endswith('/') and109 len(value) > 1)110 expected = (rhs_match or111 test.replace_template(value, escape_regex=is_regex))112 match = lhs_match...

Full Screen

Full Screen

handlers.py

Source:handlers.py Github

copy

Full Screen

...24 """25 test_key_suffix = 'store_environ'26 test_key_value = {}27 def action(self, test, key, value=None):28 data = test.extract_json_path_value(test.json_data, value)...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run gabbi automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful