Best Python code snippet using grail_python
steps.py
Source:steps.py
...3from unittest import TestCase4import grail.state as state5import grail.settings as settings6from grail.step_info import StepInfo, StepResults7def _generate_step_stack():8 stack = traceback.extract_stack()9 step_index = next(i for i, e in enumerate(stack) if e[0].endswith('steps.py'))10 stack = stack[step_index - 1:]11 stack = filter(lambda e: not (e[0].endswith('steps.py') or e[0].endswith('step_info.py')), stack)12 return stack13class _RedirectOut(object):14 def __init__(self):15 self.temp = sys.stdout16 self.step_messages = []17 self.delimited_messages = []18 self.string = ''19 def write(self, s):20 if s.strip(' '):21 self.delimited_messages.append(s)22 if not s.strip('\n'):23 self.string = ''.join(self.delimited_messages)24 self.delimited_messages = []25 self.step_messages.append(self.string)26 def flush(self):27 pass28 def out_to_lst(self):29 sys.stdout = self30 def out_to_console(self):31 sys.stdout = self.temp32 def get_captured_output(self):33 def convert(line):34 final_line = state.indentation + line35 if isinstance(final_line, unicode):36 return final_line37 return unicode(final_line, errors='replace')38 return u''.join(map(convert, self.step_messages))39def _should_skip_step():40 if settings.disable_steps:41 return True42 if settings.export_mode:43 return False44 for filename, line_number, func_name, text in traceback.extract_stack():45 if func_name in settings.skip_func:46 return True47 if state.treat_nested_steps_as_methods_global and state.step_execution_started:48 return True49class GrailValidationException(Exception):50 pass51def _validate_step_info(step_info):52 if step_info.step_group and step_info.treat_nested_steps_as_methods:53 raise GrailValidationException(u'Step logic disabling is not applicable for step groups')54 if not step_info.step_group and state.step_execution_started:55 raise GrailValidationException(u'Step is called from another step (without group): %s' %56 step_info.function.func_name)57def _execute(step_info):58 if _should_skip_step():59 return step_info.run_function()60 redirected_out = _RedirectOut()61 redirected_out.out_to_lst()62 state.indentation = state.indentation + settings.indentation_const63 _validate_step_info(step_info)64 output, result, exception_instance = None, None, None65 def print_to_console():66 redirected_out.out_to_console()67 state.step_execution_started = False68 console_message = redirected_out.get_captured_output()69 state.indentation = state.indentation[:-len(settings.indentation_const)]70 print_message = step_info.get_description(output, result, exception_instance)71 if console_message:72 print_message += u'\n'73 print_message += console_message.rstrip()74 print print_message75 try:76 if state.pending_step or state.step_first_error is not None:77 result = StepResults.IGNORED78 elif step_info.pending:79 result = StepResults.PENDING80 state.pending_step = True81 else:82 if step_info.step_group:83 output = step_info.run_function()84 if state.step_first_error:85 if isinstance(state.step_first_error, TestCase.failureException):86 result = StepResults.FAILED87 else:88 result = StepResults.ERROR89 elif state.pending_step:90 result = StepResults.PENDING91 else:92 result = StepResults.PASSED93 else:94 state.step_execution_started = True95 state.treat_nested_steps_as_methods_global = step_info.treat_nested_steps_as_methods96 if not settings.export_mode:97 output = step_info.run_function()98 result = StepResults.PASSED99 except Exception as inst:100 if isinstance(inst, TestCase.failureException):101 result = StepResults.FAILED102 else:103 result = StepResults.ERROR104 if not state.is_test_wrapped:105 print_to_console()106 raise107 if step_info.step_group:108 raise GrailValidationException(u'Unexpected exception from step group: %s' % inst)109 if isinstance(inst, GrailValidationException):110 raise111 if not state.step_first_error:112 state.step_first_error = inst113 state.step_stack = _generate_step_stack()114 state.step_exception_traceback = sys.exc_info()[2]115 exception_instance = inst116 print_to_console()117 return output118def step(func=None, description='', pending=False, step_group=False, format_description=False,119 treat_nested_steps_as_methods=False, log_output=True, log_input=True):120 step_info = StepInfo()121 def wrapper(*args, **kwargs):122 step_info.args = args123 step_info.kwargs = kwargs124 return _execute(step_info)125 def params_wrapper(function):126 step_info.function = function127 return wrapper...
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!!