How to use _parseUIAutomatorDump method in fMBT

Best Python code snippet using fMBT_python

fmbtandroid.py

Source:fmbtandroid.py Github

copy

Full Screen

...1597 intCoords = lambda x, y: (int(x), int(y))1598 self._intCoords = intCoords1599 try:1600 if dump.startswith("<?xm"):1601 self._parseUIAutomatorDump(dump, self._rawDumpFilename, displayToScreen)1602 else:1603 self._parseDump(dump, self._rawDumpFilename, displayToScreen)1604 except Exception, e:1605 self._errors.append((-1, "", "Parser error"))1606 def viewItems(self): return self._viewItems1607 def errors(self): return self._errors1608 def dumpRaw(self): return self._dump1609 def dumpItems(self, itemList = None):1610 if itemList == None: itemList = self._viewItems1611 l = []1612 for i in itemList:1613 l.append(self._dumpItem(i))1614 return '\n'.join(l)1615 def dumpTree(self, rootItem = None):1616 l = []1617 if rootItem != None:1618 l.extend(self._dumpSubTree(rootItem, 0))1619 else:1620 for i in self._viewItems:1621 if i._indent == 0:1622 l.extend(self._dumpSubTree(i, 0))1623 return '\n'.join(l)1624 def _dumpSubTree(self, viewItem, indent):1625 l = []1626 i = viewItem1627 l.append(" "*indent + self._dumpItem(viewItem))1628 for i in viewItem.children():1629 l.extend(self._dumpSubTree(i, indent + 4))1630 return l1631 def _dumpItem(self, viewItem):1632 i = viewItem1633 if i.text() != None: t = '"%s"' % (i.text(),)1634 else: t = None1635 return "id=%s cls=%s text=%s bbox=%s vis=%s" % (1636 i.id(), i.className(), t, i.bbox(), i.visibleBranch())1637 def filename(self):1638 return self._rawDumpFilename1639 def findItems(self, comparator, count=-1, searchRootItem=None, searchItems=None, onScreen=False):1640 """1641 Returns list of ViewItems to which comparator returns True.1642 Parameters:1643 comparator (function that takes one parameter (ViewItem))1644 returns True for all accepted items.1645 count (integer, optional):1646 maximum number of items to be returned.1647 The default is -1 (unlimited).1648 searchRootItem (ViewItem, optional):1649 search only among items that are children of1650 searchRootItem. The default is None (search from all).1651 searchItems (list of ViewItems, optional):1652 search only among given items. The default is None,1653 (search from all).1654 onScreen (boolean, optional):1655 search only among items that are on screen. The1656 default is False.1657 """1658 foundItems = []1659 if count == 0: return foundItems1660 if searchRootItem != None:1661 # find from searchRootItem and its children1662 if comparator(searchRootItem) and (1663 not onScreen or1664 searchRootItem.visibleBranch() and self._itemOnScreen(searchRootItem)):1665 foundItems.append(searchRootItem)1666 for c in searchRootItem.children():1667 foundItems.extend(self.findItems(comparator, count=count-len(foundItems), searchRootItem=c, onScreen=onScreen))1668 else:1669 if searchItems != None:1670 # find from listed items only1671 searchDomain = searchItems1672 else:1673 # find from all items1674 searchDomain = self._viewItems1675 for i in searchDomain:1676 if comparator(i) and (1677 not onScreen or1678 i.visibleBranch() and self._itemOnScreen(i)):1679 foundItems.append(i)1680 if count > 0 and len(foundItems) >= count:1681 break1682 return foundItems1683 def findItemsByText(self, text, partial=False, count=-1, searchRootItem=None, searchItems=None, onScreen=False):1684 """1685 Returns list of ViewItems with given text.1686 """1687 if partial:1688 c = lambda item: item.text().find(text) != -1 if item.text() != None else False1689 else:1690 c = lambda item: item.text() == text1691 return self.findItems(c, count=count, searchRootItem=searchRootItem, searchItems=searchItems, onScreen=onScreen)1692 def findItemsById(self, id, count=-1, searchRootItem=None, searchItems=None, onScreen=False):1693 """1694 Returns list of ViewItems with given id.1695 """1696 c = lambda item: item.id() == id1697 return self.findItems(c, count=count, searchRootItem=searchRootItem, searchItems=searchItems, onScreen=onScreen)1698 def findItemsByClass(self, className, partial=True, count=-1, searchRootItem=None, searchItems=None, onScreen=False):1699 """1700 Returns list of ViewItems with given class.1701 """1702 if partial: c = lambda item: item.className().find(className) != -11703 else: c = lambda item: item.className() == className1704 return self.findItems(c, count=count, searchRootItem=searchRootItem, searchItems=searchItems, onScreen=onScreen)1705 def findItemsByIdAndClass(self, id, className, partial=True, count=-1, searchRootItem=None, searchItems=None, onScreen=False):1706 """1707 Returns list of ViewItems with given id and class.1708 """1709 idOk = self.findItemsById(id, count=-1, searchRootItem=searchRootItem, onScreen=onScreen)1710 return self.findItemsByClass(className, partial=partial, count=count, searchItems=idOk, onScreen=onScreen)1711 def findItemsByContentDesc(self, content_desc, partial=False, count=-1, searchRootItem=None, searchItems=None, onScreen=False):1712 """1713 Returns list of ViewItems with given content-desc.1714 Works on uiautomatorDumps only.1715 """1716 if partial:1717 c = lambda item: item.content_desc().find(content_desc) != -11718 else:1719 c = lambda item: item.content_desc() == content_desc1720 return self.findItems(c, count=count, searchRootItem=searchRootItem, searchItems=searchItems, onScreen=onScreen)1721 def findItemsByRawProps(self, s, count=-1, searchRootItem=None, searchItems=None, onScreen=False):1722 """1723 Returns list of ViewItems with given string in properties.1724 """1725 c = lambda item: item._rawProps.find(s) != -11726 return self.findItems(c, count=count, searchRootItem=searchRootItem, searchItems=searchItems, onScreen=onScreen)1727 def findItemsByPos(self, pos, count=-1, searchRootItem=None, searchItems=None, onScreen=False):1728 """1729 Returns list of ViewItems whose bounding box contains the position.1730 Parameters:1731 pos (pair of floats (0.0..0.1) or integers (x, y)):1732 coordinates that fall in the bounding box of found items.1733 other parameters: refer to findItems documentation.1734 Items are listed in ascending order based on area. They may1735 or may not be from the same branch in the widget hierarchy.1736 """1737 x, y = self._intCoords(pos)1738 c = lambda item: (item.bbox()[0] <= x <= item.bbox()[2] and item.bbox()[1] <= y <= item.bbox()[3])1739 items = self.findItems(c, count=count, searchRootItem=searchRootItem, searchItems=searchItems, onScreen=onScreen)1740 # sort from smallest to greatest area1741 area_items = [((i.bbox()[2] - i.bbox()[0]) * (i.bbox()[3] - i.bbox()[1]), i) for i in items]1742 return [i for _, i in sorted(area_items)]1743 def findItemsInRegion(self, bbox, count=-1, searchRootItem=None, searchItems=None, onScreen=False):1744 """1745 Returns list of ViewItems whose bounding box is within the region.1746 Parameters:1747 bbox (four-tuple of floats (0.0..1.0) or integers):1748 bounding box that specifies search region1749 (left, top, right, bottom).1750 other parameters: refer to findItems documentation.1751 Returned items are listed in ascending order based on area.1752 """1753 left, top = self._intCoords((bbox[0], bbox[1]))1754 right, bottom = self._intCoords((bbox[2], bbox[3]))1755 c = lambda item: (left <= item.bbox()[0] <= item.bbox()[2] <= right and1756 top <= item.bbox()[1] <= item.bbox()[3] <= bottom)1757 items = self.findItems(c, count=count, searchRootItem=searchRootItem,1758 searchItems=searchItems, onScreen=onScreen)1759 area_items = [((i.bbox()[2] - i.bbox()[0]) * (i.bbox()[3] - i.bbox()[1]), i) for i in items]1760 return [i for _, i in sorted(area_items)]1761 def items(self):1762 """1763 Returns list of all items in the view1764 """1765 return fmbtgti.sortItems(self._viewItems, "topleft")1766 def save(self, fileOrDirName):1767 """1768 Save view dump to a file.1769 """1770 shutil.copy(self._rawDumpFilename, fileOrDirName)1771 def _parseUIAutomatorDump(self, dump, rawDumpFilename, displayToScreen):1772 """1773 Process XML output from "uiautomator dump" and create1774 a tree of ViewItems.1775 """1776 def add_elt(elt, parent, indent, results):1777 if ("resource-id" in elt.attrib and1778 "bounds" in elt.attrib):1779 try:1780 vi = ViewItem(elt.attrib["class"],1781 elt.attrib["resource-id"].split(":", 1)[-1],1782 indent,1783 elt.attrib,1784 parent,1785 "",...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run fMBT automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful