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...
Check out the latest blogs from LambdaTest on this topic:
Automation frameworks enable automation testers by simplifying the test development and execution activities. A typical automation framework provides an environment for executing test plans and generating repeatable output. They are specialized tools that assist you in your everyday test automation tasks. Whether it is a test runner, an action recording tool, or a web testing tool, it is there to remove all the hard work from building test scripts and leave you with more time to do quality checks. Test Automation is a proven, cost-effective approach to improving software development. Therefore, choosing the best test automation framework can prove crucial to your test results and QA timeframes.
QA testers have a unique role and responsibility to serve the customer. Serving the customer in software testing means protecting customers from application defects, failures, and perceived failures from missing or misunderstood requirements. Testing for known requirements based on documentation or discussion is the core of the testing profession. One unique way QA testers can both differentiate themselves and be innovative occurs when senseshaping is used to improve the application user experience.
Many theoretical descriptions explain the role of the Scrum Master as a vital member of the Scrum team. However, these descriptions do not provide an honest answer to the fundamental question: “What are the day-to-day activities of a Scrum Master?”
The web paradigm has changed considerably over the last few years. Web 2.0, a term coined way back in 1999, was one of the pivotal moments in the history of the Internet. UGC (User Generated Content), ease of use, and interoperability for the end-users were the key pillars of Web 2.0. Consumers who were only consuming content up till now started creating different forms of content (e.g., text, audio, video, etc.).
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!!