Best Python code snippet using fMBT_python
fmbtgti.py
Source:fmbtgti.py
...107 if isinstance(screenshotOrOirEngine, Screenshot):108 oirEngine = screenshotOrOirEngine.oirEngine()109 else:110 oirEngine = screenshotOrOirEngine111 return _takeArgs(oirEngine._findBitmapArgNames(), d, thatsAll)112def _takeOcrArgs(screenshotOrOcrEngine, d, thatsAll=False):113 if isinstance(screenshotOrOcrEngine, Screenshot):114 ocrEngine = screenshotOrOcrEngine.ocrEngine()115 else:116 ocrEngine = screenshotOrOcrEngine117 return _takeArgs(ocrEngine._findTextArgNames(), d, thatsAll)118def _takeArgs(argNames, d, thatsAll=False):119 """120 Returns pair:121 (dict of items where key in argNames,122 dict of items that were left in d)123 If thatsAll is True, require that all arguments have been124 consumed.125 """126 retval = {}127 for a in argNames:128 if a in d:129 retval[a] = d.pop(a, None)130 if thatsAll and len(d) > 0:131 raise TypeError('Unknown argument(s): "%s"' %132 ('", "'.join(sorted(d.keys()))))133 return retval, d134def _intCoords((x, y), (width, height)):135 if 0 <= x <= 1 and type(x) == float: x = x * width136 if 0 <= y <= 1 and type(y) == float: y = y * height137 return (int(round(x)), int(round(y)))138def _edgeDistanceInDirection((x, y), (width, height), direction):139 x, y = _intCoords((x, y), (width, height))140 direction = direction % 360 # -90 => 270, 765 => 45141 dirRad = math.radians(direction)142 if 0 < direction < 180:143 distTopBottom = y / math.sin(dirRad)144 elif 180 < direction < 360:145 distTopBottom = -(height - y) / math.sin(dirRad)146 else:147 distTopBottom = float('inf')148 if 90 < direction < 270:149 distLeftRight = -x / math.cos(dirRad)150 elif 270 < direction <= 360 or 0 <= direction < 90:151 distLeftRight = (width - x) / math.cos(dirRad)152 else:153 distLeftRight = float('inf')154 return min(distTopBottom, distLeftRight)155### Binding to eye4graphics.so156_libpath = ["", ".", distutils.sysconfig.get_python_lib(plat_specific=1)]157_suffix = ".so"158if sys.platform == "win32":159 _suffix = ".dll"160for _dirname in _libpath:161 try:162 eye4graphics = ctypes.CDLL(os.path.join(_dirname , "eye4graphics"+_suffix))163 break164 except: pass165else:166 raise ImportError("%s cannot load eye4graphics.so" % (__file__,))167class _Bbox(ctypes.Structure):168 _fields_ = [("left", ctypes.c_int32),169 ("top", ctypes.c_int32),170 ("right", ctypes.c_int32),171 ("bottom", ctypes.c_int32),172 ("error", ctypes.c_int32)]173### end of binding to eye4graphics.so174def _e4gImageDimensions(e4gImage):175 struct_bbox = _Bbox(0, 0, 0, 0, 0)176 eye4graphics.openedImageDimensions(ctypes.byref(struct_bbox), e4gImage)177 return (struct_bbox.right, struct_bbox.bottom)178def _e4gImageIsBlank(filename):179 e4gImage = eye4graphics.openImage(filename)180 rv = (eye4graphics.openedImageIsBlank(e4gImage) == 1)181 eye4graphics.closeImage(e4gImage)182 return rv183class GUITestConnection(object):184 """185 Implements GUI testing primitives needed by GUITestInterface.186 All send* and recv* methods return187 - True on success188 - False on user error (unknown keyName, coordinates out of range)189 - raise Exception on framework error (connection lost, missing190 dependencies).191 """192 def sendPress(self, keyName):193 raise NotImplementedError('sendPress("%s") needed but not implemented.' % (keyName,))194 def sendKeyDown(self, keyName):195 raise NotImplementedError('sendKeyDown("%s") needed but not implemented.' % (keyName,))196 def sendKeyUp(self, keyName):197 raise NotImplementedError('sendKeyUp("%s") needed but not implemented.' % (keyName,))198 def sendTap(self, x, y):199 raise NotImplementedError('sendTap(%d, %d) needed but not implemented.' % (x, y))200 def sendTouchDown(self, x, y):201 raise NotImplementedError('sendTouchDown(%d, %d) needed but not implemented.' % (x, y))202 def sendTouchMove(self, x, y):203 raise NotImplementedError('sendTouchMove(%d, %d) needed but not implemented.' % (x, y))204 def sendTouchUp(self, x, y):205 raise NotImplementedError('sendTouchUp(%d, %d) needed but not implemented.' % (x, y))206 def sendType(self, text):207 raise NotImplementedError('sendType("%s") needed but not implemented.' % (text,))208 def recvScreenshot(self, filename):209 """210 Saves screenshot from the GUI under test to given filename.211 """212 raise NotImplementedError('recvScreenshot("%s") needed but not implemented.' % (filename,))213 def target(self):214 """215 Returns a string that is unique to each test target. For216 instance, Android device serial number.217 """218 return "GUITestConnectionTarget"219class OrEngine(object):220 """221 Optical recognition engine. Base class for OCR and OIR engines,222 enables registering engine instances.223 """224 def __init__(self, *args, **kwargs):225 pass226 def register(self, defaultOcr=False, defaultOir=False):227 """228 Register this engine instance to the list of OCR and/or OIR229 engines.230 Parameters:231 defaultOcr (optional, boolean):232 if True, use this OCR engine by default in all new233 GUITestInterface instances. The default is False.234 defaultOir (optional, boolean):235 if True, use this OIR engine by default in all new236 GUITestInterface instances. The default is False.237 Returns the index with which the engine was registered to the238 list of OCR or OIR engines. If this instance implements both239 OCR and OIR engines, returns pair (OCR index, OIR index).240 """241 # Allow a single engine implement both OCR and OIR engine242 # interfaces. Therefore, it must be possible to call243 # e.register(defaultOcr=True, defaultOir=True).244 #245 global _g_defaultOcrEngine, _g_defaultOirEngine246 global _g_ocrEngines, _g_oirEngines247 engineIndexes = []248 if isinstance(self, OcrEngine):249 if not self in _g_ocrEngines:250 _g_ocrEngines.append(self)251 engineIndexes.append(_g_ocrEngines.index(self))252 if defaultOcr:253 _g_defaultOcrEngine = self254 if isinstance(self, OirEngine):255 if not self in _g_oirEngines:256 _g_oirEngines.append(self)257 engineIndexes.append(_g_oirEngines.index(self))258 if defaultOir:259 _g_defaultOirEngine = self260 if len(engineIndexes) == 1:261 return engineIndexes[0]262 else:263 return engineIndexes264class OcrEngine(OrEngine):265 """266 This is an abstract interface for OCR engines that can be plugged267 into fmbtgti.GUITestInterface instances and Screenshots.268 To implement an OCR engine, you need to override _findText() at269 minimum. See _findText documentation in this class for270 requirements.271 If possible in your OCR engine, you can provide _dumpOcr() to272 reveal what is recognized in screenshots.273 For efficient caching of results and freeing cached results, you274 can override _addScreenshot() and _removeScreenshot(). Every275 screenshot is added before findText() or dumpOcr().276 A typical usage of OcrEngine instance:277 - oe.addScreenshot(ss)278 - oe.findText(ss, text1, <engine/screenshot/find-specific-args>)279 - oe.findText(ss, text2, <engine/screenshot/find-specific-args>)280 - oe.removeScreenshot(ss)281 Note that there may be several screenshots added before they are282 removed.283 """284 def __init__(self, *args, **kwargs):285 super(OcrEngine, self).__init__(*args, **kwargs)286 self._ssFindTextDefaults = {}287 self._findTextDefaults = {}288 ocrFindArgs, _ = _takeOcrArgs(self, kwargs)289 self._setFindTextDefaults(ocrFindArgs)290 def dumpOcr(self, screenshot, **kwargs):291 """292 Returns what is recognized in the screenshot. For debugging293 purposes.294 """295 ocrArgs = self.__ocrArgs(screenshot, **kwargs)296 return self._dumpOcr(screenshot, **ocrArgs)297 def _dumpOcr(self, screenshot, **kwargs):298 return None299 def addScreenshot(self, screenshot, **findTextDefaults):300 """301 Prepare for finding text from the screenshot.302 Parameters:303 screenshot (fmbtgti.Screenshot)304 screenshot object to be searched from.305 other parameters (optional)306 findText defaults for this screenshot.307 Notice that there may be many screenshots simultaneously.308 Do not keep reference to the screenshot object.309 """310 self.setScreenshotFindTextDefaults(screenshot, **findTextDefaults)311 return self._addScreenshot(screenshot, **findTextDefaults)312 def _addScreenshot(self, screenshot, **findTextDefaults):313 pass314 def removeScreenshot(self, screenshot):315 """316 OCR queries on the screenshot will not be made anymore.317 """318 self._removeScreenshot(screenshot)319 try:320 del self._ssFindTextDefaults[id(screenshot)]321 except KeyError:322 raise KeyError('screenshot "%s" does not have findTextDefaults. '323 'If OcrEngine.addScreenshot() is overridden, it '324 '*must* call parent\'s addScreenshot.' % (screenshot.filename(),))325 def _removeScreenshot(self, screenshot):326 pass327 def setFindTextDefaults(self, **findTextDefaults):328 return self._setFindTextDefaults(findTextDefaults, screenshot=None)329 def setScreenshotFindTextDefaults(self, screenshot, **findTextDefaults):330 return self._setFindTextDefaults(findTextDefaults, screenshot=screenshot)331 def _setFindTextDefaults(self, defaults, screenshot=None):332 """333 Set default values for optional arguments for findText().334 Parameters:335 defaults (dictionary)336 Default keyword arguments and their values.337 screenshot (optional, fmbtgti.Screenshot instance)338 Use the defaults for findText on this screenshot. If339 the defaults are None, make them default for all340 screenshots. Screenshot-specific defaults override341 engine default.342 """343 if screenshot == None:344 self._findTextDefaults.update(defaults)345 else:346 ssid = id(screenshot)347 if not ssid in self._ssFindTextDefaults:348 self._ssFindTextDefaults[ssid] = self._findTextDefaults.copy()349 self._ssFindTextDefaults[ssid].update(defaults)350 def findTextDefaults(self, screenshot=None):351 if screenshot == None:352 return self._findTextDefaults353 elif id(screenshot) in self._ssFindTextDefaults:354 return self._ssFindTextDefaults[id(screenshot)]355 else:356 return None357 def _findTextArgNames(self):358 """359 Returns names of optional findText arguments.360 """361 return inspect.getargspec(self._findText).args[3:]362 def __ocrArgs(self, screenshot, **priorityArgs):363 ocrArgs = {}364 ocrArgs.update(self._findTextDefaults)365 ssId = id(screenshot)366 if ssId in self._ssFindTextDefaults:367 ocrArgs.update(self._ssFindTextDefaults[ssId])368 ocrArgs.update(priorityArgs)369 return ocrArgs370 def findText(self, screenshot, text, **kwargs):371 """372 Return list of fmbtgti.GUIItems that match to text.373 """374 ocrArgs = self.__ocrArgs(screenshot, **kwargs)375 return self._findText(screenshot, text, **ocrArgs)376 def _findText(self, screenshot, text, **kwargs):377 """378 Find appearances of text from the screenshot.379 Parameters:380 screenshot (fmbtgti.Screenshot)381 Screenshot from which text is to be searched382 for. Use Screenshot.filename() to get the filename.383 text (string)384 text to be searched for.385 other arguments (engine specific)386 kwargs contain keyword arguments given to387 findText(screenshot, text, ...), already extended388 first with screenshot-specific findTextDefaults, then389 with engine-specific findTextDefaults.390 _findText *must* define all engine parameters as391 explicit keyword arguments:392 def _findText(self, screenshot, text, engArg1=42):393 ...394 Return list of fmbtgti.GUIItems.395 """396 raise NotImplementedError("_findText needed but not implemented.")397class _EyenfingerOcrEngine(OcrEngine):398 """399 OCR engine parameters that can be used in all400 ...OcrText() methods (swipeOcrText, tapOcrText, findItemsByOcrText, ...):401 match (float, optional):402 minimum match score in range [0.0, 1.0]. The default is403 1.0 (exact match).404 area ((left, top, right, bottom), optional):405 search from the given area only. Left, top, right and406 bottom are either absolute coordinates (integers) or407 floats in range [0.0, 1.0]. In the latter case they are408 scaled to screenshot dimensions. The default is (0.0,409 0.0, 1.0, 1.0), that is, search everywhere in the410 screenshot.411 pagesegmodes (list of integers, optional):412 try all integers as tesseract -pagesegmode413 arguments. The default is [3], another good option could414 be [3, 6].415 preprocess (string, optional):416 preprocess filter to be used in OCR for better417 result. Refer to eyenfinger.autoconfigure to search for418 a good one.419 """420 class _OcrResults(object):421 __slots__ = ("filename", "screenSize", "pagesegmodes", "preprocess", "area", "words")422 def __init__(self, filename, screenSize):423 self.filename = filename424 self.screenSize = screenSize425 self.pagesegmodes = None426 self.preprocess = None427 self.area = None428 self.words = None429 def __init__(self, *args, **engineDefaults):430 engineDefaults["area"] = engineDefaults.get("area", (0.0, 0.0, 1.0, 1.0))431 engineDefaults["match"] = engineDefaults.get("match", 1.0)432 engineDefaults["pagesegmodes"] = engineDefaults.get("pagesegmodes", _OCRPAGESEGMODES)433 engineDefaults["preprocess"] = engineDefaults.get("preprocess", _OCRPREPROCESS)434 super(_EyenfingerOcrEngine, self).__init__(*args, **engineDefaults)435 self._ss = {} # OCR results for screenshots436 def _addScreenshot(self, screenshot, **findTextDefaults):437 ssId = id(screenshot)438 self._ss[ssId] = _EyenfingerOcrEngine._OcrResults(screenshot.filename(), screenshot.size())439 def _removeScreenshot(self, screenshot):440 ssId = id(screenshot)441 if ssId in self._ss:442 del self._ss[ssId]443 def _findText(self, screenshot, text, match=None, preprocess=None, area=None, pagesegmodes=None):444 ssId = id(screenshot)445 self._assumeOcrResults(screenshot, preprocess, area, pagesegmodes)446 for ppfilter in self._ss[ssId].words.keys():447 try:448 score_text_bbox_list = eyenfinger.findText(449 text, self._ss[ssId].words[ppfilter], match=match)450 if not score_text_bbox_list:451 continue452 else:453 break454 except eyenfinger.BadMatch:455 continue456 else:457 return []458 retval = [GUIItem("OCR text (match %.2f)" % (score,),459 bbox, self._ss[ssId].filename,460 ocrFind=text, ocrFound=matching_text)461 for score, matching_text, bbox in score_text_bbox_list]462 return retval463 def _dumpOcr(self, screenshot, match=None, preprocess=None, area=None, pagesegmodes=None):464 ssId = id(screenshot)465 if self._ss[ssId].words == None:466 self._assumeOcrResults(screenshot, preprocess, area, pagesegmodes)467 w = []468 for ppfilter in self._ss[ssId].preprocess:469 for word in self._ss[ssId].words[ppfilter]:470 for appearance, (wid, middle, bbox) in enumerate(self._ss[ssId].words[ppfilter][word]):471 (x1, y1, x2, y2) = bbox472 w.append((word, (x1, y1, x2, y2)))473 return sorted(set(w), key=lambda i:(i[1][1]/8, i[1][0]))474 def _assumeOcrResults(self, screenshot, preprocess, area, pagesegmodes):475 ssId = id(screenshot)476 if not type(preprocess) in (list, tuple):477 preprocess = [preprocess]478 if self._ss[ssId].words == None or self._ss[ssId].preprocess != preprocess or self._ss[ssId].area != area:479 self._ss[ssId].words = {}480 self._ss[ssId].preprocess = preprocess481 self._ss[ssId].area = area482 for ppfilter in preprocess:483 pp = ppfilter % { "zoom": "-resize %sx" % (self._ss[ssId].screenSize[0] * 2) }484 eyenfinger.iRead(source=self._ss[ssId].filename, ocr=True, preprocess=pp, ocrArea=area, ocrPageSegModes=pagesegmodes)485 self._ss[ssId].words[ppfilter] = eyenfinger._g_words486def _defaultOcrEngine():487 if _g_defaultOcrEngine:488 return _g_defaultOcrEngine489 else:490 _EyenfingerOcrEngine().register(defaultOcr=True)491 return _g_defaultOcrEngine492class OirEngine(OrEngine):493 """494 This is an abstract interface for OIR (optical image recognition)495 engines that can be plugged into fmbtgti.GUITestInterface496 instances and Screenshots.497 To implement an OIR engine, you need to override _findBitmap() at498 minimum. See _findBitmap documentation in this class for499 requirements.500 This base class provides full set of OIR parameters to501 _findBitmap. The parameters are combined from502 - OirEngine find defaults, specified when OirEngine is503 instantiated.504 - Screenshot instance find defaults.505 - bitmap / bitmap directory find defaults (read from the506 .fmbtoirrc that is in the same directory as the bitmap).507 - ...Bitmap() method parameters.508 The latter in the list override the former.509 For efficient caching of results and freeing cached results, you510 can override _addScreenshot() and _removeScreenshot(). Every511 screenshot is added before findBitmap().512 A typical usage of OirEngine instance:513 - oe.addScreenshot(ss)514 - oe.findBitmap(ss, bmpFilename1, <engine/screenshot/find-specific-args>)515 - oe.findBitmap(ss, bmpFilename2, <engine/screenshot/find-specific-args>)516 - oe.removeScreenshot(ss)517 Note that there may be several screenshots added before they are518 removed. ss is a Screenshot instance. Do not keep references to519 Screenshot intances, otherwise garbage collector will not remove520 them.521 """522 def __init__(self, *args, **kwargs):523 super(OirEngine, self).__init__(*args, **kwargs)524 self._ssFindBitmapDefaults = {}525 self._findBitmapDefaults = {}526 oirArgs, _ = _takeOirArgs(self, kwargs)527 self._setFindBitmapDefaults(oirArgs)528 def addScreenshot(self, screenshot, **findBitmapDefaults):529 """530 Prepare for finding bitmap from the screenshot.531 Parameters:532 screenshot (fmbtgti.Screenshot)533 screenshot object to be searched from.534 other parameters (optional)535 findBitmap defaults for this screenshot.536 Notice that there may be many screenshots simultaneously.537 Do not keep reference to the screenshot object.538 """539 self.setScreenshotFindBitmapDefaults(screenshot, **findBitmapDefaults)540 return self._addScreenshot(screenshot, **findBitmapDefaults)541 def _addScreenshot(self, screenshot, **findBitmapDefaults):542 pass543 def removeScreenshot(self, screenshot):544 """545 OIR queries on the screenshot will not be made anymore.546 """547 self._removeScreenshot(screenshot)548 try:549 del self._ssFindBitmapDefaults[id(screenshot)]550 except KeyError:551 raise KeyError('screenshot "%s" does not have findBitmapDefaults. '552 'If OirEngine.addScreenshot() is overridden, it '553 '*must* call parent\'s addScreenshot.' % (screenshot.filename(),))554 def _removeScreenshot(self, screenshot):555 pass556 def setFindBitmapDefaults(self, **findBitmapDefaults):557 return self._setFindBitmapDefaults(findBitmapDefaults, screenshot=None)558 def setScreenshotFindBitmapDefaults(self, screenshot, **findBitmapDefaults):559 return self._setFindBitmapDefaults(findBitmapDefaults, screenshot=screenshot)560 def _setFindBitmapDefaults(self, defaults, screenshot=None):561 """562 Set default values for optional arguments for findBitmap().563 Parameters:564 defaults (dictionary)565 Default keyword arguments and their values.566 screenshot (optional, fmbtgti.Screenshot instance)567 Use the defaults for findBitmap on this screenshot. If568 the defaults are None, make them default for all569 screenshots. Screenshot-specific defaults override570 engine default.571 """572 if screenshot == None:573 self._findBitmapDefaults.update(defaults)574 else:575 ssid = id(screenshot)576 if not ssid in self._ssFindBitmapDefaults:577 self._ssFindBitmapDefaults[ssid] = self._findBitmapDefaults.copy()578 self._ssFindBitmapDefaults[ssid].update(defaults)579 def findBitmapDefaults(self, screenshot=None):580 if screenshot == None:581 return self._findBitmapDefaults582 elif id(screenshot) in self._ssFindBitmapDefaults:583 return self._ssFindBitmapDefaults[id(screenshot)]584 else:585 return None586 def _findBitmapArgNames(self):587 """588 Returns names of optional findBitmap arguments.589 """590 return inspect.getargspec(self._findBitmap).args[3:]591 def __oirArgs(self, screenshot, bitmap, **priorityArgs):592 oirArgs = {}593 oirArgs.update(self._findBitmapDefaults)594 ssId = id(screenshot)595 if ssId in self._ssFindBitmapDefaults:596 oirArgs.update(self._ssFindBitmapDefaults[ssId])597 oirArgs.update(priorityArgs)598 return oirArgs599 def findBitmap(self, screenshot, bitmap, **kwargs):600 """...
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!!