Best Python code snippet using toolium_python
env_utils.py
Source:env_utils.py
...44 msg = 'trying to execute a step in the environment: \n' \45 ' - Exception: %s' % exc46 if self.logger is not None:47 self.logger.warning(msg)48 self.by_console(' WARN - %s' % msg)49 def error(self, exc):50 """51 log an error message:52 :param exc: exception message53 """54 msg = 'trying to execute a step in the environment: \n' \55 ' - Exception: %s' % exc56 if self.logger is not None:57 self.logger.error(msg)58 self.by_console(' ERROR - %s' % msg)59 def debug(self, value):60 """61 log a debug message62 :param value: text to log63 """64 if self.logger is not None:65 self.logger.debug(value)66 def by_console(self, text_to_print):67 """68 print in console avoiding output buffering69 :param text_to_print: Text to print by console70 """71 if self.show:72 sys.stdout.write("%s\n" % text_to_print)73 sys.stdout.flush()74class DynamicEnvironment:75 """76 This class is useful when we would like execute generic steps: before the feature, before each scenario,77 after the feature or/and after each scenario.78 It is necessary to append certain lines in the environment.py:79 from common.utils.env_utils import DynamicEnvironment80 def before_all(context):81 context.dyn_env = DynamicEnvironment(logger=context.logger)82 def before_feature(context, feature):83 # ---- get all steps defined in the feature description associated to each action ----84 context.dyn_env.get_steps_from_feature_description(context, feature.description)85 # ---- actions before the feature ----86 context.dyn_env.execute_before_feature_steps(context)87 def before_scenario(context, scenario):88 # ---- actions before each scenario ----89 context.dyn_env.execute_before_scenario_steps(context)90 def after_scenario(context, scenario):91 # ---- actions after each scenario ----92 context.dyn_env.execute_after_scenario_steps(context)93 def after_feature(context, feature):94 # ---- actions after feature ----95 context.dyn_env.execute_after_feature_steps(context)96 """97 def __init__(self, **kwargs):98 """99 constructor100 :param kwargs: parameters set101 :param logger: logger instance102 :param show: determine if messages are displayed by console103 """104 logger_class = kwargs.get("logger", None)105 self.show = kwargs.get("show", True)106 self.logger = Logger(logger_class, self.show)107 self.init_actions()108 self.scenario_counter = 0109 self.feature_error = False110 self.scenario_error = False111 def init_actions(self):112 """clear actions lists"""113 self.actions = {ACTIONS_BEFORE_FEATURE: [],114 ACTIONS_BEFORE_SCENARIO: [],115 ACTIONS_AFTER_SCENARIO: [],116 ACTIONS_AFTER_FEATURE: []}117 def get_steps_from_feature_description(self, description):118 """119 get all steps defined in the feature description associated to each action120 :param description: feature description121 """122 self.init_actions()123 label_exists = EMPTY124 step_text_start = False125 for row in description:126 if label_exists != EMPTY:127 # in case of a line with a comment, it is removed128 if "#" in row:129 row = row[0:row.find("#")].strip()130 if any(row.startswith(x) for x in KEYWORDS):131 self.actions[label_exists].append(row)132 elif row.strip()[-3:] in STEP_TEXT_SEPARATORS and step_text_start:133 self.actions[label_exists][-1] = "%s\n %s" % (self.actions[label_exists][-1], row)134 step_text_start = False135 elif row.find(TABLE_SEPARATOR) >= 0 or step_text_start:136 self.actions[label_exists][-1] = "%s\n %s" % (self.actions[label_exists][-1], row)137 elif row.strip()[:3] in STEP_TEXT_SEPARATORS and not step_text_start:138 self.actions[label_exists][-1] = "%s\n %s" % (self.actions[label_exists][-1], row)139 step_text_start = True140 else:141 label_exists = EMPTY142 for action_label in self.actions:143 if row.lower().find(action_label) >= 0:144 label_exists = action_label145 def __remove_prefix(self, step):146 """147 remove the step prefix to will be replaced by Given148 :param step: step text149 """150 step_length = len(step)151 for k in KEYWORDS:152 step = step.lstrip(k)153 if len(step) < step_length:154 break155 return step156 def __print_step_by_console(self, step):157 """158 print the step by console if the show variable is enabled159 :param step: step text160 """161 step_list = step.split('\n')162 for s in step_list:163 self.logger.by_console(' %s' % repr(s).replace("u'", "").replace("'", ""))164 def __execute_steps_by_action(self, context, action):165 """166 execute a steps set by action167 :param context: Itâs a clever place where you and behave can store information to share around,168 automatically managed by behave.169 :param action: action executed: see labels allowed above.170 """171 if len(self.actions[action]) > 0:172 if action == ACTIONS_BEFORE_SCENARIO:173 self.logger.by_console('\n')174 self.scenario_counter += 1175 self.logger.by_console(176 " ------------------ Scenario Nº: %d ------------------" % self.scenario_counter)177 self.logger.by_console(' %s:' % action)178 elif action in [ACTIONS_BEFORE_FEATURE, ACTIONS_AFTER_FEATURE]:179 self.logger.by_console('\n')180 for item in self.actions[action]:181 self.scenario_error = False182 try:183 self.__print_step_by_console(item)184 context.execute_steps('''%s%s''' % (GIVEN_PREFIX, self.__remove_prefix(item)))185 self.logger.debug('step defined in pre-actions: %s' % repr(item))186 except Exception as exc:187 self.feature_error = action in [ACTIONS_BEFORE_FEATURE]188 self.scenario_error = action in [ACTIONS_BEFORE_SCENARIO]189 self.logger.error(exc)190 self.error_exception = exc191 break192 def reset_error_status(self):193 """194 Check if the dyn_env has got any exception when executing the steps and restore the value of status to False.195 :return: True if any exception has been raised when executing steps196 """197 try:...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!