Best Python code snippet using fMBT_python
fmbtgti.py
Source:fmbtgti.py
...270 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 """601 Return list of fmbtgti.GUIItems that match to bitmap.602 """603 oirArgs = self.__oirArgs(screenshot, bitmap, **kwargs)604 return self._findBitmap(screenshot, bitmap, **oirArgs)605 def _findBitmap(self, screenshot, bitmap, **kwargs):606 """607 Find appearances of bitmap from the screenshot.608 Parameters:609 screenshot (fmbtgti.Screenshot)610 Screenshot from which bitmap is to be searched611 for. Use Screenshot.filename() to get the filename.612 bitmap (string)613 bitmap to be searched for.614 other arguments (engine specific)615 kwargs contain keyword arguments given to616 findBitmap(screenshot, bitmap, ...), already extended617 first with screenshot-specific findBitmapDefaults, then618 with engine-specific findBitmapDefaults.619 _findBitmap *must* define all engine parameters as620 explicit keyword arguments:621 def _findBitmap(self, screenshot, bitmap, engArg1=42):622 ...623 Returns list of fmbtgti.GUIItems.624 """625 raise NotImplementedError("_findBitmap needed but not implemented.")626class _Eye4GraphicsOirEngine(OirEngine):627 """OIR engine parameters that can be used in all628 ...Bitmap() methods (swipeBitmap, tapBitmap, findItemsByBitmap, ...):629 colorMatch (float, optional):630 required color matching accuracy. The default is 1.0631 (exact match). For instance, 0.75 requires that every632 pixel's every RGB component value on the bitmap is at633 least 75 % match with the value of corresponding pixel's634 RGB component in the screenshot.635 opacityLimit (float, optional):636 threshold for comparing pixels with non-zero alpha637 channel. Pixels less opaque than the given threshold are638 skipped in match comparison. The default is 0, that is,639 alpha channel is ignored.640 area ((left, top, right, bottom), optional):641 search bitmap from the given area only. Left, top right642 and bottom are either absolute coordinates (integers) or643 floats in range [0.0, 1.0]. In the latter case they are644 scaled to screenshot dimensions. The default is (0.0,645 0.0, 1.0, 1.0), that is, search everywhere in the646 screenshot.647 limit (integer, optional):648 number of returned matches is limited to the limit. The649 default is -1: all matches are returned. Applicable in650 findItemsByBitmap.651 allowOverlap (boolean, optional):652 allow returned icons to overlap. If False, returned list653 contains only non-overlapping bounding boxes. The654 default is False.655 scale (float or pair of floats, optional):656 scale to be applied to the bitmap before657 matching. Single float is a factor for both X and Y658 axis, pair of floats is (xScale, yScale). The default is659 1.0.660 bitmapPixelSize (integer, optional):661 size of pixel rectangle on bitmap for which there must662 be same color on corresponding screenshot rectangle. If663 scale is 1.0, default is 1 (rectangle is 1x1). If scale664 != 1.0, the default is 2 (rectangle is 2x2).665 screenshotPixelSize (integer, optional):666 size of pixel rectangle on screenshot in which there667 must be a same color pixel as in the corresponding668 rectangle on bitmap. The default is scale *669 bitmapPixelSize.670 If unsure about parameters, but you have a bitmap that should be671 detected in a screenshot, try obj.oirEngine().adjustParameters().672 Example:673 d.enableVisualLog("params.html")674 screenshot = d.refreshScreenshot()675 results = d.oirEngine().adjustParameters(screenshot, "mybitmap.png")676 if results:677 item, params = results[0]678 print "found %s with parameters:" % (item,)679 print "\n".join(sorted([" %s = %s" % (k, params[k]) for k in params]))680 print "verify:", d.verifyBitmap("mybitmap.png", **params)681 Notice, that you can force refreshScreenshot to load old screenshot:682 d.refreshScreenshot("old.png")683 """684 def __init__(self, *args, **engineDefaults):685 engineDefaults["colorMatch"] = engineDefaults.get("colorMatch", 1.0)686 engineDefaults["opacityLimit"] = engineDefaults.get("opacityLimit", 0.0)687 engineDefaults["area"] = engineDefaults.get("area", (0.0, 0.0, 1.0, 1.0))688 engineDefaults["limit"] = engineDefaults.get("limit", -1)689 engineDefaults["allowOverlap"] = engineDefaults.get("allowOverlap", False)690 engineDefaults["scale"] = engineDefaults.get("scale", 1.0)691 engineDefaults["bitmapPixelSize"] = engineDefaults.get("bitmapPixelSize", 0)692 engineDefaults["screenshotPixelSize"] = engineDefaults.get("screenshotPixelSize", 0)693 OirEngine.__init__(self, *args, **engineDefaults)694 self._openedImages = {}695 self._findBitmapCache = {}696 def _addScreenshot(self, screenshot, **findBitmapDefaults):697 filename = screenshot.filename()698 self._openedImages[filename] = eye4graphics.openImage(filename)699 # make sure size() is available, this can save an extra700 # opening of the screenshot file.701 if screenshot.size(allowReadingFile=False) == None:702 screenshot.setSize(_e4gImageDimensions(self._openedImages[filename]))703 self._findBitmapCache[filename] = {}704 def _removeScreenshot(self, screenshot):705 filename = screenshot.filename()706 eye4graphics.closeImage(self._openedImages[filename])707 del self._openedImages[filename]708 del self._findBitmapCache[filename]709 def adjustParameters(self, screenshot, bitmap,710 scaleRange = [p/100.0 for p in range(110,210,10)],711 colorMatchRange = [p/100.0 for p in range(100,60,-10)],712 pixelSizeRange = range(2,5),713 resultCount = 1,714 **oirArgs):715 """716 Search for scale, colorMatch, bitmapPixelSize and717 screenshotPixelSize parameters that find the bitmap in the718 screenshot....
matcher4fmbt.py
Source:matcher4fmbt.py
...46 mconfig = MatcherWrapper.MatcherConfig(useOCR = True, useOIR = False)47 self._matcher = MatcherWrapper.Matcher(mconfig)48 def _addScreenshot(self, screenshot, **neverMindDefaults):49 self._matcher.loadImage(screenshot.filename())50 def _removeScreenshot(self, screenshot):51 self._matcher.unloadImage(screenshot.filename())52 def _findText(self, screenshot, text, match=1.0, area=(0.0, 0.0, 1.0, 1.0)):53 top, left = fmbtgti._intCoords(area[:2], screenshot.size())54 bottom, right = fmbtgti._intCoords(area[2:], screenshot.size())55 threshold = int(match*100)56 result = self._matcher.match(57 screenshot = screenshot.filename(),58 icon = text,59 threshold = threshold,60 method = "OCR",61 searcharea = _area2searcharea(screenshot, area),62 resultimage = "")63 if result.result[0]/100.0 < match:64 return []...
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!!