Skip to content

Commit f0cb50e

Browse files
author
Laurent Pinson
committed
add 'Should (not) Have Value in JSON' keywords
1 parent 8e10faa commit f0cb50e

File tree

4 files changed

+84
-2
lines changed

4 files changed

+84
-2
lines changed

JSONLibrary/JSONLibraryKeywords.py

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ def convert_json_to_string(self, json_object):
155155
"""
156156
return json.dumps(json_object)
157157

158-
@keyword('Convert String to JSON')
158+
@keyword('Convert String To JSON')
159159
def convert_string_to_json(self, json_string):
160160
"""Convert String to JSON object
161161
@@ -169,10 +169,60 @@ def convert_string_to_json(self, json_string):
169169
"""
170170
return json.loads(json_string)
171171

172-
@keyword('Dump JSON to file')
172+
@keyword('Dump JSON To File')
173173
def dump_json_to_file(self, dest_file, json_object):
174+
"""Dump JSON to file
175+
176+
Arguments:
177+
- dest_file: destination file
178+
- json_object: json as a dictionary object.
179+
180+
Export the JSON object to a file
181+
182+
Examples:
183+
| Dump JSON To File | ${OUTPUTID)${/}output.json | ${json} |
184+
"""
174185
json_str = self.convert_json_to_string(json_object)
175186
with open(dest_file, "w") as json_file:
176187
json_file.write(json_str)
177188
return str(dest_file)
178189

190+
@keyword('Should Have Value In Json')
191+
def should_have_value_in_json(self, json_object, json_path):
192+
"""Should Have Value In JSON using JSONPath
193+
194+
Arguments:
195+
- json_object: json as a dictionary object.
196+
- json_path: jsonpath expression
197+
198+
Fail if no value is found
199+
200+
Examples:
201+
| Should Have Value In Json | ${json} | $..id_card_number |
202+
"""
203+
try:
204+
self.get_value_from_json(json_object, json_path)
205+
except AssertionError:
206+
fail(f"No value found for path {json_path}")
207+
208+
209+
@keyword('Should Not Have Value In Json')
210+
def should_not_have_value_in_json(self, json_object, json_path):
211+
"""Should Not Have Value In JSON using JSONPath
212+
213+
Arguments:
214+
- json_object: json as a dictionary object.
215+
- json_path: jsonpath expression
216+
217+
Fail if at least one value is found
218+
219+
Examples:
220+
| Should Not Have Value In Json | ${json} | $..id_card_number |
221+
"""
222+
try:
223+
rv=self.get_value_from_json(json_object, json_path)
224+
except AssertionError:
225+
pass
226+
else:
227+
fail(f"Match found for parent {json_path}: {rv}")
228+

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ ${value}= | Get Value From Json | ${json_obj} | $..country |
3737
|${value_to_update}=| Set Variable | Japan | | |
3838
|${json_obj}= | Update Value To Json | ${json_obj} | $..country | ${value_to_update}|
3939
|Should Be Equal As Strings | ${json_obj['country'] | ${value_to_update} | | |
40+
|Should Have Value In Json | ${json_obj} | $..isMarried |
41+
|Should Not Have Value In Json | ${json_obj} | $..hasSiblings |
42+
|Dump JSON To File | ${OUTPUTID)${/}output.json | ${json} |
43+
4044

4145
# Documentation
4246
For the detail keyword documentation. Go to this following link:

acceptance/JSONLibrary.robot

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,18 @@ TestUpdateValueByJSONPath
3636
${updated_city}= Get Value From Json ${json_obj} $..address.city
3737
Should Be Equal As Strings ${updated_city[0]} Bangkok
3838

39+
TestShouldHaveValueByJSONPath
40+
[Documentation] Check a value can be found in json object using JSONPath
41+
Should Have Value In Json ${json_obj} $..isMarried
42+
43+
Run Keyword And Expect Error *No value found* Should Have Value In Json ${json_obj} $..hasSiblings
44+
45+
TestShouldNotHaveValueByJSONPath
46+
[Documentation] Check a value cannot be found in json object using JSONPath
47+
Should Not Have Value In Json ${json_obj} $..hasSiblings
48+
49+
Run Keyword And Expect Error *Match found* Should Not Have Value In Json ${json_obj} $..isMarried
50+
3951
TestDeleteObjectByJSONPath
4052
[Documentation] Delete object from json object using JSONPath
4153
${json_obj}= Delete Object From Json ${json_obj} $..isMarried

tests/test_JSONLibrary.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,22 @@ def test_get_value_from_json_path_not_found(self):
5555
json_path = '$..notfound'
5656
self.assertRaises(AssertionError, self.test.get_value_from_json, self.json, json_path)
5757

58+
def test_has_value_from_json_path_passed(self):
59+
json_path = '$..isMarried'
60+
self.test.should_have_value_in_json(self.json, json_path)
61+
62+
def test_has_value_from_json_path_failed(self):
63+
json_path = '$..hasSiblings'
64+
self.assertRaises(AssertionError, self.test.should_have_value_in_json, self.json, json_path)
65+
66+
def test_has_no_value_from_json_path_passed(self):
67+
json_path = '$..hasSiblings'
68+
self.test.should_not_have_value_in_json(self.json, json_path)
69+
70+
def test_has_no_value_from_json_path_failed(self):
71+
json_path = '$..isMarried'
72+
self.assertRaises(AssertionError, self.test.should_not_have_value_in_json, self.json, json_path)
73+
5874
def test_update_value_to_json(self):
5975
json_path = '$..address.streetAddress'
6076
value_to_update = 'Ratchadapisek Road'

0 commit comments

Comments
 (0)