Python : Data Driven Automation Framework using JSON Loader
import json
class json_py():
def __init__(self,jsonFilePath,**kwargs):
self.d= self.__readJson(jsonFilePath)
def get_values(self,testCaseName):
result_dict=self.d[testCaseName][0]
return(result_dict)
def __readJson(self,sPath):
try:
with open(sPath,"r") as F:
return(json.loads(F.read()))
except FileNotFoundError:
print("Please provide correct file path")
except ValueError:
print("JSON is invalid")
sPath='D:\\personal\\json_python_2.json'
j=json_py(sPath)
print(j.get_values("testcase3")["data2"])
OutPut:
['hi', 'deepak']
---------------------------------------------------------------------------------------
json_python_2.json
{
"testcase1":[{ "id":1, "data1":["hi","deepak"]}],
"testcase2":[{ "id":2, "data1":["data"]}],
"testcase3":[{ "id":3,
"data1":["hi","deepak"],
"data2":["hi","deepak"],
"data3":[{"key":"value"}]
}]
}
No comments:
Post a Comment