1
+ import json
2
+ import types
1
3
import allure
2
4
import requests
3
5
import yaml
@@ -29,7 +31,7 @@ def verify_response_code(status_code):
29
31
result = Store .current_response .status_code == status_code
30
32
assert (result is True ), "Response status code is not matched, \n " \
31
33
"expected: " + status_code + "\n " \
32
- "actual: " + Store .current_response .status_code
34
+ "actual: " + Store .current_response .status_code
33
35
34
36
@staticmethod
35
37
def create_file_if_not_present (file_path ):
@@ -58,6 +60,7 @@ def verify_response_json(file_name, key_name):
58
60
allure .attach (str (Store .current_response .json ()), name = "Response JSON" )
59
61
try :
60
62
expected_json = yaml_load [key_name ]
63
+ allure .attach (str (expected_json ), name = "Expected JSON" )
61
64
if Var .env ("snap" ) == "1" :
62
65
yaml_load [key_name ] = Store .current_response .json ()
63
66
Api .dump_in_dynamic_variable_file (file_path , yaml_load )
@@ -66,7 +69,47 @@ def verify_response_json(file_name, key_name):
66
69
if Var .env ("snap" ) == "1" :
67
70
yaml_load [key_name ] = Store .current_response .json ()
68
71
Api .dump_in_dynamic_variable_file (file_path , yaml_load )
69
- assert (expected_json == Store .current_response .json ()), "Expected Json doesn't match with stored json" \
70
- "file \n Expected: " + str (expected_json ) + "\n " \
71
- "Actual response: " \
72
- "" + str (Store .current_response .json ())
72
+ assert (Api .json_compare (expected_json , Store .current_response .json ())), \
73
+ "Response doesn't match with stored json \n Expected: " + str (expected_json ) + \
74
+ "\n Actual response: " + str (Store .current_response .json ())
75
+
76
+ @staticmethod
77
+ def ignore_keys (keys ):
78
+ Store .ignore_keys = keys .split ("," )
79
+
80
+ @staticmethod
81
+ def json_compare (json1 , json2 ):
82
+ ignore_keys = Store .ignore_keys
83
+ allure .attach (str (ignore_keys ), name = "Keys Ignored while comparing" )
84
+ d1_filtered = dict ((k , v ) for k , v in json1 .items () if k not in ignore_keys )
85
+ d2_filtered = dict ((k , v ) for k , v in json2 .items () if k not in ignore_keys )
86
+ for k , v in d1_filtered .items ():
87
+ if v == "$notnull" :
88
+ assert (d2_filtered [k ] != "Null" ), "Key value " + k + " is null in response"
89
+ d1_filtered [k ] = d2_filtered [k ]
90
+ elif v == "$null" :
91
+ assert (d2_filtered [k ] == "Null" ), "Key value " + k + " is not null in response"
92
+ d1_filtered [k ] = d2_filtered [k ]
93
+ elif v == "$array" :
94
+ assert (type (d2_filtered [k ]) in (tuple , list ) is True ), "Key " + k + " is not in array format"
95
+ d1_filtered [k ] = d2_filtered [k ]
96
+ elif v == "$json" :
97
+ try :
98
+ json .loads (d2_filtered [k ])
99
+ result = True
100
+ except ValueError as e :
101
+ result = False
102
+ assert (result is True ), "Key " + k + " is not in json format"
103
+ d1_filtered [k ] = d2_filtered [k ]
104
+ elif v == "$boolean" :
105
+ result = type (d2_filtered [k ]) is bool
106
+ assert (result is True ), "Key " + k + " is not in boolean format"
107
+ d1_filtered [k ] = d2_filtered [k ]
108
+ elif v == "$number" :
109
+ result = isinstance (d2_filtered [k ], (int , float , complex )) and not isinstance (d2_filtered [k ], bool )
110
+ assert (result is True ), "Key " + k + " is not in number format"
111
+ d1_filtered [k ] = d2_filtered [k ]
112
+ elif v == "$string" :
113
+ result = isinstance (d2_filtered [k ], str )
114
+ assert (result is True ), "Key " + k + " is not in string format"
115
+ return d1_filtered == d2_filtered
0 commit comments