@@ -86,23 +86,25 @@ def add_object_to_json(self, json_object, json_path, object_to_add):
8686 return json_object
8787
8888 @keyword ("Get Value From Json" )
89- def get_value_from_json (self , json_object , json_path ):
89+ def get_value_from_json (self , json_object , json_path , fail_on_empty = False ):
9090 """Get Value From JSON using JSONPath
9191
9292 Arguments:
9393 - json_object: json as a dictionary object.
9494 - json_path: jsonpath expression
95+ - fail_on_empty: fail the testcases if nothing is returned
9596
9697 Return array of values
9798
9899 Examples:
99100 | ${values}= | Get Value From Json | ${json} | $..phone_number |
101+ | ${values}= | Get Value From Json | ${json} | $..missing | fail_on_empty=${True} |
100102 """
101103 json_path_expr = parse (json_path )
102104 rv = json_path_expr .find (json_object )
103- # make the keyword fails if nothing was return
104- assert_true (rv is not None and len (rv )!= 0 ,
105- f"Get Value From Json keyword failed to find a value for { json_path } " )
105+ # optional: make the keyword fails if nothing was return
106+ if fail_on_empty is True and (rv is None or len (rv )== 0 ):
107+ fail ( f"Get Value From Json keyword failed to find a value for { json_path } " )
106108 return [match .value for match in rv ]
107109
108110 @keyword ("Update Value To Json" )
@@ -210,7 +212,7 @@ def should_have_value_in_json(self, json_object, json_path):
210212 | Should Have Value In Json | ${json} | $..id_card_number |
211213 """
212214 try :
213- self .get_value_from_json (json_object , json_path )
215+ self .get_value_from_json (json_object , json_path , fail_on_empty = True )
214216 except AssertionError :
215217 fail (f"No value found for path { json_path } " )
216218
@@ -229,7 +231,7 @@ def should_not_have_value_in_json(self, json_object, json_path):
229231 | Should Not Have Value In Json | ${json} | $..id_card_number |
230232 """
231233 try :
232- rv = self .get_value_from_json (json_object , json_path )
234+ rv = self .get_value_from_json (json_object , json_path , fail_on_empty = True )
233235 except AssertionError :
234236 pass
235237 else :
0 commit comments