11import os
22import yaml
3+ import allure
34
45
56class Var :
67 file_name = None
78 local_variable = None
9+ dynamic_variable = None
10+ file_path = None
811
9- def __init__ (self , file_name ):
12+ def __init__ (self , file_name , type ):
1013 self .file_name = file_name
11- try :
12- root_dir = os .path .dirname (os .path .abspath (__file__ )).replace ("/Library" , "" )
13- with open (root_dir + '/Data/TestData/' + file_name ) as file :
14- self .local_variable = yaml .load (file , Loader = yaml .FullLoader )
15- except Exception as e :
16- print (e )
14+ if type == "local" :
15+ try :
16+ root_dir = os .path .dirname (os .path .abspath (__file__ )).replace ("/Library" , "" )
17+ self .file_path = root_dir + '/Data/TestData/' + file_name
18+ with open (self .file_path ) as file :
19+ self .local_variable = yaml .load (file , Loader = yaml .FullLoader )
20+ allure .attach .file (self .file_path , name = self .file_name , attachment_type = allure .attachment_type .TEXT )
21+ except Exception as e :
22+ print (e )
23+ if type == "dynamic" :
24+ try :
25+ root_dir = os .path .dirname (os .path .abspath (__file__ )).replace ("/Library" , "" )
26+ self .file_path = root_dir + '/Data/DynamicData/' + file_name
27+ if not os .path .isfile (self .file_path ):
28+ if str (self .env ("snap" )) == "1" :
29+ f = open (self .file_path , "w" )
30+ f .close ()
31+ with open (self .file_path ) as file :
32+ self .dynamic_variable = yaml .load (file , Loader = yaml .FullLoader )
33+ allure .attach .file (self .file_path , name = self .file_name , attachment_type = allure .attachment_type .TEXT )
34+ except IOError as e :
35+ print ("File is not accessible\n " + str (e ))
36+ except Exception as e :
37+ print (e )
1738
1839 @staticmethod
1940 def env (string ):
@@ -34,9 +55,44 @@ def glob(string):
3455 print (e )
3556 return "None"
3657
37- def loc (self , string ):
58+ def local_value_for (self , string ) -> str :
3859 try :
3960 return self .local_variable [string ]
4061 except Exception as e :
4162 print (e )
4263 return ""
64+
65+ def write (self , params ):
66+ if self .env ("snap" ) == "1" :
67+ with open (self .file_path , 'w' ) as file :
68+ documents = yaml .dump (params , file )
69+
70+ def dynamic_value_for (self , string ) -> str :
71+ try :
72+ return self .dynamic_variable [string ]
73+ except Exception as e :
74+ print (e )
75+ return ""
76+
77+ def compare (self , displayed_variable ):
78+ if self .env ("snap" ) == "1" :
79+ self .write (displayed_variable )
80+ for key , value in displayed_variable .items ():
81+ try :
82+ file_value = self .dynamic_variable [key ]
83+ except Exception as e :
84+ print (e )
85+ file_value = "key_not_available"
86+ if file_value == "key_not_available" :
87+ with allure .step ("Verifying the key: " + str (key )):
88+ assert (file_value == value ), "Key is not available in the dynamic data file\n Key:- " + key \
89+ + "\n To store the displayed value try running the suite with\n " \
90+ + "snap=1 pytest"
91+ else :
92+ with allure .step ("Verifying the key: " + str (key )):
93+ assert (file_value == value ), "Value for the Key:- " + key + ", Mismatches\n " \
94+ + "File Value:- " + file_value \
95+ + "\n Displayed Value:- " + value \
96+ + "\n File used for validation is:" + self .file_name \
97+ + "\n To change the Dynamic file value run the suite with" \
98+ + "\n snap=1 pytest"
0 commit comments