How to use visibleBranch method in fMBT

Best Python code snippet using fMBT_python

fmbtandroid.py

Source:fmbtandroid.py Github

copy

Full Screen

...1491 self._visible = True1492 self._text = self._p["text"]1493 fmbtgti.GUIItem.__init__(self, className, self._calculateBbox(displayToScreen), dumpFilename)1494 def addChild(self, child):1495 child._parentsVisible = self.visibleBranch()1496 self._children.append(child)1497 def _calculateBbox(self, displayToScreen):1498 if "bounds" in self._p:1499 try:1500 left, top, right, bottom = [1501 int(v) for v in1502 ViewItem._boundsRegEx.findall(self._p["bounds"])[0]]1503 except IndexError:1504 raise ValueError('invalid bounds "%s"' % (self._p["bounds"],))1505 width = right - left1506 height = bottom - top1507 elif "layout:getLocationOnScreen_x()" in self._p:1508 left = int(self._p["layout:getLocationOnScreen_x()"])1509 top = int(self._p["layout:getLocationOnScreen_y()"])1510 height = int(self._p["layout:getHeight()"])1511 width = int(self._p["layout:getWidth()"])1512 elif "layout:mLeft" in self._p:1513 left = int(self._p["layout:mLeft"])1514 top = int(self._p["layout:mTop"])1515 parent = self._parent1516 while parent:1517 pp = parent._p1518 left += int(pp["layout:mLeft"]) - int(pp["scrolling:mScrollX"])1519 top += int(pp["layout:mTop"]) - int(pp["scrolling:mScrollY"])1520 parent = parent._parent1521 height = int(self._p["layout:getHeight()"])1522 width = int(self._p["layout:getWidth()"])1523 else:1524 raise ValueError("bounding box not found, layout fields missing")1525 screenLeft, screenTop = displayToScreen(left, top)1526 screenRight, screenBottom = displayToScreen(left + width, top + height)1527 return (screenLeft, screenTop, screenRight, screenBottom)1528 def children(self): return self._children1529 def className(self): return self._className1530 def code(self): return self._code1531 def indent(self): return self._indent1532 def id(self): return self._id1533 def parent(self): return self._parent1534 def properties(self): return self._p1535 def property(self, propertyName):1536 return self._p.get(propertyName, None)1537 def visibleBranch(self):1538 """Returns True if this item and all items containing this are visible1539 up to the root node"""1540 return self._parentsVisible and self.visible()1541 def text(self):1542 return self._text1543 def content_desc(self):1544 if "content-desc" in self._p:1545 return self._p["content-desc"]1546 elif "accessibility:getContentDescription()" in self._p:1547 return self._p["accessibility:getContentDescription()"]1548 else:1549 return None1550 def visible(self):1551 return self._visible1552 def dump(self):1553 p = self._p1554 return ("ViewItem(\n\tchildren = %d\n\tclassName = '%s'\n\tcode = '%s'\n\t" +1555 "indent = %d\n\tproperties = {\n\t\t%s\n\t})") % (1556 len(self._children), self._className, self._code, self._indent,1557 '\n\t\t'.join(['"%s": %s' % (key, p[key]) for key in sorted(p.keys())]))1558 def dumpProperties(self):1559 rv = []1560 if self._p:1561 for key in [k for k in sorted(self._p.keys()) if not "layout:" in k and not "padding:" in k and not "drawing:" in k]: # sorted(self._p.keys()): # [k for k in sorted(self._p.keys()) if not ":" in k]:1562 rv.append("%s=%s" % (key, self._p[key]))1563 return "\n".join(rv)1564 def __str__(self):1565 if self.text():1566 text = ", text=%s" % (repr(self.text()),)1567 else:1568 text = ""1569 if "content-desc" in self._p and self._p["content-desc"]:1570 text += ", content_desc=%s" % (repr(self.content_desc(),))1571 return ("ViewItem(className=%s, id=%s, bbox=%s%s)" % (1572 repr(self._className), repr(self.id()), self.bbox(), text))1573class View(object):1574 """1575 View provides interface to screen dumps from Android. It parses1576 the dump to a hierarchy of ViewItems. find* methods enable searching1577 for ViewItems based on their properties.1578 """1579 def __init__(self, screenshotDir, serialNumber, dump, displayToScreen=None,1580 itemOnScreen=None, intCoords=None):1581 self.screenshotDir = screenshotDir1582 self.serialNumber = serialNumber1583 self._viewItems = []1584 self._errors = []1585 self._lineRegEx = re.compile("(?P<indent>\s*)(?P<class>[\w.$]+)@(?P<id>[0-9A-Fa-f]{4,8} )(?P<properties>.*)")1586 self._olderAndroidLineRegEx = re.compile("(?P<indent>\s*)(?P<class>[\w.$]+)@(?P<id>\w)(?P<properties>.*)")1587 self._propRegEx = re.compile("(?P<prop>(?P<name>[^=]+)=(?P<len>\d+),)(?P<data>[^\s]* ?)")1588 self._dump = dump1589 self._rawDumpFilename = self.screenshotDir + os.sep + fmbtgti._filenameTimestamp() + "-" + self.serialNumber + ".view"1590 file(self._rawDumpFilename, "w").write(self._dump)1591 if displayToScreen == None:1592 displayToScreen = lambda x, y: (x, y)1593 if itemOnScreen == None:1594 itemOnScreen = lambda item: True1595 self._itemOnScreen = itemOnScreen1596 if intCoords == None: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):...

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