Best Python code snippet using ATX
assertscraper.py
Source:assertscraper.py
...24 self.logger.setLevel(loglevel)25 self.allow_non_numeric_args = allow_non_numeric_args26 def _log(self, *args):27 self.logger.log(logging.DEBUG, " ".join(args))28 def _add_assert(self, node, reverse=False):29 assert_name = None30 args=[]31 if isinstance(node, ast.Call):32 assert_name = node.func.attr if isinstance(node.func, ast.Attribute) else node.func.id33 if len(node.args) > 2:34 args = node.args[2:] # save the extra arguments\35 elif len(node.keywords) > 0:36 args = node.keywords37 elif isinstance(node, ast.Assert):38 assert_name = 'assert'39 else:40 print("Unknown assert : %s" % astunparse.unparse(node).strip())41 return42 end_line = self._get_end_line(node)43 end_line = node.lineno if end_line == -1 else end_line44 assert_spec = AssertSpec(Test(self.filename, self.classname, self.funcname),45 line=node.lineno,46 end_line=end_line,47 col_offset=node.col_offset,48 assert_type=AssertType.get_assert_type(assert_name),49 assert_string=astunparse.unparse(node).strip(),50 args=args,51 reverse=reverse)52 if self.filename not in self.class_mapping:53 self.class_mapping[self.filename] = dict()54 if self.classname not in self.class_mapping[self.filename]:55 self.class_mapping[self.filename][self.classname] = []56 assert_str = assert_spec.get_uniq_str()57 if assert_str in self.assert_strings:58 #print("Duplicate " + assert_str)59 return60 self.class_mapping[self.filename][self.classname].append(assert_spec)61 self.asserts.append(assert_spec)62 self.assert_strings.append(assert_spec.get_uniq_str())63 def _get_end_line(self, node):64 if hasattr(node, 'parent'):65 ind = -166 parent = node.parent67 children = list(ast.iter_child_nodes(parent))68 #if hasattr(parent, 'body'):69 for i, n in enumerate(children):70 if n == node:71 ind = i72 break73 if ind == -1:74 print("Node not found")75 elif ind < len(children) - 1:76 if hasattr(children[ind+1], 'lineno'):77 return children[ind+1].lineno - 178 else:79 return self._get_end_line(parent)80 else:81 return self._get_end_line(parent)82 return -183 def visit_Assert(self, node):84 self._log(">>", astunparse.unparse(node).strip())85 self.get_threshold(node.test, node)86 self.generic_visit(node)87 def visit_Call(self, node: ast.Call):88 if isinstance(node.func, ast.Attribute):89 if node.func.attr in ['assertTrue', 'assertFalse']:90 self._log(">>", astunparse.unparse(node).strip())91 if isinstance(node.args[0], ast.Compare):92 self.get_threshold(node.args[0], node)93 else:94 self._log("Not handled:", astunparse.unparse(node).strip())95 elif node.func.attr in ['assertGreater', 'assertGreaterEqual', 'assertLessEqual', 'assertLess', 'assertAlmostEqual', 'assertNotAlmostEqual']:96 self._log(">>", astunparse.unparse(node).strip())97 if self._is_fp_value(node.args[1]):98 self._log(node.func.attr + ':' + astunparse.unparse(node.args[1]).strip())99 self._add_assert(node) # call node100 self.count += 1101 elif self._is_fp_value(node.args[0]):102 self._log(node.func.attr + ':' + astunparse.unparse(node.args[0]).strip())103 self._add_assert(node, reverse=True) # call node104 self.count += 1105 else:106 self._log("Not handled:", astunparse.unparse(node).strip())107 elif node.func.attr in ['assert_almost_equal', 'assert_approx_equal', 'assert_array_almost_equal',108 'assert_allclose', 'assert_array_almost_equal_nulp', 'assert_array_max_ulp',109 'assert_array_less', 'assert_equal']:110 # numpy asserts111 self._log(">>", astunparse.unparse(node).strip())112 # check if we want to consider comparison to a constant? capture atol, rtol or decimal arguments113 if Util.is_numerical_value(node.args[0]):114 self._add_assert(node, reverse=True)115 else:116 self._add_assert(node)117 self.count+=1118 elif node.func.attr in ['assertAllClose']:119 self._log(">>", astunparse.unparse(node).strip())120 if Util.is_numerical_value(node.args[0]):121 self._add_assert(node, reverse=True)122 else:123 self._add_assert(node)124 self.count += 1125 elif isinstance(node.func, ast.Name):126 if node.func.id in ['assertTrue', 'assertFalse']:127 self._log(">>", astunparse.unparse(node).strip())128 if isinstance(node.args[0], ast.Compare):129 self.get_threshold(node.args[0], node)130 else:131 self._log("Not handled:", astunparse.unparse(node).strip())132 elif node.func.id in ['assertGreater', 'assertGreaterEqual', 'assertLessEqual', 'assertLess', 'assertAlmostEqual', 'assertNotAlmostEqual']:133 self._log(">>", astunparse.unparse(node).strip())134 if self._is_fp_value(node.args[1]):135 self._log(node.func.id + ':' + astunparse.unparse(node.args[1]).strip())136 self._add_assert(node) # call node137 self.count += 1138 elif self._is_fp_value(node.args[0]):139 self._log(node.func.attr + ':' + astunparse.unparse(node.args[0]).strip())140 self._add_assert(node, reverse=True) # call node141 self.count += 1142 else:143 self._log("Not handled:", astunparse.unparse(node).strip())144 elif node.func.id in ['assert_almost_equal', 'assert_approx_equal', 'assert_array_almost_equal',145 'assert_allclose', 'assert_array_almost_equal_nulp', 'assert_array_max_ulp',146 'assert_array_less', 'assert_equal']:147 # numpy asserts148 self._log(">>", astunparse.unparse(node).strip())149 # check if we want to consider comparison to a constant? capture atol, rtol or decimal arguments150 if Util.is_numerical_value(node.args[0]):151 self._add_assert(node, reverse=True)152 else:153 self._add_assert(node)154 self.count += 1155 elif node.func.id in ['assertAllClose']:156 self._log(">>", astunparse.unparse(node).strip())157 if Util.is_numerical_value(node.args[0]):158 self._add_assert(node, reverse=True)159 else:160 self._add_assert(node)161 self.count += 1162 self.generic_visit(node)163 def _is_fp_value(self, node):164 if self.allow_non_numeric_args:165 return True166 if isinstance(node, ast.Num):167 return True168 if isinstance(node, ast.UnaryOp) and isinstance(node.operand, ast.Num):169 return True170 def get_threshold(self, node, parent_node):171 if isinstance(node, ast.Compare) \172 and isinstance(node.ops[0], (ast.LtE, ast.Lt, ast.Gt, ast.GtE))\173 and len(node.comparators) == 1\174 and self._is_fp_value(node.comparators[0]): # only handling the case with single comparator operator175 self._log(astunparse.unparse(node.comparators[0]).strip())176 self._add_assert(parent_node)177 self.count += 1178 elif isinstance(node, ast.Compare) \179 and isinstance(node.ops[0], (ast.LtE, ast.Lt, ast.Gt, ast.GtE)) \180 and len(node.comparators) == 1\181 and self._is_fp_value(node.left): # only handling the case with single comparator operator182 self._log(astunparse.unparse(node.left).strip())183 self._add_assert(parent_node, reverse=True)184 self.count += 1185 else:186 self._log("Not handled : ", ast.dump(node))187class AssertScraper:188 def __init__(self, library_dir, libraryname, loglevel=logging.ERROR, allow_non_numeric_args=False):189 self.testfiles = glob.glob("{0}/**/*.py".format(library_dir) if not library_dir.endswith(".py") else library_dir, recursive=True)190 self.assertcount = 0191 self.markedflaky = 0192 self.testnames = []193 self.asserts = list()194 self.assert_strings = list()195 self.class_mapping = dict()196 self.loglevel=loglevel197 self.libraryname=libraryname...
__init__.py
Source:__init__.py
...204 'success': is_success,205 'screenshot': self._take_screenshot(screenshot, name_prefix='assert'),206 }207 self.steps.append(step)208 def _add_assert(self, **kwargs):209 """210 if screenshot is None, only failed case will take screenshot211 """212 # convert screenshot to relative path from <None|True|False|PIL.Image>213 screenshot = kwargs.get('screenshot')214 is_success = kwargs.get('success')215 screenshot = (not is_success) if screenshot is None else screenshot216 kwargs['screenshot'] = self._take_screenshot(screenshot=screenshot, name_prefix='assert')217 action = kwargs.pop('action', 'assert')218 self.add_step(action, **kwargs)219 if not is_success:220 message = kwargs.get('message')221 frame, filename, line_number, function_name, lines, index = inspect.stack()[2]222 print('Assert [%s: %d] WARN: %s' % (filename, line_number, message))223 if not kwargs.get('safe', False):224 raise AssertionError(message)225 def assert_equal(self, v1, v2, **kwargs):#, desc=None, screenshot=False, safe=False):226 """ Check v1 is equals v2, and take screenshot if not equals227 Args:228 - desc (str): some description229 - safe (bool): will omit AssertionError if set to True230 - screenshot: can be type <None|True|False|PIL.Image>231 """232 is_success = v1 == v2233 if is_success:234 message = "assert equal success, %s == %s" %(v1, v2)235 else:236 message = '%s not equal %s' % (v1, v2)237 kwargs.update({238 'message': message,239 'success': is_success,240 })241 self._add_assert(**kwargs)242 def assert_image_exists(self, pattern, timeout=20.0, **kwargs):243 """244 Assert if image exists245 Args:246 - pattern: image filename # not support pattern for now247 - timeout (float): seconds248 - safe (bool): not raise assert error even throung failed.249 """250 pattern = self.d.pattern_open(pattern)251 match_kwargs = kwargs.copy()252 match_kwargs.pop('safe', None)253 match_kwargs.update({254 'timeout': timeout,255 'safe': True,256 })257 res = self.d.wait(pattern, **match_kwargs)258 is_success = res is not None259 message = 'assert image exists'260 if res:261 x, y = res.pos262 kwargs['position'] = {'x': x, 'y': y}263 message = 'image exists\npos %s\nconfidence=%.2f\nmethod=%s' % (res.pos, res.confidence, res.method)264 else:265 res = self.d.match(pattern)266 if res is None:267 message = 'Image not found'268 else:269 th = kwargs.get('threshold') or pattern.threshold or self.image_match_threshold270 message = 'Matched: %s\nPosition: %s\nConfidence: %.2f\nThreshold: %.2f' % (271 res.matched, res.pos, res.confidence, th)272 kwargs['target'] = self._save_screenshot(pattern, name_prefix='target')273 kwargs['screenshot'] = self.last_screenshot274 kwargs.update({275 'action': 'assert_image_exists',276 'message': message,277 'success': is_success,278 })279 self._add_assert(**kwargs)280 def assert_ui_exists(self, ui, **kwargs):281 """ For Android & IOS282 Args:283 - ui: need have property "exists"284 - desc (str): description285 - safe (bool): will omit AssertionError if set to True286 - screenshot: can be type <None|True|False|PIL.Image>287 - platform (str, default:android): android | ios288 """289 is_success = ui.exists290 if is_success:291 if kwargs.get('screenshot') is not None:292 if self.d.platform == 'android':293 bounds = ui.info['bounds'] # For android only.294 kwargs['position'] = {295 'x': (bounds['left']+bounds['right'])//2,296 'y': (bounds['top']+bounds['bottom'])//2,297 }298 elif self.d.platform == 'ios':299 bounds = ui.bounds # For iOS only.300 kwargs['position'] = {301 'x': self.d.scale*(bounds.x+bounds.width//2),302 'y': self.d.scale*(bounds.y+bounds.height//2),303 }304 message = 'UI exists'305 else:306 message = 'UI not exists'307 kwargs.update({308 'message': message,309 'success': is_success,310 })311 self._add_assert(**kwargs)312 def _listener(self, evt):313 d = self.d314 # keep screenshot for every call315 if not evt.is_before and evt.flag == consts.EVENT_SCREENSHOT:316 self.__last_screenshot = evt.retval317 if evt.depth > 1: # base depth is 1318 return319 if evt.is_before: # call before function320 if evt.flag == consts.EVENT_CLICK:321 self.__last_screenshot = d.screenshot() # Maybe no need to set value here.322 (x, y) = evt.args323 cv_img = imutils.from_pillow(self.last_screenshot)324 cv_img = imutils.mark_point(cv_img, x, y)325 self.__last_screenshot = imutils.to_pillow(cv_img)...
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!!