Best Python code snippet using fMBT_python
fmbtgti.py
Source:fmbtgti.py  
...2336        """2337        Visualize high contrast regions, write image to given file.2338        Experimental.2339        """2340        items = self.findItemsByHcr(**hcrArgs)2341        eyenfinger.drawBboxes(self.filename(), filename,2342                             [i.bbox() for i in items])2343    def dumpOcr(self, **kwargs):2344        """2345        Return what OCR engine recognizes on this screenshot.2346        Not all OCR engines provide this functionality.2347        """2348        self._notifyOcrEngine()2349        return self._ocrEngine.dumpOcr(self, **kwargs)2350    def dumpOcrWords(self, **kwargs):2351        """2352        Deprecated, use dumpOcr().2353        """2354        return self.dumpOcr(**kwargs)2355    def filename(self):2356        return self._filename2357    def _findFirstMatchingBitmapCandidate(self, bitmap, **oirArgs):2358        for candidate in self._paths.abspaths(bitmap):2359            found = self._oirEngine.findBitmap(self, candidate, **oirArgs)2360            if found:2361                return found2362        return []2363    def findItemsByBitmap(self, bitmap, **oirFindArgs):2364        if self._oirEngine != None:2365            self._notifyOirEngine()2366            oirArgsList = self._paths.oirArgsList(bitmap)2367            results = []2368            if oirArgsList:2369                for oirArgs in oirArgsList:2370                    oirArgs, _ = _takeOirArgs(self._oirEngine, oirArgs.copy())2371                    oirArgs.update(oirFindArgs)2372                    results.extend(self._findFirstMatchingBitmapCandidate(2373                        bitmap, **oirArgs))2374                    if results: break2375            else:2376                oirArgs = oirFindArgs2377                results.extend(self._findFirstMatchingBitmapCandidate(2378                    bitmap, **oirArgs))2379            return results2380        else:2381            raise RuntimeError('Trying to use OIR on "%s" without OIR engine.' % (self.filename(),))2382    def findItemsByDiff(self, image, colorMatch=1.0, limit=1, area=None):2383        """2384        Return list of items that differ in this and the reference images2385        Parameters:2386          image (string):2387                  filename of reference image.2388          colorMatch (optional, float):2389                  required color matching accuracy. The default is 1.02390                  (exact match)2391          limit (optional, integer):2392                  max number of matching items to be returned.2393                  The default is 1.2394        """2395        foundItems = []2396        closeImageA = False2397        closeImageB = False2398        self._notifyOirEngine()2399        try:2400            # Open imageA and imageB for comparison2401            if (self.filename() in getattr(self._oirEngine, "_openedImages", {})):2402                # if possible, use already opened image object2403                imageA = self._oirEngine._openedImages[self.filename()]2404            else:2405                imageA = _e4gOpenImage(self.filename())2406                closeImageA = True2407            imageB = _e4gOpenImage(image)2408            closeImageB = True2409            # Find differing pixels2410            bbox = _Bbox(-1, 0, 0, 0, 0)2411            while limit != 0:2412                found = eye4graphics.findNextDiff(2413                    ctypes.byref(bbox),2414                    ctypes.c_void_p(imageA),2415                    ctypes.c_void_p(imageB),2416                    ctypes.c_double(colorMatch),2417                    ctypes.c_double(1.0), # opacityLimit2418                    None, # searchAreaA2419                    None, # searchAreaB2420                    1)2421                if found != 1:2422                    break2423                rgbDiff = (bbox.error >> 16 & 0xff,2424                           bbox.error >> 8 & 0xff,2425                           bbox.error & 0xff)2426                foundItems.append(2427                    GUIItem("DIFF %s" %2428                            (rgbDiff,),2429                            (bbox.left, bbox.top, bbox.right, bbox.bottom),2430                            self))2431                limit -= 12432        finally:2433            if closeImageA:2434                eye4graphics.closeImage(imageA)2435            if closeImageB:2436                eye4graphics.closeImage(imageB)2437        return foundItems2438    def findItemsByColor(self, rgb888, colorMatch=1.0, limit=1, area=None,2439                         invertMatch=False, group=""):2440        """2441        Return list of items that match given color.2442        Parameters:2443          rgb888 (integer triplet (red, green, blue)):2444                  color to be searched for.2445          colorMatch (optional, float):2446                  required color matching accuracy. The default is 1.02447                  (exact match).2448          limit (optional, integer):2449                  max number of matching items to be returned.2450                  The default is 1.2451          area ((left, top, right, bottom), optional):2452                  subregion in the screenshot from which items will be2453                  searched for. The default is (0.0, 0.0, 1.0, 1.0), that2454                  is whole screen.2455          invertMatch (optional, boolean):2456                  if True, search for items *not* matching the color.2457                  The default is False.2458          group (optional, string):2459                  group matching pixels to large items. Accepted2460                  values are "adjacent" (group pixels that are next to2461                  each other) and "" (no grouping). The default is "".2462        """2463        self._notifyOirEngine()2464        if (self.filename() in getattr(self._oirEngine, "_openedImages", {})):2465            # if possible, use already opened image object2466            image = self._oirEngine._openedImages[self.filename()]2467            closeImage = False2468        else:2469            image = _e4gOpenImage(self.filename())2470            closeImage = True2471        bbox = _Bbox(-1, 0, 0, 0, 0)2472        color = _Rgb888(*rgb888)2473        ssSize = self.size()2474        if area == None:2475            area = (0.0, 0.0, 1.0, 1.0)2476        leftTopRightBottomZero = (_intCoords((area[0], area[1]), ssSize) +2477                                  _intCoords((area[2], area[3]), ssSize) +2478                                  (0,))2479        areaBbox = _Bbox(*leftTopRightBottomZero)2480        foundItems = []2481        coord2item = {} # (x, y) -> viewItem2482        try:2483            while limit != 0:2484                found = eye4graphics.findNextColor(2485                    ctypes.byref(bbox),2486                    ctypes.c_void_p(image),2487                    ctypes.byref(color),2488                    ctypes.c_double(colorMatch),2489                    ctypes.c_double(1.0), # opacityLimit2490                    ctypes.c_int(invertMatch),2491                    ctypes.byref(areaBbox))2492                if found != 1:2493                    break2494                foundColor = int(bbox.error)2495                foundRgb = (foundColor >> 16 & 0xff,2496                            foundColor >> 8 & 0xff,2497                            foundColor & 0xff)2498                if invertMatch:2499                    comp = "!="2500                else:2501                    comp = "=="2502                if not group:2503                    foundItems.append(2504                        GUIItem("RGB#%.2x%.2x%.2x%s%.2x%.2x%.2x (%s)" %2505                                (rgb888 + (comp,) + foundRgb + (colorMatch,)),2506                                (bbox.left, bbox.top, bbox.right, bbox.bottom),2507                                self))2508                    limit -= 12509                elif group == "adjacent":2510                    x = bbox.left2511                    y = bbox.top2512                    itemA = None2513                    itemB = None2514                    if (x-1, y) in coord2item:2515                        # AAAx2516                        itemA = coord2item[(x-1, y)]2517                        if itemA._bbox[2] < x: # right < x2518                            itemA._bbox = (itemA._bbox[0], itemA._bbox[1], x, itemA._bbox[3])2519                        coord2item[(x, y)] = itemA2520                    if (x, y-1) in coord2item:2521                        # BBB2522                        # x2523                        itemB = coord2item[(x, y-1)]2524                        if itemB._bbox[3] < y: # bottom < y2525                            itemB._bbox = (itemB._bbox[0], itemB._bbox[1], itemB._bbox[2], y)2526                        coord2item[(x, y)] = itemB2527                        if itemA:2528                            #    BBB2529                            # AAAx2530                            if itemB != itemA:2531                                itemB._bbox = (min(itemA._bbox[0], itemB._bbox[0]),2532                                               min(itemA._bbox[1], itemB._bbox[1]),2533                                               max(itemA._bbox[2], itemB._bbox[2]),2534                                               max(itemA._bbox[3], itemB._bbox[3]))2535                                for ax in xrange(itemA._bbox[0], itemA._bbox[2]+1):2536                                    for ay in xrange(itemA._bbox[1], itemA._bbox[3]+1):2537                                        if coord2item.get((ax, ay), None) == itemA:2538                                            coord2item[(ax, ay)] = itemB2539                                foundItems.remove(itemA)2540                                limit += 12541                    if not itemA and not itemB:2542                        itemA = GUIItem("RGB#%.2x%.2x%.2x%s%.2x%.2x%.2x (%s)" %2543                                (rgb888 + (comp,) + foundRgb + (colorMatch,)),2544                                (bbox.left, bbox.top, bbox.right, bbox.bottom),2545                                self)2546                        limit -= 12547                        foundItems.append(itemA)2548                        coord2item[(x, y)] = itemA2549        finally:2550            if closeImage:2551                eye4graphics.closeImage(image)2552        return foundItems2553    def findItemsByOcr(self, text, **ocrEngineArgs):2554        if self._ocrEngine != None:2555            self._notifyOcrEngine()2556            return self._ocrEngine.findText(self, text, **ocrEngineArgs)2557        else:2558            raise RuntimeError('Trying to use OCR on "%s" without OCR engine.' % (self.filename(),))2559    def findItemsByHcr(self, xRes=24, yRes=24, threshold=0.1):2560        """2561        Return "high contrast regions" in the screenshot.2562        Experimental. See if it finds regions that could be2563        interacted with.2564        """2565        ppFilename = "%s-hcrpp.png" % (self.filename(),)2566        _convert(self.filename(),2567                 ["-colorspace", "gray", "-depth", "3"],2568                 ppFilename)2569        bbox = _Bbox(0, 0, 0, 0, 0)2570        foundItems = []2571        try:2572            image = _e4gOpenImage(ppFilename)2573            while True:...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!!
