Best Python code snippet using fMBT_python
fmbtwindows.py
Source:fmbtwindows.py  
...1689                raise ValueError('no window with title "%s"' % (window,))1690        else:1691            raise ValueError('illegal window "%s", expected integer or string (hWnd or title)' % (window,))1692        return rv1693    def _parseDumps(self, dumps, dump_suffix, error_template):1694        dumpFilename = self._device.getDumpFilename(dump_suffix) + '.log'1695        error_message = ''1696        rv = []1697        prop_data = {}1698        items = None1699        with open(dumpFilename, 'w') as f:1700            for dump in dumps:1701                f.write(dump + '\n')1702                for line in dump.split('\0'):1703                    if line.startswith('!'):1704                        error_message = line[1:]1705                        continue1706                    if line.startswith('#'):  # It's a comment / debug output: skip it!1707                        continue1708                    if line == '[':1709                        items = []1710                        continue1711                    if line == ']':1712                        rv.append(items)1713                        continue1714                    if items is not None:1715                        items.append(line)1716                    if "=" not in line:1717                        continue1718                    prop_name, prop_value = line.split("=", 1)1719                    if prop_name == "hash" and prop_data:1720                        rv.append(prop_data)1721                        prop_data = {}1722                    prop_data[prop_name] = prop_value.replace(r"\r\n", "\n").replace(r"\\", "\\")1723        if prop_data:1724            rv.append(prop_data)1725        if error_message:1726            raise FMBTWindowsError(error_template % error_message)1727        return rv1728    def recvViewUIAutomation(self, window=None, items=[], properties=None, area=None, walker="raw",1729        filterType="none", filterCondition="", dumpChildClass="", dumpChildName="", doNotDump=False):1730        """returns list of dictionaries, each of which contains properties of1731        an item"""1732        if not walker in ["raw", "control", "content"]:1733            raise ValueError('invalid walker %r' % walker)1734        hwnd = self._window2hwnd(window) if window else None1735        if properties is None:1736            properties = []1737        else:1738            # make sure certain properties are always included1739            properties = list(frozenset(properties) | frozenset(["BoundingRectangle"]))1740        dumps = []1741        if items:1742            for item in items:1743                dumps.append(self.evalPython("dumpUIAutomationElements(%r, %r, %r, %r, %r, %r, %r, %r, %r, %r)" % (1744                    hwnd,1745                    [str(item.id()) for item in item.branch()],1746                    properties,1747                    area,1748                    walker,1749                    filterType,1750                    filterCondition,1751                    dumpChildClass,1752                    dumpChildName,1753                    doNotDump)))1754        else:1755            dumps.append(self.evalPython("dumpUIAutomationElements(%r, %r, %r, %r, %r, %r, %r, %r, %r, %r)" % (1756                hwnd,1757                [],1758                properties,1759                area,1760                walker,1761                filterType,1762                filterCondition,1763                dumpChildClass,1764                dumpChildName,1765                doNotDump)))1766        return self._parseDumps(dumps, "dumpUIAutomationElements",1767            "view is not available. An error happened while collecting UI elements with refreshView().\n%s")1768    def sendSetCacheMode(self, mode):1769        dump = self.evalPython("serverSetCacheMode(%r)" % mode)1770        self._parseDumps([dump], "serverSetCacheMode",1771            "An error happened while setting the cache mode.\n%s")1772    def sendClearCache(self):1773        dump = self.evalPython("serverClearCache()")1774        self._parseDumps([dump], "serverClearCache",1775            "An error happened while clearing the cache.\n%s")1776    def sendSetFocus(self, eltHash):1777        dump = self.evalPython("setAutomationElementFocus(%s)" % eltHash)1778        self._parseDumps([dump], "setAutomationElementFocus",1779            "An error happened while setting the focus.\n%s")1780    def sendCollapse(self, eltHash):1781        dump = self.evalPython("AutomationElementCollapse(%s)" % eltHash)1782        self._parseDumps([dump], "AutomationElementCollapse",1783            "An error happened while collapsing the viewitem.\n%s")1784    def sendExpand(self, eltHash):1785        dump = self.evalPython("AutomationElementExpand(%s)" % eltHash)1786        self._parseDumps([dump], "AutomationElementExpand",1787            "An error happened while expanding the viewitem.\n%s")1788    def recvViewCachedItem(self, eltHash):1789        dump = self.evalPython("getCachedAutomationElement(%s)" % eltHash)1790        return self._parseDumps([dump], "getCachedAutomationElement",1791            "viewitem is not available. An error happened while dumping it with cachedElement().\n%s")1792    def recvViewItemPatterns(self, eltHash):1793        dump = self.evalPython("getAutomationElementPatterns(%s)" % eltHash)1794        return self._parseDumps([dump], "getAutomationElementPatterns",1795            "viewitem patterns are not available. An error happened while collecting them with patterns().\n%s")1796    def recvViewItemProperties(self, eltHash):1797        dump = self.evalPython("getAutomationElementProperties(%s)" % eltHash)1798        return self._parseDumps([dump], "getAutomationElementProperties",1799            "viewitem properties are not available. An error happened while collecting them with refresh().\n%s")1800    def recvViewItemItems(self, eltHash, propertyName, separator, filterSubChildClassName, maxSubChildren):1801        dump = self.evalPython("getAutomationElementItems(%s, %r, %r, %r, %r)" % (1802            eltHash, propertyName, separator, filterSubChildClassName, maxSubChildren))1803        return self._parseDumps([dump], "getAutomationElementItems",1804            "viewitem items aren't available. An error happened while getting it with items().\n%s")1805    def recvViewItemContainerItems(self, eltHash, propertyName, separator, filterSubChildClassName, maxSubChildren, scroll):1806        dump = self.evalPython("getAutomationElementContainerItems(%s, %r, %r, %r, %r, %r)" % (1807            eltHash, propertyName, separator, filterSubChildClassName, maxSubChildren, scroll))1808        return self._parseDumps([dump], "getAutomationElementContainerItems",1809            "viewitem items aren't available. An error happened while getting it with containerItems().\n%s")1810    def recvViewItemSelectedItems(self, eltHash, propertyName, separator, filterSubChildClassName, maxSubChildren, scroll):1811        dump = self.evalPython("getAutomationElementSelectedItems(%s, %r, %r, %r, %r, %r)" % (1812            eltHash, propertyName, separator, filterSubChildClassName, maxSubChildren, scroll))1813        return self._parseDumps([dump], "getAutomationElementSelectedItems",1814            "viewitem selected items aren't available. An error happened while getting it with selectedItems().\n%s")1815    def recvViewItemText(self, eltHash, maxLength):1816        dump = self.evalPython("getAutomationElementText(%s, %s)" % (eltHash, maxLength))1817        return self._parseDumps([dump], "getAutomationElementText",1818            "viewitem text isn't available. An error happened while getting it with longText().\n%s")1819    def recvViewItemSetValue(self, eltHash, value):1820        dump = self.evalPython("setAutomationElementValue(%s, %r)" % (eltHash, value))1821        return self._parseDumps([dump], "setAutomationElementValue",1822            "viewitem value isn't editable. An error happened while setting it with setValue().\n%s")1823    def recvWindowList(self):1824        return self.evalPython("windowList()")1825    def _window2hwnd(self, window):1826        if isinstance(window, str) or isinstance(window, unicode):1827            windowList = self.recvWindowList()1828            hwndList = [w["hwnd"] for w in windowList if w["title"] == window]1829            if not hwndList:1830                raise ValueError('no window with title "%s"' % (window,))1831            hwnd = hwndList[0]1832        elif isinstance(window, dict) and "hwnd" in window:1833            hwnd = window["hwnd"]1834        elif isinstance(window, int) or isinstance(window, long):1835            hwnd = window...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!!
