Best Python code snippet using fMBT_python
fmbtgti.py
Source:fmbtgti.py
...1511 """1512 assert self._lastScreenshot != None, "Screenshot required."1513 dragArgs, rest = _takeDragArgs(dragAndOcrArgs)1514 ocrArgs, _ = _takeOcrArgs(self._lastScreenshot, rest, thatsAll=True)1515 items = self._lastScreenshot.findItemsByOcr(text, **ocrArgs)1516 if len(items) == 0:1517 return False1518 return self.swipeItem(items[0], direction, distance, **dragArgs)1519 def tap(self, (x, y), long=False, hold=0.0, count=1, delayBetweenTaps=0.175, button=None):1520 """1521 Tap screen on coordinates (x, y).1522 Parameters:1523 coordinates (floats in range [0.0, 1.0] or integers):1524 floating point coordinates in range [0.0, 1.0] are1525 scaled to full screen width and height, others are1526 handled as absolute coordinate values.1527 count (integer, optional):1528 number of taps to the coordinates. The default is 1.1529 delayBetweenTaps (float, optional):1530 time (seconds) between taps when count > 1.1531 The default is 0.175 (175 ms).1532 long (boolean, optional):1533 if True, touch the screen for a long time.1534 hold (float, optional):1535 time in seconds to touch the screen.1536 button (integer, optional):1537 send tap using given mouse button. The default is1538 None: button parameter is not passed to the1539 underlying connection layer (sendTouchDown etc.),1540 the default in the underlying layer will be used.1541 Note that all connection layers may not support1542 this parameter.1543 Returns True if successful, otherwise False.1544 """1545 x, y = self.intCoords((x, y))1546 count = int(count)1547 if long and hold == 0.0:1548 hold = self._longTapHoldTime1549 extraParams = {}1550 if button != None:1551 extraParams['button'] = button1552 if count == 0:1553 self._conn.sendTouchMove(x, y)1554 while count > 0:1555 if hold > 0.0:1556 try:1557 assert self._conn.sendTouchDown(x, y, **extraParams)1558 time.sleep(hold)1559 assert self._conn.sendTouchUp(x, y, **extraParams)1560 except AssertionError:1561 return False1562 else:1563 if not self._conn.sendTap(x, y, **extraParams):1564 return False1565 count = int(count) - 11566 return True1567 def tapBitmap(self, bitmap, **tapAndOirArgs):1568 """1569 Find a bitmap from the latest screenshot, and tap it.1570 Parameters:1571 bitmap (string):1572 filename of the bitmap to be tapped.1573 optical image recognition arguments (optional)1574 refer to help(obj.oirEngine()).1575 tapPos (pair of floats (x,y)):1576 refer to tapItem documentation.1577 long, hold, count, delayBetweenTaps, button (optional):1578 refer to tap documentation.1579 Returns True if successful, otherwise False.1580 """1581 assert self._lastScreenshot != None, "Screenshot required."1582 tapArgs, rest = _takeTapArgs(tapAndOirArgs)1583 oirArgs, _ = _takeOirArgs(self._lastScreenshot, rest, thatsAll=True)1584 oirArgs["limit"] = 11585 items = self._lastScreenshot.findItemsByBitmap(bitmap, **oirArgs)1586 if len(items) == 0:1587 return False1588 return self.tapItem(items[0], **tapArgs)1589 def tapItem(self, viewItem, **tapArgs):1590 """1591 Tap the center point of viewItem.1592 Parameters:1593 viewItem (GUIItem object):1594 item to be tapped, possibly returned by1595 findItemsBy... methods in Screenshot or View.1596 tapPos (pair of floats (x,y)):1597 position to tap, relative to the item.1598 (0.0, 0.0) is the top-left corner,1599 (1.0, 0.0) is the top-right corner,1600 (1.0, 1.0) is the lower-right corner.1601 Values < 0 and > 1 tap coordinates outside the item.1602 long, hold, count, delayBetweenTaps, button (optional):1603 refer to tap documentation.1604 """1605 if "tapPos" in tapArgs:1606 posX, posY = tapArgs["tapPos"]1607 del tapArgs["tapPos"]1608 x1, y1, x2, y2 = viewItem.bbox()1609 tapCoords = (x1 + (x2-x1) * posX,1610 y1 + (y2-y1) * posY)1611 else:1612 tapCoords = viewItem.coords()1613 return self.tap(tapCoords, **tapArgs)1614 def tapOcrText(self, text, appearance=0, **tapAndOcrArgs):1615 """1616 Find text from the latest screenshot using OCR, and tap it.1617 Parameters:1618 text (string):1619 the text to be tapped.1620 long, hold, count, delayBetweenTaps, button (optional):1621 refer to tap documentation.1622 OCR engine specific arguments1623 refer to help(obj.ocrEngine())1624 Returns True if successful, otherwise False.1625 """1626 assert self._lastScreenshot != None, "Screenshot required."1627 tapArgs, rest = _takeTapArgs(tapAndOcrArgs)1628 ocrArgs, _ = _takeOcrArgs(self._lastScreenshot, rest, thatsAll=True)1629 items = self._lastScreenshot.findItemsByOcr(text, **ocrArgs)1630 if len(items) <= appearance:1631 return False1632 return self.tapItem(items[appearance], **tapArgs)1633 def type(self, text):1634 """1635 Type text.1636 """1637 return self._conn.sendType(text)1638 def verifyOcrText(self, text, **ocrArgs):1639 """1640 Verify using OCR that the last screenshot contains the text.1641 Parameters:1642 text (string):1643 text to be verified.1644 OCR engine specific arguments1645 refer to help(obj.ocrEngine())1646 Returns True if successful, otherwise False.1647 """1648 assert self._lastScreenshot != None, "Screenshot required."1649 ocrArgs, _ = _takeOcrArgs(self._lastScreenshot, ocrArgs, thatsAll=True)1650 return self._lastScreenshot.findItemsByOcr(text, **ocrArgs) != []1651 def verifyBitmap(self, bitmap, **oirArgs):1652 """1653 Verify that bitmap is present in the last screenshot.1654 Parameters:1655 bitmap (string):1656 filename of the bitmap file to be searched for.1657 optical image recognition arguments (optional)1658 refer to help(obj.oirEngine()).1659 """1660 assert self._lastScreenshot != None, "Screenshot required."1661 oirArgs, _ = _takeOirArgs(self._lastScreenshot, oirArgs, thatsAll=True)1662 oirArgs["limit"] = 11663 return self._lastScreenshot.findItemsByBitmap(bitmap, **oirArgs) != []1664 def wait(self, refreshFunc, waitFunc, waitFuncArgs=(), waitFuncKwargs={}, waitTime = 5.0, pollDelay = 1.0):1665 """1666 Wait until waitFunc returns True or waitTime has expired.1667 Parameters:1668 refreshFunc (function):1669 this function is called before re-evaluating1670 waitFunc. For instance, refreshScreenshot.1671 waitFunc, waitFuncArgs, waitFuncKwargs (function, tuple,1672 dictionary):1673 wait for waitFunc(waitFuncArgs, waitFuncKwargs) to1674 return True1675 waitTime (float, optional):1676 max. time in seconds to wait for. The default is1677 5.0.1678 pollDelay (float, optional):1679 time in seconds to sleep between refreshs. The1680 default is 1.0.1681 Returns True if waitFunc returns True - either immediately or1682 before waitTime has expired - otherwise False.1683 refreshFunc will not be called if waitFunc returns immediately1684 True.1685 """1686 if waitFunc(*waitFuncArgs, **waitFuncKwargs):1687 return True1688 startTime = time.time()1689 endTime = startTime + waitTime1690 now = startTime1691 while now < endTime:1692 time.sleep(min(pollDelay, (endTime - now)))1693 now = time.time()1694 refreshFunc()1695 if waitFunc(*waitFuncArgs, **waitFuncKwargs):1696 return True1697 return False1698 def waitAnyBitmap(self, listOfBitmaps, **waitAndOirArgs):1699 """1700 Wait until any of given bitmaps appears on screen.1701 Parameters:1702 listOfBitmaps (list of strings):1703 list of bitmaps (filenames) to be waited for.1704 optical image recognition arguments (optional)1705 refer to help(obj.oirEngine()).1706 waitTime, pollDelay (float, optional):1707 refer to wait documentation.1708 Returns list of bitmaps appearing in the first screenshot that1709 contains at least one of the bitmaps. If none of the bitmaps1710 appear within the time limit, returns empty list.1711 If the bitmap is not found from most recently refreshed1712 screenshot, waitAnyBitmap updates the screenshot.1713 """1714 if listOfBitmaps == []: return []1715 if not self._lastScreenshot: self.refreshScreenshot()1716 waitArgs, rest = _takeWaitArgs(waitAndOirArgs)1717 oirArgs, _ = _takeOirArgs(self._lastScreenshot, rest, thatsAll=True)1718 foundBitmaps = []1719 def observe():1720 for bitmap in listOfBitmaps:1721 if self._lastScreenshot.findItemsByBitmap(bitmap, **oirArgs):1722 foundBitmaps.append(bitmap)1723 return foundBitmaps != []1724 self.wait(self.refreshScreenshot, observe, **waitArgs)1725 return foundBitmaps1726 def waitAnyOcrText(self, listOfTexts, **waitAndOcrArgs):1727 """1728 Wait until OCR recognizes any of texts on the screen.1729 Parameters:1730 listOfTexts (list of string):1731 texts to be waited for.1732 waitTime, pollDelay (float, optional):1733 refer to wait documentation.1734 OCR engine specific arguments1735 refer to help(obj.ocrEngine())1736 Returns list of texts that appeared in the first screenshot1737 that contains at least one of the texts. If none of the texts1738 appear within the time limit, returns empty list.1739 If any of texts is not found from most recently refreshed1740 screenshot, waitAnyOcrText updates the screenshot.1741 """1742 if listOfTexts == []: return []1743 if not self._lastScreenshot: self.refreshScreenshot()1744 waitArgs, rest = _takeWaitArgs(waitAndOcrArgs)1745 ocrArgs, _ = _takeOcrArgs(self._lastScreenshot, rest, thatsAll=True)1746 foundTexts = []1747 def observe():1748 for text in listOfTexts:1749 if self.verifyOcrText(text, **ocrArgs):1750 foundTexts.append(text)1751 return foundTexts != []1752 self.wait(self.refreshScreenshot, observe, **waitArgs)1753 return foundTexts1754 def waitBitmap(self, bitmap, **waitAndOirArgs):1755 """1756 Wait until bitmap appears on screen.1757 Parameters:1758 bitmap (string):1759 filename of the bitmap to be waited for.1760 optical image recognition arguments (optional)1761 refer to help(obj.oirEngine()).1762 waitTime, pollDelay (float, optional):1763 refer to wait documentation.1764 Returns True if bitmap appeared within given time limit,1765 otherwise False.1766 If the bitmap is not found from most recently refreshed1767 screenshot, waitBitmap updates the screenshot.1768 """1769 return self.waitAnyBitmap([bitmap], **waitAndOirArgs) != []1770 def waitOcrText(self, text, **waitAndOcrArgs):1771 """1772 Wait until OCR detects text on the screen.1773 Parameters:1774 text (string):1775 text to be waited for.1776 waitTime, pollDelay (float, optional):1777 refer to wait documentation.1778 OCR engine specific arguments1779 refer to help(obj.ocrEngine())1780 Returns True if the text appeared within given time limit,1781 otherwise False.1782 If the text is not found from most recently refreshed1783 screenshot, waitOcrText updates the screenshot.1784 """1785 return self.waitAnyOcrText([text], **waitAndOcrArgs) != []1786class Screenshot(object):1787 """1788 Screenshot class takes and holds a screenshot (bitmap) of device1789 display, or a forced bitmap file if device connection is not given.1790 """1791 def __init__(self, screenshotFile=None, paths=None,1792 ocrEngine=None, oirEngine=None, screenshotRefCount=None):1793 self._filename = screenshotFile1794 self._ocrEngine = ocrEngine1795 self._ocrEngineNotified = False1796 self._oirEngine = oirEngine1797 self._oirEngineNotified = False1798 self._screenshotRefCount = screenshotRefCount1799 if (type(self._screenshotRefCount) == dict and self._filename):1800 self._screenshotRefCount[self._filename] = (1 +1801 self._screenshotRefCount.get(self._filename, 0))1802 self._screenSize = None1803 self._paths = paths1804 def __del__(self):1805 if self._ocrEngine and self._ocrEngineNotified:1806 self._ocrEngine.removeScreenshot(self)1807 if self._oirEngine and self._oirEngineNotified:1808 if (self._ocrEngineNotified == False or1809 id(self._oirEngine) != id(self._ocrEngine)):1810 self._oirEngine.removeScreenshot(self)1811 if (type(self._screenshotRefCount) == dict and self._filename):1812 self._screenshotRefCount[self._filename] -= 11813 def isBlank(self):1814 """1815 Returns True if screenshot is blank, otherwise False.1816 """1817 return _e4gImageIsBlank(self._filename)1818 def setSize(self, screenSize):1819 self._screenSize = screenSize1820 def size(self, allowReadingFile=True):1821 """1822 Returns screenshot size in pixels, as pair (width, height).1823 """1824 if self._screenSize == None and allowReadingFile:1825 e4gImage = eye4graphics.openImage(self._filename)1826 self._screenSize = _e4gImageDimensions(e4gImage)1827 eye4graphics.closeImage(e4gImage)1828 return self._screenSize1829 def _notifyOcrEngine(self):1830 if self._ocrEngine and not self._ocrEngineNotified:1831 self._ocrEngine.addScreenshot(self)1832 self._ocrEngineNotified = True1833 if id(self._ocrEngine) == id(self._oirEngine):1834 self._oirEngineNotified = True1835 def _notifyOirEngine(self):1836 if self._oirEngine and not self._oirEngineNotified:1837 self._oirEngine.addScreenshot(self)1838 self._oirEngineNotified = True1839 if id(self._oirEngine) == id(self._ocrEngine):1840 self._ocrEngineNotified = True1841 def dumpOcr(self, **kwargs):1842 """1843 Return what OCR engine recognizes on this screenshot.1844 Not all OCR engines provide this functionality.1845 """1846 self._notifyOcrEngine()1847 return self._ocrEngine.dumpOcr(self, **kwargs)1848 def dumpOcrWords(self, **kwargs):1849 """1850 Deprecated, use dumpOcr().1851 """1852 return self.dumpOcr(**kwargs)1853 def filename(self):1854 return self._filename1855 def findItemsByBitmap(self, bitmap, **oirFindArgs):1856 if self._oirEngine != None:1857 self._notifyOirEngine()1858 oirArgsList = self._paths.oirArgsList(bitmap)1859 results = []1860 if oirArgsList:1861 for oirArgs in oirArgsList:1862 oirArgs, _ = _takeOirArgs(self._oirEngine, oirArgs.copy())1863 oirArgs.update(oirFindArgs)1864 results.extend(self._oirEngine.findBitmap(1865 self, self._paths.abspath(bitmap), **oirArgs))1866 if results: break1867 else:1868 oirArgs = oirFindArgs1869 results.extend(self._oirEngine.findBitmap(1870 self, self._paths.abspath(bitmap), **oirArgs))1871 return results1872 else:1873 raise RuntimeError('Trying to use OIR on "%s" without OIR engine.' % (self.filename(),))1874 def findItemsByOcr(self, text, **ocrEngineArgs):1875 if self._ocrEngine != None:1876 self._notifyOcrEngine()1877 return self._ocrEngine.findText(self, text, **ocrEngineArgs)1878 else:1879 raise RuntimeError('Trying to use OCR on "%s" without OCR engine.' % (self.filename(),))1880 def save(self, fileOrDirName):1881 shutil.copy(self._filename, fileOrDirName)1882 def ocrEngine(self):1883 return self._ocrEngine1884 def oirEngine(self):1885 return self._oirEngine1886 def __str__(self):1887 return 'Screenshot(filename="%s")' % (self._filename,)1888class GUIItem(object):...
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!!