Best Python code snippet using lemoncheesecake
scripts.py
Source:scripts.py
...583 return ret584def _merge_objects(o1, o2):585 for key, value in o2.items():586 o1[key] = value587def _lookup_step(name, steps, stepname):588 for step in steps:589 if step.get('name', '') == stepname:590 return step591 if not stepname and len(steps) == 1:592 return steps[0]593 if not stepname:594 raise ValueError("Parameter '%s' not found" % (name))595 raise ValueError("Referenced step '%s' not found in '%s'" % (stepname, name))596def _process_agent_include(script, include):597 from . import ra598 agent = include['agent']599 info = ra.get_ra(agent)600 meta = info.meta()601 if meta is None:602 raise ValueError("No meta-data for agent: %s" % (agent))603 name = include.get('name', meta.get('name'))604 if not name:605 cls, provider, name = ra.disambiguate_ra_type(agent)606 if 'name' not in include:607 include['name'] = name608 step = _listfindpend(name, script['steps'], lambda x: x.get('name'), lambda: {609 'name': name,610 'longdesc': '',611 'shortdesc': '',612 'parameters': [],613 })614 step['longdesc'] = include.get('longdesc') or _meta_text(meta, 'longdesc')615 step['shortdesc'] = _strip(include.get('shortdesc') or _meta_text(meta, 'shortdesc'))616 step['required'] = include.get('required', True)617 step['parameters'].append({618 'name': 'id',619 'shortdesc': 'Identifier for the cluster resource',620 'longdesc': '',621 'required': True,622 'unique': True,623 'type': 'resource',624 })625 def newparamobj(param):626 pname = param.get('name')627 return _listfindpend(pname, step['parameters'], lambda x: x.get('name'), lambda: {'name': pname})628 for param in meta.xpath('./parameters/parameter'):629 pobj = newparamobj(param)630 pobj['required'] = _make_boolean(param.get('required', False))631 pobj['unique'] = _make_boolean(param.get('unique', False))632 pobj['longdesc'] = _meta_text(param, 'longdesc')633 pobj['shortdesc'] = _strip(_meta_text(param, 'shortdesc'))634 # set 'advanced' flag on all non-required agent parameters by default635 # a UI should hide these parameters unless "show advanced" is set636 pobj['advanced'] = not pobj['required']637 ctype = param.xpath('./content/@type')638 cexample = param.xpath('./content/@default')639 if ctype:640 pobj['type'] = ctype[0]641 if cexample:642 pobj['example'] = cexample[0]643 for param in include.get('parameters', []):644 pobj = newparamobj(param)645 # Make any overriden parameters non-advanced646 # unless explicitly set to advanced647 pobj['advanced'] = False648 for key, value in param.items():649 if key in ('shortdesc', 'longdesc'):650 pobj[key] = value651 elif key == 'value':652 pobj[key] = Text(script, value)653 else:654 pobj[key] = value655 if 'value' in pobj:656 pobj['required'] = False657 # If the script doesn't have any base parameters658 # and the name of this step is the same as the659 # script name itself, then make this the base step660 hoist = False661 hoist_from = None662 if step['name'] == script['name']:663 zerostep = _listfind('', script['steps'], lambda x: x.get('name', ''))664 if not zerostep:665 hoist = True666 elif zerostep.get('parameters'):667 zp = zerostep['parameters']668 for pname in [p['name'] for p in step['parameters']]:669 if _listfind(pname, zp, lambda x: x['name']):670 break671 else:672 hoist, hoist_from = True, zerostep673 # use step['name'] here in case we did the zerostep hoist674 step['value'] = Text.cib(script, _make_cib_for_agent('' if hoist else step['name'],675 agent, step, include.get('ops', '')))676 if hoist:677 step['name'] = ''678 if hoist_from:679 step['parameters'] = hoist_from['parameters'] + step['parameters']680 script['steps'] = [s for s in script['steps'] if s != hoist_from]681 if not step['name']:682 del step['name']683 # this works despite possible hoist above,684 # since name is still the actual name685 for action in script['actions']:686 if 'include' in action and action['include'] == name:687 del action['include']688 action['cib'] = step['value']689def _process_script_include(script, include):690 script_name = include['script']691 if 'name' not in include:692 include['name'] = script_name693 subscript = load_script(script_name)694 name = include['name']695 scriptstep = {696 'name': name,697 'shortdesc': subscript['shortdesc'],698 'longdesc': subscript['longdesc'],699 'required': _make_boolean(include.get('required', True)),700 'steps': deepcopy(subscript['steps']),701 'sub-script': subscript,702 }703 def _merge_step_params(step, params):704 for param in params:705 _merge_step_param(step, param)706 def _merge_step_param(step, param):707 for p in step.get('parameters', []):708 if p['name'] == param['name']:709 for key, value in param.items():710 if key in ('shortdesc', 'longdesc'):711 p[key] = value712 elif key == 'value' and Text.isa(value):713 p[key] = Text(script, value)714 else:715 p[key] = value716 if 'value' in p:717 p['required'] = False718 break719 else:720 raise ValueError("Referenced parameter '%s' not found in '%s'" % (param['name'], name))721 for incparam in include.get('parameters', []):722 if 'step' in incparam and 'name' not in incparam:723 _merge_step_params(_lookup_step(name, scriptstep.get('steps', []), incparam['step']),724 incparam['parameters'])725 else:726 _merge_step_param(_lookup_step(name, scriptstep.get('steps', []), ''),727 incparam)728 script['steps'].append(scriptstep)729def _process_include(script, include):730 """731 includes add parameter steps and actions732 an agent include works like a hawk template:733 it adds a parameter step734 a script include however adds any number of735 parameter steps and actions736 OK. here's what to do: Don't rescope the steps737 and actions. Instead, keep the actions attached738 to script step 0, as above. And for each step, add739 a scope which states its scope. Then, when evaluating740 handles, build custom environments for those scopes to...
writer.py
Source:writer.py
...14 return self.report.get_suite(suite)15 def _add_step_log(self, log, event):16 result = self.report.get(event.location)17 assert result, "Cannot find location %s in the report" % event.location18 step = self._lookup_step(event)19 assert step, "Cannot find active step for %s" % event.location20 assert not step.end_time, "Cannot update step '%s', it has already been ended" % step.description21 step.add_log(log)22 @staticmethod23 def _initialize_result(start_time):24 result = Result()25 result.start_time = start_time26 return result27 @staticmethod28 def _initialize_test_result(test, start_time):29 result = TestResult(test.name, test.description)30 result.tags.extend(test.tags)31 result.properties.update(test.properties)32 result.links.extend(test.links)33 result.rank = test.rank34 result.start_time = start_time35 return result36 @staticmethod37 def _finalize_result(result, end_time):38 result.end_time = end_time39 result.status = "passed" if result.is_successful() else "failed"40 def _lookup_step(self, event):41 try:42 return self.active_steps[event.thread_id]43 except KeyError:44 return None45 def on_test_session_start(self, event):46 self.report.start_time = event.time47 def on_test_session_end(self, event):48 self.report.end_time = event.time49 def on_test_session_setup_start(self, event):50 self.report.test_session_setup = self._initialize_result(event.time)51 def on_test_session_setup_end(self, event):52 self._finalize_result(self.report.test_session_setup, event.time)53 def on_test_session_teardown_start(self, event):54 self.report.test_session_teardown = self._initialize_result(event.time)55 def on_test_session_teardown_end(self, event):56 self._finalize_result(self.report.test_session_teardown, event.time)57 def on_suite_start(self, event):58 suite = event.suite59 suite_result = SuiteResult(suite.name, suite.description)60 suite_result.start_time = event.time61 suite_result.tags.extend(suite.tags)62 suite_result.properties.update(suite.properties)63 suite_result.links.extend(suite.links)64 suite_result.rank = suite.rank65 if suite.parent_suite:66 parent_suite_result = self._get_suite_result(suite.parent_suite)67 parent_suite_result.add_suite(suite_result)68 else:69 self.report.add_suite(suite_result)70 def on_suite_end(self, event):71 suite_result = self._get_suite_result(event.suite)72 suite_result.end_time = event.time73 def on_suite_setup_start(self, event):74 suite_result = self._get_suite_result(event.suite)75 suite_result.suite_setup = self._initialize_result(event.time)76 def on_suite_setup_end(self, event):77 suite_result = self._get_suite_result(event.suite)78 self._finalize_result(suite_result.suite_setup, event.time)79 def on_suite_teardown_start(self, event):80 suite_result = self._get_suite_result(event.suite)81 suite_result.suite_teardown = self._initialize_result(event.time)82 def on_suite_teardown_end(self, event):83 suite_result = self._get_suite_result(event.suite)84 self._finalize_result(suite_result.suite_teardown, event.time)85 def on_test_start(self, event):86 test_result = self._initialize_test_result(event.test, event.time)87 suite_result = self._get_suite_result(event.test.parent_suite)88 suite_result.add_test(test_result)89 def on_test_end(self, event):90 test_result = self._get_test_result(event.test)91 self._finalize_result(test_result, event.time)92 def _bypass_test(self, test, status, status_details, time):93 test_result = self._initialize_test_result(test, time)94 test_result.end_time = time95 test_result.status = status96 test_result.status_details = status_details97 suite_result = self._get_suite_result(test.parent_suite)98 suite_result.add_test(test_result)99 def on_test_skipped(self, event):100 self._bypass_test(event.test, "skipped", event.skipped_reason, event.time)101 def on_test_disabled(self, event):102 self._bypass_test(event.test, "disabled", event.disabled_reason, event.time)103 def on_step_start(self, event):104 result = self.report.get(event.location)105 step = Step(event.step_description)106 step.start_time = event.time107 result.add_step(step)108 self.active_steps[event.thread_id] = step109 def on_step_end(self, event):110 step = self._lookup_step(event)111 step.end_time = event.time112 def on_log(self, event):113 self._add_step_log(114 Log(event.log_level, event.log_message, event.time), event115 )116 def on_check(self, event):117 self._add_step_log(118 Check(event.check_description, event.check_is_successful, event.check_details, event.time), event119 )120 def on_log_attachment(self, event):121 self._add_step_log(122 Attachment(event.attachment_description, event.attachment_path, event.as_image, event.time), event123 )124 def on_log_url(self, event):...
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!!