Best Python code snippet using fMBT_python
eyenfinger.py
Source:eyenfinger.py
...41'42setPreprocessFilter() sets given filter to be used when reading text from images.43Debugging44---------45iClickWord() capture parameter visualises coordinates to be clicked. Example:46python -c '47from eyenfinger import *48setPreprocessFilter("-sharpen 5 -filter Mitchell -resize 1600x -level 40%,50%,3.0")49iRead(source="screenshot.png")50iClickWord("[initial", clickPos=(-2,3), capture="highlight.png", dryRun=True)51'52"""53import distutils.sysconfig54import time55import subprocess56import re57import math58import htmlentitydefs59import sys60import os61import tempfile62import atexit63import shlex64import shutil65import ctypes66import platform67import struct68import warnings69import fmbt_config70def _DEPRECATED():71 warnings.warn("eyenfinger.py API is deprecated, use fmbtx11 instead.",72 DeprecationWarning, stacklevel=2)73_g_preprocess = "-sharpen 5 -filter Mitchell -resize 1920x1600 -level 40%%,70%%,5.0 -sharpen 5"74_g_readImage = None75_g_origImage = None76_g_hocr = ""77_g_words = None78_g_lastWindow = None79_g_defaultClickDryRun = False80_g_defaultDelayedDrawing = False81_g_defaultIconMatch = 1.082_g_defaultIconColorMatch = 1.083_g_defaultIconOpacityLimit = 0.084_g_defaultInputKeyDevice = None85_g_defaultReadWithOCR = True86# windowsOffsets maps window-id to (x, y) pair.87_g_windowOffsets = {None: (0,0)}88# windowsSizes maps window-id to (width, height) pair.89_g_windowSizes = {None: (0,0)}90# screenSize is a (width, height) pair.91_g_screenSize = (0, 0)92_g_tempdir = tempfile.mkdtemp(prefix="eyenfinger.%s." % (os.getpid(),))93SCREENSHOT_FILENAME = _g_tempdir + "/screenshot.png"94LOG_FILENAME = _g_tempdir + "/eyenfinger.log"95MOUSEEVENT_MOVE, MOUSEEVENT_CLICK, MOUSEEVENT_DOWN, MOUSEEVENT_UP = range(4)96# Xkeys contains key names known to X11, see keysymdef.h.97Xkeys = [98 "BackSpace", "Tab", "Linefeed", "Clear", "Return", "Pause",99 "Scroll_Lock", "Sys_Req", "Escape", "Delete", "Home", "Left",100 "Up", "Right", "Down", "Prior", "Page_Up", "Next", "Page_Down",101 "End", "Begin", "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8",102 "F9", "F10", "F11", "F12", "Shift_L", "Shift_R", "Control_L",103 "Control_R", "Caps_Lock", "Shift_Lock", "Meta_L", "Meta_R",104 "Alt_L", "Alt_R", "space", "exclam", "quotedbl", "numbersign",105 "dollar", "percent", "ampersand", "apostrophe", "quoteright",106 "parenleft", "parenright", "asterisk", "plus", "comma", "minus",107 "period", "slash", "0", "1", "2", "3", "4", "5", "6", "7", "8",108 "9", "colon", "semicolon", "less", "equal", "greater", "question",109 "at", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L",110 "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y",111 "Z", "bracketleft", "backslash", "bracketright", "asciicircum",112 "underscore", "grave", "quoteleft", "a", "b", "c", "d", "e", "f",113 "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",114 "t", "u", "v", "w", "x", "y", "z", "braceleft", "bar",115 "braceright"]116# InputKeys contains key names known to input devices, see117# linux/input.h or http://www.usb.org/developers/hidpage. The order is118# significant, because keyCode = InputKeys.index(keyName).119InputKeys = [120 "RESERVED", "ESC","1", "2", "3", "4", "5", "6", "7", "8", "9", "0",121 "MINUS", "EQUAL", "BACKSPACE", "TAB",122 "Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P",123 "LEFTBRACE", "RIGHTBRACE", "ENTER", "LEFTCTRL",124 "A", "S", "D", "F", "G", "H", "J", "K", "L",125 "SEMICOLON", "APOSTROPHE", "GRAVE", "LEFTSHIFT", "BACKSLASH",126 "Z", "X", "C", "V", "B", "N", "M",127 "COMMA", "DOT", "SLASH", "RIGHTSHIFT", "KPASTERISK", "LEFTALT",128 "SPACE", "CAPSLOCK",129 "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10",130 "NUMLOCK", "SCROLLLOCK",131 "KP7", "KP8", "KP9", "KPMINUS",132 "KP4", "KP5", "KP6", "KPPLUS",133 "KP1", "KP2", "KP3", "KP0", "KPDOT",134 "undefined0",135 "ZENKAKUHANKAKU", "102ND", "F11", "F12", "RO",136 "KATAKANA", "HIRAGANA", "HENKAN", "KATAKANAHIRAGANA", "MUHENKAN",137 "KPJPCOMMA", "KPENTER", "RIGHTCTRL", "KPSLASH", "SYSRQ", "RIGHTALT",138 "LINEFEED", "HOME", "UP", "PAGEUP", "LEFT", "RIGHT", "END", "DOWN",139 "PAGEDOWN", "INSERT", "DELETE", "MACRO",140 "MUTE", "VOLUMEDOWN", "VOLUMEUP",141 "POWER",142 "KPEQUAL", "KPPLUSMINUS", "PAUSE", "SCALE", "KPCOMMA", "HANGEUL",143 "HANGUEL", "HANJA", "YEN", "LEFTMETA", "RIGHTMETA", "COMPOSE"]144_inputKeyShorthands = {145 "-": "MINUS", "=": "EQUAL",146 "[": "LEFTBRACE", "]": "RIGHTBRACE", "\n": "ENTER",147 ";": "SEMICOLON",148 ",": "COMMA", ".": "DOT", "/": "SLASH",149 " ": "SPACE" }150class EyenfingerError(Exception):151 pass152class BadMatch (EyenfingerError):153 pass154class BadWindowName (EyenfingerError):155 pass156class BadSourceImage(EyenfingerError):157 pass158class BadIconImage(EyenfingerError):159 pass160class NoOCRResults(EyenfingerError):161 pass162try:163 import fmbt164 def _log(msg):165 fmbt.adapterlog("eyenfinger: %s" % (msg,))166except ImportError:167 def _log(msg):168 file(LOG_FILENAME, "a").write("%13.2f %s\n" %169 (time.time(), msg))170try:171 _libpath = ["", ".",172 os.path.dirname(os.path.abspath(__file__)),173 distutils.sysconfig.get_python_lib(plat_specific=1)]174 _suffix = ".so"175 if os.name == "nt":176 _suffix = ".dll"177 for _dirname in _libpath:178 try:179 eye4graphics = ctypes.CDLL(os.path.join(_dirname , "eye4graphics"+_suffix))180 break181 except: pass182 else:183 raise ImportError("%s cannot load eye4graphics%s" % (__file__, _suffix))184 class Bbox(ctypes.Structure):185 _fields_ = [("left", ctypes.c_int32),186 ("top", ctypes.c_int32),187 ("right", ctypes.c_int32),188 ("bottom", ctypes.c_int32),189 ("error", ctypes.c_int32)]190except Exception, e:191 Bbox = None192 eye4graphics = None193 _log('Loading icon recognition library failed: "%s".' % (e,))194# See struct input_event in /usr/include/linux/input.h195if platform.architecture()[0] == "32bit":196 _InputEventStructSpec = 'IIHHi'197else:198 _InputEventStructSpec = 'QQHHi'199# Event and keycodes are in input.h, too.200_EV_KEY = 0x01201# _inputKeyNameCodeMap is a dictionary keyName -> keyCode202_inputKeyNameCodeMap = {}203for code, name in enumerate(InputKeys):204 _inputKeyNameCodeMap[name] = code205def _inputKeyNameToCode(keyName):206 if keyName in _inputKeyNameCodeMap:207 return _inputKeyNameCodeMap[keyName]208 elif keyName in _inputKeyShorthands:209 return _inputKeyNameCodeMap[_inputKeyShorthands[keyName]]210 else:211 raise ValueError('Invalid key name "%s"' % (keyName,))212def error(msg, exitstatus=1):213 sys.stderr.write("eyenfinger: %s\n" % (msg,))214 sys.exit(1)215def printEventsFromFile(filename):216 fd = os.open(filename, os.O_RDONLY)217 try:218 while 1:219 evString = os.read(fd, struct.calcsize(_InputEventStructSpec))220 if not evString: break221 tim, tus, typ, cod, val = struct.unpack(_InputEventStructSpec, evString)222 if cod < len(InputKeys):223 nam = InputKeys[cod]224 else:225 nam = "N/A"226 print "time: %8s, susc: %8s, type: %8s, keyCode: %5s name: %10s value: %8s" % \227 (tim, tus, typ, cod, nam, val)228 finally:229 os.close(fd)230def printEventsFromDevice(deviceName):231 devices = dict(_listInputDevices())232 if not deviceName in devices:233 error('Unknown device "%s". Available devices: %s' %234 (deviceName, sorted(devices.keys())))235 else:236 printEventsFromFile(devices[deviceName])237def _exitHandler():238 shutil.rmtree(_g_tempdir, ignore_errors=True)239atexit.register(_exitHandler)240def _runcmd(cmd):241 global _g_last_runcmd_error242 p = subprocess.Popen(cmd, shell=isinstance(cmd, basestring),243 stdout=subprocess.PIPE, stderr=subprocess.PIPE)244 output = p.stdout.read()245 exit_status = p.wait()246 _g_last_runcmd_error = p.stderr.read()247 if exit_status != 0:248 _log("runcmd: %s" % (cmd,))249 _log("exit status: " + str(exit_status))250 _log("stdout: " + output)251 _log("stderr: " + _g_last_runcmd_error)252 else:253 p.stderr.read()254 return exit_status, output255def _runDrawCmd(inputfilename, cmd, outputfilename):256 if not _g_defaultDelayedDrawing:257 return _runcmd([fmbt_config.imagemagick_convert,258 inputfilename] + cmd + [outputfilename])259 # Do delayed drawing to save test execution time. If the output260 # file does not exist, just copy inputfile to outputfile and start261 # logging delayed draw commands to262 # outputfile.delayeddraw. Otherwise append latest command to263 # outputfile.delayeddraw.264 delayedCmd = '%s "%s" "%s" "%s"\n' % (265 fmbt_config.imagemagick_convert,266 outputfilename, '%s' % ('" "'.join(cmd)), outputfilename)267 delayedDrawFilename = outputfilename + ".delayeddraw"268 try:269 if os.access(outputfilename, os.R_OK) == False:270 shutil.copy(inputfilename, outputfilename)271 file(delayedDrawFilename, "w").write(delayedCmd)272 else:273 file(delayedDrawFilename, "a").write(delayedCmd)274 except:275 _log("error on delayed drawing: %s" % (delayedCmd,))276 raise277 _log("delayed drawing: %s" % (delayedCmd,))278 return (0, "")279def _safeForShell(s):280 # convert all non-ascii and bad chars to _281 try: s = unicode(s, "utf-8")282 except: pass283 return ''.join([(c, "_")[ord(c)>128 or c in "'\"\\`"] for c in s])284def _coordsToInt((x,y), (width, height)=(None, None)):285 """286 Convert percentages to screen coordinates287 """288 if (width == None or height == None):289 width, height = screenSize()290 if 0.0 <= x <= 1.0 and type(x) == float:291 x = int(round(x * width))292 else:293 x = int(x)294 if 0.0 <= y <= 1.0 and type(y) == float:295 y = int(round(y * height))296 else:297 y = int(y)298 return (x, y)299def setPreprocessFilter(preprocess):300 global _g_preprocess301 _g_preprocess = preprocess302def iSetDefaultClickDryRun(dryRun):303 """304 Set the default value for optional dryRun parameter for iClick*305 functions.306 """307 global _g_defaultClickDryRun308 _g_defaultClickDryRun = dryRun309def iSetDefaultDelayedDrawing(delayedDrawing):310 """311 Set the default for delaying drawing operations on captured312 screenshots.313 If delayedDrawing == False, drawing actions on screenshots (like314 highlighting icon and clicked coordinates) takes place during the315 function execution (like iClickIcon).316 If delayedDrawing == True, the screenshot is saved without317 highlighted areas, and <screenshot filename>.delayeddraw file318 contains all draw commands that can be executed after the test319 run. This may save a lot test execution time and CPU on the device320 that runs eyenfinger.321 The default is False.322 """323 global _g_defaultDelayedDrawing324 _g_defaultDelayedDrawing = delayedDrawing325def iSetDefaultIconMatch(match):326 """327 Set the default icon matching value, ranging from 0 to 1. The328 value will be used in iClickIcon and iVerifyIcon, if the optional329 match parameter is omitted. Value 1.0 will use pixel-perfect330 matching (the default), values below 1.0 will use fuzzy matching.331 Fuzzy matching is EXPERIMENTAL.332 """333 global _g_defaultIconMatch334 _g_defaultIconMatch = match335def iSetDefaultIconColorMatch(colorMatch):336 """337 Set the default color matching value, ranging from 0 to 1. When338 using pixel-perfect matching this will allow given error in pixel339 colors.340 For instance, when comparing 24 bit RGB images, value 0.97 will341 allow 256 - int(256 * .97) = 8 difference on each color channel.342 """343 global _g_defaultIconColorMatch344 _g_defaultIconColorMatch = colorMatch345def iSetDefaultIconOpacityLimit(opacityLimit):346 """347 Set the default minimum opacity for pixels to be matched. Defaults348 to 0.0, all pixels are matched independently of their opacity.349 """350 global _g_defaultIconOpacityLimit351 _g_defaultIconOpacityLimit = opacityLimit352def iSetDefaultInputKeyDevice(deviceName):353 """354 Use deviceName as a default input device for iInputKey.355 iSetDefaultInputKeyDevice("/dev/input/event0")356 iInputKey(["enter"])357 """358 global _g_defaultInputKeyDevice359 _g_defaultInputKeyDevice = deviceName360def iSetDefaultReadWithOCR(ocr):361 """362 Set the default for using OCR when reading images or windows.363 """364 global _g_defaultReadWithOCR365 _g_defaultReadWithOCR = ocr366def screenSize():367 """368 Returns the size of the screen as a pair (width, height).369 """370 if _g_screenSize == (0, 0):371 _getScreenSize()372 return _g_screenSize373def windowSize():374 """375 Returns the size of the window as a pair (width, height).376 Choose a window first, for instance with iRead() or iUseWindow().377 """378 if _g_lastWindow == None:379 raise BadWindowName("undefined window")380 return _g_windowSizes[_g_lastWindow]381def windowXY():382 """383 Returns screen coordinates of the top-left corner of the window as384 a pair (x, y).385 Choose a window first, for instance with iRead() or iUseWindow().386 """387 if _g_lastWindow == None:388 raise BadWindowName("undefined window")389 return _g_windowOffsets[_g_lastWindow]390def imageSize(imageFilename):391 """392 Returns image size as pair (width, height).393 """394 struct_bbox = Bbox(0,0,0,0,0)395 err = eye4graphics.imageDimensions(ctypes.byref(struct_bbox),396 imageFilename)397 if err != 0:398 return None, None399 return struct_bbox.right, struct_bbox.bottom400def iRead(windowId = None, source = None, preprocess = None, ocr=None, capture=None, ocrArea=(0, 0, 1.0, 1.0), ocrPageSegModes=(3,), lang="eng", configfile=None):401 """402 DEPRECATED - use fmbtx11.Screen.refreshScreenshot instead.403 Read the contents of the given window or other source. If neither404 of windowId or source is given, reads the contents of active405 window. iClickWord and iVerifyWord can be used after reading with406 OCR.407 Parameters:408 windowId id (0x....) or the title of the window to be read.409 Defaults to None.410 source name of the file to be read, for instance a screen411 capture. Defaults to None.412 preprocess preprocess specification to override the default413 that is set using setPreprocessFilter. Defaults414 to None. Set to "" to disable preprocessing before415 OCR.416 ocr words will be read using OCR if True417 (the default). Read object can be used with418 iClickIcon and iVerifyIcon without OCR, too.419 capture save image with read words highlighted to this420 file. Default: None (nothing is saved).421 ocrArea (top, left, right, bottom) coordinates -422 area of the image to be read with OCR.423 ocrPageSegModes424 tuple of integers, see tesseract -pagesegmodes425 lang Tesseract language setting, the default is "eng".426 Refer to LANGUAGES in Tesseract documentation or427 man page.428 configfile Tesseract configuration file.429 Returns list of words detected by OCR from the read object.430 """431 global _g_hocr432 global _g_lastWindow433 global _g_words434 global _g_readImage435 global _g_origImage436 _g_words = None437 _g_readImage = None438 _g_origImage = None439 if ocr == None:440 ocr = _g_defaultReadWithOCR441 if not source:442 iUseWindow(windowId)443 # take a screenshot444 import fmbtx11445 fmbtx11.Screen().refreshScreenshot().save(SCREENSHOT_FILENAME + ".png")446 _runcmd("%s %s.png -crop %sx%s+%s+%s +repage '%s'" %447 (fmbt_config.imagemagick_convert, SCREENSHOT_FILENAME,448 _g_windowSizes[_g_lastWindow][0], _g_windowSizes[_g_lastWindow][1],449 _g_windowOffsets[_g_lastWindow][0], _g_windowOffsets[_g_lastWindow][1],450 SCREENSHOT_FILENAME))451 source = SCREENSHOT_FILENAME452 else:453 iUseImageAsWindow(source)454 _g_origImage = source455 orig_width, orig_height = _g_windowSizes[_g_lastWindow][0], _g_windowSizes[_g_lastWindow][1]456 x1, y1 = _coordsToInt(ocrArea[:2], (orig_width, orig_height))457 x2, y2 = _coordsToInt(ocrArea[2:], (orig_width, orig_height))458 if x2 <= x1 or y2 <= y1:459 raise EyenfingerError("Invalid area size: %s => %s" % (ocrArea, (x1, y1, x2, y2)))460 if orig_width <= 0 or orig_height <= 0:461 raise EyenfingerError("Invalid image size: %sx%s" % (orig_width, orig_height))462 if not ocr:463 if capture:464 drawWords(_g_origImage, capture, [], [])465 return []466 if preprocess == None:467 preprocess = _g_preprocess468 # convert to text469 _g_readImage = _g_origImage + "-pp.png"470 if ocrArea == (0, 0, 1.0, 1.0):471 croparea = []472 wordXOffset = 0473 wordYOffset = 0474 else:475 croparea = ["-crop", "%sx%s+%s+%s" % (x2-x1, y2-y1, x1, y1), "+repage"]476 wordXOffset = x1477 wordYOffset = y1478 # rescale possible resize preprocessing parameter479 resize_m = re.search('-resize ([0-9]+)x([0-9]*)', preprocess)480 if resize_m:481 origXResize = int(resize_m.group(1))482 newXResize = int(origXResize/float(orig_width) * (x2-x1))483 preprocess = (preprocess[:resize_m.start()] +484 ("-resize %sx" % (newXResize,)) +485 preprocess[resize_m.end():])486 _g_words = {}487 for psm in ocrPageSegModes:488 convert_cmd = ([fmbt_config.imagemagick_convert, _g_origImage] +489 croparea +490 shlex.split(preprocess) +491 [_g_readImage])492 tesseract_cmd = ["tesseract", _g_readImage, SCREENSHOT_FILENAME,493 "-l", lang, "-psm", str(psm), "hocr"]494 if isinstance(configfile, basestring):495 tesseract_cmd += [configfile]496 elif isinstance(configfile, list) or isinstance(configfile, tuple):497 tesseract_cmd += configfile498 exit_status, output = _runcmd(convert_cmd)499 if exit_status != 0:500 raise NoOCRResults("Convert returned exit status (%s): %s"501 % (exit_status, _g_last_runcmd_error))502 exit_status, output = _runcmd(tesseract_cmd)503 if exit_status != 0:504 raise NoOCRResults("Tesseract returned exit status (%s): %s"505 % (exit_status, _g_last_runcmd_error))506 hocr_filename = SCREENSHOT_FILENAME + ".html" # Tesseract 3.02507 if not os.access(hocr_filename, os.R_OK):508 hocr_filename = SCREENSHOT_FILENAME + ".hocr" # Tesseract 3.03509 if not os.access(hocr_filename, os.R_OK):510 raise NoOCRResults("HOCR output missing. Tesseract OCR 3.02 or greater required.\n")511 # store every word and its coordinates512 _g_words.update(_hocr2words(file(hocr_filename).read()))513 # convert word coordinates to the unscaled pixmap514 try:515 ocr_page_line = [line for line in file(hocr_filename).readlines() if "class='ocr_page'" in line][0]516 except IndexError:517 raise NoOCRResults("Could not read ocr_page class information from %s" % (hocr_filename,))518 scaled_width, scaled_height = re.findall('bbox 0 0 ([0-9]+)\s*([0-9]+)', ocr_page_line)[0]519 scaled_width, scaled_height = float(scaled_width) / (float(x2-x1)/orig_width), float(scaled_height) / (float(y2-y1)/orig_height)520 for word in sorted(_g_words.keys()):521 for appearance, (wordid, middle, bbox) in enumerate(_g_words[word]):522 _g_words[word][appearance] = \523 (wordid,524 (int(middle[0]/scaled_width * orig_width) + wordXOffset,525 int(middle[1]/scaled_height * orig_height) + wordYOffset),526 (int(bbox[0]/scaled_width * orig_width) + wordXOffset,527 int(bbox[1]/scaled_height * orig_height) + wordYOffset,528 int(bbox[2]/scaled_width * orig_width) + wordXOffset,529 int(bbox[3]/scaled_height * orig_height) + wordYOffset))530 _log('found "' + word + '": (' + str(bbox[0]) + ', ' + str(bbox[1]) + ')')531 if capture:532 drawWords(_g_origImage, capture, _g_words, _g_words)533 return sorted(_g_words.keys())534def iVerifyWord(word, match=0.33, appearance=1, capture=None):535 """536 DEPRECATED - use fmbtx11.Screen.verifyOcrText instead.537 Verify that word can be found from previously iRead() image.538 Parameters:539 word word that should be checked540 appearance if word appears many times, appearance to541 be clicked. Defaults to the first one.542 match minimum matching score543 capture save image with verified word highlighted544 to this file. Default: None (nothing is saved).545 Returns pair: ((score, matchingWord), (left, top, right, bottom)), where546 score score of found match (1.0 for perfect match)547 matchingWord corresponding word detected by OCR548 (left, top, right, bottom)549 bounding box of the word in read image550 Throws BadMatch error if word is not found.551 Throws NoOCRResults error if there are OCR results available552 on the current screen.553 """554 if _g_words == None:555 raise NoOCRResults('iRead has not been called with ocr=True')556 score, matching_word = findWord(word)557 if capture:558 drawWords(_g_origImage, capture, [word], _g_words)559 if score < match:560 raise BadMatch('No matching word for "%s". The best candidate "%s" with score %.2f, required %.2f' %561 (word, matching_word, score, match))562 return ((score, matching_word), _g_words[matching_word][appearance-1][2])563def iVerifyText(text, match=0.33, capture=None):564 """565 DEPRECATED - use fmbtx11.Screen.verifyOcrText instead.566 Verify that text can be found from previously iRead() image.567 Parameters:568 text multiple words that should be checked569 match minimum matching score570 capture save image with verified text highlighted571 to this file. Default: None (nothing is saved).572 Returns pair:573 ((score, matchingText), (left, top, right, bottom)), where574 score score of found match (1.0 for perfect match)575 matchingText corresponding text detected by OCR576 (left, top, right, bottom)577 bounding box of the text in read image578 Throws BadMatch error if text is not found.579 Throws NoOCRResults error if there are OCR results available580 on the current screen.581 """582 if _g_words == None:583 raise NoOCRResults('iRead has not been called with ocr=True')584 score_text_bbox_list = findText(text, match)585 if len(score_text_bbox_list) == 0:586 raise BadMatch('No match >= %s for text "%s"' % (score, text))587 score, text, bbox = score_text_box_list[0]588 if capture:589 drawBbox(_g_origImage, capture, bbox, "%.2f %s" % (score, text))590 return ((score, matching_text), bbox)591def iVerifyIcon(iconFilename, match=None, colorMatch=None, opacityLimit=None, capture=None, area=(0.0, 0.0, 1.0, 1.0), _origin="iVerifyIcon"):592 """593 DEPRECATED - use fmbtx11.Screen.verifyBitmap instead.594 Verify that icon can be found from previously iRead() image.595 Parameters:596 iconFilename name of the icon file to be searched for597 match minimum matching score between 0 and 1.0,598 1.0 is perfect match (default)599 colorMatch 1.0 (default) requires exact color match. Value600 below 1.0 defines maximum allowed color601 difference. See iSetDefaultIconColorMatch.602 opacityLimit 0.0 (default) requires exact color values603 independently of opacity. If lower than 1.0,604 pixel less opaque than given value are skipped605 in pixel perfect comparisons.606 capture save image with verified icon highlighted607 to this file. Default: None (nothing is saved).608 area rectangle (left, top, right, bottom). Search609 icon inside this rectangle only. Values can be610 absolute coordinates, or floats in range [0.0,611 1.0] that will be scaled to image dimensions.612 The default is (0.0, 0.0, 1.0, 1.0), that is613 full rectangle.614 Returns pair: (score, (left, top, right, bottom)), where615 score score of found match (1.0 for perfect match)616 (left, top, right, bottom)617 bounding box of found icon618 Throws BadMatch error if icon is not found.619 """620 if not eye4graphics:621 _log('ERROR: %s("%s") called, but eye4graphics not loaded.' % (_origin, iconFilename))622 raise EyenfingerError("eye4graphics not available")623 if not _g_origImage:624 _log('ERROR %s("%s") called, but source not defined (iRead not called).' % (_origin, iconFilename))625 raise BadSourceImage("Source image not defined, cannot search for an icon.")626 if not (os.path.isfile(iconFilename) and os.access(iconFilename, os.R_OK)):627 _log('ERROR %s("%s") called, but the icon file is not readable.' % (_origin, iconFilename))628 raise BadIconImage('Icon "%s" is not readable.' % (iconFilename,))629 if match == None:630 match = _g_defaultIconMatch631 if match > 1.0:632 _log('ERROR %s("%s"): invalid match value, must be below 1.0. ' % (_origin, iconFilename,))633 raise ValueError("invalid match value: %s, should be 0 <= match <= 1.0" % (match,))634 if colorMatch == None:635 colorMatch = _g_defaultIconColorMatch636 if not 0.0 <= colorMatch <= 1.0:637 _log('ERROR %s("%s"): invalid colorMatch value, must be between 0 and 1. ' % (_origin, iconFilename,))638 raise ValueError("invalid colorMatch value: %s, should be 0 <= colorMatch <= 1.0" % (colorMatch,))639 if opacityLimit == None:640 opacityLimit = _g_defaultIconOpacityLimit641 if not 0.0 <= opacityLimit <= 1.0:642 _log('ERROR %s("%s"): invalid opacityLimit value, must be between 0 and 1. ' % (_origin, iconFilename,))643 raise ValueError("invalid opacityLimit value: %s, should be 0 <= opacityLimit <= 1.0" % (opacityLimit,))644 if area[0] > area[2] or area[1] >= area[3]:645 raise ValueError("invalid area: %s, should be rectangle (left, top, right, bottom)" % (area,))646 leftTopRightBottomZero = (_coordsToInt((area[0], area[1]), windowSize()) +647 _coordsToInt((area[2], area[3]), windowSize()) +648 (0,))649 struct_area_bbox = Bbox(*leftTopRightBottomZero)650 struct_bbox = Bbox(0,0,0,0,0)651 threshold = int((1.0-match)*20)652 err = eye4graphics.findSingleIcon(ctypes.byref(struct_bbox),653 _g_origImage, iconFilename, threshold,654 ctypes.c_double(colorMatch),655 ctypes.c_double(opacityLimit),656 ctypes.byref(struct_area_bbox))657 bbox = (int(struct_bbox.left), int(struct_bbox.top),658 int(struct_bbox.right), int(struct_bbox.bottom))659 if err == -1 or err == -2:660 msg = '%s: "%s" not found, match=%.2f, threshold=%s, closest threshold %s.' % (661 _origin, iconFilename, match, threshold, int(struct_bbox.error))662 if capture:663 drawIcon(_g_origImage, capture, iconFilename, bbox, 'red')664 _log(msg)665 raise BadMatch(msg)666 elif err != 0:667 _log("%s: findSingleIcon returned %s" % (_origin, err,))668 raise BadMatch("%s not found, findSingleIcon returned %s." % (iconFilename, err))669 if threshold > 0:670 score = (threshold - int(struct_bbox.error)) / float(threshold)671 else:672 score = 1.0673 if capture:674 drawIcon(_g_origImage, capture, iconFilename, bbox, area=leftTopRightBottomZero[:4])675 return (score, bbox)676def iClickIcon(iconFilename, clickPos=(0.5,0.5), match=None,677 colorMatch=None, opacityLimit=None,678 mouseButton=1, mouseEvent=MOUSEEVENT_CLICK, dryRun=None, capture=None):679 """680 DEPRECATED - use fmbtx11.Screen.tapBitmap instead.681 Click coordinates relative to the given icon in previously iRead() image.682 Parameters:683 iconFilename read icon from this file684 clickPos position to be clicked,685 relative to word top-left corner of the bounding686 box around the word. X and Y units are relative687 to width and height of the box. (0,0) is the688 top-left corner, (1,1) is bottom-right corner,689 (0.5, 0.5) is the middle point (default).690 Values below 0 or greater than 1 click outside691 the bounding box.692 match 1.0 (default) requires exact match. Value below 1.0693 defines minimum required score for fuzzy matching694 (EXPERIMENTAL). See iSetDefaultIconMatch.695 colorMatch 1.0 (default) requires exact color match. Value696 below 1.0 defines maximum allowed color697 difference. See iSetDefaultIconColorMatch.698 opacityLimit 0.0 (default) requires exact color values699 independently of opacity. If lower than 1.0,700 pixel less opaque than given value are skipped701 in pixel perfect comparisons.702 mouseButton mouse button to be synthesized on the event, default is 1.703 mouseEvent event to be synthesized, the default is MOUSEEVENT_CLICK,704 others: MOUSEEVENT_MOVE, MOUSEEVENT_DOWN, MOUSEEVENT_UP.705 dryRun if True, does not synthesize events. Still returns706 coordinates of the clicked position and illustrates707 the clicked position on the capture image if708 given.709 capture name of file where image of highlighted icon and710 clicked point are saved.711 Returns pair (score, (clickedX, clickedY)), where712 score score of found match (1.0 for perfect match)713 (clickedX, clickedY)714 X and Y coordinates of clicked position on the715 screen.716 Throws BadMatch error if could not find a matching word.717 """718 _DEPRECATED()719 score, bbox = iVerifyIcon(iconFilename, match=match,720 colorMatch=colorMatch, opacityLimit=opacityLimit,721 capture=capture, _origin="iClickIcon")722 clickedXY = iClickBox(bbox, clickPos, mouseButton, mouseEvent, dryRun,723 capture, _captureText = iconFilename)724 return (score, clickedXY)725def iClickWord(word, appearance=1, clickPos=(0.5,0.5), match=0.33,726 mouseButton=1, mouseEvent=1, dryRun=None, capture=None):727 """728 DEPRECATED - use fmbtx11.Screen.tapOcrText instead.729 Click coordinates relative to the given word in previously iRead() image.730 Parameters:731 word word that should be clicked732 appearance if word appears many times, appearance to733 be clicked. Defaults to the first one.734 clickPos position to be clicked,735 relative to word top-left corner of the bounding736 box around the word. X and Y units are relative737 to width and height of the box. (0,0) is the738 top-left corner, (1,1) is bottom-right corner,739 (0.5, 0.5) is the middle point (default).740 Values below 0 or greater than 1 click outside741 the bounding box.742 capture name of file where image of highlighted word and743 clicked point are saved.744 Returns pair: ((score, matchingWord), (clickedX, clickedY)), where745 score score of found match (1.0 for perfect match)746 matchingWord corresponding word detected by OCR747 (clickedX, clickedY)748 X and Y coordinates of clicked position on the749 screen.750 Throws BadMatch error if could not find a matching word.751 Throws NoOCRResults error if there are OCR results available752 on the current screen.753 """754 _DEPRECATED()755 (score, matching_word), bbox = iVerifyWord(word, appearance=appearance, match=match, capture=False)756 clickedX, clickedY = iClickBox(bbox, clickPos, mouseButton, mouseEvent, dryRun, capture=False)757 windowId = _g_lastWindow758 _log('iClickWord("%s"): word "%s", match %.2f, bbox %s, window offset %s, click %s' %759 (word, matching_word, score,760 bbox, _g_windowOffsets[windowId],761 (clickedX, clickedY)))762 if capture:763 drawWords(_g_origImage, capture, [word], _g_words)764 drawClickedPoint(capture, capture, (clickedX, clickedY))765 return ((score, matching_word), (clickedX, clickedY))766def iClickBox((left, top, right, bottom), clickPos=(0.5, 0.5),767 mouseButton=1, mouseEvent=1, dryRun=None,768 capture=None, _captureText=None):769 """770 DEPRECATED - use fmbtx11.Screen.tapItem instead.771 Click coordinates relative to the given bounding box, default is772 in the middle of the box....
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!!