Best Python code snippet using fMBT_python
eyenfinger.py
Source:eyenfinger.py
...17eyenfinger - DEPRECATED GUI testing library based on OCR and X event generation18Use fmbtx11 instead.19Configuring low-level key presses20---------------------------------21printEventsFromFile() prints input events from Linux chosen22/dev/input/eventXX file. Example:23python -c '24import eyenfinger25eyenfinger.printEventsFromFile("/dev/input/event0")26'27Alternatively, you can use device names in /proc/bus/input/devices and28printEventsFromDevice("device name").29Configuring OCR30---------------31autoconfigure() evaluates number of preprocessing filters to give the32best result on finding given words from given image. Example:33python -c '34from eyenfinger import *35autoconfigure("screenshot.png", ["Try", "to", "find", "these", "words"])36'37evaluatePreprocessFilter() highlights words detected on given image. Example:38python -c '39from eyenfinger import *40evaluatePreprocessFilter("screenshot.png", "-sharpen 5 -resize 1600x", ["File", "View"])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)...
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!!