Best Python code snippet using fMBT_python
fmbttizen-agent.py
Source:fmbttizen-agent.py  
...91        pass # will return None92    else:93        raise ValueError('invalid touch device "%s"' % (touchSpec,))94    return touch_device95def openMouseDevice(mouseSpec=None, fallback=None):96    mouse_device = None97    if mouseSpec == None:98        if fallback != None:99            return openMouseDevice(fallback)100        else:101            pass # will return None102    elif mouseSpec.startswith("virtual:"):103        abs_or_rel = mouseSpec.split(":",1)[1]104        if abs_or_rel == "abs":105            absoluteMove = True106        elif abs_or_rel == "rel":107            absoluteMove = False108        else:109            raise ValueError('invalid mouse "%s"' % (mouseSpec,))110        mouse_device = fmbtuinput.Mouse(absoluteMove=absoluteMove).create()111    elif mouseSpec.startswith("file:") or mouseSpec.startswith("name:"):112        mouse_device = fmbtuinput.Mouse().open(mouseSpec.split(":",1)[1])113    elif mouseSpec == "disabled":114        pass # will return None115    else:116        raise ValueError('invalid mouse device "%s"' % (mouseSpec,))117    return mouse_device118def debug(msg):119    if g_debug:120        sys.stdout.write("debug: %s\n" % (msg,))121        sys.stdout.flush()122def error(msg, exitstatus=1):123    sys.stdout.write("fmbttizen-agent: %s\n" % (msg,))124    sys.stdout.flush()125    sys.exit(exitstatus)126iAmRoot = (os.getuid() == 0)127try:128    libc           = ctypes.CDLL("libc.so.6")129    libX11         = ctypes.CDLL("libX11.so.6")130    libXtst        = ctypes.CDLL("libXtst.so.6")131    g_Xavailable = True132    g_keyb = None # no need for virtual keyboard133except OSError:134    g_Xavailable = False135if g_Xavailable:136    class XImage(ctypes.Structure):137        _fields_ = [138            ('width'            , ctypes.c_int),139            ('height'           , ctypes.c_int),140            ('xoffset'          , ctypes.c_int),141            ('format'           , ctypes.c_int),142            ('data'             , ctypes.c_void_p),143            ('byte_order'       , ctypes.c_int),144            ('bitmap_unit'      , ctypes.c_int),145            ('bitmap_bit_order' , ctypes.c_int),146            ('bitmap_pad'       , ctypes.c_int),147            ('depth'            , ctypes.c_int),148            ('bytes_per_line'   , ctypes.c_int),149            ('bits_per_pixel'   , ctypes.c_int)]150    libc.write.argtypes           = [ctypes.c_int, ctypes.c_void_p, ctypes.c_size_t]151    libX11.XAllPlanes.restype     = ctypes.c_ulong152    libX11.XGetImage.restype      = ctypes.POINTER(XImage)153    libX11.XRootWindow.restype    = ctypes.c_uint32154    libX11.XOpenDisplay.restype   = ctypes.c_void_p155    libX11.XDefaultScreen.restype = ctypes.c_int156    libX11.XGetKeyboardMapping.restype = ctypes.POINTER(ctypes.c_uint32)157    # X11 constants, see Xlib.h158    X_CurrentTime  = ctypes.c_ulong(0)159    X_False        = ctypes.c_int(0)160    X_NULL         = ctypes.c_void_p(0)161    X_True         = ctypes.c_int(1)162    X_ZPixmap      = ctypes.c_int(2)163    NoSymbol       = 0164# InputKeys contains key names known to input devices, see165# linux/input.h or http://www.usb.org/developers/hidpage. The order is166# significant, because keyCode = InputKeys.index(keyName).167InputKeys = [168    "RESERVED", "ESC","1", "2", "3", "4", "5", "6", "7", "8", "9", "0",169    "MINUS", "EQUAL", "BACKSPACE", "TAB",170    "Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P",171    "LEFTBRACE", "RIGHTBRACE", "ENTER", "LEFTCTRL",172    "A", "S", "D", "F", "G", "H", "J", "K", "L",173    "SEMICOLON", "APOSTROPHE", "GRAVE", "LEFTSHIFT", "BACKSLASH",174    "Z", "X", "C", "V", "B", "N", "M",175    "COMMA", "DOT", "SLASH", "RIGHTSHIFT", "KPASTERISK", "LEFTALT",176    "SPACE", "CAPSLOCK",177    "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10",178    "NUMLOCK", "SCROLLLOCK",179    "KP7", "KP8", "KP9", "KPMINUS",180    "KP4", "KP5", "KP6", "KPPLUS",181    "KP1", "KP2", "KP3", "KP0", "KPDOT",182    "undefined0",183    "ZENKAKUHANKAKU", "102ND", "F11", "F12", "RO",184    "KATAKANA", "HIRAGANA", "HENKAN", "KATAKANAHIRAGANA", "MUHENKAN",185    "KPJPCOMMA", "KPENTER", "RIGHTCTRL", "KPSLASH", "SYSRQ", "RIGHTALT",186    "LINEFEED", "HOME", "UP", "PAGEUP", "LEFT", "RIGHT", "END", "DOWN",187    "PAGEDOWN", "INSERT", "DELETE", "MACRO",188    "MUTE", "VOLUMEDOWN", "VOLUMEUP",189    "POWER",190    "KPEQUAL", "KPPLUSMINUS", "PAUSE", "SCALE", "KPCOMMA", "HANGEUL",191    "HANGUEL", "HANJA", "YEN", "LEFTMETA", "RIGHTMETA", "COMPOSE"]192_inputKeyNameToCode={}193for c, n in enumerate(InputKeys):194    _inputKeyNameToCode[n] = c195# See struct input_event in /usr/include/linux/input.h196if platform.architecture()[0] == "32bit": _input_event = 'IIHHi'197else: _input_event = 'QQHHi'198# Event and keycodes are in input.h, too.199_EV_KEY = 0x01200_EV_ABS             = 0x03201_ABS_X              = 0x00202_ABS_Y              = 0x01203_ABS_MT_SLOT        = 0x2f204_ABS_MT_POSITION_X  = 0x35205_ABS_MT_POSITION_Y  = 0x36206_ABS_MT_TRACKING_ID = 0x39207_BTN_MOUSE          = 0x110208# Set input device names (in /proc/bus/input/devices)209# for pressing hardware keys.210try: cpuinfo = file("/proc/cpuinfo").read()211except: cpuinfo = ""212def readDeviceInfo():213    global devices214    try:215        devices = file("/proc/bus/input/devices").read()216    except:217        devices = ""218readDeviceInfo()219kbInputDevFd = None220if ('max77803-muic' in devices or221    'max77804k-muic' in devices):222    debug("detected max77803-muic or max77804k-muic")223    hwKeyDevice = {224        "POWER": "qpnp_pon",225        "VOLUMEUP": "gpio-keys",226        "VOLUMEDOWN": "gpio-keys",227        "HOME": "gpio-keys",228        "BACK": "sec_touchkey",229        "MENU": "sec_touchkey"230        }231    _inputKeyNameToCode["HOME"] = 139 # KEY_MENU232    _inputKeyNameToCode["MENU"] = 169 # KEY_PHONE233    if iAmRoot:234        touch_device = openTouchDevice(_opt_touch, "name:sec_touchscreen")235elif 'max77693-muic' in devices:236    debug("detected max77693-muic")237    hwKeyDevice = {238        "POWER": "gpio-keys",239        "VOLUMEUP": "gpio-keys",240        "VOLUMEDOWN": "gpio-keys",241        "HOME": "gpio-keys",242        "MENU": "gpio-keys",243        }244    _inputKeyNameToCode["HOME"] = 139245    if iAmRoot:246        touch_device = openTouchDevice(_opt_touch, "name:sec_touchscreen")247elif 'TRATS' in cpuinfo:248    debug("detected TRATS")249    # Running on Lunchbox250    hwKeyDevice = {251        "POWER": "gpio-keys",252        "VOLUMEUP": "gpio-keys",253        "VOLUMEDOWN": "gpio-keys",254        "HOME": "gpio-keys"255        }256    _inputKeyNameToCode["HOME"] = 139257    if iAmRoot:258        touch_device = openTouchDevice(_opt_touch, "file:/dev/input/event2")259elif 'QEMU Virtual CPU' in cpuinfo:260    debug("detected QEMU Virtual CPU")261    if "Maru Virtio Hwkey" in devices:262        _hwkeydev = "Maru Virtio Hwkey" # Tizen 2.3b emulator263    else:264        _hwkeydev = "AT Translated Set 2 hardkeys"265    # Running on Tizen emulator266    hwKeyDevice = {267        "POWER": "Power Button",268        "VOLUMEUP": _hwkeydev,269        "VOLUMEDOWN": _hwkeydev,270        "HOME": _hwkeydev,271        "BACK": _hwkeydev,272        "MENU": _hwkeydev273        }274    del _hwkeydev275    _inputKeyNameToCode["HOME"] = 139276    _inputKeyNameToCode["MENU"] = 169 # KEY_PHONE277    if iAmRoot:278        touch_device = openTouchDevice(_opt_touch, "file:/dev/input/event2")279elif 'Synaptics_RMI4_touchkey' in devices:280    debug("detected Synaptics_RMI4_touchkey")281    # Running on Geek282    hwKeyDevice = {283        "POWER": "mid_powerbtn",284        "VOLUMEUP": "gpio-keys",285        "VOLUMEDOWN": "gpio-keys",286        "HOME": "Synaptics_RMI4_touchkey",287        "BACK": "Synaptics_RMI4_touchkey",288        "MENU": "Synaptics_RMI4_touchkey"289        }290    if iAmRoot:291        touch_device = openTouchDevice(_opt_touch, "file:/dev/input/event1")292elif 'mxt224_key_0' in devices:293    debug("detected mxt225_key_0")294    # Running on Blackbay295    hwKeyDevice = {296        "POWER": "msic_power_btn",297        "VOLUMEUP": "gpio-keys",298        "VOLUMEDOWN": "gpio-keys",299        "HOME": "mxt224_key_0"300        }301    if iAmRoot:302        touch_device = openTouchDevice(_opt_touch, "file:/dev/input/event0")303elif 'eGalax Inc. eGalaxTouch EXC7200-7368v1.010          ' in devices:304    debug("detected eGalax Inc. eGalaxTouch EXC7200-7368v1.010")305    if iAmRoot:306        touch_device = openTouchDevice(307            _opt_touch,308            "name:eGalax Inc. eGalaxTouch EXC7200-7368v1.010          ")309        mouse_device = openMouseDevice(_opt_mouse, None)310        keyboard_device = openKeyboardDevice(_opt_keyboard)311elif iAmRoot:312    debug("hardware detection uses generic defaults")313    # Unknown platform, guessing best possible defaults for devices314    _d = devices.split("\n\n")315    try:316        power_devname = re.findall('Name=\"([^"]*)\"', [i for i in _d if "power" in i.lower()][0])[0]317    except IndexError:318        power_devname = "gpio-keys"319    touch_device = None320    try:321        touch_device_f = "file:/dev/input/" + re.findall('[ =](event[0-9]+)\s',  [i for i in _d if "touch" in i.lower()][0])[0]322    except IndexError:323        touch_device_f = None324    touch_device = openTouchDevice(_opt_touch, touch_device_f)325    mouse_device = None326    try:327        mouse_device_f = "file:/dev/input/" + re.findall('[ =](event[0-9]+)\s', [i for i in _d if "Mouse" in i][0])[0]328    except IndexError:329        mouse_device_f = None330    mouse_device = openMouseDevice(_opt_mouse, mouse_device_f)331    keyboard_device = openKeyboardDevice(_opt_keyboard)332    hwKeyDevice = {333        "POWER": power_devname,334        "VOLUMEUP": "gpio-keys",335        "VOLUMEDOWN": "gpio-keys",336        "HOME": "gpio-keys"337        }338    if isinstance(mouse_device, fmbtuinput.Mouse):339        time.sleep(1)340        mouse_device.move(-4096, -4096)341        mouse_device.setXY(0, 0)342    del _d343if iAmRoot and g_debug:344    debug("touch device: %s" % (touch_device,))...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!!
