Best Python code snippet using Airtest
logAnalysis.py
Source:logAnalysis.py
...72 title = self._translate_title(name,step)73 code = self._translate_code(step)74 desc = self._translate_desc(step, code)75 screen = self._translate_screen(step, code)76 info = self._translate_info(step)77 assertion = self._translate_assertion(step)78 # set test failed if any traceback exists79 if info[0]:80 self.test_result = False81 self.FieldNum = self.FieldNum +182 if self.needResetPath and screen != None:83 self.resetScreen(screen,self.caseName)84 translated = {85 "title": title,86 "time": step["time"],87 "code": code,88 "screen": screen,89 "desc": desc,90 "traceback": info[0],91 "log": info[1],92 "assert": assertion,93 }94 return translated95 def _translate_title(self,name,step):96 title = {97 "touch":u"Touch",98 "swipe":u"Swipe",99 "wait":u"Wait",100 "exists": u"Exists",101 "text": u"Text",102 "keyevent": u"Keyevent",103 "sleep": u"Sleep",104 "assert_exists": u"Assert exists",105 "assert_not_exists": u"Assert not exists",106 "snapshot": u"Snapshot",107 "assert_equal": u"Assert equal",108 "assert_not_equal": u"Assert not equal",109 }110 return title.get(name,name)111 def _translate_code(self,step):112 if step["tag"] != "function":113 return None114 step_data = step["data"]115 args = []116 code = {117 "name":step_data["name"],118 "args":args,119 }120 for key,value in step_data["call_args"].items():121 args.append({122 "key":key,123 "value":value,124 })125 for k,arg in enumerate(args):126 value = arg["value"]127 if isinstance(value,dict) and value.get("__class__") == "Template":128 if self.export_dir:# all relative path129 image_path = value['filename']130 if not os.path.isfile(os.path.join(self.script_root,image_path)) and value['_filepath']:131 # copy image used by using statement132 shutil.copyfile(value['_filepath'], os.path.join(self.script_root, value['filename']))133 else:134 image_path = os.path.abspath(value['_filepath'] or value['filename'])135 arg["image"] = image_path136 if not value['_filepath'] and not os.path.exists(value['filename']):137 crop_img = imread(os.path.join(self.script_root,value['filename']))138 else:139 crop_img = imread(value['_filepath'] or value['filename'])140 arg["resolution"] = get_resolution(crop_img)141 return code142 def resetScreen(self,screen,CaseName):143 screen['src'] = self.splitPath(screen['src'],CaseName)144 screen['_filepath'] = self.splitPath(screen['_filepath'], CaseName)145 screen['thumbnail'] = self.splitPath(screen['thumbnail'], CaseName)146 def splitPath(self,str,caseName):147 s = str.split(caseName)148 return str.replace(s[0], "")149 def _translate_desc(self,step,code):150 #""" å½æ°æè¿°"""151 if step['tag'] != "function":152 return None153 name = step['data']['name']154 res = step['data'].get('ret')155 args = {i['key']: i["value"] for i in code["args"]}156 desc ={157 "snapshot": lambda : u"Screenshot descriptions: %s" % args.get("msg"),158 "touch": lambda: u"Touch %s" % ("target image" if isinstance(args['v'],dict) else "coordinstes %s" % args['v']),159 "swipe": u"Swipe on screen",160 "wait": u"Wait for target image to appear",161 "exists": lambda: u"Image %s exists" % ("" if res else "not"),162 "text": lambda : u"Click [%s] button" % args.get('text'),163 "keyevent": lambda: u"Click [%s] button" % args.get('keyname'),164 "sleep": lambda: u"Wait for %s seconds" % args.get('secs'),165 "assert_exists": u"Assert target image exists",166 "assert_not_exists": u"Assert target image does not exists",167 }168 # todo: æ好ç¨jséçå¤è¯è¨å®ç°169 desc_zh = {170 "snapshot": lambda: u"æªå¾æè¿°: %s" % args.get("msg"),171 "touch": lambda: u"ç¹å» %s" % (u"ç®æ å¾ç" if isinstance(args['v'], dict) else u"å±å¹åæ %s" % args['v']),172 "swipe": u"æ»å¨æä½",173 "wait": u"çå¾
ç®æ å¾çåºç°",174 "exists": lambda: u"å¾ç%såå¨" % ("" if res else u"ä¸"),175 "text": lambda: u"è¾å
¥æå:%s" % args.get('text'),176 "keyevent": lambda: u"ç¹å»[%s]æé®" % args.get('keyname'),177 "sleep": lambda: u"çå¾
%sç§" % args.get('secs'),178 "assert_exists": u"æè¨ç®æ å¾çåå¨",179 "assert_not_exists": u"æè¨ç®æ å¾çä¸åå¨",180 }181 #if self.lang == "zh":182 desc = desc_zh183 ret = desc.get(name)184 if callable(ret):185 ret = ret()186 return ret187 def _translate_info(self,step):188 trace_msg,log_msg = "",""189 if "traceback" in step["data"]:190 # è¥å
å«ætracebackå
容ï¼å°ä¼è®¤å®æ¥éª¤å¤±è´¥191 trace_msg = step["data"]["traceback"]192 if step["tag"] == "info":193 if "log" in step["data"]:194 # æ®éææ¬logå
容ï¼ä»
æ¾ç¤º195 log_msg = step["data"]["log"]196 return trace_msg,log_msg197 def _translate_assertion(self,step):198 if "assert" in step["data"]["name"] and "msg" in step["data"]["call_args"]:199 return step["data"]["call_args"]["msg"]200 def _translate_screen(self,step,code):201 if step['tag'] not in ["function","info"] or not step.get("__children__"):...
trips.py
Source:trips.py
...29 price = random.choice(prices)30 if price < balance:31 balance -= price32 tour_pack.append(location)33 tour_pack = _translate_info(tour_pack)34 return jsonify(tour_pack)35@trips.route("/pack/random", methods=["GET"])36def pack_random_trip():37 locations_mock = get_locations_info()38 pack_size = random.randint(MIN_GENERATED_TOUR_NUMBER, MAX_GENERATED_TOUR_NUMBER)39 tour_pack = random.sample(locations_mock, pack_size)40 tour_pack = _translate_info(tour_pack)41 return jsonify(tour_pack)42def _translate_info(tour_pack) -> List[dict]:43 tour_pack = [translate_categories(loc) for loc in tour_pack]44 tour_pack = [translate_tags(loc) for loc in tour_pack]...
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!!