|
| 1 | +import os |
| 2 | +import yaml |
| 3 | +import allure |
| 4 | +from Library.store import Store |
| 5 | + |
| 6 | + |
| 7 | +class Var: |
| 8 | + file_name = None |
| 9 | + static_variable = None |
| 10 | + dynamic_variable = None |
| 11 | + file_path = None |
| 12 | + static_data_path = Store.static_data_path |
| 13 | + dynamic_data_path = Store.dynamic_data_path |
| 14 | + global_data_path = Store.global_data_path |
| 15 | + |
| 16 | + def __init__(self, file_name, type): |
| 17 | + self.file_name = file_name |
| 18 | + if type == "static": |
| 19 | + try: |
| 20 | + self.file_path = self.static_data_path + '/' + file_name |
| 21 | + with open(self.file_path) as file: |
| 22 | + self.static_variable = yaml.load(file, Loader=yaml.FullLoader) |
| 23 | + allure.attach.file(self.file_path, name=self.file_name, attachment_type=allure.attachment_type.TEXT) |
| 24 | + except Exception as e: |
| 25 | + print(e) |
| 26 | + if type == "dynamic": |
| 27 | + try: |
| 28 | + self.file_path = self.dynamic_data_path + '/' + file_name |
| 29 | + if not os.path.isfile(self.file_path): |
| 30 | + if str(self.env("snap")) == "1": |
| 31 | + f = open(self.file_path, "w") |
| 32 | + f.close() |
| 33 | + with open(self.file_path) as file: |
| 34 | + self.dynamic_variable = yaml.load(file, Loader=yaml.FullLoader) |
| 35 | + allure.attach.file(self.file_path, name=self.file_name, attachment_type=allure.attachment_type.TEXT) |
| 36 | + except IOError as e: |
| 37 | + print("File is not accessible\n" + str(e)) |
| 38 | + except Exception as e: |
| 39 | + print(e) |
| 40 | + |
| 41 | + @staticmethod |
| 42 | + def env(string): |
| 43 | + try: |
| 44 | + return os.environ[string] |
| 45 | + except Exception as e: |
| 46 | + print(e) |
| 47 | + return "None" |
| 48 | + |
| 49 | + @staticmethod |
| 50 | + def glob(string): |
| 51 | + try: |
| 52 | + with open(Var.global_data_path) as file: |
| 53 | + global_data = yaml.load(file, Loader=yaml.FullLoader) |
| 54 | + return global_data[string] |
| 55 | + except Exception as e: |
| 56 | + print(e) |
| 57 | + return "None" |
| 58 | + |
| 59 | + def static_value_for(self, string) -> str: |
| 60 | + try: |
| 61 | + return self.static_variable[string] |
| 62 | + except Exception as e: |
| 63 | + print(e) |
| 64 | + return "" |
| 65 | + |
| 66 | + def write(self, params): |
| 67 | + if self.env("snap") == "1": |
| 68 | + with open(self.file_path, 'w') as file: |
| 69 | + documents = yaml.dump(params, file) |
| 70 | + print(documents) |
| 71 | + |
| 72 | + def dynamic_value_for(self, string) -> str: |
| 73 | + try: |
| 74 | + return self.dynamic_variable[string] |
| 75 | + except Exception as e: |
| 76 | + print(e) |
| 77 | + return "" |
| 78 | + |
| 79 | + def compare(self, displayed_variable): |
| 80 | + if self.env("snap") == "1": |
| 81 | + self.write(displayed_variable) |
| 82 | + for key, value in displayed_variable.items(): |
| 83 | + try: |
| 84 | + file_value = self.dynamic_variable[key] |
| 85 | + except Exception as e: |
| 86 | + print(e) |
| 87 | + file_value = "key_not_available" |
| 88 | + if file_value == "key_not_available": |
| 89 | + with allure.step("Verifying the key: " + str(key)): |
| 90 | + assert (file_value == value), "Key is not available in the dynamic data file\n Key:- " + key \ |
| 91 | + + "\nTo store the displayed value try running the suite with\n" \ |
| 92 | + + "snap=1 pytest" |
| 93 | + else: |
| 94 | + with allure.step("Verifying the key: " + str(key)): |
| 95 | + assert (str(file_value) == str(value)), "Value for the Key:- " + str(key) + ", Mismatches\n" \ |
| 96 | + + "File Value:- " + str(file_value) \ |
| 97 | + + "\nDisplayed Value:- " + str(value) \ |
| 98 | + + "\nFile used for validation is:" + self.file_name \ |
| 99 | + + "\nTo change the Dynamic file value run the suite with" \ |
| 100 | + + "\nsnap=1 pytest" |
0 commit comments